td and mysqli results

Discussion in 'PHP' started by Nora, Mar 25, 2008.

  1. #1
    I'm trying to figure out how to put two rows from a table beside each other like this:

    <table><tr><td>row1</td><td>row2</td></tr>
    <tr><td>row3</td><td>row4</td></tr>
    etc..</table>
    Code (markup):
    Right now I have this:
    <tr><td><p><? echo "$name"; ?></p></td></tr>
    Code (markup):
    and it only shows the name of the rows below each other..
    I've been searching everywhere but I can't seem to find a way to get this done :S
     
    Nora, Mar 25, 2008 IP
  2. jestep

    jestep Prominent Member

    Messages:
    3,659
    Likes Received:
    215
    Best Answers:
    19
    Trophy Points:
    330
    #2
    You need to use the modulus operator '%' when you are looping.

    Something like:
    
    
    $i = 0;
    $rows = mysql_num_rows($query);
    $columns = 2; //enter number of columns you want
    
    while($row = $mysql_fetch_array($query))
    {
    
    if(($i%$columns==0) || ($i == 0)){
    echo "<tr>";
    }
    
    echo "<td>".$row['stuff']."</td>";
    
    if(($i%$columns==0) || ($i == $rows)){
    echo "</tr>";
    }
    
    $i++;
    
    }
    
    
    PHP:
    If you're using mysqli it would be:

    
    
    $query = $db->query("SELECT STUFF HERE...");
    
    $i = 0;
    $rows = $query->num_rows;
    $columns = 2; //enter number of columns you want
    
    while($row = $query->fetch_assoc())
    {
    
    if(($i%$columns==0) || ($i == 0)){
    echo "<tr>";
    }
    
    echo "<td>".$row['stuff']."</td>";
    
    if(($i%$columns==0) || ($i == $rows)){
    echo "</tr>";
    }
    
    $i++;
    
    }
    
    
    PHP:
     
    jestep, Mar 25, 2008 IP
    Nora likes this.
  3. Nora

    Nora Well-Known Member

    Messages:
    2,105
    Likes Received:
    76
    Best Answers:
    0
    Trophy Points:
    140
    #3
    It works, thank you so much :)
     
    Nora, Mar 25, 2008 IP