zang8027 Posted December 28, 2008 Share Posted December 28, 2008 So i cant figure out why this is not working. Here is my code <?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']; //This sets my custom variable = to whatever hotel_ID is asked for. In this case, city 4 is asked for. Currently, the only hotel in city 4 is a hotel 8. Hotel 8's hotel ID is 8. So $ID = 8 because only $ID that has a value of 8 is being displayed. $hotelFK = array(); array_push($hotelFK,$ID); $arrayCount = count($hotelFK); print "$arrayCount <br/>"; } //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< $arrayCount; $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; } I am passing 2 values into an array. The values are "1" and "2". When i print out $hotelFK[0] and $hotelFK[1], i get "1" and "2". So I know my values are being placed in the array. Now, when i print out count($hotelFK), i SHOULD get two because there are two values in the array.. right? But i get the following : 1 1 Why is it doing this? I want to run the query as many times as I have values in the array. Is this not the best way to go with it too? my desired result is it to print out ( since my array is $hotelFK(1,2)): 1 2 (which actually references another table and would print out something different.. but for explanation sake, ill just say that) Link to comment https://forums.phpfreaks.com/topic/138612-solved-help-with-arrays/ Share on other sites More sharing options...
flyhoney Posted December 28, 2008 Share Posted December 28, 2008 I think your code got cut off. Try resposting all of it. Also, array_push() is more or less defunct. Use: <?php $hotelFK = array(); $hotelFK[] = 1; $hotelFK[] = 2; echo count($hotelFK); ?> Link to comment https://forums.phpfreaks.com/topic/138612-solved-help-with-arrays/#findComment-724753 Share on other sites More sharing options...
zang8027 Posted December 28, 2008 Author Share Posted December 28, 2008 But i am using a database to retrieve the values so the amount of values, and the value of each is going to change. I have been messing around and I still dont think that that will work, even if i get this to work. How can i print out each value in the array while using that query. my database tables are set up like this: table 1: PK | City name -------------- 1 | Butler 2 | Cranberry Table 2: PK | hotel name ---------------- 1 | holiday inn 2 | hampton inn table 3: city | hotel ------------------ 1 | 1 1 | 2 So with this creud drawing, u can see that city 1 (Butler) has 2 hotels. So the values are coming in to display 1 and 2. Thats whats entering my array. Now i want to display "holiday inn" and "hampton inn" on my document. Its hard to explain but is this the best way to do what i want? Link to comment https://forums.phpfreaks.com/topic/138612-solved-help-with-arrays/#findComment-724774 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.