SitePoint Sponsor

User Tag List

Results 1 to 6 of 6
  1. #1
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $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:
    PHP Code:
    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

    PHP Code:
    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

  2. #2
    hi galen's Avatar
    Join Date
    Jan 2006
    Location
    New Haven, CT
    Posts
    1,228
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you're going to have to start a counter in the loop. And test it each time
    PHP Code:
    while ($row mysql_fetch_assoc($result)) {
        ++
    $row_num;
        echo 
    $row_num == '<strong>'.$row['title'].'</strong>' $row['title'];
        echo 
    $row['description'];
        echo 
    " <img border='0' src='images" $row['picture']; . ".jpg'> ";


  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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.
    PHP Code:
    while($rows mysql_fetch_assoc($result)){
        if(
    $rows['is_bold'] == 'Y'){
            
    // make the record bold
        
    }
        if(
    $rows['is_special'] == 'Y'){
            
    // then make something different
        
    }

    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    <?php

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

    ++$row_num

    //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'> ";


    //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'> ";

    //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?>

  5. #5
    SitePoint Guru
    Join Date
    Jan 2005
    Location
    heaven
    Posts
    953
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by IamAdam View Post
    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:

    <?php

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

    ++$row_num

    //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'> ";


    //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'> ";

    //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 Code:
    <?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 == '<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 == '<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'> ";
     }
    }
    ?>
    Creativity knows no other restraint than the
    confines of a small mind.
    - Me
    Geekly Humor
    Oh baby! Check out the design patterns on that framework!

  6. #6
    SitePoint Addict
    Join Date
    Apr 2002
    Posts
    395
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thank You!

    This works perfectly


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •