Outputing the result of a query in 3 columns? PLEASE HELP

Discussion in 'PHP' started by romic, Apr 1, 2007.

  1. #1
    hello to all

    I am not sure if I should have posted this here or in "Databases" but this is related to PHP also, so:

    I have a PHP script that contains a query to a MySQL database and it outputs the result of the query in a table with one column(and one row). Now what I want to do is split the result to 3 columns, so, if for example the query would return 20 results I would like them split as follows: 1st column (1 row) - 7 results, 2nd column (1 row) - 7 results, 3rd column (1 row) - 6 results.


    Any ideas on how this can be done would be appreciated.
     
    romic, Apr 1, 2007 IP
  2. Ryandarin

    Ryandarin Peon

    Messages:
    66
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Ryandarin, Apr 1, 2007 IP
  3. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks for your reply. To tell you the truth I'm familiar with what is written at http://www.freewebmasterhelp.com/tutorials/phpmysql/4 and http://www.freewebmasterhelp.com/tutorials/phpmysql/5 but unfortunately that's not what I'm looking for.

    The script I need should return the result in the following form:

    <table><tr>
    <td>
    result 1<br />
    result 2<br />
    result 3<br />
    result 4<br />
    result 5<br />
    result 6<br />
    result 7<br /></td>
    <td>
    result 8<br />
    result 9<br />
    result 10<br />
    result 11<br />
    result 12<br />
    result 13<br />
    result 14<br /></td>
    <td>
    result 15<br />
    result 16<br />
    result 17<br />
    result 18<br />
    result 19<br />
    result 20<br /></td>
    </tr><table>
     
    romic, Apr 1, 2007 IP
  4. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #4
    That's a really interesting problem that I always wanted to solve. I will work on it tonight for myself actually and if by tomorrow this thread will still be in need for something like this I will paste it.
     
    manilodisan, Apr 1, 2007 IP
  5. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Try this

    $items = array('ALABAMA','ALASKA');//create an array with your data
    
    // Default # of Columns
    $numcols = 4;
    
    // Number of Items
    $numitems = count($items);
    
    // Number of Rows
    $numrows = ceil($numitems/$numcols);
    
        echo '<table>';
        for ($row=1; $row <= $numrows; $row++)
        {
    		$cell = 0;
    		echo ' <tr>'."\n";
            for ($col=1; $col <= $numcols; $col++)
            {
    		echo '  <td>'."\n";
    
            if ($col===1)
    		{
            	$cell += $row;
                print $items[$cell - 1];
        	}
    		else {
            	$cell += $numrows;
                print $items[$cell - 1];
            }
    		echo '  </td>'."\n";
        	}
    		echo ' </tr>'."\n";
    	}
    	echo '</table>';
    PHP:
     
    manilodisan, Apr 1, 2007 IP
  6. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #6
    Thank you. The script you provided works. But I need it a bit different and look how I changed it

    
    // Default # of Columns
    $numcols = 4;
    
    // Number of Items
    $numitems = mysql_num_rows($csq);
    
    // Number of Rows
    $numrows = ceil($numitems/$numcols);
    
    	while($csq1=mysql_fetch_array($csq)){
    
        for ($row=1; $row <= $numrows; $row++)
        {
            $cell = 0;
            echo ' <tr>'."\n";
           for ($col=1; $col <= $numcols; $col++)
            {
            echo '  <td>'."\n";
    
            if ($col===1)
            {
                $cell += $row;
    		      print "<A href=\"/$csq1[var1]\">$csq1[var2]</a><br>";
            }
            else {
                $cell += $numrows;
    		      print "<A href=\"/$csq1[var1]\">$csq1[var2]</a><br>";
            }
            echo '  </td>'."\n";
            }
            echo ' </tr>'."\n";
        }
    }
    
    PHP:
    After I have changed it, it outputs every result for a lot of times :( . Can you look at it and tell me what did I do wrong? Maybe I missed something that should have been changed?
     
    romic, Apr 2, 2007 IP
  7. jitesh

    jitesh Peon

    Messages:
    81
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    just idea :

    <table>
    <tr>
    <?php for($i=0;$i<count($records);$i++){ ?>
    <td><?php echo $records[$i];></td>
    <?php if($i % 3 == 0){ ?>
    </tr><tr>
    <?php }

    ?>

    </table>
     
    jitesh, Apr 2, 2007 IP
  8. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #8
    while($csq1=mysql_fetch_array($csq))

    you're not limiting your results/column. The fetch will do it job and display all data because that's what you told him to do. You have to work with the array.

    Here's another example that will split the original query in x number of queries based on what your settings are. I made it last night and I doubt it's the best solution but here we go.


    <?php
    function execute($db_table, $table_row_filtered, $parameter, $max_columns)
    {
    
    	global $your_connection_details_here;//very important
    	mysql_select_db($database_your_connection_details_here, $your_connection_details_here);//very important
    	
    	$query_cat = "SELECT * FROM " . $db_table . " WHERE";
    	$query_cat .= " " . $table_row_filtered . " = " . $parameter . "";
    
    	$cat = mysql_query($query_cat, $anunturi) or die(mysql_error());
    	$total = mysql_num_rows($cat);
    
    	$results_per_column = round($total/$max_columns);
    
    	for($col=1; $col<=$max_columns; $col++)
    	{
    		$col_arr = array($col => $results_per_column);//make an array with columns based on your settings
    	}
    
    	foreach($col_arr as $key => $value)
    	{
    
    		echo '<table width="100%">'."\n";
    		echo ' <tr>'."\n";
    
    		for($q=1; $q<=$key; $q++)
    		{
    		echo '  <td valign="top" width="'.round(100/$max_columns).'%">'."\n";//determine the width of column
    		echo '  <ul class="list">'."\n";
    
    			if($q>1 && $q<$key)
    			{
    				$limit1 = $results_per_column;
    				$limit2 = $q*$limit1;
    			}
    			elseif($q==$key)
    			{
    				$limit1 = ($q*$results_per_column)-$results_per_column;
    			}
    			else
    			{
    				$limit1 = $q*$results_per_column;
    			}
    
    			$sql[$key] = $query_cat;
    			$sql[$key] .= " LIMIT " . $limit1 . "";
    			if($q>1 && $q<$key)
    			{
    				$sql[$key] .= ", " . $limit1 ."";
    			}
    			elseif($q==$key)
    			{
    
    				for($rpc=$max_columns*$results_per_column; $rpc<$total; $rpc++)
    				{
    					$results_per_column++;
    				}
    
    				$sql[$key] .= ", ".$results_per_column;
    			}
    
    			$do_query=mysql_query($sql[$key]);
    			while($row=mysql_fetch_assoc($do_query))
    			{
    				echo '   <li>'.$row['title'].'</li>'."\n";
    			}
    		}
    
    		echo '  </ul>'."\n";
    		echo '  </td>'."\n";
    		}
    		echo ' </tr>'."\n";
    		echo '</table>'."\n";
    	}
    
    }
    ?>
    PHP:
    Usage:

    <?=execute("mytable", "filtered_row", "parameter", "number_of_columns");?>
    PHP:
    Let me know if you get any bugs.

    The unordered list has a class called "list" so you can also style the way it looks (list-style:none yada yada yada). I'm already using it so I'm really interested in your feedback. Also, if you need to add additional parameters to your query and don't know how Pm me and I'll extend this functions. right now is based on my own needs.

    I will also present you tomorrow a better solution based on arrays. It's not good to use selects when you should not. Anyways, try to understand the procedure and the ways to acomplish one thing, what does what and you will be able to write your own code.
     
    manilodisan, Apr 2, 2007 IP
  9. manilodisan

    manilodisan Peon

    Messages:
    224
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Here's the first solution, modified to suit your database needs. I recommend it since it's more elegant. The above one might be used in other situations which I cant think of right now but might help someone anyways.

    
            $query_cat = "SELECT * FROM "table" WHERE";
    	$query_cat .= " "filtered_row" = "parameter"";
    
     	$cat = mysql_query($query_cat) or die(mysql_error());
    	$total = mysql_num_rows($cat);
    
    	while($row = mysql_fetch_array($cat))
    	{
    		$items[] = array(1 => $row['title'], 2 => $row['id'], 3 => $row['author']);
    	}
    		// Default # of Columns
    		$numcols = 4;
    
    		// Number of Items
    		$numitems = count($items);
    
    		// Number of Rows
    		$numrows = ceil($numitems/$numcols);
    
    	    echo '<table width="100%">';
    	    for ($row=1; $row <= $numrows; $row++)
    	    {
    	        $cell = 0;
    	        echo ' <tr>'."\n";
    	        for ($col=1; $col <= $numcols; $col++)
    	        {
    	        echo '  <td width="'.round(100/$numcols).'%">'."\n";
    
    	        if ($col===1)
    	        {
    	            $cell += $row;
    	            print $items[$cell - 1][1]; /*  $items[$cell - 1][1] to display title  $items[$cell - 1][2] etc... */
    	        }
    	        else {
    	            $cell += $numrows;
    	            print $items[$cell - 1][1];
    	        }
    	        echo '  </td>'."\n";
    	        }
    	        echo ' </tr>'."\n";
    	    }
    	    echo '</table>';
    PHP:
     
    manilodisan, Apr 2, 2007 IP
  10. romic

    romic Peon

    Messages:
    81
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Thank you a lot! The last solution works perfect for me :)
     
    romic, Apr 2, 2007 IP