I need the first ID from a loop.
Loop:
while ($row = $db->sql_fetchrow($result))
{
echo ($row['post_id']) . '<br /><br />';
}
Echo Result:
817856<br /><br />817865<br /><br />817870<br /><br />817871<br /><br />817873<br /><br />817874<br /><br />
In this case I need only the Number 817856
var_dump
var_dump($row['post_id']);
var_dump Result:
string(6) "817856" string(6) "817865" string(6) "817870" string(6) "817871" string(6) "817873" string(6) "817874"
I tried:
reset and array_shift without success. Thank you
You need to use a variable to track the loop and get the first ID while looping. By default you have to assign a variable as true and get the ID inside the loop also make that variable false so that the variable always stay false. Using the IF condition it will be OK.
$flag = true;
while ($row = $db->sql_fetchrow($result)){
if($flag === true){
$ID = $row['post_id'];
$flag = false;
}
echo ($row['post_id']) . '<br /><br />';
}
echo $ID; // 817856
If you need just the first ID then it will be shorter version. Cause the query only returns the first row when we don't use the loop.
$row = $db->sql_fetchrow($result);
echo $row['post_id']; //817856
You can do something like this
$i = 0;
$result;
while ($row = $db->sql_fetchrow($result))
{
if ($i > 0)
{
break;
}
echo ($row['post_id']) . '<br /><br />';
$result = $row['post_id'];
i++;
}