...

View Full Version : need help with variables ....



HormonX
01-28-2003, 05:20 PM
Hi,

amhaving a little problem and i don't know how to overcome it. Here it is ....

I have a template page i have done with a pull down menu. Menu items are taken from a database of products. Manu dispalys only product categories but when you select a product it takes you to other page where subcategoris for that particular category should be displayed .. ( pull down menu stays on the page because it's a part of the template). Everything would work fine but doesn't matter if i select first option or any other the result on the other page will only list subcategory of the last selection on the pull down menu.

please help me :)

this is menu section that appears on all pages. this where you make your selection ... page name index.php



<?
include('connect.php');
$query = "SELECT DISTINCT prod_id, prod_cat FROM products ORDER BY prod_cat ASC";
$result = mysql_query($query, $link);
?>

<select name="menu1" onChange="MM_jumpMenu('parent',this,0)" class="formelements">
<option selected>Select One</option>
<?
while($r=mysql_fetch_array($result))
{

$prod_id = $r['prod_id'];
$prod_cat = $r['prod_cat'];

?>
<option value="../inv.php?prod_id=<? echo "$prod_id"; ?>"><? echo "$prod_cat"; ?></option>
<?
}
?>



subcategories should be listed according tothe selection made ... page name inv.php


<?
$query = "SELECT DISTINCT sprod_cat, sprod_id FROM products WHERE prod_id='$prod_id'";
$result = mysql_query($query, $link);

while($r1=mysql_fetch_array($result))
{

$sprod_cat = $r1['sprod_cat'];
$sprod_id = $r1['sprod_id'];
?>
<tr>
<td class="smallletters"><a href="invdet.php?sprod_id=<? echo "$sprod_id"; ?>"><? echo "$sprod_cat"; ?></a></td>
</tr>
<?
}


any help you be very very very appeciated.

HormonX

mordred
01-28-2003, 07:00 PM
Originally posted by HormonX
Manu dispalys only product categories but when you select a product it takes you to other page where subcategoris for that particular category should be displayed .. ( pull down menu stays on the page because it's a part of the template). Everything would work fine but doesn't matter if i select first option or any other the result on the other page will only list subcategory of the last selection on the pull down menu.


:confused: huh?

I have problems discerning what is happening at all on your two pages, and what you want to achieve. Do you want that the subcategories should be displayed in the select list or should they rather be printed to the screen without affecting the content of the select list?

Perhaps error_reporting(E_ALL) helps for a start?

BTW, using commas improves readability a lot.

HormonX
01-28-2003, 08:01 PM
I am sorry for confusig you.. and probably the rest of the forum ... i'll try again but without confusing everyone along the way. :)

On all of my pages i have the pull down menu with product categories. That menu never changes , it allways lists categories, doesn't matter on what page visitor is. After making a selection it takes you to another page called inv.php where a list of subcategories for selected category is printed to the screen.

What ends up happening is this ... let's say there is 3 categories in the list menu; Category1, Category2 and Category3 and i select Category2, all i will see on the inv.php is subcategories of Category3 not subcategoris on Category2.

I hope this is a bit more clear, belive me am trying my best not to confuse anyone .. myself included :)

Thanx in advance.

mordred
01-28-2003, 11:30 PM
Where does $prod_id come from? That's important because it's the criteria for your WHERE part of the SQL query, and your description sounds like you're doing a selection based on false criteria... or something with a similar effect.

So how does $prod_id come into your query? Is it defined on the page itself? Is the value of this variable taken from a GET, POST, or cookie parameter? Or from a session? Is your query working correctly if you hard-code the WHERE part?


I think it the source of your problem *could* lie in the fact that you use the variable name $prod_id on occasions. First time in the select list generation:

$prod_id = $r['prod_id'];

And then in the query on inv.php:

$query = "SELECT DISTINCT sprod_cat, sprod_id FROM products WHERE prod_id='$prod_id'";

If nothing else than the code posted by you is happening on the pages, I guess that you accidentelly always select the sprod_cat and sprod_id based on the last retrieved 'prod_id' from your option list generation part. If that's the case, try using different variable names or reset the $prod_id variable to a default value before using it in your query.

HormonX
01-30-2003, 12:12 AM
$prod_id in the second query comes from selecting an option from the menu. Lets assume that index.php has that menu when you make a selection it takes you to a page inv.php but the link looks like this ex:inv.php?prod_id=2.
So this is where $prod_id comes from on inv.php

You mentioned in your previous post about reseting the variable .. how would i do that ?

if it would make it easier to see how it works .. i can give you a link to that page so you can see it.

Thanx for your help :)

HormonX

mordred
01-30-2003, 10:28 AM
I think I understand now how it's supposed to work. A link is always helpful, but since you the source code is only the outcome of your PHP script, it won't help that much for debugging your script.



On all of my pages i have the pull down menu with product categories. That menu never changes , it allways lists categories, doesn't matter on what page visitor is.


That means that the code for the select list menu is always in place, doesn't it? And that means in return that there's always a $prod_id variable defined eventually before you make your second query.
If your variable that defines the criteria for the WHERE part comes from a query string, why don't access it in the proper way?

$_GET['prod_id']; // or $HTTP_GET_VARS['prod_id'];

so your query becomes



$query = "SELECT DISTINCT sprod_cat, sprod_id FROM products WHERE prod_id='" . $_GET['prod_id'] . "'";


although if your prod_id field is of an integer type, you don't need those extra single quotes around prod_id.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum