Need help pulling a query

Discussion in 'MySQL' started by scottlpool2003, Sep 10, 2012.

  1. #1
    Maybe a little confusing but here goes...

    I'm querying categories/subcategories and have 2 tables 'categories' and 'cat_objects'

    The way they are set out is

    CATEGORY
    object, object [limit 5]

    Now, I need a way to divide the categories/objects equally and split them into 2 so I can display them in side-by-side DIVs

    |CATEGORY|===============|CATEGORY|
    |object,object,object|========|object,object,object|
    ==========================================
    |CATEGORY|===============|CATEGORY|
    |object,object,obect|========|object,object,object|

    I am totally hopeless with using arrays :-(

    Here's what I have so far:

    <?php
    
    //GET CATEGORIES
    
    $result = mysql_query("SELECT * FROM categories");
    
    if(mysql_num_rows($result)!=0)
    {
    while($row = mysql_fetch_array($result))
      {
    $title = $row['title'];
    $catid = $row['id'];
    $caturl = $row['url'];
    
    echo "<h3><a href=\"categories/$caturl/\" title=\"$title Categories\">$title</a></h3>";
    
    
    //Get subcats
    $resultb = mysql_query("SELECT * FROM cat_objects WHERE parent_id = $catid LIMIT 5");
    if(mysql_num_rows($resultb)!=0)
    {
    while($rowb = mysql_fetch_array($resultb))
      {
    $subcattitle = $rowb['title'];
    $url = $rowb['url'];
    echo "<a href=\"/$caturl/$url/\" title=\"$subcattitle\">$subcattitle</a>, ";
    }
    echo "<a href=\"$caturl/\" title=\"View all $title categories\">...</a> 	";
    
    }
    }
    }
    
    ?>
    PHP:
    It displays everything fine but in 1 column, I need it split into 2
     
    scottlpool2003, Sep 10, 2012 IP
  2. scottlpool2003

    scottlpool2003 Well-Known Member

    Messages:
    1,708
    Likes Received:
    49
    Best Answers:
    9
    Trophy Points:
    150
    #2
    Solved

    <?php
    
    //GET CATEGORIES
    
    $numCols = 2;
    
    $result = mysql_query("SELECT * FROM categories");
    
    
    $numPerCol = ceil(mysql_num_rows($result) / $numCols);
    echo "<div style=\"width:364px;\">\n";
    for($col = 1; $col <= $numCols; $col++) {
       echo "<div style=\"width:46.7%;float:left;margin-left:5px;\">";
       for($row = 0; $row < $numPerCol; $row++) {
          $resultRow = mysql_fetch_assoc($result);
          if($resultRow == false) {
             break;
          }
    	$title = $resultRow['title'];
    	$id = $resultRow['id'];
    	$url = $resultRow['url'];
          echo "<h3><a href=\"categories/$url/\" title=\"All $title Categories\">$title</a></h3>";
    	$resultb = mysql_query("SELECT * FROM cat_objects WHERE parent_id = '$id'");
    if(mysql_num_rows($resultb)==0){
      $message = "There are no subcategories";
    }
    
    else 
    {
      $message = "<a href=\"categories/$url/\" title=\"All $title Categories\">...</a>";
    }
    while($rowb = mysql_fetch_array($resultb))
    
      {	
    	     if ($count++ > 0) echo ",";
    	  $titleb = $rowb['title']; 
    	  $urlb = $rowb['url'];
    	  echo "<a href=\"categories/$url/$urlb/\" title=\"$titleb\">$titleb</a>";
      }
    	echo "$message";
       }
       echo "\n</div>\n";
    }
    echo "</div>\n";
    ?>
    PHP:
     
    scottlpool2003, Sep 10, 2012 IP