How to use html encode function in php to prevent browsers from using it as an HTML

In this section we are going check the usage of html encode in PHP here our question is how to use html encode function in php to prevent browsers from using it as an HTML .sometimes we have to display html element in the front end as it is without coveting it.for example we need to display below line

<h2>Hello</h2> <p>Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML   </p>

but we know that normally it display like below

use html encode function php to prevent browsers from using it as an HTML

we are lucky enough to handle this situation we have a html encode function in php it prevent browsers from using it as an HTML lets see the function htmlentities

htmlentities(string,flags,character-set,double_encode)
htmlentities ( string $string [, int $flags = ENT_COMPAT | ENT_HTML401 [, string $encoding = ini_get("default_charset") [, bool $double_encode = TRUE ]]] ) 

default
htmlentities($htmlcode);

See example how it is working

 $htmlcode='<h2>Hello</h2>
Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML';
echo htmlentities($htmlcode);

Output

Hello

Hello here here we are disusing the usage of html encode function php to prevent browsers from using it as an HTML

How to apply array_slice in PHP object array

Here my requirement is to fetch first five items from an Object if it is a normal array means i can fetch the first nth item from the php array using the PHP build in function array_slice() for example we have an array like below
let we check how to get first element of array in using array_slice()
after we will discuss how to apply array_slice in PHP object array

$myarray=array("Mangoes","Jack Fruits","coconuts","Bananas","Apple","Orange");

print_r(array_slice($myarray,0,2));

Output

Array ( [0] => Mangoes [1] => Jack Fruits)

In the given example fetching first 2 values from the array using build in function array_slice();

Now my issues is here my array is not normal array it is an object array so i can’t use directly array_slice()

First i have to convert Object array to normal array and i can i apply the code see example below

$Myarray=(array)$OBJ_array;

print_r(array_slice($Myarray,0,5));

it will return an array with first five value of $OBJ_array

how to redirect all dynamic pages to single php page using .htaccess

In core PHP sometimes we need to create dynamic pages for all the post added in the backed,if we have hundreds of post we don’t want to create 100 pages to display all the post.Here am going to display how we can use slug to identity each post and pass that slug in the URL suppose we have to create a URL like post_POSTNAME_page.so each post having the same URL structure we will replace POSTNAME with slug, that we created from the post tile or post name
so i want to call all post_POSTNAME_page to a single php page ,we can do it with the help of .htaccess .in the below post am going to explain you how to redirect all dynamic pages to single php page using .htaccess. First i need to create a php page name postpage.php
In the postpage.php we can get the value of slug by using the get method

$page_slug=$_GET[‘slug’];

by using this page slug we can use mysql to fetch all data related to that post

Here is the rules to redirect all dynamic pages to single php page using .htaccess

RewriteEngine On
Options +FollowSymLinks
RewriteRule post_(.*)_page$   postpage.php?slug=$1

See how it going to work

https://www.tutorialshore.com/post_Indian_history_page

It will call postpage.php?slug=indian_history

So if we use $_GET[‘slug’], we will get page slug Indian_history by using this slug we can access the post content for Indian history
same way you can add pages also

RewriteRule page_(.*)_content$ page.php?slug=$1

How to change value of one drop down box based on other drop-down box change using JQuery

Here we are going to discus how to change value of one drop down box based on other drop-down box change using JQuery

Part 1

Html Code

 <div class="select-box">
<select id="courseId" name="courseId" class="select" onchange="course_changeFunct(this.value)">
<option value="1">Option1</option>
<option value="2">Option2</option>
<option value="3">Option3</option>
<option value="4">Option4</option> 
</select>
</div>
<div class="select-box">
<select id="location" name="location" class="select">
<option value="location1">location1</option>
<option value="location2">location2</option>
<option value="location3">location3</option>
<option value="location4">location4</option> 
</select>
</div>

Part 2

JQuery Script

function course_changeFunct(course_id)
{

var menu_type =$("#domains").val();
$.post("site_action_view.php?action=get_location_by_course_id", { course_id: course_id }).done(function( data ) {
	
$("#location").empty();
if(data=='')
{
var select_data='Sorry no Location';
}
else
{
var select_data='Select program'+data+'';	
}
  
$("#location").append(select_data);



});
}

Part 3

site_action_view.php Action Page

	case 'get_location_by_course_id':
        $result = $obj->get_location_by_course_id($_REQUEST['course_id']);
		//print_r($result);
		$var_option="";
		foreach($result as $key=>$data_value)
		{
		$var_option.=''.$data_value.'';	
		}
		echo $var_option;
        break;