how to display 2 or more records per row?

Discussion in 'PHP' started by Kyriakos, May 3, 2008.

  1. #1
    hi guys,

    i want to display 2 or more records per row. no only one. how i can do this? my php code is this:
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products ORDER BY price");
    ?>
    <table width="200" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#666666">
    <?php while($row = mysql_fetch_assoc($result)) {?>
      <tr>
        <td align="center" bgcolor="#FFFFFF"><?php echo $row['product_id'];?></td>
      </tr>
    <?php } mysql_close($con); ?>
    </table>
    PHP:
    can anyone help me?

    thanks in advance
     
    Kyriakos, May 3, 2008 IP
  2. aminemx

    aminemx Peon

    Messages:
    78
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    replace mysql_fetch_assoc by mysql_fetch_array
    and be work
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products ORDER BY price");
    ?>
    <table width="200" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#666666">
    <?php while($row = mysql_fetch_array($result)) {?>
      <tr>
        <td align="center" bgcolor="#FFFFFF"><?php echo $row['product_id'];?></td>
      </tr>
    <?php } mysql_close($con); ?>
    </table>
    PHP:
     
    aminemx, May 3, 2008 IP
  3. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #3
    Hey,

    I am not trying to hijack your thread but I think you might be looking for the same answer I am and just did not word your question the same.

    Here is what I am trying to do:

    I am using a simple while() loop to print out a list of categories and turn them into links. The format below produces a single column of those links.
    What I would like to do is have them appear in two columns. Is there anyway to do that without reading them to an array first. I'm not even sure how to make it happen after I read them into it.

    Is that what you were asking? And can anyone shed some light for either of us.
     
    Colbyt, May 3, 2008 IP
  4. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #4
    it's dosn't working. i want to set the number of records per row. any other idea?
     
    Kyriakos, May 3, 2008 IP
  5. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Ok since I do this so much:
    
    <?php
    $con = mysql_connect("localhost","username","password");
    if (!$con)
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("storedb", $con);
    
    $result = mysql_query("SELECT * FROM products ORDER BY price");
    ?>
    <table width="200" border="0" align="center" cellpadding="4" cellspacing="1" bgcolor="#666666"><tr>
    <?php while($row = mysql_fetch_array($result)) {
    
    if($i==2){ echo "</tr><tr>"; $i=0;}
    ?>
    
    <td align="center" bgcolor="#FFFFFF"><?php echo $row['product_id'];?></td>
    
    <?$i++;
    }
    echo "</tr>";
    mysql_close($con);
    
    ?>
    </table>
    
    Code (markup):
    Hope that helps all. If not, hit me up via a PM or email me @
     
    NatalicWolf, May 3, 2008 IP
    Colbyt likes this.
  6. Colbyt

    Colbyt Notable Member

    Messages:
    3,224
    Likes Received:
    185
    Best Answers:
    0
    Trophy Points:
    210
    #6
    Thank you very much! Green rep added.

    I had to tweak it just a little but your code was sound. It turned a single column into two perfectly.
     
    Colbyt, May 4, 2008 IP
  7. Kyriakos

    Kyriakos Active Member

    Messages:
    155
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    51
    #7
    thank you very much NatalicWolf. it's working.
     
    Kyriakos, May 4, 2008 IP