TEENFRONT Posted April 23, 2008 Share Posted April 23, 2008 Hey Il jump straight in with my question. I have a while() loop outputting info from the db but im having trouble telling if a certain value has changed from the last loop, and if it is different i want to show an image. Showing an image is not the problem, just the telling if the value is different to the last loop. OK heres my code $date = explode(" ", $date); // This splits the date and time (EG 22/01/08 22:15) field into just the date giving me $date[0] in this format DD/MM/YYYY $jobday = explode("/", $date[0]); // This splits the $date[0] to just the first set of numbers so it outputs DD (EG 22) as $jobday[0] echo $jobday[0]; so it loops through the results and $jobday[0] will output the following results in 6 loops along with an image EG 22 img 22 img 22 img 21 img 21 img 20 img how do i tell if the number has changed between loops? So i only insert an img if the number is different from the last....sooo like this 22 img 22 22 21 img 21 20 img I tried things like if($jobday[0] != $jobday[0]) but as $jobday[0] is set inside my loop, $jobday[0] with always equal $jobday[0]. And if i try setting $jobday[0] outside of the while loop then obviosly its not set right for each loop... Any help????? Im going mad Link to comment https://forums.phpfreaks.com/topic/102481-checking-if-the-value-has-changed-in-a-while-loop-help/ Share on other sites More sharing options...
kenrbnsn Posted April 23, 2008 Share Posted April 23, 2008 You need to use another variable to store the previous value: <?php $temp = array(); for ($i=0;$i<6;$i++) $temp[] = rand(20,24); sort($temp); $prev = ''; foreach ($temp as $v) { echo $v; echo ($v != $prev)?' something':''; echo '<br>'; $prev = $v; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/102481-checking-if-the-value-has-changed-in-a-while-loop-help/#findComment-524771 Share on other sites More sharing options...
TEENFRONT Posted April 23, 2008 Author Share Posted April 23, 2008 Hey Thanks for that. Wooooooosh straight over my head. Any additional help would be appreciated. This is a cut down version of my code, how would i implement kenrbnsn's example above? while($r=mysql_fetch_array($result)) { $jobday = explode("/", $date[0]); echo $jobday[0]; echo $image; } So the above will output this (say there is 6 records to loop through from the db) 22 img 22 img 21 img 21 img 20 img 20 img i only want to show "img" if the date number if different to the previous date. kenrbnsn's example looks like what i need when i run it in isolation but i dont know how to fit that into my while loop to work? Link to comment https://forums.phpfreaks.com/topic/102481-checking-if-the-value-has-changed-in-a-while-loop-help/#findComment-524794 Share on other sites More sharing options...
kenrbnsn Posted April 23, 2008 Share Posted April 23, 2008 Try: <?php $prev = ''; while($r=mysql_fetch_array($result)) { $jobday = explode("/", $date[0]); echo $jobday[0]; if ($jobday[0] != $prev) echo ' ' . $image . '<br>'; $prev = $jobday[0]; } ?> Ken Link to comment https://forums.phpfreaks.com/topic/102481-checking-if-the-value-has-changed-in-a-while-loop-help/#findComment-524834 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.