0

I m trying to get it work in php but i cant, since i m new in PHP :( i was wondering if someone can help and i would really appreciate it!! :*

Description:
In a php page i want to choose from a drop down menu (Houses) a choise for example a house and after that on a second drop down menu below to show me automatically the rooms of the house i choosed on the menu above..

I created 2 tables on a database houses_db in mysql
i. one table called houses with one column with house1, house2,...etc
ii. and on second table called rooms with first column called house1 and the rooms in it, second column house2 and the rooms in it and so on..

so on the php page when i select from the first menu a house on the second menu to show me the rooms that it has

i hope that was clear enough, i m sure this can be done with a mysql query but i dont kno how i write something but it doesnt work i m sure is wrong!!!!!i m confused on the use of the variables..

<?php
$db = mysql_connect("localhost", "root", "password");

mysql_select_db("houses_db",$db);

?>
               <select name=rooms>
                 <?php

$query = "SELECT houses, rooms from rooms";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($houses, $rooms) = mysql_fetch_row($result))
{
echo "<option value=$houses>$rooms</option>"; 
}
mysql_free_result($result);
?>

PLEASE HELP i m so confused!!!!:'(

Edited by mike_2000_17: Fixed formatting

4
Contributors
10
Replies
11
Views
7 Years
Discussion Span
Last Post by vaibhav1983
0

I m trying to get it work in php but i cant, since i m new in PHP :( i was wondering if someone can help and i would really appreciate it!! :*

Description:
In a php page i want to choose from a drop down menu (Houses) a choise for example a house and after that on a second drop down menu below to show me automatically the rooms of the house i choosed on the menu above..

I created 2 tables on a database houses_db in mysql
i. one table called houses with one column with house1, house2,...etc
ii. and on second table called rooms with first column called house1 and the rooms in it, second column house2 and the rooms in it and so on..

so on the php page when i select from the first menu a house on the second menu to show me the rooms that it has

i hope that was clear enough,,,i m sure this can be done with a mysql query but i dont kno how i write something but it doesnt work i m sure is wrong!!!!!i m confused on the use of the variables..

<?php
$db = mysql_connect("localhost", "root", "password");

mysql_select_db("houses_db",$db);

?>
               <select name=rooms>
                 <?php

$query = "SELECT houses, rooms from rooms";
$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " . mysql_error());
while (list($houses, $rooms) = mysql_fetch_row($result))
{
echo "<option value=$houses>$rooms</option>"; 
}
mysql_free_result($result);
?>

PLEASE HELP i m so confused!!!!:'(

what error messages are you getting? I've added comments to your code where I think the errors may be.

<?php
$db = mysql_connect("localhost", "root", "password");

mysql_select_db("houses_db",$db);

?>
<select name=rooms>
                 <?php
//make sure the query is correct. double check in mysql by running the query
$query = "SELECT houses, rooms from rooms";

//you don't need the $connection
//$result = mysql_db_query($db, $query, $connection) or die ("Error in query: $query. " //. mysql_error());

$result = mysql_db_query($db, $query) or die ("Error in query: $query. " . mysql_error());

//try this instead 
while($row = mysql_fetch_array($result, MYSQL_ASSOC)){
    echo "<option value=$houses>$row['rooms']</option>"; 
}

/*
while (list($houses, $rooms) = mysql_fetch_row($result))
{
echo "<option value=$houses>$rooms</option>"; 
}*/

mysql_free_result($result);
?>

PS please wrap all codes with the CODE tags

0

it seems that i m having a problem in line
echo "<option value=$houses>$row</option>";
cause i me getting this error
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

but even so,, thank u so much ,,appreciated!!

0

yea thats it ,,,

but

still dont working the second box is still emplty when i select something from the first..

in case of misunderstanding i m not using post method here with a press button,,
still working it..:(

0

so if I understand correctly, when you choose a house from the first drop down menu, you want the second drop down menu to populate with the rooms it has?

If you want to automatically populate the second menu (rooms) without "submitting", you'll have to use javascript along with AJAX.

0

it sounds difficult any ideas??

i ll give in another way a try with different php pages were in one page there will be the choise of the houses and then with submit button on a second page using post i will check the value of the variable houses and acccording to that i will give and the corresponding page with the according drop down menu of the rooms..more complecated but it will work..

but any ideas for the javascript and the Ajax (the last was playing yesterday with Real i think.. :) and lost 2-0)

0

so if I understand correctly, when you choose a house from the first drop down menu, you want the second drop down menu to populate with the rooms it has?

If you want to automatically populate the second menu (rooms) without "submitting", you'll have to use javascript along with AJAX.

Mostly true. Ajax would only be required if the information requested might change between requests, if it doesn't then there is no point in going as far as Ajax.

Simply have the php page put the information of the rooms into javascript arrays that the script can then use the automatically populate the lists. Although to start out with you should probably master the GET/POST aspects of form control within php first but that's just my opinion.

0

AJAX is pretty complicated in itself, you could download Jquery at http://jquery.com to make it more simpler. The simplest way I can think of, is setting the <FORM action="second_page.php">. That way, you can carry the values over to another page.

if you were to use AJAX in jquery, you'd probably do something like this.

var house_name = $("#house_drop_down option:selected").text();

//assuming the dropdown ID is house_drop_down
$("#house_drop_down").change(function(){
       $.ajax({
             type:"post",
             url:"second_page.php",//assuming this is the page you are to run the query for rooms
             data:"house="+house_name;
             success: function(room){
                  //assuming that second_page.php returns an ARRAY
                  $.each(room, function(value, text){
                         //assuming your dropdown id for room is room_drop_down
                         $("#room_drop_down").append($('<option></option>').val(val).html(text));
                  });
             }

       });

});

the code above is just something rough and has never been tested so it probably doesn't work but the logic is there. hope this helps

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.