note 47074 added to control-structures.for
| From: | fb_mathman at hotmail dot com | Date: | Tue, 02 Nov 2004 07:20:08 +0000 |
| Subject: | note 47074 added to control-structures.for | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79479@lists.php.net to get a copy of this message | ||
It is intersting how to use a structure that can substitue "for".
In the next lines, for instance, I create a function that get the smallest free_id for a table. It
has good applications for the phpBB forum.
<?php
function assign_free_id($table, $id_field)
{
global $db;
$sql = "SELECT $id_field FROM $table WHERE 1 ORDER BY $id_field ASC";
if( !$result = $db->sql_query($sql) )
{
message_die(GENERAL_ERROR, "Couldn't get id parameters for all the elements of the
table", "", __LINE__, __FILE__, $sql);
}
while ($arr = $db->sql_fetchrow($result))
{
$row[]=$arr;
}
$max_elements = count($row);
if ($max_elements==0)
{
$id= 1;
return ($id);
}
//here I could insert a double-for structure, but I did as it follows, and I think it is more easy
to process.
//local variable defining....
$i=1;
for ( $j=0; $j<$max_elements; $j++)
{
if ($i==intval($row[$j][0]))
{
// ...go to next cycle
$i++;
}
else
{
$id=$i;
return $id;
}
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/control-structures.for.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+47074
Delete -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+47074&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+47074&report=yes
Search -- http://master.php.net/manage/user-notes.php