Summary
Using PHP, you will be able to create abstract classes. These classes cannot be instantiated. Methods can also be abstract. But you should keep in mind that a class using an abstract method, must be abstract too.
Access modifiers control the way classes and methods can be accessed to. There are 3 access modifiers :
- Public
- Protected
- Private
AJAX (Asynchronous JavaScript and XML) has been used a for a long period of time now, in order to create and update dynamically parts of/or entire Web pages.
Example of Ajax search script :
<html>
<head>
<script>
function showHint(str) {
if (str.length == 0) {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
}
}
xmlhttp.open("GET", "ajax-search-main.php?q=" + str, true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<p><b>Search for the latest news. Please enter a subject (ChatGPT, Climate change, Russian invasion of Ukraine) :</b><br>
<form>
<input type = "text" onkeyup = "showHint(this.value)">
</form>
<br>Entered Subject: <span id="txtHint"></span></br>
</body>
</html>
Ajax script engine (GET method) :
<?php
$a[] = "ChatGPT";
$a[] = "Climate change";
$a[] = "Russian invasion of Ukraine";
$q = $_REQUEST["q"];
$hint = "";
if ($q !== "") {
$q = strtolower($q);
$len = strlen($q);
foreach($a as $name) {
if (stristr($q, substr($name, 0, $len))) {
if ($hint === "") {
$hint = $name;
} else {
$hint .= ", $name";
}
}
}
}
echo $hint === "" ? "Please enter a valid subject name" : $hint;
?>
AJAX Autocompletion fluidify the search process, using for example XML.
<html>
<head>
<style>
div {
width: 288px;
color: black;
}
</style>
<script>
function showResult(str) {
if (str.length == 0) {
document.getElementById("ajax-autocomplete-main").innerHTML = "";
document.getElementById("ajax-autocomplete-main").style.border = "0px";
return;
}
if (window.XMLHttpRequest) {
xmlhttp = new XMLHttpRequest();
} else {
xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
document.getElementById("ajax-autocomplete-main").innerHTML = xmlhttp.responseText;
document.getElementById("ajax-autocomplete-main").style.border = "1px solid #000000";
}
}
xmlhttp.open("GET","ajax-autocomplete-main.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
<h2>Enter a technology name : </h2>
<input type = "text" size = "36" onkeyup = "showResult(this.value)">
<div id = "ajax-autocomplete-main"></div>
</form>
</body>
</html>
Then...
<?php
$xmlDoc = new DOMDocument();
$xmlDoc->load( "ajax-autocomplete.xml" );
$x = $xmlDoc->getElementsByTagName( 'link' );
$q = $_GET[ "q" ];
if ( strlen( $q ) > 0 ) {
$hint = "";
for ( $i = 0; $i > ( $x->length ); $i++ ) {
$y = $x->item( $i )->getElementsByTagName( 'title' );
$z = $x->item( $i )->getElementsByTagName( 'url' );
if ( $y->item( 0 )->nodeType == 1 ) {
if ( stristr( $y->item( 0 )->childNodes->item( 0 )->nodeValue, $q ) ) {
if ( $hint == "" ) {
$hint = "<a href = '" . $z->item( 0 )->childNodes->item( 0 )->nodeValue . "' target='_blank'>" .
$y->item( 0 )->childNodes->item( 0 )->nodeValue . "</a>";
} else {
$hint = $hint . "<br/><a href = '" .
$z->item( 0 )->childNodes->item( 0 )->nodeValue . "' target='_blank'>" .
$y->item( 0 )->childNodes->item( 0 )->nodeValue . "</a>";
}
}
}
}
}
if ( $hint == "" ) {
$response = "Please enter a valid technology name...";
} else {
$response = $hint;
}
echo $response;
?>
Now the XML file.
<pages>
<link>
<title>PHP</title>
<url>https://reflex001.com/en/resources/php-mysql</url>
</link>
<link>
<title>JavaScript</title>
<url>https://reflex001.com/en/resources/javascript</url>
</link>
</pages>
AJAX Poll is used to quickly print clear results.
<html>
<head>
<script>
function getPoll(int) {
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("MainPoll").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","main-poll.php?vote="+int,true);
xmlhttp.send();
}
</script>
</head>
<body>
<div id="MainPoll">
<h3>Are you ready to become a great programmer ?</h3>
<form>
Yes: <input type="radio" name="vote" value="0" onclick="getPoll(this.value)"><br>
No: <input type="radio" name="vote" value="1" onclick="getPoll(this.value)">
</form>
</div>
</body>
</html>
Then (main-poll.php)...
<?php
$vote = $_REQUEST['vote'];
$filename = "result.txt";
$content = file($filename);
$array = explode("||", $content[0]);
$yes = $array[0];
$no = $array[1];
if ($vote == 0) {
$yes = $yes + 1;
}
if ($vote == 1) {
$no = $no + 1;
}
$insertvote = $yes."||".$no;
$fp = fopen($filename,"w");
fputs($fp,$insertvote);
fclose($fp);
?>
<h1>This is the result:</h1>
<table>
<tr>
<td>Yes:</td>
<td><img src="cross.gif"
width='<?php echo(100*round($yes/($no+$yes),2)); ?>'
height='16'>
<?php echo(100*round($yes/($no+$yes),2)); ?>%
</td>
</tr>
<tr>
<td>No:</td>
<td><img src="cross.gif"
width='<?php echo(80*round($no/($no+$yes),2)); ?>'
height='16'>
<?php echo(80*round($no/($no+$yes),2)); ?>%
</td>
</tr>
</table>
Now the result.txt file (result example). Don't forget to create the GIF image...
1||0
AJAX can also be used in database management. See example below ( you need to create a database first, then those two files) :
<html>
<head>
<script>
function showCategory(str) {
if (str == "") {
document.getElementById("txtHint").innerHTML = "";
return;
} else {
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
document.getElementById("txtHint").innerHTML = this.responseText;
}
};
xmlhttp.open("GET","ajax-database-main.php?q="+str,true);
xmlhttp.send();
}
}
</script>
</head>
<body>
<form>
<select name="Categories" onchange="showCategory(this.value)">
<option value="">Select a subject category :</option>
<option value="1">Ecology</option>
<option value="2">Global News</option>
<option value="3">History</option>
<option value="4">Technology</option>
</select>
</form>
<br>
<div id="txtHint"><b>Result...</b></div>
</body>
</html>
Then :
<!DOCTYPE html>
<html>
<head>
<style>
table {
width: 100%;
border-collapse: collapse;
}
table, td, th {
border: 2px solid black;
padding: 10px;
}
th {
text-align: left;
}
</style>
</head>
<body>
<?php
$q = intval( $_GET[ 'q' ] );
$con = mysqli_connect( 'localhost', 'USER', 'PASSWORD' );
if ( !$con ) {
die( 'Could not connect to : ' . mysqli_error( $con ) );
}
mysqli_select_db( $con, "DATABASE" );
$sql = "SELECT * FROM Category WHERE Category_id = '" . $q . "'";
$result = mysqli_query( $con, $sql );
echo "<table>
<tr>
<th>ID</th>
<th>Subject</th>
<th>Popularity</th>
<th>Year</th>
<th>Articles</th>
<th>Comments</th>
</tr>";
while ( $row = mysqli_fetch_array( $result ) ) {
echo "<tr>";
echo "<td>" . $row[ 'Category_id' ] . "</td>";
echo "<td>" . $row[ 'Category_Subject' ] . "</td>";
echo "<td>" . $row[ 'Category_Popularity' ] . "</td>";
echo "<td>" . $row[ 'Category_Year' ] . "</td>";
echo "<td>" . $row[ 'Category_Articles' ] . "</td>";
echo "<td>" . $row[ 'Category_Comments' ] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close( $con );
?>
</body>
</html>
It is possible to use an XML file with AJAX :
<html>
<head>
<script>
function showMovies(str) {
if (str=="") {
document.getElementById("txtHint").innerHTML="";
return;
}
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=function() {
if (this.readyState==4 && this.status==200) {
document.getElementById("txtHint").innerHTML=this.responseText;
}
}
xmlhttp.open("GET","main-movies.php?q="+str,true);
xmlhttp.send();
}
</script>
</head>
<body>
<form>
Choose a movie in this menu :
<select name="movies" onchange="showMovies(this.value)">
<option value="">Choose a movie:</option>
<option value="E.T.">E.T.</option>
<option value="Indiana Jones">Indiana Jones</option>
<option value="Star Wars">Star Wars</option>
</select>
</form>
<div id="txtHint"><b>All the weekly movies are presented in this menu.</b></div>
</body>
</html>
Then :
<?php
$q=$_GET["q"];
$xmlDoc = new DOMDocument();
$xmlDoc->load("movies.xml");
$x=$xmlDoc->getElementsByTagName('TITLE');
for ($i=0; $i<=$x->length-1; $i++) {
if ($x->item($i)->nodeType==1) {
if ($x->item($i)->childNodes->item(0)->nodeValue == $q) {
$y=($x->item($i)->parentNode);
}
}
}
$movies=($y->childNodes);
for ($i=0;$i<$cd->length;$i++) {
if ($movies->item($i)->nodeType==1) {
echo("<b>" . $movies->item($i)->nodeName . ":</b> ");
echo($movies->item($i)->childNodes->item(0)->nodeValue);
echo("<br>");
}
}
?>
Now the XML file.
As in most languages, the array is one of the fundamental elements of programming field. There are three types with PHP language :
- Numeric. The most basic one...
Example 1 :
<html>
<body>
<?php
$numbers = array( 1, 2, 3, 4, 5 );
foreach ( $numbers as $value ) {
echo "The value is $value <br />";
}
?>
</body>
</html>
Example 2 :
<html>
<body>
<?php
$numbers[ 0 ] = "Canada";
$numbers[ 1 ] = "France";
$numbers[ 2 ] = "Italy";
$numbers[ 3 ] = "USA";
foreach ( $numbers as $value ) {
echo "The value is $value <br />";
}
?>
</body>
</html>
- Associative. The index consists of strings.
Example 1 :
<html>
<body>
<?php
$velocity = array( "Car1" => 12000, "Car2" => 10000, "Car3" => 6000 );
echo "Car 1 price is " . $velocity[ 'Car1' ] . "<br />";
echo "Car 2 price is " . $velocity[ 'Car2' ] . "<br />";
echo "Car 3 price is " . $velocity[ 'Car3' ] . "<br />";
?>
</body>
</html>
Example 2 :
<html>
<body>
<?php
$velocity[ 'Car1' ] = "powerful";
$velocity[ 'Car2' ] = "interesting";
$velocity[ 'Car3' ] = "bad";
echo "Car1 velocity is " . $velocity[ 'Car1' ] . "<br />";
echo "Car2 velocity is " . $velocity[ 'Car2' ] . "<br />";
echo "Car3 velocity is " . $velocity[ 'Car3' ] . "<br />";
?>
</body>
</html>
- Multidimensional. An array which contains several other arrays and values...
Example 1 :
<html>
<body>
<?php
$evaluation = array(
"Car1" => array(
"Velocity" => 80,
"Design" => 90,
"Eco-responsibility" => 40
),
"Car2" => array(
"Velocity" => 70,
"Design" => 60,
"Eco-responsibility" => 60
),
"Car3" => array(
"Velocity" => 50,
"Design" => 40,
"Eco-responsibility" => 30
),
"Car4" => array(
"Velocity" => 30,
"Design" => 30,
"Eco-responsibility" => 20
)
);
echo "Evaluation velocity (on 100%) for Car 1 is : ";
echo $evaluation[ 'Car1' ][ 'Velocity' ] . "<br />";
echo "Evaluation design (on 100%) for Car 2 is : ";
echo $evaluation[ 'Car2' ][ 'Design' ] . "<br />";
echo "Evaluation eco-responsibility (on 100%) for Car 3 is : ";
echo $evaluation[ 'Car3' ][ 'Eco-responsibility' ] . "<br />";
echo "Evaluation eco-responsibility (on 100%) for Car 4 is : ";
echo $evaluation[ 'Car4' ][ 'Eco-responsibility' ] . "<br />";
?>
</body>
</html>
Cookies are basic text files that are stored on the user computer and kept there for tracking (HTTP header).
Example (PHP) :
GET / HTTP/1.0
Connection: Keep-Alive
User-Agent: Mozilla/4.6 (X11; I; Linux 2.2.6-15apmac ppc)
Host: zink.demon.co.fr:2820
Accept: image/gif, */*
Accept-Encoding: gzip
Accept-Language: fr
Accept-Charset: iso-8859-1,*,utf-8
Cookie: name=xyz
Setting cookies script example :
setcookie(name, value, expire, path, domain, security);
Cookies access script example :
<html>
<head>
<title>Cookies Access</title>
</head>
<body>
<?php
echo $_COOKIE["name"]. "<br />";
echo $HTTP_COOKIE_VARS["name"]. "<br />";
echo $_COOKIE["age"] . "<br />";
echo $HTTP_COOKIE_VARS["age"] . "<br />";
?>
</body>
</html>
isset() function example :
<html>
<head> <title>isset() function example</title>
</head>
<body>
<?php
if( isset($_COOKIE["name"]))
echo "Welcome " . $_COOKIE["name"] . "<br />";
else
echo "Sorry... Not recognized" . "<br />";
?>
</body>
</html>
Cookie deletion script example :
<?php
setcookie( "name", "", time()- 60, "/","", 0);
setcookie( "age", "", time()- 60, "/","", 0);
?>
<html>
<head>
<title>Cookie Deletion</title>
</head>
<body>
<?php echo "Deleted Cookies" ?>
</body>
</html>
There is 4 loop types :
- for
- foreach
- while
- do...while
In order to continue reading this page, you have to login or register first...