This is a redone post as my last one contained all of my code and I wanted to simply for the post.

Here is what I am doing:
I show the categories that the user has placed a referral in on the page "my_categoreis.php". The user then can select a category from their list and be taken to the "my_subcategories.php" page. On this page, the intent is that they see all the subcategories in which they have entered a referral for the category they selected from the "my_categories.php" page.

The problem:
No matter what category the user chooses, the results on the "my_subcategories.php" page is the same.

Example:

On the "my_categories.php" page, the categories listed for one user are:

Auto-Car
Baby-Children

Regardless of what the user selects, the "my_subcategory.php" page shows:

Babysitters

This is correct for the selection "Baby-Children" but incorrect for the selection "Auto-Car". If "Auto-Car" is selected, the "my_subcategories.php" page should show:

Insurance

I believe the variable is grabbing the last loop as opposed to correlating to what selection the user has made. Please help me to have the correct subcategory show based upon the selection made by the user.

Here is the code:

my_subcateogy.php

<ul class="pageitem">
	<?php	$query = "SELECT DISTINCT category FROM referrals WHERE username='$_SESSION[username]' ORDER BY category ASC"; 
			$result = mysql_query($query) or die(mysql_error());
			$num=mysql_num_rows($result);
			mysql_close();
			$i=0;
			while ($i < $num) { 
				$catsel=mysql_result($result,$i,"category");
				
				?>		
						<li class='menu'><span class='name'>
						<a class="noeffect"  onclick="document.mycategories.submit();return false" />
						<input type="hidden" name="catsel" value="<?php echo $catsel; ?>">
<?php	echo $catsel; ?>
						</span><span class='arrow'></span></a></li>
<?php	$i++;	} ?>
					</ul>

my_subcategory.php

include "include/session.php";
include "fb-config.php";
$catsel=$_POST["catsel$i"];
$catsel=mysql_real_escape_string($catsel);

<ul class="pageitem">
	<?php	$query = "SELECT DISTINCT subcategory FROM referrals WHERE username='$_SESSION[username]' AND category='$catsel' ORDER BY subcategory ASC"; 
			$result = mysql_query($query) or die(mysql_error());
			$num=mysql_num_rows($result);
			mysql_close();
			$i=0;
			while ($i < $num) { 
				$subcatsel=mysql_result($result,$i,"subcategory");
				
				?>	
						<li class='menu'><span class='name'>
						<a class="noeffect"  onclick="document.mysubcategories.submit();return false" />
						<input type="hidden" name="subcatsel" value="<?php echo $subcatsel; ?>">
<?php	echo $subcatsel; 

?>
						</span><span class='arrow'></span></a></li>
<?php	$i++;	} ?>

					</ul>

Recommended Answers

All 9 Replies

Move 'mysql_close' to the very last of the statements. Also, 'mysql_close' has one arguments which was the current opening connection. Like below:

$con = mysql_connect($host, $user, $pass); // open the connection, $host as mysql host, $user as mysql user, $pass as mysql password

mysql_close($con); // close the current connection

I have "include "config.php"; which connect to my database. Do I still need to reconnect using $con? Also, I moved $mysql_close; to the last and nothing changed. I also deleted it and nothing changed. Still the same result.

I may not have much to add to your conversation.
I am an object oriented programmer and love to make classes and get php 'logic' off of the webpage. you should gain new insite on building classes and handling data, separate of your presentation on web pages.
If you want to learn how to code some bad ass shit, properly, pm me tonight, i'm going to bed but will look tomorrow. send me your db structure with any tables included that hold data for your web page above.

$catsel=$_POST["catsel$i"];

where did you define the variable $i that you used in the above line?

Apologies. I had placed that in there as I was trying everything under the sun from reading endless posts in forums. Please disregard the $i. It had zero impact on my results having it in there or not. I was trying everything under the sun, from countless forum posts, to make this work and that was a residual attempt. The answer I am really looking for (I believe) doesn't necessarily have to do with the code I have written, but rather the code that I am missing. Maybe this is what you were getting at. Can you tell me, what do I need to write in so that the variable sticks to the category selection made by the user so that their specific subcategories are displayed relative to their category selection.

Why does it most replies come shortly after the initial post of the question/issue, but then quickly drop off to no more replies within a day or so, even though no one has addressed my primary question?

I pm'd you. that is my reply. respond if you want real help... but it will be tomorrow.

while loop not necessary put num count remove and put condition like

while($result=mysql_fetch_assos(mysql_query('qry here')))
{

}

try above format

I pm'd you. that is my reply. respond if you want real help... but it will be tomorrow.

I would like real help, but learning object oriented programming seemed like quite an undertaking to overcome this issue. I greatly appreciate your offer to help. As it would take up a good amount of your time, is there a compensation structure that accompanies the assistance?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.