Displaying something every 4 rows

Discussion in 'PHP' started by crazyryan, Jul 9, 2007.

  1. #1
    Basically I have the following code, I want to display a message every 4 games/rows. Help very much appreciated :)
    game
    game
    game
    game
    message
    game etc

    
    $sql = mysql_query("SELECT * FROM ava_cats ORDER BY name");
    while($row = mysql_fetch_array($sql)) {
    echo '<div class="style3" id=itemheader>'.$row['name'].'</div>';
    $sql2 = mysql_query("SELECT * FROM ava_games WHERE catergory_id=".$row['id']." AND published=1 ORDER BY id desc");
        echo '<table border="0" width="98%">
       <tr>';
       // gamenumber is used for table columns
       $gamenumber="1";
       while($row2 = mysql_fetch_array($sql2)) {
    if (strlen($row2['description']) > 60) {
    $description = substr($row2['description'], 0, 60)."...";}
    else {
    $description = $row2['description'];
    }
    if (strlen($row2['name']) > 23) {
    $name = substr($row2['name'], 0, 23)."...";}
    else {
    $name = $row2['name'];
    }
    // this is what is output when viewing a cat
    $abcd= $row2['name'];
    $abcd = str_replace (" ", "-", $abcd);   
    
    if ($seo_on == 0) {$url = ''.$site_url.'/index.php?task=view&id='.$row2['id'].'';}
    else {$url = ''.$site_url.'/view/'.$row2['id'].'/'.$abcd.'.htm';}
     echo '    <td width="33%" valign="top">
    <table width="95%" border="0" cellspacing="0" cellpadding="0" class="back">
      <tr>
        <td class="td3" width="1%"><div align="center"><a href="'.$url.'">';
        if ($row2['import'] == 1) {
        echo '<img src="'.$site_url.'/games/images/'.$row2['url'].'.png" width="'.$image_width.'" height="'.$image_height.'" alt="" />';}else {echo '<img src="'.$row2['image'].'" width="'.$image_width.'" height="'.$image_height.'" alt="" />';}
        echo '</a></div></td>
        <td valign="top"><table width="100%" border="0" cellspacing="0" cellpadding="0">
          <tr>
            <td class="td1">&nbsp;<a href="'.$url.'">'.$name.'</a></td>
            </tr>
          <tr>
            <td width="70%" valign="top" class="td5">'.$description.'</td>';
            $link = $row2['id'];               
    $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM ava_comments WHERE link_id='$link'"),0);
        echo ' <tr>
            <td class="td2">Times Played: '.$row2['hits'].' |<a href="'.$url.'">Comments ('.$total_results.')</a>';
           
    
            echo '</td>
            </tr>       
        </table></td>
      </tr>
      </tr>
    </table>
        </td>';
    PHP:
     
    crazyryan, Jul 9, 2007 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    
    
    $int = 0;
    
    while (/* condition */)
    {
        if (++$int % 4 == 0)
        {
            // Display message
        }
    }
    
    PHP:
     
    nico_swd, Jul 9, 2007 IP
  3. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #3
    Sorry, confused - where would I put that?
     
    crazyryan, Jul 9, 2007 IP
  4. Cloudberries

    Cloudberries Peon

    Messages:
    74
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #4
    It's just an example of a while loop that satisfies a condition every 4th count, using the modulus (%) operator - modulus can also be thought of as the remainder after a division. (i.e. 2%4 = 2, 3%4 = 3, 4%4 = 0, 5%4 = 1) - it's not specific code suited to your example, just a demonstration of a technique that would work in your case.

    You can use this to output a specific thing once only every 4 repetitions. The "while" loop should surround everything you want to output one after the other (the list of games). The "if" condition should only surround the code you want to output every 4 lines (the message)

    ---in your case, the while *condition* would be the mysql array fetching -
    while($row = mysql_fetch_array($sql)) {
    Code (markup):
     
    Cloudberries, Jul 9, 2007 IP