Jump to content

Recommended Posts

I have a database thats set up like this:

 

 

citiesSmall:

 

PK | City name

--------------

1  | Butler

2  | Cranberry

 

 

hotels:

 

PK  |  hotel name

----------------

1  | holiday inn

2  | hampton inn

 

 

hotelsToCity:

 

city | hotel

------------------

1  |  1

1  |  2

 

 

Now, i want to use PHP to grab all the hotels that are in city 1, and display the name of those hotels from the hotel table.  Here is what I have, and is not working:

 

        
<?php
   //Make a connection to the server... connect("servername","username","password")
   $link=mysql_connect("localhost","root") or die("No server connection".mysql_error());
   //connect to our database
   $db=mysql_select_db("dineonline_db") or die("No Database Connection ".mysql_error());
   //construct a SQL query that shows all records from products table
   $query="SELECT * FROM hotelsToCity WHERE city_ID = 1";
   //run the query
   $result=mysql_query($query);
   //for each row returned by our query, do what is in curly braces
   while($row = mysql_fetch_array($result)){
   
   //store into a variable the ID # for the current row 
   $ID=$row['hotel_ID'];
   //store into a variable the Name for the current row
   $name=$row['city_ID'];
   
   $hotelFK = array();
   array_push($hotelFK,$ID);
   
    echo "$hotelFK[0] , hotelFK[1]";  // I get 1, 2
   }
   
   //run a second query that uses the hotel table to retrieve the hotel name that is being asked for from the custom variable
   for($i=0; $i< count($hotelFK); $i++){
   $query = "SELECT * FROM hotels WHERE hotel_ID = $hotelFK[$i]";
   }
   
   $result = mysql_query($query);
   //for each row returned by our query, do what is in curly braces
   while($row = mysql_fetch_array($result)){
   //Set up the rows from the table
   $IDHotel=$row['hotel_ID'];
   $nameHotel = $row['hotel_name'];
   
   //Print out the name from the hotel (from the hotel table)
   print $nameHotel;
   
   }   
   

 

 

How can i do this to achieve this on screen:

 

"Holiday inn and hampton inn"

Link to comment
https://forums.phpfreaks.com/topic/138616-array-problem-ignore-other-post/
Share on other sites

"and is not working"

 

Very vague. How is it not working?

 

   for($i=0; $i< count($hotelFK); $i++){
       $query = "SELECT * FROM hotels WHERE hotel_ID = $hotelFK[$i]";
   }

 

This makes no sense. You overwrite the query each time so it will only pull the last query.

 

Try replacing it with:

 

   $hotelIds = implode(", ", $hotelFK);
   $query = "SELECT * FROM hotels WHERE hotel_ID IN(" . $hotelIds . ")";

 

Should yield the proper results.

ok so i tried it out and it works! Thank you! Now the next problem I can't figure out is why my values are not being put into an array when i try to use the array push. Here is the small snippet of code:

 

<?php
...

$query="SELECT * FROM hotelsToCity WHERE city_ID = 1";
//run the query
$result=mysql_query($query);
//for each row returned by our query, do what is in curly braces
while($row = mysql_fetch_array($result)){

//store into a variable the ID # for the current row 
$ID=$row['hotel_ID'];
//store into a variable the Name for the current row
$name=$row['city_ID'];

//Create an empty array to store the hotels
$myArray = array();

	//add values into the array
	for($i=0;$i<3;$i++)
	{
		array_push($myArray,$ID);
	}
}

?>

 

 

as you see, i tried running a for loop to put them in and they don't go in. The whole things works if i add values into the array when i define it. But now it wont add values into the array.. any ideas?

Btw.. the values that are being brought in are 1, 2, and 7. So thats three values I wish to put into the array.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.