I would like to display the information in this way i.e:
-----------------------------------------
First name | Smith
------------------------------------
last Nmae | Alan
------------------------------------
Arrival date| 28/02/2009
------------------------------------
and so on ......
Any suggestion as I am new to PHP. I have attached the image to show how the information is displayed.
Regards
HB25
<?php
//do your query
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$last_insert_booking_id."'");
//because your query uses a primary key, we only have one result and so don't have to use the while function. instead, save all the query results into an array.
$result_array = mysql_fetch_array($result);
//for each piece of information saved in the array, display it to the user.
foreach($result_array as $key => $value)
{
if( !is_int($key) )
{
echo $key.' | '.$value.'';
}
}
?>
That will write all the results nicely to the page with titles for the user. You can organise it nicely into tables by changing the code echo'd in the foreach section if you want.
yeah sure. assuming you understand arrays, the foreach function cycles through each array segment and runs a specific piece of code for that segment. The segment's title is saved in the $key variable, and it's contents is saved in the $value variable.
So from your mysql query, the $result_array would be filled with each columns value. The $key will be the column name, and the value of it will be the value of that column in the query.
So doing this:
echo $result_array['bookingID'];
would write that user's booking id.
doing this:
echo $result_array['first_name'];
would write that users first name (providing that information was saved in the database of course...)
Hi
This code work but not solve the program completely as i want to display the information in table, this is what I get:
booking has been changedbookingID | 33
roomID | 0
clientID | 33
startdate | 2009-03-01
enddate | 2009-03-02
adults | 1
children | 0
roomtype | Double
requirements | PC
2009-03-012009-03-0210DoublePC
there is two problem:
1. It does not display the infor mation in table
2. It does bring back everything from the table
What do you mean by:
2) It does bring back everything from the table
As for bringing it back in a table, use this:
<?php
//do your query
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$last_insert_booking_id."'");
//because your query uses a primary key, we only have one result and so don't have to use the while function. instead, save all the query results into an array.
$result_array = mysql_fetch_array($result);
//setup your table
echo '<table>';
//for each piece of information saved in the array, display it to the user.
foreach($result_array as $key => $value)
{
if( !is_int($key) )
{
echo '<tr><td>';
echo $key;
echo '</td><td>';
echo $value;
echo '</td></tr>';
}
}
//cloes your table
echo '</table>';
You almost solved my thread, what I meant by (It does bring back everything from the table ) is it will bring back the information from the first tow row as well in my table which I don’t want them to be retrieved and showed to the client. i only want the last 6 row to show.
BTW is it possible for my to write the row titles rather than retrieving them from the table.
Thanks for your help and as I am new PHP and Mysql.
Regards
HB25
<?php
//do your query
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$last_insert_booking_id."'");
//because your query uses a primary key, we only have one result and so don't have to use the while function. instead, save all the query results into an array.
$result_array = mysql_fetch_array($result);
echo '<table>';
//for each piece of information saved in the array, display it to the user.
foreach($result_array as $key => $value)
{
echo '<tr><td>Start Date</td><td>'.$result_array['startdate'].'</td></tr>';
echo '<tr><td>End Date</td><td>'.$result_array['enddate'].'</td></tr>';
echo '<tr><td>Adults</td><td>'.$result_array['adults'].'</td></tr>';
echo '<tr><td>Children</td><td>'.$result_array['children'].'</td></tr>';
echo '<tr><td>Roomtype</td><td>'.$result_array['roomtype'].'</td></tr>';
echo '<tr><td>Requirements</td><td>'.$result_array['requirements'].'</td></tr>';
}
echo '</table>';
?>
what do you mean:
BTW is it possible for my to write the row titles rather than retrieving them from the table.
Hi
Thanks for sorting out my thread we almost there, but there is a very small problem which is after retrieving the information it will repeat the table more than 9 times, I was wondering how this could be fixed.
For your information I have attached image with this thread for you to see what is happening.
the only possible reason for this, is that in your database, you are searching for all records where the bookingID is 33. You must have 9 different records that fit this description.
You have 2 options here:
- Add "LIMIT 1" to the end of your query, however this will limit the result to one row, but might get the wrong one...
- Change the bookingID column in your table to an auto increment primary key ( http://grafax.co.uk/OTHER/max/daniweb/primarykey_AI-autoincrement.bmp )
It would also help if you sent me your whole code now too, and mayeb a screenshot of your database setup.
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$_POST['bookingID']."'");
sometimes when you don't encapsulate the second part of the where condition in speech marks it treats it like a search term when your saying "select * from bookings where bookingID like '%term%'
is saying that for every piece of information saved inside the results array, write a table with the results. Take that out of the foreach function and have it on its own. Here how the whole code should look:
<?php
// open database connection code and then my code as follows
$sql="UPDATE bookings SET startdate ='$_POST[startdate]',enddate='$_POST[enddate]',adults='$_POST[adults]',children='$_POST[children]',roomtype='$_POST[roomtype]', requirements='$_POST[requirements]'
WHERE bookingID = '$_POST[bookingID]'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
print "Your booking ID ".$bookingID;
echo " has been changed";
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$_POST['bookingID']."'");
$result_array = mysql_fetch_array($result);
echo '<table border=1>';
echo '<tr><td>Start Date</td><td>'.$result_array['startdate'].'</td></tr>';
echo '<tr><td>End Date</td><td>'.$result_array['enddate'].'</td></tr>';
echo '<tr><td>Adults</td><td>'.$result_array['adults'].'</td></tr>';
echo '<tr><td>Children</td><td>'.$result_array['children'].'</td></tr>';
echo '<tr><td>Roomtype</td><td>'.$result_array['roomtype'].'</td></tr>';
echo '<tr><td>Requirements</td><td>'.$result_array['requirements'].'</td></tr>';
echo '</table>';
mysql_close($con);
?>
The query returned 9 pieces of information and stored them inside an array. Then the foreach function was going through all nine items in the array and printing your table of results
Hi MaxMumford
This did work; I just wanted to say a big thank you for solving my thread
My last problem is at the moment user will have to type date in this format (YYYY-MM-DD) as my phpMyadmin will only store date in this format any idea how I could make it to store date in this format (DD-MM-YYYY)
Once again thanks ever so much for your help.
Regards
HB25
<?php
// open database connection code and then my code as follows
$sql="UPDATE bookings SET startdate ='$_POST[startdate]',enddate='$_POST[enddate]',adults='$_POST[adults]',children='$_POST[children]',roomtype='$_POST[roomtype]', requirements='$_POST[requirements]'
WHERE bookingID = '$_POST[bookingID]'";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
print "Your booking ID ".$bookingID;
echo " has been changed";
$result = mysql_query("SELECT * FROM bookings WHERE bookingID='".$_POST['bookingID']."'");
if(mysql_num_rows($result) > 0 ) {
//only print this if there is atleast 1 row. This will avoid a lot of problems which could arise from mysql_fetch_array if there are no rows in the table
//I would also declare the table headers when I am sure at least 1 record exist. If there aren't any records, it wont print the html table
echo '<table border=1>
<tr><td>Start Date</td><td>End Date</td><td>Adults</td><td>Children</td><td>Roomtype</td><td>Requirements</td></tr>';
$result_array = mysql_fetch_array($result);
echo '<td>'.$result_array['startdate'].'</td>';
echo '<td>'.$result_array['enddate'].'</td>';
echo '<td>'.$result_array['adults'].'</td>';
echo '<td>'.$result_array['children'].'</td>';
echo '<td>'.$result_array['roomtype'].'</td>';
echo '<td>'.$result_array['requirements'].'</td>';
echo '</tr>';
echo '</table>'; // this way it looks more neat
}
mysql_close($con);
?>
Also, mysql stores date in the default format yyyy-mm-dd . If you want to change this, you have to specify the column datatype as varchar. I don't recommend it since you can do so many date functions if the column is of date datatype.
Also, php's date function is so vast, you can modify any of your date format to whatever format you want.
Check here. http://in.php.net/date