Showing a message based on the mysql row number..

Discussion in 'PHP' started by crazyryan, Sep 27, 2007.

  1. #1
    Sorry if this post is confusing.

    <ul>
    <li><div class="box">text</div></li>
    <li><div class="box">text</div></li>
    <li><div class="box">text</div></li>
    </ul>
    HTML:
    I've got 3 boxes on each row on my site design, coded like the above. Problem is I can just echo the content from the database out into the box because the coding wouldn't be right, because for every <ul></ul> there has to be three box codes within (see above code).

    So I need to show <ul> before the 1st, 4th, 7th row etc. and show </ul> after the 3rd, 6th, row etc

    I hope you understand that confusing question, all help appreciated.
     
    crazyryan, Sep 27, 2007 IP
  2. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #2
    try something like :
    
    $i=1;
    query here
    while (parse it)
    {
    if ($i%3==0) {echo '</ul>';}
    if ($i%4==0 || $i==1) {echo '<ul>';}
    echo the rest
    $i++;
    }
    
    Code (markup):
    Not sure if the above is error free but you'll get the idea
     
    maiahost, Sep 27, 2007 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Sorry, I don't quite get the code above, can you explain it a bit more, thanks!
     
    crazyryan, Sep 27, 2007 IP
  4. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #4
    even shorter (found a mistake):
    
    $i=1;
    $result=mysql_query("select * from yourtable where something = something");
    while ($row=mysql_fetch_assoc($result))
    {
    if ($i%3==0) {echo '</ul><ul>';}
    if($i==1){echo '<ul>';}
    echo '<li><div class="box">'.$row["thetext"].'</div></li>';
    $i++;
    }
    
    Code (markup):
     
    maiahost, Sep 27, 2007 IP
  5. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #5
    Thanks, when I had 5 rows in a database though, it showed
    <ul> before 2 boxes and </ul> after, instead of 3, the next 3 boxes it showed <ul> to start but didn't end the </ul>
     
    crazyryan, Sep 28, 2007 IP
  6. maiahost

    maiahost Guest

    Messages:
    664
    Likes Received:
    35
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Just play with $i=1 ($i=0?) and make sure you end it with </ul> like :
    
    $i=1;
    $result=mysql_query("select * from yourtable where something = something");
    while ($row=mysql_fetch_assoc($result))
    {
    if ($i%3==0) {echo '</ul><ul>';}
    if($i==1){echo '<ul>';}
    echo '<li><div class="box">'.$row["thetext"].'</div></li>';
    $i++;
    }
    echo '</ul>';
    
    Code (markup):
    Note that it might close the tag twice but it will work.
     
    maiahost, Sep 28, 2007 IP