Welcome, Guest: Register On Nairaland / LOGIN! / Trending / Recent / NewStats: 3,153,217 members, 7,818,740 topics. Date: Sunday, 05 May 2024 at 11:44 PM |
Nairaland Forum / Science/Technology / Programming / Need Help On This Php Code (1301 Views)
Pls Help Me With This Php Code / Help On Php Code For A Dice Game / Help With This Php Code. (2) (3) (4)
Need Help On This Php Code by dmark4real: 2:43pm On Jun 27, 2012 |
I need you friends to help look into this php code. This is the logic, If a user should select a country from the the drop down menu e.g "select your country" BELGIUM then ALL the states under BELGIUM be loaded inside the "select a states" input tag in the form. I created a database, two tables i.e Table 1, countries with (id,country_id, country). Table 2, (id,state_id,states). with following code frm php: <td>NATIONALITY: </td> <td><select name="na"><option name="c">-SELECT YOUR COUNTRY-</option> <?php $sql = "select * from countries"; $result = mysql_query($sql); while($data = mysql_fetch_assoc($result)){ echo "<option>".$data["countries"]."</option>"."<br>"; } ?> </select></td> </tr> <tr> <td>STATE OF ORIGIN: </td> <td><select> <?php $sql = "SELECT countries.country, state FROM countries, state WHERE countries.country_id = state.state_id LIMIT 0 , 30"; $result = mysql_query($sql); $count =mysql_num_rows($result); if($count==1 && $_REQUEST["na"]==1){ while($row = mysql_fetch_assoc($result)){ echo "<option>".$row["state"]."</option>"."<br>"; } } ?> </select></td> So, the problem I have with above code is that when the page load and a country is selected, the page does not auto load the state of the selected country. Can someone help here pls. |
Re: Need Help On This Php Code by delomos(m): 3:03pm On Jun 27, 2012 |
looking at your query: "SELECT countries.country, state FROM countries, state WHERE <b>countries.country_id = state.state_id</b>" perhaps what you intend to say is: "SELECT state FROM countries WHERE country_id=". $_REQUEST['selected_country_id'] (notice I've made the query shorter, and a bit more efficient, you don't need the aliases) then: $row["state"] should give the right state. |
Re: Need Help On This Php Code by dmark4real: 3:17pm On Jun 27, 2012 |
"SELECT state FROM countries WHERE country_id=". $_REQUEST['selected_country_id'] so you mean STATE will be a field in the COUNTRIES table, like that? for all the countries i.e if i have 10countries under country field then their states or do you mean the STATE will hav its own table? thanks |
Re: Need Help On This Php Code by delomos(m): 4:05pm On Jun 27, 2012 |
If you're trying to represent countries and their respective states, a schema you can consider is: [countries] <-- table 1 + id (auto_increment) + name (country names) [states] <-- table 2 + id (auto_increment) + name (state name) + country_id (foreign key referencing countries.id, be sure your MySQL engine is set to InnoDB if not this will not work) So to see what country has what state or vice-versa If someone selects a country ID of 45, your DB structure will look like: [countries] id name ---- | --------- 45 Nigeria [states] id | name | country_id --- ------ ----------- 23 Lagos 45 And your query is: "SELECT name FROM states WHERE country_id=".$_REQUEST['country_value_selected'] OR if you want to see what country Lagos belongs to, now comes the fancy query : "SELECT c.name, s.name FROM states AS s INNER JOIN countries AS c ON c.id = s.country_id WHERE s.country_id=".$_REQUEST['country_value_selected'] (notice how I used aliasing now, because I'm referencing two different tables vs when i'm referencing the same table) So it's up to you, just keep your data as rational as possible to get the query you want (btw, i didn't test any of these, just from the top of head, so test carefully if you're using this in production). |
Re: Need Help On This Php Code by dmark4real: 7:25am On Jun 29, 2012 |
d states are 36 sum country 20 states u made reference to lagos alone. I hav al 36 states in state table and so on for other countries. Pls see to this |
Re: Need Help On This Php Code by delomos(m): 12:50am On Jun 30, 2012 |
I'm using that as an example. Just use that as an example for multiple states, same principle applies |
Re: Need Help On This Php Code by jesro0908: 9:11am On Jul 03, 2012 |
so you mean STATE will be a field in the COUNTRIES table, like that? [img]http://www.dubaa.info/hardware.gif[/img] |
Re: Need Help On This Php Code by delomos(m): 2:04pm On Jul 03, 2012 |
Read my above analysis carefully. |
Re: Need Help On This Php Code by Lisa1: 3:30am On Jul 05, 2012 |
Thanks a lot |
(1) (Reply)
I don't have a story. What is your story? / Android App I Made Need Your Comments / Where Do I Get Practice Questions On Python?
(Go Up)
Sections: politics (1) business autos (1) jobs (1) career education (1) romance computers phones travel sports fashion health religion celebs tv-movies music-radio literature webmasters programming techmarket Links: (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Nairaland - Copyright © 2005 - 2024 Oluwaseun Osewa. All rights reserved. See How To Advertise. 24 |