Compressing (or imploding) sql select result (newbie q)
Moderator: General Moderators
-
tomharding
- Forum Newbie
- Posts: 5
- Joined: Thu Dec 14, 2006 8:35 am
Compressing (or imploding) sql select result (newbie q)
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
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
- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
-
tomharding
- Forum Newbie
- Posts: 5
- Joined: Thu Dec 14, 2006 8:35 am
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:
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);- John Cartwright
- Site Admin
- Posts: 11470
- Joined: Tue Dec 23, 2003 2:10 am
- Location: Toronto
- Contact:
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);-
tomharding
- Forum Newbie
- Posts: 5
- Joined: Thu Dec 14, 2006 8:35 am
-
tomharding
- Forum Newbie
- Posts: 5
- Joined: Thu Dec 14, 2006 8:35 am
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:
I assume using the explode(); function inside the fetch array is the first right step?
Thanks again,
Tom
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];Thanks again,
Tom
- Kieran Huggins
- DevNet Master
- Posts: 3635
- Joined: Wed Dec 06, 2006 4:14 pm
- Location: Toronto, Canada
- Contact:
-
tomharding
- Forum Newbie
- Posts: 5
- Joined: Thu Dec 14, 2006 8:35 am
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.Kieran Huggins wrote:you seem to have everything in order.. does this code fail you in some way?
Cheers,
Kieran
Figured i'd kick out the middle man
Thanks, anyway haha.