Forum Moderators: coopster

Message Too Old, No Replies

compare field values across iterations

php mysql - current name -vs- previous name

         

Phatzo

6:21 am on Mar 11, 2006 (gmt 0)

10+ Year Member



Need some help here guys! Any help would rock :)

Im writing a banner list script...user taps in what type of image they're looking for, and the script returns a list. I can do everything I need to do except one thing: categorize the results... What I want to produce is something like this:

Site----Name & Link---
heading: Site Name #1
Site1---banner1----
Site1---banner2----
heading: Site Name #2
Site2---banner3----
Site2---banner4----

Basically, I want to run my for statement, and say something like

if($Name[this_array_reference]!= $Name[this_previous_array] )
{ echo "Heading: Site[this_array_reference]";

The cut down version of what Im running is this:


echo "<table><tr>";
for($i = 0;$row = mysql_fetch_array($result);$i++)
{
echo "<td>".$row['Name_TE']."</td>\n";
echo "<td>".$row['img_name']."</td>\n";
}
echo "</tr></table>";

I just cannot seem to get the right code to check the current iteration value of name against the previous iteration value of name.

Thanks ;)
Phatzo

coopster

5:45 pm on Mar 11, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Looks like you have it to me, just get it inside the loop. I modified the syntax and while loop a bit but it should be fairly self-explanatory ... every time a new site is being read from the result set, you output the heading:
echo "<table><tr>"; 
$previous = false; // initialize
while [php.net] ($row = mysql_fetch_array($result)) {
if ($row['site'] !== $previous) {
echo "Heading: {$row['site']}";
$previous = $row['site'];
}
echo "<td>".$row['Name_TE']."</td>\n";
echo "<td>".$row['img_name']."</td>\n";
}
echo "</tr></table>";