PHP/MySQL Output problems....

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
ebarton
Forum Newbie
Posts: 2
Joined: Wed Mar 02, 2005 10:15 pm

PHP/MySQL Output problems....

Post by ebarton »

Problem:
I am doing a join between 2 tables then trying to display the results. The output should list the the Category name and a description next to it. On the line below that should be a list of 3 stores with pipes in between until the last where the term "more" should be listed. There should then be a blank line then the next category. The problems I am having with the code below:
1) Cateogry 1 has 5 stores, so it is showing category 1 twice then adding another section for the same category with the remaining stores.
2) If the category has less then 3 stores it doesnt add the "more" and doesnt put the blank line.

Current Output:
Category 1 - Category 1 description.
Store 1 | Store 2 | Store 3 | More

Category 1 - Category 1 description.
Store 1 | Store 2 |
Category 2 - Category 2 description.
Store 1 | Store 2 | Store 3 | More

Category 3 - Category 3 description.
Store 1 | Store 2 |

Want to look like:
Category 1 - Category 1 description.
Store 1 | Store 2 | Store 3 | More

Category 2 - Category 2 description.
Store 1 | Store 2 | Store 3 | More

Category 3 - Category 3 description.
Store 1 | Store 2

Code (this is a mess and I am a newbie... no laughing :) )

Code: Select all

$result = mysql_query("SELECT scat.scatID AS Sid, 
                                               scat.scat_name AS Sname,
                                               scat.scat_desc AS Sdesc, 
                                               pages.name AS Pname,
                                               pages.pageID as Pid
                                    FROM scat INNER JOIN pages
                                    ON scat.scatID = pages.scat_id
                                    ORDER BY scat.scat_name ASC, 
                                                    pages.name ASC");

       $previous_main = "";
       while ($record = mysql_fetch_assoc($result)) {
          if ($recordї"Sname"] != $previous_main) {
              echo "<br><b>".$record&#1111;"Sname"]."</b> - ".$record&#1111;"Sdesc"].
              "<br>";
              $loop_count = 0;
	      &#125;
          if ($loop_count > 1) &#123;
	          echo " <a href='discount-shopping-mall/online_shop.php?online_shop=".$record&#1111;"Pid"]."'>".
	                 $record&#1111;"Pname"]."</a> | More<br>";
	          $previous_main = "";
	      &#125; else &#123;
	          echo " <a href='discount-shopping-mall/online_shop.php?online_shop=".$record&#1111;"Pid"]."'>".
	                 $record&#1111;"Pname"]."</a> |";
              $previous_main = $record&#1111;"Sname"];
 	      &#125;
          $loop_count++;
       &#125;
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

keep tracking the last category.. if the counter went past 4 records, ignore additional records in that category. Display the pipe in front of the store, but only when it's not a new category.
Post Reply