Page 1 of 1
Double Drop Down Menu - problem with code!!!
Posted: Tue Aug 23, 2005 11:01 am
by alixir
Hi there, took ages to understand how to get this far!!! Looking to see if anybody has any way of helping me with the following.
I have two tables:
clients (client_id, client_name)
location (client_id, client_site_name)
The drop down for the clients I have already written in the code below. I have the logical solution in my head already and all the conditions that need to be met for the second drop down to work.....
Trying to make this work in PHP is driving me nuts.....just doesn't seem to work the same way as programming in basic! (understandably!)
Any help with this would be great, I could then get on and create all the other drop downs for the sections......
Cheers!
code so far:
CONFIG.INC.PHP
Code: Select all
<?php
// Variables for the connection
$dbserver="localhost";
$dbusername="root";
$dbpassword="password";
$dbtablename="clients";
$db="fixitsupport";
?>
***************************DBCONNECT.INC.PHP
<?
include_once ("config.inc.php"); ?>
<?php
// This will connect to the database or return an error
// String containing the mysql connection command
$dbconnect = @mysql_connect($dbserver, $dbusername, $dbpassword) or die (mysql_error());
// Command to connect to the database
mysql_select_db($db, $dbconnect) or die (mysql_error);
?>
**********************DROPDOWN.PHP
Code: Select all
<?
// Include the Database connection script
include_once ('dbconnect.inc.php');
// Perform the mysql query on the clients table
$link = mysql_connect ($dbserver, $dbusername, $dbpassword);
$query = "Select client_name from $dbtablename order by client_name";
$results = mysql_db_query($db, $query, $link);
?>
Which Client would you like to select?
<select name = "clientdropdownselection" size="1">
<Option Value=" ">Select One:</option>
<? //Begins PHP
for($u=0;$u<mysql_num_rows($results); $u++) {
$client=mysql_result($results,$u,'client_name');
?>
<option value="<? echo($client); ?>"><? echo($client); ?></option>
<? //Begins PHP
}
?>
</select>
<form method=post action="pagetolinkto.php">
<input type=submit value="Next">
</form>
feyd | Please use Code: Select all
tags where approriate when posting code. Read: [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]
Posted: Tue Aug 23, 2005 1:33 pm
by feyd
what's the actual problem?
Posted: Thu Aug 25, 2005 3:27 am
by alixir
The problem is that I've read through various topics and walkthroughs to get a second menu populated.....basically i want to achieve the following:
when a client is selected, you then have the option to use a secondary pulldown menu to select a location related to the client which will be populated from the location table:
location (client_id, client_site_name)
I know that I can link the client_id from both tables and that client_id is not able to be a primary key since it can be linked to many client site names.
The problem is that I dont know how to get the drop down to work within one for loop and every other example I have seen has been a multitude of code that I just don't understand why it has to be soooooo long and complex, just looking for a nifty trick that doesn't involve me having to learn 3 languages at the same time (java examples I have seen). Or a logical pointer in the right direction will do!
My original way of thinking way just to have another drop down menu outside of the for loop that ran the sql query to select all the client_site_name matches where the client_id was the same as the choice in the client drop down menu but I don't know how to pull the client_id from the first pull down menu to populate the second!
Cheers!
Posted: Thu Aug 25, 2005 3:44 am
by JayBird
you will have to use javascript to populate the second menu, or, if you really dont want to use javascript, when an option in the first menu is select, the page will need to be automatically submitted to the server and reloaded to populate the second menu.
Javascript would be the way to go in my opinion.
Take a look at this code, this does what you want. You will have to populate the Javascript arryas using php
Code: Select all
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<SCRIPT LANGUAGE="JavaScript">
<!--
main_list=new Array()
main_list[main_list.length] = new Array("fruits one","fruits two","fruits three")
main_list[main_list.length] = new Array("veg one","veg two","veg three","veg four","veg five")
main_list[main_list.length] = new Array("bev one","bev two","bev three")
main_list[main_list.length] = new Array("sweets one","sweets two","sweets three","sweets four")
main_list[main_list.length] = new Array("grain one","grain two","grain three","grain four","grain five","grain six")
function load() {
var the_list = menu.list.options[menu.list.selectedIndex].value;
select = window.document.menu.SubCategory;
select.options.length = 0;
for(i=0;i<main_list[the_list].length;i++){
select.options[i] = new Option( main_list[the_list][i] );
}
}
// -->
</SCRIPT>
</head> <body>
<FORM NAME="menu">
<table border="1" width="400" height="200">
<tr valign="top">
<td>
<SELECT NAME="list" onChange="load()">
<OPTION VALUE=0>Fruits
<OPTION VALUE=1>Veggies
<OPTION VALUE=2>Beverages
<OPTION VALUE=3>Sweets
<OPTION VALUE=4>Grains
</SELECT>
<INPUT TYPE="submit" VALUE="Load">
</td>
<td width=200>
<select name="SubCategory" style="width:120;"></select></td>
</tr>
</table></FORM>
</body>
</html>
Posted: Thu Aug 25, 2005 3:49 am
by alixir
Cheers for the reply, although I think that the javascript method you have posted is a non dynamic way to do it, this has to be populated from mysql, unless I read your code incorrectly???
Would I have to GET the result of the drop down list on a second page or can I force the php script to refresh itself to display all on the same page without having to go to another page?
Still trying to associate how php runs scripts in comparison to basic or machine code, those are what I'm normally used to (gosubs, returns etc...). Does php script continuously loop the code?
Cheers...
Posted: Thu Aug 25, 2005 3:51 am
by JayBird
alixir wrote:Cheers for the reply, although I think that the javascript method you have posted is a non dynamic way to do it, this has to be populated from mysql, unless I read your code incorrectly???
Which is why i said "You will have to populate the Javascript arrays using php"
I have given you an example to get you started.
PHP will only run when you submit information back to the server, so PHP alone cannot update the second menu
without submitting the page back to the server, running a query, and sebding the data back to the user.
The method i have given yo is the way to go. The above code is by no means the final code you will need....but it gives you a 70% head start
Posted: Thu Aug 25, 2005 7:31 am
by alixir
Still way too complex and as I said, I will have to start learning javascript to pull your scripting apart and tailor it for my use. The whole idea of me learning php is to understand php fully. As I've found throughout these forums, people just post up code with little or no explanation of how it works and expect other people to just understand it because they do. I found a much simpler solution elsewhere that is understandable which involves minimal understandable java.
Posted: Thu Aug 25, 2005 8:11 am
by JayBird
...and perhaps you should post your simple solution so others can benefit in the future.
My example was not too complex...i believe in other words you meant..."i cant be bothered to figure it out".
As for understanding PHP fully, great idea, and good luck to you, but if you are going to be building web applications, then you are going to have to use other technologies somewhere along the way...you cant hide from the fact.
Always remember, the people giving you help on these forums do so at their free will and in their own spare time. Im quite happy to answer questions on soltuions i have provided. Im not going to waste time explaning every details of the solutions i give becuase i have no idea of your skill level and which bits you may or may not understand.
Posted: Thu Aug 25, 2005 8:31 am
by alixir
I sort Mac and PC problems for people in other forums on the net and when I do so, I provide concise instructions no matter what I think the user can handle, always for the novice upwards as people with more aptitude will skip the bits they know and use the ones they don't, there is no such thing as wasting time, half explained instructions are never going to work and yes, there are even dyslexic people in this world. And yes, when I get the rest of the menus working, the complete solution will be posted up here and elsewhere so all can see.
"My example was not too complex...i believe in other words you meant..."i cant be bothered to figure it out". " - is always the way advanced users think, lazy is one thing I am not mate, especially when it comes to C++
That kind of response is something I would expect from a teenager from Lower Bentham in Yorkshire.
Posted: Thu Aug 25, 2005 8:37 am
by feyd
advice: Don't go searching for a fight here.
Posted: Thu Aug 25, 2005 8:40 am
by alixir
response:those were not fighting words, he insulted me first by calling me lazy, I have not used any expletives or fighting gestures.....
Posted: Thu Aug 25, 2005 8:44 am
by JayBird
1) I explained EXACTLY what you needed to do to get the solution working...but as you highlighted, you didnt even read my first post properly.
2) What do you think helps people learn more, spoon feeding the answers...or getting the guy to think a little bit for himself...in my 10 years experience, the latter has ALWAYS been true.
3) I never said you were lazy, but i was correct in saying that you couldn't be bothered to look further into my solution to see how it worked, and as i said, i was/am quite prepared to explain anything you didnt understand. Soving PC and MAC problems is far easier giving a concise answer than me explaining every line of the code i gave.
4) Looking forward to seeing your solution
5) Im sure there are some lovely people in Lower Bentham in Yorkshire
Posted: Thu Aug 25, 2005 9:03 am
by alixir
1) True - I admit, I misread whilst reading
2) This would not be labelled as "spoon feeding" especially to people that know nothing about Java. People who learn how to modify other's code to get it working then go on to understand the syntax and how the module works to further understand how the various instructions can be used in other developments. Either way, the learner has to think for themselves.
3) "couldn't be bothered" - translates in many people's heads into lazy. Looking into your Java idea led to the other solution I found and understood enough to implement a version of it into my code, so no, saying I couldn't be bothered is wrong...
4) Indeed you will
5) Go visit, I'm sure you'll change your mind!!! =)