note 47074 deleted from control-structures.for by felipe
| From: | felipe@php.net | Date: | Tue, 29 Jan 2008 11:22:04 +0000 |
| Subject: | note 47074 deleted from control-structures.for by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135518@lists.php.net to get a copy of this message | ||
Note Submitter: fb_mathman at hotmail dot com
----
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;
}
}
}
?>