Jump to content

[SOLVED] Searching a Query...


slawrence10

Recommended Posts

queries have been produced in Dreamweaver for my system as Recordsets

I'm creating a room booking system and once the user has selected Week, Day and Time a query searches the booking table for any bookings which match all three.

 

The next page displays a list of all rooms using the do function

 

do{
//the stuff i want it to do
} while ($row_rooms = mysql_fetch_assoc($rooms));

 

I want it to check the query for each room, to see if there has been a booking...meaning I can allow booking or display "Booked"

 

if($room==$row_timetabled['room_id']){

 

I tried using the above if statement however it only seems to check $room against the first row / record of the query...could anyone tell me how i need to tackle this?

 

Regards and thanks for your time,

Scott

Link to comment
Share on other sites

do {
$room=$row_rooms['id'];
if($room==$row_booked['room_id']){
print  "<td width=\"50\">".$row_rooms['name']."</td>";
print "<td width=\"200\" colspan=\"2\"><span class=\"SubtitleRed\">Timetabled Lesson</span></td></tr>";
}else{
print "<td width=\"50\"><form name=\"".$room."\" method=\"post\" action=\"book3.php\">";
print  $row_rooms['name']."</td>";
print "<td width=\"100\"><span class=\"SubtitleGreen\">Available</span></td>";
print "<td width=\"100\"><input name=\"week\" type=\"hidden\" id=\"week\" value=\"".$_POST['week']."\">";
print "<input name=\"day\" type=\"hidden\" id=\"day\" value=\"".$_POST['day']."\">";
print "<input name=\"period\" type=\"hidden\" id=\"period\" value=\"".$_POST['period']."\">";
print "<input name=\"room\" type=\"hidden\" id=\"room\" value=\"".$room."\">";
print "<input type=\"submit\" name=\Book Room\" value=\"Book ".$row_rooms['name']."\"></td></tr></form>";
}
} while ($row_rooms = mysql_fetch_assoc($rooms));

 

Regards for your time,

Scott

Link to comment
Share on other sites

That's the query / Recordset I think...not entirely sure...I dunno if the following will help, the coding for the Recordset created by dreamweaver...

$colname_booked = "-1";
if (isset($_POST['week'])) {
  $colname_booked = (get_magic_quotes_gpc()) ? $_POST['week'] : addslashes($_POST['week']);
}
$colname2_booked = "-1";
if (isset($_POST['period'])) {
  $colname2_booked = (get_magic_quotes_gpc()) ? $_POST['period'] : addslashes($_POST['period']);
}
$colname3_booked = "-1";
if (isset($_POST['day'])) {
  $colname3_booked = (get_magic_quotes_gpc()) ? $_POST['day'] : addslashes($_POST['day']);
}
mysql_select_db($database_connect, $connect);
$query_booked = sprintf("SELECT * FROM booking WHERE week_id = %s AND period = %s AND day = %s ORDER BY id ASC", GetSQLValueString($colname_booked, "int"),GetSQLValueString($colname2_booked, "int"),GetSQLValueString($colname3_booked, "int"));
$booked = mysql_query($query_booked, $connect) or die(mysql_error());
$row_booked = mysql_fetch_assoc($booked);
$totalRows_booked = mysql_num_rows($booked);

Link to comment
Share on other sites

I've currently created 3 Booking of the 6 Bookings but it is only displaying the first room as booked, i think that the if statement only checks the first result of the query, what I'm really asking I guess is how to check all results of $row_booked['room_id'] for $room.

 

Regards for your time,

Scott

Link to comment
Share on other sites

Okay, I'm trying to figure this out, sorry if I'm being slow.

 

You have a table of rooms, and another of room bookings.

 

You need to get all the bookings for a certain week, and a list of all the rooms.

Then you need to tell which rooms are booked, so you can mark them as such when you print the records.

 

Do I have this right?

Link to comment
Share on other sites

Yeh basically the user chooses the Week, Day and Period first through a form...the recordet I set up searches through the Bookings table for any records which match all three...and yes I'm currently looping throught the rooms table to print them showing whether they are available or already booked.

Link to comment
Share on other sites

Here, try something like this:

<?
//Make a big array for all our bookings
$bookings = array();
$query_booked = "SELECT * FROM booking WHERE week_id = '$weekID' AND period = '$period' AND day = '$day' ORDER BY id ASC");
$booked = mysql_query($query_booked, $connect) or die(mysql_error());
while($row_booked = mysql_fetch_assoc($booked)){
//Get the bookings for this room
$roomID	= $row_booked['roomID'];
$roomBookings = $bookings[$roomID];
$roomBookings[]	= //fill in the date info, etc.
$bookings[$roomID] = $roomBookings; //Put it back in the big array.
}

print_r($bookings);
?>

 

Then, you'll have an array for each room based on ID, of all their bookings, so when you loop through the rooms to show them, you can just use that array which is in $bookings. I added a print_r at the end so you could see what it looks like.

 

Edit: Using a mysql JOIN here is probably best, but I don't know how to do them properly, so I can't help you there. This is how I'd do it.

Link to comment
Share on other sites

I've had a quick play with what you said I'm not that confident with it though...will see if i can work it...

 

 

doesn't the recordset $row_bookings create an array of the bookings already? I just need to work out how to search it for each room.

 

thanks for your time,

Scott.

 

 

Link to comment
Share on other sites

it's getting there  ;D thanks so much for your help so far much appreciated...

 

if(array_key_exists($room, $bookings))

 

this seems to work HOWEVER

the array seems to be missing the first record of the query....i'm testing it by having records of Room 1 ,2 and 6 being booked,

 

the array only prints 2 and 6

 

thanks for your time,

Scott

Link to comment
Share on other sites

 <?php
//Make a big array for all our bookings
$bookings = array();
while($row_booked = mysql_fetch_assoc($booked)){
//Get the bookings for this room
$roomID	= $row_booked['room_id'];
$bookings[$roomID] = $roomBookings; //Put it back in the big array.
}
print_r($bookings);
		  
		  print "<table cellpadding=0 cellspacing=0 border=0><tr>";
		  do {
		  $room=$row_rooms['id'];
		  if(array_key_exists($room, $bookings)){
		  print  "<td width=\"50\">".$row_rooms['name']."</td>";
		  print "<td width=\"200\" colspan=\"2\"><span class=\"SubtitleRed\">Booked</span></td></tr>";
		  }else{
		  print "<td width=\"50\"><form name=\"".$room."\" method=\"post\" action=\"book3.php\">";
		  print  $row_rooms['name']."</td>";
		  print "<td width=\"100\"><span class=\"SubtitleGreen\">Available</span></td>";
		  print "<td width=\"100\"><input name=\"week\" type=\"hidden\" id=\"week\" value=\"".$_POST['week']."\">";
		  print "<input name=\"day\" type=\"hidden\" id=\"day\" value=\"".$_POST['day']."\">";
		  print "<input name=\"period\" type=\"hidden\" id=\"period\" value=\"".$_POST['period']."\">";
		  print "<input name=\"room\" type=\"hidden\" id=\"room\" value=\"".$room."\">";
		  print "<input type=\"submit\" name=\Book Room\" value=\"Book ".$row_rooms['name']."\"></td></tr></form>";
		  }
		  } while ($row_rooms = mysql_fetch_assoc($rooms));
		  print "</table>";
		  ?>			  

 

 

here are a couple of screenshots maybe they will help..

http://www.dizine.co.uk/shot.gif

http://www.dizine.co.uk/shot2.gif

 

Thanks for your time,

Scott.

Link to comment
Share on other sites

You don't have mysql_fetch_assoc($booked) anywhere BEFORE

while($row_booked = mysql_fetch_assoc($booked)){

do you? Because that will take the first row, then the while moves it to the second before you can put the first in the array.

Link to comment
Share on other sites


mysql_select_db($database_connect, $connect);
$query_booked = sprintf("SELECT * FROM booking WHERE week_id = %s AND period = %s AND day = %s ORDER BY id ASC", GetSQLValueString($colname_booked, "int"),GetSQLValueString($colname2_booked, "int"),GetSQLValueString($colname3_booked, "int"));
$booked = mysql_query($query_booked, $connect) or die(mysql_error());
$row_booked = mysql_fetch_assoc($booked);
$totalRows_booked = mysql_num_rows($booked);

 

It's in the coding done by dreamweaver ^second from last line^, would it be safe to remove the statement $row_booked = mysql_fetch_assoc($booked) from there?

 

Regards and thanks for your time,

Scott.

 

Link to comment
Share on other sites

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.