PDA

View Full Version : Change subcategories values...


pavmoxo
06-27-2006, 12:46 PM
I have 2 dropdown that I have to associate:


This is the category dropdown:
<select name="cat_ID" id="cat_ID" tabindex="3">
<?
$sql = "select cat_ID, cat_nome from cat_links order by cat_nome" ;
$query = query($sql);
$conta = mysql_num_rows($query);
for ($i=0;$i<$conta;$i++)
{
$arrl = mysql_fetch_array($query);
?>

<option value="<?php echo $arrl["cat_ID"]; ?>"><?php echo $arrl["cat_nome"]; ?></option>
<? } ?>

</select>

And this is the subcategory dropdown:
<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select s.subcat_ID, s.subcat_nome from subcat_links s, cat_links c where c.cat_ID = ".$arrl["cat_ID"]." order by s.subcat_nome" ;
$query = query($sql);
$conta = mysql_num_rows($query);
for ($i=0;$i<$conta;$i++)
{
$arrl = mysql_fetch_array($query);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>

</select>


What I want is when in choose a category, the subcategories will change to correpond. Can you help me??

cubsfan
06-27-2006, 02:17 PM
Only options I can think of would be to use javascript to resubmit the form after each selection and rebuild the dropdowns, or you can probably build some javascript dynamically from the database when the page loads.

Or possibly investigate AJAX.

pavmoxo
06-28-2006, 03:31 PM
I think one possibity is use the event OnChange in first dropdown but I don´t know how to do it!! Can You help me??

cubsfan
06-29-2006, 05:01 PM
Should be able to use this or some variation of it.

http://gethelp.devx.com/techtips/js_pro/10min/10min1298/10min1298.asp

pavmoxo
06-30-2006, 10:18 AM
can I put PHP in a javascript function?

I try another example before a see the last post:

<script type="text/javascript">
<!--

function selectChange(control, controlToPopulate, ItemArray, GroupArray) {
var myEle ;
var x ;
// Empty the second drop down box of any choices
for (var q=controlToPopulate.options.length;q>=0;q--) controlToPopulate.options[q]=null;

// ADD Default Choice - in case there are no values
myEle=document.createElement("option");
theText=document.createTextNode("[SELECCIONE]");
myEle.appendChild(theText);
myEle.setAttribute("value","0");
controlToPopulate.appendChild(myEle);
// Now loop through the array of individual items
// Any containing the same child id are added to
// the second dropdown box
for ( x = 0 ; x < ItemArray.length ; x++ ) {
if ( GroupArray[x] == control.value ) {
myEle = document.createElement("option") ;
//myEle.value = x ;
myEle.setAttribute("value",x);
// myEle.text = ItemArray[x] ;
var txt = document.createTextNode(ItemArray[x]);
myEle.appendChild(txt)
// controlToPopulate.add(myEle) ;
controlToPopulate.appendChild(myEle)
}
}
}
//-->
</script>


<td>
<? if ($_POST["nova_cat"] != "Nova ...") {?>

<?
$arrItems1 = array();
$arrItemsGrp1 = array();

$sql = "select * from subcat_links s, cat_links c where c.cat_ID=s.cat_ID order by s.subcat_nome" ;
$querys = query($sql);
$contal = mysql_num_rows($querys);
for ($j=0;$j<$contal;$j++)
{
$arrs = mysql_fetch_array($querys);

$arrItems1[$arrs["cat_ID"]] = $arrs["subcat_nome"];
$arrItemsGrp1[$arrs["cat_ID"]] = $arrs["cat_ID"];
}

?>

<!-- onChange="selectChange(this, form.subcat_ID, <? //echo $arrItems1; ?>, <? //echo $arrItemsGrp1; ?>);" -->
<select name="cat_ID" id="cat_ID" tabindex="3" onChange="selectChange(this, form.subcat_ID, <? echo $arrItems1; ?>, <? echo $arrItemsGrp1; ?>);">


<?
$sql = "select cat_ID, cat_nome from cat_links order by cat_nome" ;
$query = query($sql);
$conta = mysql_num_rows($query);
for ($i=0;$i<$conta;$i++)
{
$arr = mysql_fetch_array($query);
?>

<option value="<?php echo $arr["cat_ID"]; ?>"><?php echo $arr["cat_nome"]; ?></option>
<? } ?>

</select>
<input name="nova_cat" type="submit" value="Nova ...">
<input name="del_cat" type="submit" value="Apagar">

<? } else { ?>

<input name="cat_nome" type="text" class="frm1" id="cat_nome" size="10">
<input name="insert_cat" type="submit" value="Inserir">
<? } ?>


</td>
<td>

<? if ($_POST["nova_subcat"] != "Nova ...") {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3"></select>

<!-- <select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
/* $sql = "select s.subcat_ID, s.subcat_nome from subcat_links s order by s.subcat_nome" ;
$query = query($sql);
$conta = mysql_num_rows($query);
for ($i=0;$i<$conta;$i++)
{
$arrl = mysql_fetch_array($query); */
?>

<option value="<?php //echo $arrl["subcat_ID"]; ?>"><?php //echo $arrl["subcat_nome"]; ?></option>
<? //} ?>

</select> -->

<input name="nova_subcat" type="submit" value="Nova ...">
<input name="del_subcat" type="submit" value="Apagar">

<? } else { ?>

<input name="subcat_nome" type="text" class="frm1" id="subcat_nome" size="10">
<input name="insert_subcat" type="submit" value="Inserir">
<? } ?>


</td>{

I don't obtain the subcategories. Do you know why??

cubsfan
07-02-2006, 02:00 AM
I would guess that either the array is not in the format expected by the selectchange function or there are other errors. If you are using a mozilla browser variant, type "javascript:" (without the quotes) and see if the javascript is running without errors.

pavmoxo
07-03-2006, 12:41 PM
I'm very dificulty to it. Do you some script in PHP that change values like that. I think with javascript is hard to do it. Can You help me??

cubsfan
07-03-2006, 06:27 PM
I can't really give you much exact code because I don't know much about how the database you are using. But the general idea is to setup the form to post to itself and set the select to post the form whenever is it changed using onChange="document.FormName.submit;". Use PHP to grab the child values based on the parent option posted and write the new select values out.





echo "<select name='child_select'>";

$sql= "select id, name from child_table where parent_id='" . $_POST['parent_id'] . "'";
$query = mysql_query($sql);
while ($row=mysql_fetch_array($query)) {
echo "<option value='" . $row['id'] . "'>" . $row['name'] . "\n";
}

pavmoxo
07-04-2006, 11:02 AM
I tried as the last post but with no results.
At this moment the part of the script is like this:


<tr>
<td>Categoria</td>
<td>SubCategoria</td>
</tr>
<tr>
<td>
<? if ($_POST["nova_cat"] != "Nova ...") {?>

<select name="cat_ID" id="cat_ID" tabindex="3" onChange="subcat_ID.submit;">


<?
$sql = "select cat_ID, cat_nome from cat_links order by cat_nome" ;
$query = query($sql);
$conta = mysql_num_rows($query);
for ($i=0;$i<$conta;$i++)
{
$arr = mysql_fetch_array($query);
?>

<option value="<?php echo $arr["cat_ID"]; ?>"><?php echo $arr["cat_nome"]; ?></option>
<? } ?>

</select>
<input name="nova_cat" type="submit" value="Nova ...">
<input name="del_cat" type="submit" value="Apagar">

<? } else { ?>

<input name="cat_nome" type="text" class="frm1" id="cat_nome" size="10">
<input name="insert_cat" type="submit" value="Inserir">
<? } ?>


</td>
<td>

<? if ($_POST["nova_subcat"] != "Nova ...") {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select s.subcat_ID, s.subcat_nome from subcat_links s, cat_links c where c.cat_ID=".$arr["cat_ID"]." order by s.subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>

</select>

<input name="nova_subcat" type="submit" value="Nova ...">
<input name="del_subcat" type="submit" value="Apagar">

<? } else { ?>

<input name="subcat_nome" type="text" class="frm1" id="subcat_nome" size="10">
<input name="insert_subcat" type="submit" value="Inserir">
<? } ?>


</td></tr>

I need help quickly!!!....

pavmoxo
07-04-2006, 07:22 PM
I don't know what to do more!!! I tried with this form:



<td>

<select name="cat_ID" id="cat_ID" onChange="linkst.action='linkst.php?cat=cat_ID'; linkst.submit();">
<option value="0" selected>[SELECCIONE]</option>
<option value="1">Comunidades</option>
<option value="2">Cultura e Informação</option>
<option value="3">Espaços e Eventos</option>
<option value="4">Fotografia e Imagem</option>
<option value="5">Shopping</option>
<option value="6">Pessoais e Blogs</option>
</select>

</td>
<td>



<? if ($_POST["nova_subcat"] != "Nova ...") {?>

<?
if($_GET["cat"] == '1')
{
?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 1 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } elseif($_GET["cat"] == '2') {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 2 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } elseif($_GET["cat"] == '3') {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 3 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } elseif($_GET["cat"] == '4') {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 4 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } elseif($_GET["cat"] == '5') {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 5 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } elseif($_GET["cat"] == '6') {?>

<select name="subcat_ID" id="subcat_ID" tabindex="3">
<?
$sql = "select subcat_ID, subcat_nome from subcat_links where cat_ID = 6 order by subcat_nome" ;
$queryl = query($sql);
$contal = mysql_num_rows($queryl);
for ($j=0;$j<$contal;$j++)
{
$arrl = mysql_fetch_array($queryl);
?>

<option value="<?php echo $arrl["subcat_ID"]; ?>"><?php echo $arrl["subcat_nome"]; ?></option>
<? } ?>
</select>

<? } ?>



<input name="nova_subcat" type="submit" value="Nova ...">
<input name="del_subcat" type="submit" value="Apagar">

<? } else { ?>

<input name="subcat_nome" type="text" class="frm1" id="subcat_nome" size="10">
<input name="insert_subcat" type="submit" value="Inserir">
<? } ?>


</td>



But nothing...
Have you any script for this case?? Please I need help!!!!

cubsfan
07-05-2006, 03:26 PM
<select name="cat_ID" id="cat_ID" tabindex="3" onChange="subcat_ID.submit;">


Is subcat_ID the name of your form? Is the javascript running on the page without errors?