Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New Coder
    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts

    Help splitting this output across 2 or more columns

    I have the below code to pull data from my simple mySQL db. How can I get the two results split between columns? Right now it just has the category with the ad names below. Some categories will only have one name, so I guess it's a division of the total output between columns. Not a set number per columns. So, not cat 1 cat 2 with cat 3 and cat 4 underneath. Flow an even number between columns, so it goes down col 1, to col 2, to col 3 if necessary and so on.

    Code:
    	<?php
    		$query = "select * from category";
    		 $result = mysql_query($query);
    
    		 while($row = mysql_fetch_array($result)) {
    			$categoryid = $row['id'];
    			$categoryname = $row['category'];
    				$query2 = "select * from advertisers where category_id='$categoryid' and active=1";
    				$result2 = mysql_query($query2);
    
    		 if(mysql_num_rows($result2) > 0 ) {
    			 echo "<h1>".$categoryname."</h1>";
    				while($row2 = mysql_fetch_array($result2)) {
    				$advertisername = $row2['name'];
    			 echo $advertisername. "<br />";
       		   }
    
          } else {
        	  echo "No advertisers found for this category.";
       			   }	  
          }
    	?>
    Thanks,
    Donna
    Last edited by dmac68; 07-27-2008 at 02:30 PM. Reason: Better explanation of desired output

  • #2
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    I think you are looking for CSS. You can't "flow" columns using PHP - you can retrieve the data which is to be in the columns - but you can't really position them without CSS.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #3
    New Coder
    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts
    Actually, I saw something, I'm just not sure how to apply. Here's the code I found:

    PHP Code:
    $query "SELECT stuff, morestuff FROM mystuff ORDER BY stuff";
    $result mysql_query($query);

    $num_rows mysql_num_rows($result);


    $rows ceil($num_rows $columns);

    while(
    $row mysql_fetch_array($result)) {
        
    $data[] = $row['stuff'];
        
        
    //store the other field into an array
        
    $data2[] = $row['morestuff'];
    }

    echo 
    "&lt;TABLE BORDER=\"0\"&gt;\n";

    for(
    $i 0$i &lt$rows$i++) {

        echo 
    "&lt;TR&gt;\n";
        
        for(
    $j 0$j &lt$columns$j++) {
            if(isset(
    $data[$i + ($j $rows)])) {
                echo 
    "&lt;TD&gt;" $data[$i + ($j $rows)] . "&lt;/TD&gt;\n";
                
                
    //echo out the field
                
    echo "&lt;TD&gt;" $data2[$i + ($j $rows)] . "&lt;/TD&gt;\n";
            }
        }
        echo 
    "&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?> 
    Any direction on how I can work that with what I have?

    So, it would be pretty much like this on display

    Cate1 CatNumber?
    Name Name
    Name Name
    Cate2 CatNumber?
    Name Name
    Cate3 CatNumber?
    Name Name
    Name CatNumber?
    Cate4 Name
    Name Name
    Cate5
    Name


    Thanks,
    Donna
    Last edited by dmac68; 07-27-2008 at 08:34 PM. Reason: Use php wrapper

  • #4
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    Why not do exactly what you posted on the bottom of your last post? Use a switch statment, processes everything which falls under that specific case, and then move on to the next?
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #5
    New Coder
    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts
    I'm not exactly sure what that means

    I'm very new to this.

    I tried to combine like below, but got no output

    PHP Code:
    <?php require_once("includes/connection.php"); ?>

        <?php
        $columns 
    4;
            
    $query "select * from category";
             
    $result mysql_query($query);
             
    $num_rows mysql_num_rows($result);
             
             
    $rows ceil($num_rows $columns);

             while(
    $row mysql_fetch_array($result)) {
                
    $categoryid $row['id'];
                
    $categoryname $row['category'];
                    
    $query2 "select * from advertisers where category_id='$categoryid' and active=1";
                    
    $result2 mysql_query($query2);

             echo 
    "&lt;TABLE BORDER=\"0\"&gt;\n";

    for(
    $i 0$i &lt$rows$i++) {

        echo 
    "&lt;TR&gt;\n";
        
        for(
    $j 0$j &lt$columns$j++) {
            if(isset(
    $query[$i + ($j $rows)])) {
                echo 
    "&lt;TD&gt;" $query[$i + ($j $rows)] . "&lt;/TD&gt;\n";
                
                
    //echo out the field
                
    echo "&lt;TD&gt;" $query2[$i + ($j $rows)] . "&lt;/TD&gt;\n";
            }
        }
        echo 
    "&lt;/TR&gt;\n";
    }
    echo 
    "&lt;/TABLE&gt;\n";
    ?>
    Thanks,
    Donna

  • #6
    Regular Coder BWiz's Avatar
    Join Date
    Mar 2006
    Location
    Sol System
    Posts
    471
    Thanks
    7
    Thanked 21 Times in 21 Posts
    What I'am saying, is to use a switch statment, something like this:
    PHP Code:
    switch($VarThatSaysWhichRow)case 'category1':       // do something       break;case // and so on 
    Rearrange your code so it processes your data case by case.
    EDIT:Sorry, I'm on my smartphone right now, so I can't really help you with code, just theory.
    Last edited by BWiz; 07-27-2008 at 09:30 PM.
    BWiz :: Happy Coding!
    2006
    2007 2008 2009
    2010 2011
    Irrational numbers make no sense.

  • #7
    New Coder
    Join Date
    Jul 2008
    Location
    Maryland
    Posts
    34
    Thanks
    6
    Thanked 0 Times in 0 Posts
    You are quick, Bwiz.

    I'll try to figure out what you posted.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •