propellerads adcombo
Dismiss Notice
Welcome to Our Community
Wanting to join the rest of our members? Feel free to sign up today.

PHP loop through database and limit number of entries

Discussion in 'Programming and Scripts' started by gilbertsavier, Aug 4, 2009.

  1. gilbertsavier

    gilbertsavier Affiliate affiliate

    31
    0
    0
    Hello,
    Going to try to explain this as best I can. I have a bunch of stuff in a database and I need to display this info within a table. This isn't a problem at all but, I need it to only display 3 td tags per line and then move on to the row.

    so i need it to do :

    code:

    <table>
    <tr>
    <td>My First Row</td>
    <td>My First Row 2</td>
    <td>My First Row 3</td>
    </tr>

    <tr>
    <td>My Second Row</td>
    <td>My Second Row 2</td>
    <td>My Second Row 3</td>
    </tr>
    </table>



    and continue to do this through every entry in the database table.

    so I have something like this that will display the info:

    code:

    for ($i = 1; $i <= $num; $i++){

    $chairArray = mysql_fetch_array($chairResult);
    $chairName = $chairArray['name'];
    $chairDescription = $chairArray['description'];
    $chairImage = $chairArray['image'];

    echo "<tr>";
    echo "<td>";
    echo "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
    echo "<br />";
    echo $chairName;
    echo "</td>";
    echo "</tr>";


    }
     
  2. myfile

    myfile Affiliate affiliate

    1
    0
    0
    I think this is what you need.
    PHP:
    echo'<table><tr>';

    $a2=0;
    for (
    $i 1$i <= $num$i++){
    $a2++;
    $chairArray mysql_fetch_array($chairResult);
    $chairName $chairArray['name'];
    $chairDescription $chairArray['description'];
    $chairImage $chairArray['image'];

    if(
    $a2==4){echo'</tr><tr>';$a2=1;}
    echo 
    "<td>";
    echo 
    "<img src=\"images/catalog/ottomans/thumbs/$chairImage\" width=\"157\" height=\"147\" />";
    echo 
    "<br />";
    echo 
    $chairName;
    echo 
    "</td>";

    }

    echo
    '</tr></table>';
     
  3. bobbydigital

    bobbydigital Affiliate affiliate

    5
    0
    0
    this is my version :

    PHP:
    echo'<table>';
    $i = 0;
    while($chairArray = mysql_fetch_array($chairResult)){
        $chairName = $chairArray['name'];
        $chairDescription = $chairArray['description'];
        $chairImage = $chairArray['image'];
        if($i == 0){
            echo '<tr>';
        }
        ?>
            <td>
                <img src="images/catalog/ottomans/thumbs/<?=$chairImage?>" width="157" height="147" />
                <br />
                <?=$chairName?>
            </td>
        <?
        $i
    ++;
        if(
    $i 3){
            echo 
    '</tr>';
            
    $i 0;
        }
    }

    if(
    $i <= 3){
        echo 
    '</tr>';
    }

    echo
    '</table>';
     

Featured Resources (View All)

adsxposed