Page 1 of 1
Populating a list from a MySQL query
Posted: Thu Sep 16, 2004 7:42 pm
by auburn
I am trying to populate one drop down list based on the contents of another list. When a user selects an item from the first list, I want to run a MySQL query based on the value of that item, then populate the second list from the results of the query.
I realize that PHP is server-side, and that this will require the page to be reloaded, but as long as the functionality is as I described, then its okay. The main issues are having the second list populate automatically once an item from the first list is selected and to populate the second list with a MySQL query using PHP.
Anyone have any ideas as to how this can be accomplished? Any help at all would be appreciated.
Posted: Thu Sep 16, 2004 7:44 pm
by dethron
First you have two choices,
1) you can reload page one more time, and hadle using PHP
2) when page is opened PHP access to db, then handle rest using DOM
Make your choice, if you have ay further question on pro/con, just ask

Posted: Thu Sep 16, 2004 7:48 pm
by feyd
you could actually have pre-stored the contents of each result from the first list.... and use Javascript to repopulate the second list after the selection of the first list....
Posted: Thu Sep 16, 2004 7:59 pm
by auburn
I don't know anything about DOM so that probably won't work for me.
I suppose I could just have a Javascript function call the page again with a variable in the query string which PHP could use to populate the second list. I'd like to avoid reloads as much as possible though.
If I pre-store the results of each possible query (there would only be a handfull to store), I'm still not sure how I can use Javascript to choose the correct set of results. Can I just stick a PHP block in a Javascript function?
Posted: Thu Sep 16, 2004 8:03 pm
by dethron
DOM = document.object

in other words, it is javascript.
if you want to reduce number of reloads, then you are on the 2nd way
here is an example
Code: Select all
<form action="prices.php" method="post" name="calcit">
<table cellpadding="2" cellspacing="2" align="center">
<tr><td colspan="3"><img src="images/spacer.gif" height="5"></td></tr>
<tr><td>KALKIÞ</td><td>:</td><td>
<select name="start" onchange="change()" style="width: 190px">
<option selected value="xnull">Kalkýþ için bir yer seçiniz.</option>
<option value="kosekoy">Ýzmit (Köseköy)</option>
<option value="afyon">Afyon</option>
<option value="sivas">Sivas</option>
</select>
</td></tr>
<tr><td>VARIÞ</td><td>:</td><td align="left">
<select name="finish" style="width: 190px">
<option selected value="xnull">Varýþ için bir yer seçiniz.</option>
</select>
</td></tr>
<tr><td>AÐIRLIK (ton)</td><td>:</td><td><input type="text" value="0" name="ton" size="14"></td></tr>
<tr><td>ÜCRET (TL)</td><td>:</td><td><input type="text" value="0,000" name="ton" size="14" disabled></td></tr>
<tr><td colspan="3"><img src="images/spacer.gif" height="3"></td></tr>
<tr><td colspan="3" align="center"><input type="submit" name="Hesapla" value="Hesapla" class="xinput"></td></tr>
<tr><td colspan="3"><img src="images/spacer.gif" height="5"></td></tr>
</table>
</form>
and the javascript part goes here
Code: Select all
<script language="javascript">
var arFieldText1=new Array("Eskiþehir","Afyon","Ankara","Kayseri","Gaziantep","Mersin","Samsun","Diyarbakýr","Van(Tatvan)","Van(Kapýköy)","Erzurum","Erzincan","Kars","Zonguldak","Denizli","Burdur");
var arFieldText2=new Array("Ýzmir(Alsancak)","Ýzmir(Basmane)");
var arFieldText3=new Array("Samsun");
var arFieldValue1=new Array("eskisehir","afyon","ankara","kayseri","gaziantep","mersin","samsun","diyarbakir","van1","van2","erzurum","erzincan","kars","zonguldak","denizli","burdur");
var arFieldValue2=new Array("izmir1","izmir2");
var arFieldValue3=new Array("samsun");
function change() {
for (var i=document.calcit.finish.length; i>=0; i--) {
document.calcit.finish[i] = null;
}
if(document.calcit.start.selectedIndex == 1){
//alert("1");
for (var i=0; i < arFieldValue1.length; i++)
document.calcit.finish[i] = new Option(arFieldText1[i],arFieldValue1[i]);
//alert(arFieldText1);
}
else if(document.calcit.start.selectedIndex == 2){
//alert("2");
for (var i=0; i < arFieldValue2.length; i++)
document.calcit.finish[i] = new Option(arFieldText2[i],arFieldValue2[i]);
//alert(arFieldValue2.length);
//alert(arFieldText2);
}
else if(document.calcit.start.selectedIndex == 3){
//alert("3");
for (var i=0; i < arFieldValue3.length; i++)
document.calcit.finish[i] = new Option(arFieldText3[i],arFieldValue3[i]);
}
}
function contin() {
change();
}
function makeArray() {
for (i = 0; i<makeArray.arguments.length; i++)
this[i] = makeArray.arguments[i];
}
</script>
Posted: Thu Sep 16, 2004 8:05 pm
by feyd
the php will get executed before the page hits the browser.. so not really.. having a new list of options for each selection is what I was hinting at.. it would use the Option object in Javascript.
Posted: Thu Sep 16, 2004 8:05 pm
by dethron
this is something i wrote before,
if you need to see it online, use
http://www.servicexpress-tr.com/prices.php
Posted: Thu Sep 16, 2004 8:21 pm
by auburn
Okay, I understand now how I can use javascript to control the list displayed based on a specified value. Now how do I incorporate MySQL into the mix?
In your example dethron, you hard-coded the possible lists in javascript, but the values in the lists I am using are dynamic. The data in both lists comes from a MySQL database so I won't know until run-time what the lists are supposed to contain.
Posted: Thu Sep 16, 2004 8:25 pm
by feyd
you do all that processing before the page is fully sent to the browser.
Posted: Thu Sep 16, 2004 8:28 pm
by dethron
they are dynamic in my example too.
this is just the output.
as feyd stated above, connect to db at the very top of the page, and create arrays, using PHP
good luck,
i'm sure the issue will be solved soon

Posted: Thu Sep 16, 2004 8:45 pm
by auburn
Forgive my ignorance, I've only been using PHP for a week, but I still can't figure this out. I know I can load arrays in PHP at the top of the page, but I can't figure out how to use them as needed. How can I access the correct array in javascript?
Posted: Thu Sep 16, 2004 8:48 pm
by dethron
1) do you know access to db and retrieve data?
2) use PHP to echo some of this data (it may me some javascript functions

3) you are done
my first advice to you, retrieve data from db, and echo them to display...
Posted: Fri Sep 17, 2004 6:22 am
by John Cartwright
My first advice to you is head over to
[php_man]array[/php_man]