User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the PHP section within the Web Development category of DaniWeb, a massive community of 426,659 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 1,532 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our PHP advertiser: Lunarpages PHP Web Hosting
Views: 426 | Replies: 4
Reply
Join Date: Feb 2008
Posts: 3
Reputation: ddwatkins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ddwatkins ddwatkins is offline Offline
Newbie Poster

Help...i think it is simple....Im Just new

  #1  
Feb 13th, 2008
I have a set of 2 drop downs. Once a selection is made in the first one, the second one popluates based on the first choice...I also have a a dynamic table on my page that works fine but shows every record from what ever recordset it is based on....here is what I want to happen....'
user makes a choice in first drop down, the second drop down then narrows down and shows those choices ( this all works fine up to this point currently) then when the user selects the second drop down selection. i want my dynamic table to show only records that corespond to the selection in the drop down

Example: user chooses Model of car drop down.....different models are shown in the second drop down...then user selects type model they have then my table only shows parts for this model....Please help ...I know it is pretty simple..i think it is just a simple sql statement or something ..but I am new and this is my first data driven web site to build...thanks so much
If it matters I am using Dreamweaver Cs3

Thanks in advance to all
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Help...i think it is simple....Im Just new

  #2  
Feb 13th, 2008
Well, You mean, based on one dropdown's value, the other dropdown should be populated ? You can use ajax to do this. If you don't have enough time to learn ajax, then the simplest solution would be to submit the page when 'onchange' event occurs in the first dropdown.
  1. <select name="dropdown1" onchange="javascript: dropdown();">
  2. <option value=1>value1</option>
  3. <option value=2>value2</option>
  4. ....
  5. </select>
In function dropdown(), get the selected value, assign it to a hidden variable in the form and submit the page.

  1. function dropdown(){
  2. var value=document.formname.dropdown1.value;
  3. document.formname.hidden.value=value;
  4. document.formname.submit();
  5. }
When the form is submitted, get the hidden value, query the table based on the hidden value condition and list it in dropdown box 2.
  1. $selectedvalue=$_POST['hidden'];
  2. $query="select * from models where car_category='$selectedvalue'";
get the records and populate 2nd dropdown.

Hope it helps..
Last edited by nav33n : Feb 13th, 2008 at 6:51 pm.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 3
Reputation: ddwatkins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ddwatkins ddwatkins is offline Offline
Newbie Poster

Re: Help...i think it is simple....Im Just new

  #3  
Feb 13th, 2008
No, I do not need one drop down to popuate the other....I have this part working fine....I need when a user selects a value in the last drop down for that "value" to be the search criteria for populating my dynamic table.

Example: I have a table called models that has all car models on it. Each car has a unique ID number...when a user selects lets say Honda Accord in the drop down which has a unique id of 2 it populates my dynamic table with a list of all the different parts that could be used on a honda accord by looking for the ID 2 in my parts table....My database is all set up and has relationships and all works fine in the drilling down process in the database. I just need to know what code takes that "variable" that the user selects in the drop down and then passes it on to my dyanmic table with only on parts that go with that record returned
Reply With Quote  
Join Date: Nov 2007
Location: Bangalore, India
Posts: 3,098
Reputation: nav33n has a spectacular aura about nav33n has a spectacular aura about 
Rep Power: 8
Solved Threads: 240
nav33n's Avatar
nav33n nav33n is offline Offline
Posting Sensei

Re: Help...i think it is simple....Im Just new

  #4  
Feb 13th, 2008
Look for onchange event in the select tag ? Well, we could help if you post your code.
Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning.

*PM asking for help will be ignored*
Reply With Quote  
Join Date: Feb 2008
Posts: 3
Reputation: ddwatkins is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
ddwatkins ddwatkins is offline Offline
Newbie Poster

Re: Help...i think it is simple....Im Just new

  #5  
Feb 13th, 2008
I have copied and pasted my code from my testing page...
also here is the link of what this page look likes..
http://www.transpartsplus.com/Test.php
that table is only a sample of the data...there will be thousands of lines ...and I only want the ones that have the same ID as the model to be diplayed ....( only parts for a certain make/model are displayed once a user has selected the model in the drop down... I think i want this funtion to be maybe "onclick " vs on change sice the second list does change as soon as the first has a selection made... Thanks so far for all the help everyone


<?php require_once('Connections/local.php'); ?>
<?php
if (!function_exists("GetSQLValueString")) {
function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
{
$theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;

$theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

switch ($theType) {
case "text":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "long":
case "int":
$theValue = ($theValue != "") ? intval($theValue) : "NULL";
break;
case "double":
$theValue = ($theValue != "") ? "'" . doubleval($theValue) . "'" : "NULL";
break;
case "date":
$theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
break;
case "defined":
$theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
break;
}
return $theValue;
}
}

mysql_select_db($database_local, $local);
$query_Child = "SELECT * FROM Manufactors";
$Child = mysql_query($query_Child, $local) or die(mysql_error());
$row_Child = mysql_fetch_assoc($Child);
$totalRows_Child = mysql_num_rows($Child);

mysql_select_db($database_local, $local);
$query_Grandchild = "SELECT * FROM Models";
$Grandchild = mysql_query($query_Grandchild, $local) or die(mysql_error());
$row_Grandchild = mysql_fetch_assoc($Grandchild);
$totalRows_Grandchild = mysql_num_rows($Grandchild);

$maxRows_Details = 10;
$pageNum_Details = 0;
if (isset($_GET['pageNum_Details'])) {
$pageNum_Details = $_GET['pageNum_Details'];
}
$startRow_Details = $pageNum_Details * $maxRows_Details;

mysql_select_db($database_local, $local);
$query_Details = "SELECT * FROM Detail";
$query_limit_Details = sprintf("%s LIMIT %d, %d", $query_Details, $startRow_Details, $maxRows_Details);
$Details = mysql_query($query_limit_Details, $local) or die(mysql_error());
$row_Details = mysql_fetch_assoc($Details);

if (isset($_GET['totalRows_Details'])) {
$totalRows_Details = $_GET['totalRows_Details'];
} else {
$all_Details = mysql_query($query_Details);
$totalRows_Details = mysql_num_rows($all_Details);
}
$totalPages_Details = ceil($totalRows_Details/$maxRows_Details)-1;
?>
<?php
if ($row_Grandchild) {
echo "<SC" . "RIPT>\n";
echo "var WAJA = new Array();\n";

$oldmainid = 0;
$newmainid = $row_Grandchild["ManID"];
if ($oldmainid == $newmainid) {
$oldmainid = "";
}
$n = 0;
while ($row_Grandchild) {
if ($oldmainid != $newmainid) {
echo "WAJA[".$n."] = new Array();\n";
echo "WAJA[".$n."][0] = '".WA_DD_Replace($newmainid)."';\n";
$m = 1;
}

echo "WAJA[".$n."][".$m."] = new Array();\n";
echo "WAJA[".$n."][".$m."][0] = "."'".WA_DD_Replace($row_Grandchild["ID"])."'".";\n";
echo "WAJA[".$n."][".$m."][1] = "."'".WA_DD_Replace($row_Grandchild["Model"])."'".";\n";

$m++;
if ($oldmainid == 0) {
$oldmainid = $newmainid;
}
$oldmainid = $newmainid;
$row_Grandchild = mysql_fetch_assoc($Grandchild);
if ($row_Grandchild) {
$newmainid = $row_Grandchild["ManID"];
}
if ($oldmainid != $newmainid) {
$n++;
}
}

echo "var Grandchild_WAJA = WAJA;\n";
echo "WAJA = null;\n";
echo "</SC" . "RIPT>\n";
}
function WA_DD_Replace($startStr) {
$startStr = str_replace("'", "|WA|", $startStr);
$startStr = str_replace("\\", "\\\\", $startStr);
$startStr = preg_replace("/[\r\n]{1,}/", " ", $startStr);
return $startStr;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<script type="text/javascript">
<!--
function WA_ClientSideReplace(theval,findvar,repvar) {
var retval = "";
while (theval.indexOf(findvar) >= 0) {
retval += theval.substring(0,theval.indexOf(findvar));
retval += repvar;
theval = theval.substring(theval.indexOf(findvar) + String(findvar).length);
}
retval += theval;
if (retval == "" && theval.indexOf(findvar) < 0) {
retval = theval;
}
return retval;
}

function MM_findObj(n, d) { //v4.01
var p,i,x; if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
if(!x && d.getElementById) x=d.getElementById(n); return x;
}

function WA_UnloadList(thelist,leavevals,bottomnum) {
while (thelist.options.length > leavevals+bottomnum) {
if (thelist.options[leavevals]) {
thelist.options[leavevals] = null;
}
}
return leavevals;
}

function WA_FilterAndPopulateSubList(thearray,sourceselect,targetselect,leaveval,bottomleave,usesource,delimiter) {
if (bottomleave > 0) {
leaveArray = new Array(bottomleave);
if (targetselect.options.length >= bottomleave) {
for (var m=0; m<bottomleave; m++) {
leavetext = targetselect.options[(targetselect.options.length - bottomleave + m)].text;
leavevalue = targetselect.options[(targetselect.options.length - bottomleave + m)].value;
leaveArray[m] = new Array(leavevalue,leavetext);
}
}
else {
for (var m=0; m<bottomleave; m++) {
leavetext = "";
leavevalue = "";
leaveArray[m] = new Array(leavevalue,leavetext);
}
}
}
startid = WA_UnloadList(targetselect,leaveval,0);
mainids = new Array();
if (usesource) maintext = new Array();
for (var j=0; j<sourceselect.options.length; j++) {
if (sourceselect.options[j].selected) {
mainids[mainids.length] = sourceselect.options[j].value;
if (usesource) maintext[maintext.length] = sourceselect.options[j].text + delimiter;
}
}
for (var i=0; i<thearray.length; i++) {
goodid = false;
for (var h=0; h<mainids.length; h++) {
if (thearray[i][0] == mainids[h]) {
goodid = true;
break;
}
}
if (goodid) {
theBox = targetselect;
theLength = parseInt(theBox.options.length);
theServices = thearray[i].length + startid;
var l=1;
for (var k=startid; k<theServices; k++) {
if (l == thearray[i].length) break;
theBox.options[k] = new Option();
theBox.options[k].value = thearray[i][l][0];
if (usesource) theBox.options[k].text = maintext[h] + WA_ClientSideReplace(thearray[i][l][1],"|WA|","'");
else theBox.options[k].text = WA_ClientSideReplace(thearray[i][l][1],"|WA|","'");
l++;
}
startid = k;
}
}
if (bottomleave > 0) {
for (var n=0; n<leaveArray.length; n++) {
targetselect.options[startid+n] = new Option();
targetselect.options[startid+n].value = leaveArray[n][0];
targetselect.options[startid+n].text = leaveArray[n][1];
}
}
for (var l=0; l < targetselect.options.length; l++) {
targetselect.options[l].selected = false;
}
if (targetselect.options.length > 0) {
targetselect.options[0].selected = true;
}
}
//-->
</script>
</head>

<body>
<form id="form1" name="form1" method="post" action="">
<select name="Child" id="Child" onchange="WA_FilterAndPopulateSubList(Grandchild_WAJA,MM_findObj('Child'),MM_findObj('Grandchild'),0,0,false,': ')">
<option value="">Select Manufactor</option>
<?php
do {
?>
<option value="<?php echo $row_Child['ManID']?>"><?php echo $row_Child['Manufacturer']?></option>
<?php
} while ($row_Child = mysql_fetch_assoc($Child));
$rows = mysql_num_rows($Child);
if($rows > 0) {
mysql_data_seek($Child, 0);
$row_Child = mysql_fetch_assoc($Child);
}
?>
</select>
<select name="Grandchild" id="Grandchild">
<option value="">Selcet Model</option>
<?php
do {
?>
<option value="<?php echo $row_Grandchild['ID']?>"><?php echo $row_Grandchild['Model']?></option>
<?php
} while ($row_Grandchild = mysql_fetch_assoc($Grandchild));
$rows = mysql_num_rows($Grandchild);
if($rows > 0) {
mysql_data_seek($Grandchild, 0);
$row_Grandchild = mysql_fetch_assoc($Grandchild);
}
?>
</select>
</form>
<form id="form2" name="form2" method="post" action="">
<table border="1">
<tr>
<td>DetailID</td>
<td>Cycle</td>
<td>Liter</td>
<td>Year</td>
<td>Desc</td>
<td>Fist</td>
<td>Second</td>
</tr>
<?php do { ?>
<tr>
<td><?php echo $row_Details['DetailID']; ?></td>
<td><?php echo $row_Details['Cycle']; ?></td>
<td><?php echo $row_Details['Liter']; ?></td>
<td><?php echo $row_Details['Year']; ?></td>
<td><?php echo $row_Details['Desc']; ?></td>
<td><?php echo $row_Details['Fist']; ?></td>
<td><?php echo $row_Details['Second']; ?></td>
</tr>
<?php } while ($row_Details = mysql_fetch_assoc($Details)); ?>
</table>
</form>
<p>&nbsp;</p>
</body>
</html>
<?php
mysql_free_result($Child);

mysql_free_result($Grandchild);

mysql_free_result($Details);
?>
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb PHP Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the PHP Forum

All times are GMT -4. The time now is 1:47 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC