Jump to content

foreach help


stackumhi

Recommended Posts

I am trying to display a simple table with 3 columns.

 

I have some business categories in a mysql DB(about 24 of them) and I would like 3 columns of 8 categories. I would like each category to be a link. I have read many posts here but can't seem to find my solution.

 

Any help would be great.

 

Here is my current (wrong)code:


<?php
include 'includes/dbconnect.php';

$result = mysql_query("SELECT category FROM CATEGORIES");
echo "<table cellpadding=\"2\" cellspacing=\"2\">";
$array = mysql_fetch_array($result);

foreach ($array as $temp) {
    
	echo "<tr><td>". $temp. "</td>". "<td>".$temp. "<td>".$temp."</tr>";
	echo "<br/>";
} 

?>

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/
Share on other sites

Question... what exactly is going wrong?

 

Also, are the categories the NAMES of the columns or the values of rows in the database?

 

You want to loop through by doing this:

 


<?php
include 'includes/dbconnect.php';

   $result = mysql_query("SELECT category FROM CATEGORIES");
   echo "<table cellpadding=\"2\" cellspacing=\"2\"><tr>";
   $i = 0;
   while($array = mysql_fetch_array($result)){//loop through
      
      echo "<td>". $array['category']. "</td>";
      if(!$i%3){echo "</tr><tr>";}
      $i++;
   } 

$num_rows = mysql_num_rows($result);
$find_colspan = 3 - ($num_rows%3);//if there are 6 rows, this is 0, if there are 7, this is 1.
for($i=0;$i<$find_colspan;$i++){
echo "</td>";
}
      echo "</tr></table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/#findComment-915779
Share on other sites

Thanks for the very fast reply!

 

It is getting closer now...all the cats are being pulled from the DB but they are mostly in a single <tr>.

 

Answer to your question - category is the column name...CATEGORIES is the table name

 

Thank you for any more help you can think of.

 

 

 

Here is the view source:

 

<table cellpadding="2" cellspacing="2"><tr><td>Accommodations - Hotels & Resorts</td></tr><tr><td>Arts & Entertainment</td><td>Automotive</td><td>Business & Professional Services</td><td>Clothing & Accessories</td><td>Community & Government</td><td>Computers & Electronics</td><td>Construction & Contractors</td><td>Education</td><td>Food & Dining</td><td>Government, Community, Schools, Churches</td><td>Health & Medicine</td><td>Home & Garden</td><td>Industry & Agriculture</td><td>Internet</td><td>Legal & Financial</td><td>Media & Communications</td><td>Personal Care & Services</td><td>Real Estate</td><td>Shopping</td><td>Sports & Recreation</td><td>Travel & Transportation</td><td>Wedding Services</td><td>Non-Profits</td></td></tr></table>

 

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/#findComment-915782
Share on other sites

I made the change, now all results are in the same row. I would like (if possible) 3 columns with 8 rows.  I added a border of 1 to see results better.

 

<table border="1" cellpadding="2" cellspacing="2"><tr><td>Accommodations - Hotels & Resorts</td><td>Arts & Entertainment</td><td>Automotive</td><td>Business & Professional Services</td><td>Clothing & Accessories</td><td>Community & Government</td><td>Computers & Electronics</td><td>Construction & Contractors</td><td>Education</td><td>Food & Dining</td><td>Government, Community, Schools, Churches</td><td>Health & Medicine</td><td>Home & Garden</td><td>Industry & Agriculture</td><td>Internet</td><td>Legal & Financial</td><td>Media & Communications</td><td>Personal Care & Services</td><td>Real Estate</td><td>Shopping</td><td>Sports & Recreation</td><td>Travel & Transportation</td><td>Wedding Services</td><td>Non-Profits</td></td></td></td></tr></table>

 

Thanks again.

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/#findComment-915796
Share on other sites

Got it working with the code below.

 

Now can anyone help me toward getting each category to be a link to it's corresponding page?

 

Example..."Home & Garden" would be a link to the home & garden page...etc..

 

Thanks again.

 

Working code below:

 

<?php
include 'includes/dbconnect.php';

   $result = mysql_query("SELECT category FROM CATEGORIES");
   echo "<table border=\"1\" cellpadding=\"2\" cellspacing=\"2\"><tr>";
   $i = 0;
   while($array = mysql_fetch_array($result)){//loop through
      
$i++;
      echo "<td>". $array['category']. "</td>";
      //if(!$i%3){echo "</tr><tr>";$i=0;}
  if($i == 3){echo "</tr><tr>";$i=0;}
   } 

$num_rows = mysql_num_rows($result);
$find_colspan = 3 - ($num_rows%3);//if there are 6 rows, this is 0, if there are 7, this is 1.
for($i=0;$i<$find_colspan;$i++){
echo "</td>";
}
      echo "</tr></table>";
?>

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/#findComment-915822
Share on other sites

Well, the links do not exist yet...they are next on my to-do list. I would like to have something like http://mysite.com/city/cat_id=123 and then have a page that displays all business listings in that category.

 

But someone suggested keeping my links in a database....?

 

I am open for suggestions.

 

Thanks.

Link to comment
https://forums.phpfreaks.com/topic/173729-foreach-help/#findComment-916004
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.