Posts

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));
    }
}

?>

Read more

MySQL by examples

mysql

Content

  1. Using what query in MySQL it is possible to display several maximum values?
  2. What does the error ‘Too many connections’ mean?
  3. How to provide access to MySQL Denwer from the local network?
  4. How to delete records from a table if their date is 10 days earlier than the current one?
  5. How to use queries with UPDATE MySQL command?
  6. How to output the data from the primary and subordinated table into one by means of UNION MySQL query?

Using what query in MySQL it is possible to display several maximum values?

Example of query:

SELECT field1 FROM Table ORDER BY field1 DESC LIMIT 5

or if there is one value

SELECT MAX(field) FROM Table

What does the error ‘Too many connections’ mean?

Read more

Examples of regular expressions in PHP

PHP Examples of the regular expressions

Content

  1. How to get a substring inside the brackets from the string?
  2. How to substitute a substring inside the brackets in a string taking the ending standing right after the closing bracket?
  3. How to get a value of variable from url using regular expression PHP?
  4. How to cut out text inside a tag?
  5. Exploding, cutting an src attribute of the img tag from the text
  6. Exploding, cutting an img tag from the text together with the attributes

Read more

How to sort a multidimensional array?

php programming

Let’s use the array “goods” of the following form:

<?php

$goods[65] = array("price" => 200, "manufacture" => "Zelina");
$goods[45] = array("price" => 400, "manufacture" => "Devyatkin");
$goods[78] = array("price" => 800, "manufacture" => "Agrarnik");
$goods[89] = array("price" => 790, "manufacture" => "Belyi Orel");

?>

Read more