Help with PHP syntax

Hope someone can help out again with a bit of syntax.

I have a bit of code that acts a little loop displaying records from a table :


 <?php 
     $groups = array(); 
while ($row = mysql_fetch_assoc($rs2011nominees)) { 
    $groups[$row['Alphabet']][] = $row; 
} 
foreach ($groups as $alphabet_letter => $rows) { 
    echo "<tr><td><br><h2>$alphabet_letter</h2><br></td></tr>"; 
    foreach ($rows as $row) { 
        echo "<tr><td><a href=\\"http://www.goodsafariguide.com/hit_counter2.php?LodgeID=" . $row['LodgeID'] . " \\" >" . $row['Lodge'] . " </a></td></tr>";
    } 
} 
?>

I’d like to add in another field next to the Lodge field, complicated by the fact that its to display an image. (The field is winner10, and just contains the string winner10.gif which references the gif.)

So that would normally look like :


<img src="../images/<?php echo $row['winner10']; ?>"  alt="Image Name" border="0"/>

But I’m not sure how to add that in to the existing code, so if anyone could for whom its second nature could help out, I’d be very grateful.

Thanks.

See the part in red


echo "<tr><td><a href=\\"http://www.goodsafariguide.com/hit_counter2.php?LodgeID=" . $row['LodgeID'] . " \\" >" . $row['Lodge'] . " </a>[B][COLOR="Red"]<img src=\\"../images/" . $row['winner10'] . "\\"  alt=\\"Image Name\\" border=\\"0\\"/>[/COLOR][/B]</td></tr>";

Thank you - that looks OK, in that I’m not getting any syntax errors in DW.

But the image isn’t showing :

Its the little gold circle with the 10 in it above the tabs for the years, which I added just as a regular image using :


<img src="../images/lodgeawardicons/10winner.gif" alt="2010 Winner" width="20" height="16" />

Just to check the path.

There is another subfolder /lodgeawardicons/ so the PHP part is :


 <?php 
     $groups = array(); 
while ($row = mysql_fetch_assoc($rs2011nominees)) { 
    $groups[$row['Alphabet']][] = $row; 
} 
foreach ($groups as $alphabet_letter => $rows) { 
    echo "<tr><td><br><h2>$alphabet_letter</h2><br></td></tr>"; 
    foreach ($rows as $row) { 
        echo "<tr><td><a href=\\"http://www.goodsafariguide.com/hit_counter2.php?LodgeID=" . $row['LodgeID'] . " \\" >" . $row['Lodge'] . " </a><img src=\\"../images/lodgeawardicons/" . $row['winner10'] . "\\"  alt=\\"Image Name\\" border=\\"0\\"/></td></tr>";
    } 
} 
?>

So that all looks like it should work…

Because the image is called 10winner.gif
And the value of $row[‘winner10’] is winner10.gif

D’oh! What is it they say about not seeing the wood for the trees? :slight_smile:

Thank you very much for that.

I do have one last question though, as I’ve noticed that its not ordering the results alphabetically within each group :


<?php 
     $groups = array(); 
while ($row = mysql_fetch_assoc($rs2011nominees)) { 
    $groups[$row['Alphabet']][] = $row; 
} 
foreach ($groups as $alphabet_letter => $rows) { 
    echo "<tr><td><br><h2>$alphabet_letter</h2><br></td></tr>"; 
    foreach ($rows as $row) { 
		echo "<tr><td><a href=\\"http://www.goodsafariguide.com/hit_counter2.php?LodgeID=" . $row['LodgeID'] . " \\" >" . $row['Lodge'] . " </a>
		<img src=\\"../images/lodgeawardicons/". $row['winner10']. "\\"  alt=\\"Image Name\\" border=\\"0\\"/>
		<img src=\\"../images/lodgeawardicons/". $row['runnerup10']. "\\"  alt=\\"Image Name\\" border=\\"0\\"/>
		<img src=\\"../images/lodgeawardicons/". $row['finalist10']. "\\"  alt=\\"Image Name\\" border=\\"0\\"/>
		</td></tr>";
    } 
} 
?>

And I have a non PHP issue of spacing the icons out equally. I’ve created a 1x1px spacer.gif to avoid showing the blue boxed ? where no image is found. But that results in differing gaps depending on which icons a record will have next to it.

The ordering must be done in the query that extracts the data from the database (ORDER BY).
And if you want to add from 0-3 images, then don’t use a “fixed” line, but put it together using if’s to check which images you want to add.

Thanks - I’m used to just having my queries just order by one field, so I’ve just changed my query to :

SELECT * FROM lodges ORDER BY Alphabet, Lodge Asc

and that seems to have fixed that, so that was painless enough.

The images thing I’m less sure about. I get the idea - presumably its a series of IF… checking if the field is NULL or not, and if so, to display the image?

Yes exactly that :tup:
Also one thing about your query, be specific as to what fields to retrieve from the database instead of using * to get EVERYTHING EVERYTIME it is is queried.

Saves time, resources and its easier to manipulate.

Yeah - I’ve read that before about the queries - old habits and all!

I’ve also had a rethink about the icons - instead of having specific fields for each year’s winner, runner up etc, I’ll just create ten or so awards fields that can be populated with whichever text is required. That way, there won’t be any gaps in the spacing.