Jump to content

Recommended Posts

I have managed to scrape some code together (cleanly), which displays all my categories - one in each row. However, I want to be able to have it so that it displays them evenly in 3 columns.

 

Category      Category      Category

Category      Category      Category

Category      Category      Category

Category      Category      Category

 

Like above (each one is its own category). How can I go about this? Below is the current code snippet for displaying it.

 

<?php 


$background = ($counter++%2) ? 'c1' : 'c2';

	$subcategories_content .= '<tr class="' . $background . '"> '.
		'	<td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '.
		(($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td> '.
		'</tr> ';
}


?>

Link to comment
https://forums.phpfreaks.com/topic/70671-modifying-output/
Share on other sites

Very rough outline:

 

<html>
<head>
</head>

<body>
<table>
	<?php
		$currentColumn = 0;

		while (condition)
		{
			if ($currentColumn == 0)
			{
				echo "<tr>";
			}

			echo "<td>" . $somedatafromyourdatabase . "</td>";

			if ($currentColumn == 2)
			{
				echo "</tr>";
				$currentColumn = 0;
			}
			else 
			{
				++$currentColumn;
			}
		}
	?>
</table>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355202
Share on other sites

Thanks! I tried what you said GingerRobot.

 

I fixed all the errors, but I still get all of the categories in one list.

 

This is my current code for it:

 

<?php

$sql_select_categories = $db->query("SELECT category_id, items_counter FROM " . DB_PREFIX . "categories WHERE 
	parent_id='" . $parent_id . "' AND user_id=0 ORDER BY order_id ASC, name ASC");

	if($sql_select_categories && mysql_num_rows($sql_select_categories) > 0)
{
    $i = 0;
    $max_columns = 3;

while ($cat_details = $db->fetch_array($sql_select_categories)) 
{
   
	$subcategories_content .= '<tr> '.
		'	<td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '.
		(($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td> ';

	}	if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
}
}

?>

 

Any idea?

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355207
Share on other sites

I have been working on it some more. It still doesn't work.

 

This is my current code - thanks for any help you can give.

 

<?php

$sql_select_categories = $db->query("SELECT category_id, items_counter FROM " . DB_PREFIX . "categories WHERE 
	parent_id='" . $parent_id . "' AND user_id=0 ORDER BY order_id ASC, name ASC");
	$result = mysql_query($sql_select_categories) or die("There was a problem with the SQL query: " . mysql_error()); 
	if($result && mysql_num_rows($result) > 0)

	{
    $i = 0;
    $max_columns = 3;
    while($row = mysql_fetch_array($result))        
   {
   extract($row);
   
   if($i == 0)
          echo "<tr>";
  
		echo '<td width="100%"> » <a href="categories.php?parent_id=' . $cat_details['category_id'] . '">' . $category_lang[$cat_details['category_id']] . '</a> '.
		(($setts['enable_cat_counters']) ? (($cat_details['items_counter']) ? '(<strong>' . $cat_details['items_counter'] . '</strong>)' : '') : '') . '</td>';

	if(++$i == $max_columns) 
       {
           echo "</tr>";
           $i=0;
       }  // end if 
   } // end while
} // end if results 

?>

Link to comment
https://forums.phpfreaks.com/topic/70671-modifying-output/#findComment-355225
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.