Results in a Mutiple colums

Discussion in 'MySQL' started by samindika, Jun 2, 2008.

  1. #1
    I need to know how to display results in 2 or more columns.
    Ex; currently my database results dispaly like this.

    Id url
    1 mysql.com
    2 mysql.com
    3 mysql.com
    4 mysql.com

    I want it display like this

    1 mysql.com 2 mysql.com
    3 mysql.com 4 mysql.com

    Here the my code.....please modify it
    <? 
    require("config.php");
    $icat = $_GET['cat'];
    
    mysql_connect($host,$user,$pass) or die("ERROR:".mysql_error());
    mysql_select_db($db) or die("ERROR DB:".mysql_error());
    $max = $npr; 
    $p = $_GET['p'];
    if(empty($p))
    { $p = 1; }
    $limits = ($p - 1) * $max;
    
    
    
    if(isset($_GET['act']) && $_GET['act'] == "view")
    
    { $id = $_GET['id'];
    $sql = mysql_query("SELECT * FROM $table WHERE id = '$id'");
     while($r = mysql_fetch_array($sql))
    { $title = $r['url']; $id = $r['id']; }
    
          }else{
    
        
    
          $sql = mysql_query("SELECT * FROM $table WHERE cat = '$icat' order by id   LIMIT ".$limits.",$max ") or die(mysql_error());
     
     
    
        $totalres = mysql_result(mysql_query("SELECT COUNT(id) AS tot FROM $table WHERE cat = '$icat' order by id DESC"),0);
    
     
    
          $totalpages = ceil($totalres / $max);
    
     
         
    
          while($r = mysql_fetch_array($sql))
    
          {
     
          $url = $r['url'];
    	   $lid = $r['id'];
          echo "$url - $lid<br>";
    
          }
      
          //close up the table
    
    
    
          for($i = 1; $i <= $totalpages; $i++){
    
          //this is the pagination link
      
          echo "<a href='index.php?p=$i&cat=$icat'>$i</a> | ";
    	      }
    		  echo"</div>";
    	}
          ?>
    
    
    PHP:
     
    samindika, Jun 2, 2008 IP
  2. samindika

    samindika Active Member

    Messages:
    336
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #2
    I got the coding from php.net
    Here the code for other
    <?php
    $display = 4;
    $cols = 0;
    echo "<table>";
    while($fetched = mysql_fetch_array($result)){
        if($cols == 0){
            echo "<tr>\n";
        }
        // put what you would like to display within each cell here
        echo "<td>".$fetched['id']."<br />".$fetched['name']."</td>\n";
        $cols++;
        if($cols == $display){
            echo "</tr>\n";
            $cols = 0;
        }
    }
    // added the following so it would display the correct html
    if($cols != $display && $cols != 0){
        $neededtds = $display - $cols;
        for($i=0;$i<$neededtds;$i++){
            echo "<td></td>\n";
        }
         echo "</tr></table>";
        } else {
        echo "</table>";
    }
    ?>
    PHP:
    http://www.php.net/manual/en/function.mysql-fetch-array.php#60962
     
    samindika, Jun 3, 2008 IP