Forum Moderators: coopster

Message Too Old, No Replies

Challenge _ List Values According to a Dynamic Populated Combo Box

         

rogernem

12:33 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



Hi there. I have a table called menu where there are some menu itens there. I also have a table called "produtos" where I have some products there and their corresponding menu.

MENU
==== ==
$menuitem
Wallpapers
Icons

PRODUTOS
========
$nomeproduto...........$menuitem
Tim...........................Wallpapers
Motorola....................Wallpapers
House........................Icons

Something like that

Well, I have a combo box (dropdown) that is populated according to the itens in the menu table.
What I'd like to do is to list the itens in the table corresponding to that menu item
The way the code is, it just populates the combo but it doesnt show the itens when i change the value of the combo

The code follows bellow:

[PHP]
<?php
include ("../toing_conexao.php");
?>
[/PHP]

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body>
<p align="center"><strong>ADMIN TOING - PRODUTOS : <a href="index.php">Voltar</a> <br>
</strong></p>
<table width="700" border="0" align="center" cellpadding="0" cellspacing="0">
<form action="action_addproditens.php" method="post">
<tr>
<td>* Categoria: </td>
<td>
[PHP]
<?php
$query="SELECT * FROM menu order by menuitem ASC";
$result=mysql_query($query);
$num=mysql_num_rows($result);
?>
[/PHP]
<select name="menuitem" style="width:200 " onChange="<? $acao?>">
[PHP]
<?
$i=0;
while ($i < $num) {
?>
[/PHP]
<option>
[PHP]
<?
$menuitem=mysql_result($result,$i,"menuitem");
echo $menuitem;
$i++;
if ($select!= 1) {
$acao;
}
}
?>
[/PHP]
</option>
</select>
</td>
</tr>

<tr>
<td width="205">* Nome do Produto: </td>
<td width="495"><input name="nomeproduto" type="text" ></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td colspan="2"><input type="reset" name="Reset" value="Limpar">
<input type="submit" name="Submit2" value="Adicionar"></td>
</tr>
</form>
</table>

<p><strong> Produtos existentes:</strong></p>
[PHP]
<?
if ( $acao ) {
$sql2="SELECT * FROM produtos WHERE menuitem = '$menuitem' ORDER BY nomeproduto ASC";
$result2=mysql_query($sql2,$conn);

while ($row = mysql_fetch_array($result2)) {
$id = $row["id_prod"];
$menuitem = $row["menuitem"];
$nomeproduto = $row["nomeproduto"];

echo ("<font face=verdana size=2>- $nomeproduto</font>");
echo "<br>";
}
}
?>
[/PHP]
</body>
</html>

If I select "Wallpapers" in the combo I'd like to show in the page:

Produtos existentes:
Tim
Motorola

and if I select "Icons" i'd like to show

Produtos existentes:
House

WHAT SHOUD I DO?
Thank you!

Zipper

7:31 pm on Nov 8, 2004 (gmt 0)

10+ Year Member



You can populate the drop down menu in two ways. Either server-side or client-side. In server-side, when an item in the menu is selected you will have to submit the form, send the request to the server, and populate the products drop-down menu as a part of the response.

In the client-side approach, you will be populating all the menu items and the products in the first page itself and use javascript to display the approriate products when the menu item is selected.

As you can see the first approach puts more work on the server and could be much slower depending on response time. Personally, I preffer the client-side approach, unless you need to prevent exposure to the products in the source. But using the server-side approach gives you full control over the entire process.

Let us know which way u would like to do this..

rogernem

2:42 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



I believe client-side would be better.
This is what i've started:

<script language ="JavaScript">
<!-- hide me
var Wallpapers = new Array("blah1","blah2","blah3");
var Icons = new Array("blah4","blah5","blah6");

function swap_options()
{
var select1 = document.getElementById("menuitem");
var select2 = document.getElementById("products");
var the_array = eval(select1.options[select1.selectedIndex].text);

select2.options.length = the_array.length;
for (loop=0; loop < select2.options.length; loop++)
{
select2.options[loop].value = the_array[loop];
select2.options[loop].text = the_array[loop];
}
}

// -->
</script>

<select id ="menuitem" name="menuitem" style="width:200" onChange="swap_options();"><option>make a selection</option><option>Wallpapers</option><option>Icons</options></select>
<select id="products" name="products"><option></option></select>

But the thing is: I cant write var Wallpapers = new Array("blah1","blah2","blah3"); manually

It should be something like:
var $menuitem = new Array $nomeproduto

Any suggestions?
Tkz

Salsa

7:03 pm on Nov 9, 2004 (gmt 0)

10+ Year Member



Rogernem, welcome to webmasterworld!

This is totally untested, and it could be made more complicated, but it may be a scheme that will work for you:

<? 
...
$menu_select_block = "<select name=\"menuitem\" style=\"width:200\" onChange=\"swapFunction()\">\n";
$produto_select_block = "<select name=\"produto\" style=\"width:200\">\n";
$JS_arrays = "";
$JS_header_function = "function swapFunction(...JavaScript stuff...\n";
$query = "SELECT menuitem FROM menu ORDER BY menuitem";
$result = mysql_query($query);
$i = 0;
while ($query_data = mysql_fetch_array($result)) {
$menuitem = $query_data['menuitem'];
$menu_select_block .= "<option>$menuitem</option>\n";
$JS_arrays .= "var $menuitem = new Array(";
$JS_header_function .= "...maybe more JavaScript stuff...";
// (not certain of field names in table)
$query2 = "SELECT produto FROM produtos WHERE nomeproduto = '$menuitem' ORDER BY produto";
$result2 = mysql_query($query2);
while ($query_data2 = mysql_fetch_array($result2)) {
$produto = $query_data2['produto'];
// populate produto options for first menuitem option:
if ($i == 0) $produto_select_block .= "<option>$produto</option>\n";
$JS_arrays .= "'".$produto."', ";
}
$JS_arrays = substr($JS_arrays,0,-2); // remove trailing comma and space
$JS_arrays .= ");\n"; // close the array
$i++;
}
$menu_select_block .= "</select>\n";
$produto_select_block .= "</select>\n";
$JS_header_function .= "...final swap_function() stuff...}\n";
...
?>
<html><head>
...
<script language="JavaScript" type="text/JavaScript">
<!--
<?
echo $JS_arrays;
echo $JS_header_function:
?>
...
-->
</script>
...
</head></html><body>
... <!-- and in form: -->
... <? echo $menu_select_block;?>...
... <? echo $produto_select_block;?>...
...

Salsa

4:55 am on Nov 10, 2004 (gmt 0)

10+ Year Member



I just took another peek at this and noticed that I hadn't recognized $query2 as a JOIN. The tables need to be qualified to remove ambiguities, plus I had misquoted:

$query2 = "SELECT produtos.produto FROM produtos, menu WHERE produto.nomeproduto = menu.$menuitem ORDER BY produtos.produto";