PHP examples (example source code)

php programming

Content

  1. How to create automatic counter of record number in MySQL table using PHP?
  2. How to delete all files in a folder?
  3. Is it possible in PHP to replace blocks if…else with another conditional operation?
  4. How to delete strings from a text file using the PHP language?

How to create automatic counter of record number in MySQL table using PHP?

For this, we’ll use the following function:

<?php

// The function is designed for obtainment of record number of the table 
function get_id($tbl_dt)
{   // Do descending sorting 
    $result = mysql_query("SELECT id FROM ".$tbl_dt." ORDER BY `id` DESC");   
    $myrow = mysql_fetch_array($result);
    $num_rows = mysql_num_rows($result);
    
    // if a number of records coincide with the last id, then...
    if ($num_rows==$myrow['id'])
    {  // ...take the first element  
      //in the sampling with the highest id value ,
     // by increasing this value by a unity 
       return $myrow['id'] + 1;
       } else { // Do ascending sorting, then...
        $res1=mysql_query("SELECT id FROM ".$tbl_dt." ORDER BY `id` ASC");
        $myrow1 = mysql_fetch_array($res1);
        $i = 1;
        do // ...look for "empty" id and add a record for this id
        {
            if ($i == $myrow1['id']) {
                $i++;
                continue;
            } else {
                return $i;
            }
        }
        while ($myrow1 = mysql_fetch_array($res1));
    }
}

?>

Now put it into practice:

<?php

$id = get_id($tbl_dt);
$answer = addslashes($answer);
$result = mysql_query ("INSERT INTO faq (id,post,question,answer) 
            VALUES ($id,'$post','$question','$answer')");
if ($result == 'true') {
    echo "
Your question has been successfully added!
";
} else {echo "
Your question has not been added!
";} 

?>

How to delete all files in a folder?

<?php

// This function is designed for the removal of all files in a folder 
function del_all_files()
{
    $dir = 'https://site.softmaker.kz/tmp/';
    //    delete all files in a folder 
    if($handle = opendir($dir))
    {
            while(false !== ($file = readdir($handle)))
                    if($file != "." && $file != "..") unlink($dir.$file);
            closedir($handle);
    }
} 

?>

Is it possible in PHP to replace blocks if…else with another conditional operation?

Programming language РНР offers the opportunity to replace blocks if…else with a conditional operation
(in contrast to unary and binary operations, a conditional operation is used with three operands).
In this conditional operation there are two symbols «?» and «:» and three parameters:

<?php
(expression_1) ? expression_2 : expression_3
?>

First, the value of expression_1 is calculated. If it is true, the value of expression_2 is calculated,
which becomes a result. If at the calculation of the value expression_1 is false, the result is accepted to be expression_3.
For example it is possible to write the following expression:

<?php

$myrest = ($rest == 1) ? "" : "-";

?>

In this example, the variable $myrest is assigned with empty string, if the variable $rest equals 1,
otherwise the variable $myrest is assigned with dash “-“.

How to delete strings from a text file using the PHP language?

Suppose we need to delete all strings from the text with the word «php».

<?php
//We delete the string from the file
// with the word «php»
$stroka = 'php'; 
// Let's read all file in the array 
$file = file("php_text.txt"); 
$i = 0;
// Let's find the needed string 
while ($i < sizeof($file)) {
    if (strstr($file[$i], $stroka) <> FALSE){
        unset($file[$i]);
    }
    $i++;
}
// Open the file and 
// save everything that is left in it 
$f = fopen("php_text_out.txt", 'w+');
flock($f, LOCK_EX);
foreach($file as $string)
{
        fwrite($f, $string);
}
flock($f, LOCK_UN);
fclose($f);
?>

See also similar procedure for

correct deletion of strings of the text file using the built-in languate of the system 1С8 (russian)
.