PHP is not displaying mysql results evenly in table

Discussion in 'PHP' started by meamrussian, Jul 8, 2009.

  1. #1
    Hey,

    I have a fairly simple code that doesn't display mysql results in a nice way. Is there a way to fix it? Here is my code:

        $query6 = "SELECT url,category FROM categories ORDER BY category ASC";
    	$result6 = mysql_query($query6) or die(mysql_error()."<br />".$query6);
    	
    	while($fetch6 = mysql_fetch_array($result6)){
        	$items[] = array(1 => $fetch6['url'], 2 => $fetch6['category']);
    	}
    	// Default # of Columns
        $numcols = 3;
    
        // 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).'%" valign="top" style="padding:0;" onmouseover="style.backgroundColor=\'#ede6a1\';" onmouseout="style.backgroundColor=\'#FEFDF1\'">'."\n";
        
                    if ($col===1)
                    {
                        $cell += $row;
                        echo "<a href=\"".$domainname."coupons/".$items[$cell - 1][1]."/1/\">".$items[$cell - 1][2]."</a>";
                    }
                    else {
                        $cell += $numrows;
                        if(!empty($items[$cell - 1][2])){
                            $desc = str_replace(",", "/", $items[$cell - 1][1]);
                            echo "<a href=\"".$domainname."coupons/".$items[$cell - 1][1]."/1/\">".$items[$cell - 1][2]."</a>";
                        }
                    }
                echo '</td>'."\n";
                }
                echo '</tr>'."\n";
            }
            echo '</table>';
    PHP:
    As you can see, all it does is show each category in it's own cell, with 3 columns. Now, it might look alright, but right now I have 25 categories, and this code now has 9 categories in the first column, 9 in the second, and 7 in the third. This doesn't look right. It should be 9 in the first, 8 in the second, and 8 in the third.

    Is there a better way to do this?

    Thanks.
     
    meamrussian, Jul 8, 2009 IP
  2. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Why don't you put your table in the while loop of the query
     
    wd_2k6, Jul 9, 2009 IP
  3. Bohra

    Bohra Prominent Member

    Messages:
    12,573
    Likes Received:
    537
    Best Answers:
    0
    Trophy Points:
    310
    #3
    Yea why not use while loop instead of for
     
    Bohra, Jul 9, 2009 IP
  4. meamrussian

    meamrussian Active Member

    Messages:
    780
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    58
    #4
    I'm not quite sure what the difference would be... I don't see how that would fix the problem. Can you explain?
     
    meamrussian, Jul 9, 2009 IP
  5. wd_2k6

    wd_2k6 Peon

    Messages:
    1,740
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    0
    #5
    What does the code actually do, does it just put the fetch6 category name inside an anchor which links to the fetch6 url, and each one goes in a different cell?

    For example I meant something like:

    
    echo "<table><tr>";
    $query6 = "SELECT url,category FROM categories ORDER BY category ASC";
        $result6 = mysql_query($query6) or die(mysql_error()."<br />".$query6);
        
        while($fetch6 = mysql_fetch_array($result6)){
    $url = "<a href\"".$domainname."coupons/".$fetch6['url']."/1/\">".$fetch6['category']."</a>";
    echo "<td>$url</td>";
    if ($count == 3) { 
    //Then reset counter
    count = 0; 
    //Start a new table row
    echo "</tr><tr>";
    }
    else {
    $count ++;
    }
    //Close while loop
    }
    echo "</table>";
    
    PHP:
    I thought it might be easier to access the stuff while you are looping through the query loop rather than storing them in an array and then accessing later. This also has the benefit that the while loop automatically ends when there are no results so theres no need to keep track of counters.
     
    wd_2k6, Jul 9, 2009 IP
  6. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #6
    wd_2k6 is right and has the better solution.

    Few problems with his code though. if there are N numbers of categories such that N is not divisible by 3, then the last row will not be closed by </tr>. Also, if N is exactly divisible by 3, then there is extra <tr> at the end. If you are particular about the proper closing of HTML tags, then try this:
    echo "<table>";
    $count = 1;
    $query6 = "SELECT url,category FROM categories ORDER BY category ASC";
    $result6 = mysql_query($query6) or die(mysql_error()."<br />".$query6);
    $total_categories = mysql_num_rows($result6);   
    while($fetch6 = mysql_fetch_array($result6)){
    	if($count ==1)
    		echo "<tr>";
    	$url = "<a href\"".$domainname."coupons/".$fetch6['url']."/1/\">".$fetch6['category']."</a>";
    	echo "<td>$url</td>";
    	if ($count == 3) { 
    		count = 1; 
    		echo "</tr>";
    	}
    	else
    		$count ++;
    	//Close while loop
    }
    $empty_cells = $total_categories %3;
    if($empty_cells >0)
    {
    	for ($i=1; $i<=$empty_cells; $i++)
    		echo "<td>&nbsp;</td>";
    	echo "</tr>";
    }
    echo "</table>";
    PHP:
     
    samyak, Jul 9, 2009 IP