Page 1 of 1
Compressing (or imploding) sql select result (newbie q)
Posted: Thu Dec 14, 2006 8:42 am
by tomharding
Hi all,
I'm doing an e-commerce uni assignment and I need to be able to transfer data from one table to another, but I need to be able to compress the results of the first table to put them into one cell in the other (that probably doesnt make much sense!).
Basically the structure is:
Table 1 (booking)
-------
Seat 1
Seat 2
Seat 3
Table 2 (order)
-----
Seat 1,Seat 2,Seat 3
I tried looking at the implode function, but I kept getting bad arguments from it..
Can anyone help much?
Thanks,
Tom
Posted: Thu Dec 14, 2006 8:44 am
by John Cartwright
Please post what you've done so far, and you are correct about implode() function.
Posted: Thu Dec 14, 2006 8:54 am
by tomharding
Thanks for the quick reply!
Just to clarify, my coding for this assignment is super below par. Theres not much time to actually backtrack and change the system.
Heres the code:
Code: Select all
$seatquery = "SELECT seat_id FROM cart WHERE sessid = '$session'";
//$seats = implode(",", $qu);
$seatresult = mysql_query($seatquery) or die(mysql_error());
while($row = mysql_fetch_array($seatresult)){
echo $row['seat_id'];
}
$seats = implode(",", $seatresult);
Posted: Thu Dec 14, 2006 8:57 am
by John Cartwright
Code: Select all
$seatquery = "SELECT seat_id FROM cart WHERE sessid = '$session'";
$seatresult = mysql_query($seatquery) or die(mysql_error());
$seats = array();
while($row = mysql_fetch_array($seatresult))
{
$seats[] = $row['seat_id'];
}
echo implode(", ", $seats);
You need to build an array and pass the
array to implode.
Posted: Thu Dec 14, 2006 9:01 am
by tomharding
THANKYOU!
You are some kind of buddah. I'm glad I found this forum now, seems less pretentious than places like devshed.
Posted: Sat Dec 16, 2006 11:51 am
by tomharding
Sorry to be a pest again, but how would i do the opposite of this using an explode function.
I've changed the booking system so it inserts a seat id and a time into a cell (for instance: OC001,12:34) and after a while, the cart table builds up like:
ID SEAT
----------
1 0C001,12:34
2 0C002,12:34
3 0C003,12:34
4 0C001,12:50
5 0C002,12:50
.. I want to be able to move the seat column into a seperate 'booked' table so it would appear as
ID SEAT TIME
----------------
1 0C001 12:34
2 0C002 12:34
3 0C003 12:34
4 0C001 12:50
5 0C002 12:50
I'm trying it out with the code (merged with the issue i was talking about in the other posts:
Code: Select all
//Get the list of seats this customer has booked..
$seatquery = "SELECT seat_id FROM cart1 WHERE customer_id = '$userid'";
$seatresult = mysql_query($seatquery) or die(mysql_error());
$seats = array();
while($row = mysql_fetch_array($seatresult))
{
$seatstime = explode(",", $row['seat_id']);
echo $seatstime[0];
echo $seatstime[1];
echo '<br>';
$seats[] = $row['seat_id'];
}
echo "ddf<p>";
echo $seatstime[0];
I assume using the explode(); function inside the fetch array is the first right step?
Thanks again,
Tom
Posted: Sat Dec 16, 2006 1:40 pm
by Kieran Huggins
you seem to have everything in order.. does this code fail you in some way?
Cheers,
Kieran
Posted: Sat Dec 16, 2006 1:45 pm
by tomharding
Kieran Huggins wrote:you seem to have everything in order.. does this code fail you in some way?
Cheers,
Kieran
It seemed that way. I decided not to go down that route anymore anyway. Basically, I was compressing 2 variables into 1 cell when i ordered something, only to explode it again later.
Figured i'd kick out the middle man
Thanks, anyway haha.