$row = mysql_fetch_assoc($result) display issue

I am using $row = mysql_fetch_assoc($result) to grab data from my database, and then display all my data with a loop. The problem is that with a loop all the display looks the same. But I want some of my results to look different.

For example, I want the name of the 1st row to be in bold, I want the 5th result to have a bigger picture, etc

My code is:

while ($row = mysql_fetch_assoc($result)) {

echo $row['title'];
echo $row['description'];
echo " <img border='0' src='images" . $row['picture']; . ".jpg'> ";

}

So, this shows me all my rows in the same format. But I want some results, such as the first result, to be formatted a little differently.

I tried something like

if $row[0] 
{
echo "<b> $row['title'] </b>";
echo  "FIRST ONE";
echo  $row['description'];
echo " <img border='8' src='images" . $row['picture']; . ".jpg'> ";
}

But it doesn’t seem to work

you’re going to have to start a counter in the loop. And test it each time


while ($row = mysql_fetch_assoc($result)) {
    ++$row_num;
    echo $row_num == 1 ? '<strong>'.$row['title'].'</strong>' : $row['title'];
    echo $row['description'];
    echo " <img border='0' src='images" . $row['picture']; . ".jpg'> ";
} 

In such cases, what i would do is have a (or more) flag fields which are to be bold since making a record bold means that should have something different meaning (like is special product=Y then make the product bold or different color in case of ecommerce app.). So that you can have easily done such anything in the loop.


while($rows = mysql_fetch_assoc($result)){
	if($rows['is_bold'] == 'Y'){
		// make the record bold
	}
	if($rows['is_special'] == 'Y'){
		// then make something different
	}
}

Thanks for your help.

But, I don’t seem to be able to get it to work.

rajug, the products don’t have anything different, they are in order of newest to oldest, and I want to have some products to stand out, based on when they were added to the system.

galen, your code seems to be what I’m looking for, but I can’t get it to work, this is what I have:

[COLOR=#000000][COLOR=#007700]<?php

while ([/COLOR]$row = mysql_fetch_assocCOLOR=#007700) {

[/COLOR][/COLOR]++$row_num

[COLOR=#000000]//this is how the result should look for all products
echo
$row[‘title’];
echo
$row[‘description’];
echo
" <img border=‘0’ src='images" . $row[‘picture’]; . ".jpg’> "[COLOR=#007700];

[/COLOR][/COLOR]
//this is how the result should look for 1st product[COLOR=#000000]
echo $row_num == 1 ? ‘<strong>’.$row[‘title’].‘</strong>’ : $row[‘title’];
echo
“FIRST ONE”;
echo
$row[‘description’];
echo
" <img border=‘8’ src='images" . $row[‘picture’]; . ".jpg’> "[COLOR=#007700];

[/COLOR][/COLOR]//this is how the result should look for 5th product
echo $row_num == 5 ? ‘<b>’.$row[‘title’].‘</>’ : $row[‘title’];
echo
“FIVE”;
echo
$row[‘description’];
echo
" <img border=‘8’ src='images" . $row[‘picture’]; . ".jpg’> ";

}

php?>

You probably want to use if{}else{} rather than the ternary operator “?”.


<?php
while ($row = mysql_fetch_assoc($result)) {
 ++$row_num;
 if($row_num == 1)
 {
  //this is how the result should look for 1st product
  echo $row_num == 1 ? '<strong>'.$row['title'].'</strong>' : $row['title'];
  echo "FIRST ONE";
  echo $row['description'];
  echo " <img border='8' src='images" . $row['picture']; . ".jpg'> ";
 }
 else if($row_num == 5)
 {
  //this is how the result should look for 5th product
  echo $row_num == 5 ? '<b>'.$row['title'].'</>' : $row['title'];
  echo "FIVE";
  echo $row['description'];
  echo " <img border='8' src='images" . $row['picture']; . ".jpg'> ";
 }
 else
 {
  //this is how the result should look for all products
  echo $row['title'];
  echo $row['description'];
  echo " <img border='0' src='images" . $row['picture']; . ".jpg'> ";
 }
}?>

Thank You!

This works perfectly :slight_smile: