Getting confused over looping

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
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Getting confused over looping

Post by Seona »

Me again. :)

I think I've tied myself in a mental knot and just spent too long looking at this code. Can someone please have a look at this:

Code: Select all

if (isset($g_submit)) {
			$sql3 = "SELECT * FROM $table";
			$result3 = mysql_query($sql3,$link);
			foreach ($_GETї"Sections"] as $sect) {
				$sect = explode(",",$sect);
				$headed = "false";
				while ($row3 = mysql_fetch_array($result3, MYSQL_ASSOC)) {
					if (($row3ї"ID"] == $sectї0]) && ($headed == "false")) {
						print "<img src="" . $row3&#1111;"TitleImage"] . "" alt="" . $row3&#1111;"CategoryName"] . "" class="titleImage" />";
						$headed = "true";
					&#125; // if
					$sql4 = "SELECT * FROM $table2 WHERE ID = $sect&#1111;1]";
					$result4 = mysql_query($sql4,$link);
					$row4 = mysql_fetch_array($result4, MYSQL_ASSOC);
					print "<h2>" . $row4&#1111;"SectionName"] . "</h2>";
					print $row4&#1111;"Description"] . "</p>";
				&#125; // while
			&#125; //foreach
		&#125; // if
The results of this code can be seen if you go here http://www.dare2.com.au/productsservices.php and select one or more sections and the submit the form.

The way I've written it, it outputs the same section's content over and over. What is supposed to happen is that it displays the graphic for the category's heading if anything in that category has been selected and then displays each selected section under that category before going off to the next one.

Can someone please help me re-order my code so that the right things happen in the right places? I just can't seem to figure out what I'm doing wrong and it's driving me nuts. :(
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

it has to do with your usage and position of $headed.
Seona
Forum Commoner
Posts: 33
Joined: Wed Dec 08, 2004 11:04 pm

Post by Seona »

OK, I can see how that's stopping it going on to the next category, although I'm not entirely certain what to do about that. I can't understand, however, why the same record is being repeated down the page when it should only show up once and then go on to the next record.

I'm starting to think that maybe I should just start from scratch with the logic of this whole section. The trouble is, I'm not sure how to. The code I have has been cobbled together from bits and pieces I've found online, answers to questions I've asked here, and plain trial-and-error. As a result, I'm not entirely sure what all of it's doing or why, or how one bit is affecting another.

So I pose a question to the community in the hope that someone more familiar with PHP than me (ie, most of the people on here) can enlighten me.

I have a multiple-select box. It lists the sections available in the site, grouped by category. Each <option> tag has value="CategoryID,SectionID" to associate each section with its relevant category. Because it's a multiple-select, all the results are tied to the same form field. So, using <form method="get"> to be able to see the results, you end up with something like:
...thispage.php?Sections=1,2&Sections=1,3&Sections=2,1&Sections=2,2

I would like the resultant output to be something like this:

Category 1
- Section 2
- Section 3
Category 2
- Section 1
- Section 2

The information is stored in two database tables:
Table: Category
- ID
- CategoryName
- TitleImage
Table: Section
- ID
- CategoryID
- SectionName
- Description


Is there some sort of "group by" clause available, where you can put an ORDER BY in the query and then group the output based on the distinct values in that field? I know that such an option is available in another web-based programming language, but I haven't been able to find an equivalent in PHP.

I know this has become rather long, but the more I've worked on this page the less I feel like I know what I'm doing. I could really do with a bit of help in figuring otut exactly how to go about creating this sort of structure.

Cheers,

Seona.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

group by and order by are both available through MySQL, in any combination: order by, group by, both.

equivalents in php are the sort functions as array_unique()
Post Reply