How to Display results in two columns in PHP & MYSQL

Discussion in 'PHP' started by B R Bhatt, Sep 16, 2007.

  1. #1
    Hello Friends how are you ?
    Please try to solve my problems.


    how i can display result in two columns using php and my sql.
    for example, in this way.

    ASP Progamming C Programming
    PHP Progamming C++ Progamming
    Java Programming JSP Programming



    I have used below code that displayed result in single columns. for example

    <?PHP
    include 'library/config.php';
    nclude 'library/opendb.php';


    echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>";

    $query=mysql_query("select *from programmiong") or die("Error Occured,plz try again");

    while($row=mysql_fetch_array($query))
    {
    echo "<tr>";
    echo "<td align='justify' width='100%' cellspacing='2' cellpadding='2' >";
    echo $row['description'];
    echo"</td>";
    echo"</tr>";
    }

    echo"</table>";

    ?>

    OutPut

    ASP Progamming
    PHP Progamming
    Java Programming
    C Programming
    C++ Progamming
    JSP Programming


    Please try to modify above code .
    I am waiting for replys.plz give ur logic and suggestions.

    Thanks,
     
    B R Bhatt, Sep 16, 2007 IP
  2. Pryda

    Pryda Peon

    Messages:
    506
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Try this:


    <?PHP
    include 'library/config.php';
    nclude 'library/opendb.php';


    echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>";

    $query=mysql_query("select *from programmiong") or die("Error Occured,plz try again");
    $left = true;
    while($row=mysql_fetch_array($query))
    {
    if ($left)
    {
    echo "<tr>";
    }
    echo "<td align='justify' width='100%' cellspacing='2' cellpadding='2' >";
    echo $row['description'];
    echo"</td>";
    if (!$left)
    {
    echo"</tr>";
    }
    $left = !$left;
    }

    echo"</table>";

    ?>
     
    Pryda, Sep 16, 2007 IP
  3. B R Bhatt

    B R Bhatt Peon

    Messages:
    25
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks.
    I solved my problems.

    Thanks
     
    B R Bhatt, Sep 16, 2007 IP
  4. stuartdn

    stuartdn Peon

    Messages:
    1
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hi,

    I have a My Sql database containing product information that I need to display on a website. I want the items information to show up side by side in 5 columns at 200 pixels each.

    I have found this forum thread about getting results in two columns. Have tried removing the /tr tag but all that does is load my resuts side by side indefinately. I did try and do something where every fifth record a /tr tag is added but wasn't paticularly successful.

    Here's the code.

    <?php // Dynamically Generates Products
    
    // Address error handling.
    ini_set ('display_errors', 1);
    error_reporting (E_ALL & ~E_NOTICE);
    
    	// Connect and select.
    	if ($dbc = @mysql_connect ('localhost', 'root', 'root')) {
    
    		if (!@mysql_select_db ('Route69')) {
    			die ('<p>Could not select the database because: <b>' . mysql_error() . '</b></p>');
    		}
    
    	} else {
    		die ('<p>Could not connect to MySQL because: <b>' . mysql_error() . '</b></p>');
    	}
    
    	// Define the query.
    
    echo "<table border='0' width='1024px' cellspacing='5' cellpadding='0'>";
    echo "<tr>";
    $query=mysql_query("select *from products WHERE (product_category='Hotrod Zombie Ts') ORDER BY item_no ASC LIMIT 5") or die("Error Occured,plz try again");
    $go=true;
    $one = false;
    $two = false;
    $three = false;
    $four = false;
    $five = false;
    
    while($row=mysql_fetch_array($query))
    {
    if ($go)
    echo "<td align='justify' width='100%' cellspacing='2' cellpadding='2' >";
    echo  "<b>{$row['Product_Name']}<br/></b>
    <img src=\"{$row['Product_Image']}\" alt='{$row['Product_Name']}' width='200' height='150'/><br/>
    		{$row['Product_Code']}<br/>
    		{$row['Product_Sizes']}<br/>
    		{$row['Product_Price']}<br/>
    		{$row['Product_Desc']}<br/>
    		{$row['Product_Category']}<br/></td>";
    		!$one = $one;
    		}
    if ($one)
    {
    $one = !$one;
    !$two = $two;
    $col++;
    }
    
    if ($two)
    {
    $two = !$two;
    !$three = $three;
    $col++;
    }
    
    if ($three)
    {
    $three = !$three;
    !$four = $four;
    $col++;
    }
    
    if ($four)
    {
    $four = !$four;
    !$five = $five;
    $col++;
    }
    
    if ($five)
    {
    $five = !$five;
    $col++;
    }
    
    if ($col==5)
    {echo"</tr>";
    $col=0;
    !$one = $one;
    }
    
    
    echo"</table>";
    
    	mysql_close();
    
    ?>
    Code (markup):
    Please help someone! lol

    Stuart
     
    stuartdn, Sep 13, 2009 IP
  5. hhelen

    hhelen Member

    Messages:
    352
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    33
    #5
    Great stuff! I just found this thread very helpful today. Was working on a simple project and needed to display items on a two column table.

    Good call.
     
    hhelen, Aug 14, 2011 IP
  6. JohnnySchultz

    JohnnySchultz Peon

    Messages:
    277
    Likes Received:
    4
    Best Answers:
    7
    Trophy Points:
    0
    #6
    you can try this one...
    
    
    <?php
    include 'library/config.php';
    include 'library/opendb.php';
    
    
    echo "<table border='0' width='100%' cellspacing='0' cellpadding='0'>";
    
    $query=mysql_query("select *from programmiong") or die("Error Occured,plz try again");
    
    echo "<tr>";
    $cell_ctr = 0;
    while($row=mysql_fetch_array($query))
    {
      if(($cell_ctr % 2) == 0) // 2 is the number of columns
      {
        echo"</tr>";
        echo "<tr>";
        $cell_ctr = 0;
      }
    
      echo "<td align='justify' width='100%' cellspacing='2' cellpadding='2' >";
      echo $row['description'];
      echo "</td>";
      $cell_ctr++;
    }
    if($cell_ctr == 1)
    {
      echo '<td></td>';
      echo "</tr>";
    }
    else if($cell_ctr == 2)
    {
      echo "</tr>";
    }
    
    echo"</table>";
    
    ?>
    
    
    PHP:
     
    JohnnySchultz, Aug 14, 2011 IP
    Bushranger likes this.
  7. Bushranger

    Bushranger Notable Member

    Messages:
    2,841
    Likes Received:
    257
    Best Answers:
    0
    Trophy Points:
    200
    #7
    Cheers, script works great thanks.

    How can you paginate this to only show 100 results per page with links to sub pages?
     
    Bushranger, Nov 1, 2017 IP
  8. Einheijar

    Einheijar Well-Known Member

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    3
    Trophy Points:
    165
    #8
    Einheijar, Nov 2, 2017 IP