set a certain value for ID in droppdownlist

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
daan
Forum Commoner
Posts: 25
Joined: Wed Nov 16, 2005 10:32 am

set a certain value for ID in droppdownlist

Post by daan »

This part is to create a dropdownmenu, but i want the CategoryID to be set as the value '2'
I tried to change every CategoryID in a 2 but does not work :-(
can anyone help?

Code: Select all

//create the category menu
$CategoryMenu = "<select name=cat>\n\t<option value=\"\"></option>\n\t";

$q1 = "select * from re2_categories order by CategoryName";
$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) > '0')
{
	while($a1 = mysql_fetch_array($r1))
	{
		$CategoryMenu .= "<option value=\"$a1[CategoryID]|0\">$a1[CategoryName]</option>\n\t";

		//get the subcategories
		$q2 = "select * from re2_subcategories where CategoryID = '$a1[CategoryID]' order by SubcategoryName ";
		$r2 = mysql_query($q2) or die(mysql_error());

		while($a2 = mysql_fetch_array($r2))
		{
			$CategoryMenu .= "<option value=\"$a1[CategoryID]|$a2[SubcategoryID]\">$a1[CategoryName] - $a2[SubcategoryName]</option>\n\t";
		}

	}
}
situation now :
Category 1 - Option 1
Category 1 - Option 2
Category 1 - Option 3
Category 2 - Option 4
Category 2 - Option 5
Category 2 - Option 6

wanted:
Category 2 - Option 4
Category 2 - Option 5
Category 2 - Option 6



thanx
josh
DevNet Master
Posts: 4872
Joined: Wed Feb 11, 2004 3:23 pm
Location: Palm beach, Florida

Post by josh »

Code: Select all

select * from re2_categories order by CategoryName
I'm guessing it's because you're looping through for every value of the category (because you selected every category in that query). This code looks really really inefficient as well, can you describe your schema so we can help you fix this? Also if you are putting variables like that into a string you should at least surround them with {} to expand them correctly, not to mention you didn't use htmlentities() or anything and you're using constants instead of strings as your array keys
daan
Forum Commoner
Posts: 25
Joined: Wed Nov 16, 2005 10:32 am

Post by daan »

structure:

table :re_categories:
field: CategoryID
field: CategoryName

table :re_subcategories:
field: SubcategoryID
field: SubcategoryName
field: CategoryID

thanx
daan
Forum Commoner
Posts: 25
Joined: Wed Nov 16, 2005 10:32 am

Post by daan »

well actually, I would like to split it in two
when chosen a certain option of CategoryID in the CategoryMenu, automatically adapt SubcategoryMenu

thanx
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

Hopefully, this works...

Code: Select all

<?php

$query = "select * from `re_categories`";
if (is_resource($result = mysql_query($query))){
	if (mysql_num_rows($result) > 0){
?>
		Main categories&nbsp;&nbsp;
		<form name = 'frmChooseCategory' method = 'get'>
		<select name = 'chooseCategory' onchange = 'document.window.location=frmChooseCategory.chooseCategory.value'>
			<option>Main Categories</option>
<?php 		while ($row = mysql_fetch_assoc($result)){	?>
			<option value = "<?php echo $_SERVER[PHP_SELF]."?category=".$row["CategoryID"]; ?>">
				<?php echo $row["CategoryName"]; ?>
			</option>
<?php		}						?>
		</select>
		</form><br />
<?php
	}
}

if (isset($_GET["chooseCategory"])){
	if (!empty($_GET["chooseCategory"]) && is_numeric($_GET["chooseCategory"])){
		$query = "select * from `re_subcategories` where `CategoryId` = ".$_GET["chooseCategory"];
		if (is_resource($result = mysql_query($query))){
		if (mysql_num_rows($result) > 0){
			Sub categories&nbsp;&nbsp;
			<form name = 'frmChooseSubcategory' method = 'get'>
			<select name = 'chooseSubcategory'>
				<option>Main Categories</option>
<?php 				while ($row = mysql_fetch_assoc($result)){	?>
					<option value = "<?php echo $row["SubcategoryID;] ?>">
						<?php echo $row["CategoryName"]; ?>
					</option>
<?php				}#end of while block
		}#end of is_resource if block
		}#end of rows count if block				?>

		</select>
		</form><br />
<?php
	}#end of empty check if block
}#end of isset if block
?>
daan
Forum Commoner
Posts: 25
Joined: Wed Nov 16, 2005 10:32 am

Post by daan »

can't succeed integrating you code into my file, can i send you the whole document? :roll:
User avatar
raghavan20
DevNet Resident
Posts: 1451
Joined: Sat Jun 11, 2005 6:57 am
Location: London, UK
Contact:

Post by raghavan20 »

post your document here and someone working at US time should help integrate the code..........
daan
Forum Commoner
Posts: 25
Joined: Wed Nov 16, 2005 10:32 am

Post by daan »

can anyone integrate it in this document?
for the layout i use a different file, also needed??

Code: Select all

<?
require_once("conn.php");

if(isset($_POST[s1]))
{
	if(!empty($_POST[cat]))
	{
		$CatInfo = explode("|", $_POST[cat]);

		$c = $CatInfo[0];

		if($CatInfo[1] > '0')
		{
			$s = $CatInfo[1];
		}
	}
	if(!empty($_POST[_SubcategoryName]))
	{
		$search_SubcategoryName = $_POST[search_SubcategoryName];
	}

	if(!empty($_POST[_item]))
	{
		$search_item = $_POST[search_item];
	}



	$url = "./search.php?c=$c&s=$s&search_item=$_POST[search_item]";

	header("location:$url");
	exit();
}

require_once("includes.php");


		//get the subcategories
		$q2 = "select * from re2_subcategories where CategoryID = '$a1[CategoryID]' order by SubcategoryName ";
		$r2 = mysql_query($q2) or die(mysql_error());

		while($a2 = mysql_fetch_array($r2))
		{
			$CategoryMenu .= "<option value=\"$a1[CategoryID]|$a2[SubcategoryID]\">$a1[CategoryName] - $a2[SubcategoryName]</option>\n\t";
		}

	}
}

$CategoryMenu .= "</select>\n";

$SubCategoriesMenu = "<select name=search_SubcategoryName>\n\t<option value=\"\">Alle Types</option>\n\t";

$q1 = "select distinct SubcategoryName from re2_subcategories order by SubcategoryName";
$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) > '0')
{
	while($a1 = mysql_fetch_array($r1))
	{
		$SubCategoriesMenu .= "<option value=\"$a1[SubcategoryName]\">$a1[SubcategoryName]</option>\n\t";
	}
}

$SubcategoriesMenu .= "</select>\n";




//create the item menu
$itemMenu = "<select name=search_item>\n\t<option value=\"\">Alle Items</option>\n\t";

$q1 = "select distinct item from re2_items order by item";
$r1 = mysql_query($q1) or die(mysql_error());

if(mysql_num_rows($r1) > '0')
{
	while($a1 = mysql_fetch_array($r1))
	{
		$itemMenu .= "<option value=\"$a1[item]\">$a1[item]</option>\n\t";
	}
}

$itemMenu .= "</select>\n";





require_once("templates/AdvancedSearchTemplate3.php");

if(!ereg("index.php", $_SERVER[SCRIPT_NAME]))
{
	
}

?>
Post Reply