I'm a newbie in PHP, so forgive me for a basic question.
In a While
statement, is there a way to know if a value in a row is equal to a value in the previous row?
Just store it:
$get = mysql_query("...");
$previous = '';
while ($row = mysql_fetch_assoc($get)) {
$current = $row['...'];
if ($current == $previous) {
// do stuff
}
$previous = $current;
}
$get = mysql_query("...");
$previous = '';
while ($row = mysql_fetch_assoc($get)) {
$current = $row['...'];
if ($current == $previous) {
// do stuff
}
$previous = $current;
}
This example won't work correctly. It will always skip the first record in the array. The first time through $previous will be blank so $current and $previous won't be equal. $current will have to blank the first pass, just as previous. $current will have to be made equal inside the if loop.
$get = mysql_query("...");
$previous = '';
$current = '';
while ($row = mysql_fetch_assoc($get)) {
if ($current == $previous) {
$current = $row['...'];
// do stuff
}
$previous = $current;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With