Compressing (or imploding) sql select result (newbie q)

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tomharding
Forum Newbie
Posts: 5
Joined: Thu Dec 14, 2006 8:35 am

Compressing (or imploding) sql select result (newbie q)

Post 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
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Please post what you've done so far, and you are correct about implode() function.
tomharding
Forum Newbie
Posts: 5
Joined: Thu Dec 14, 2006 8:35 am

Post 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);
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post 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.
tomharding
Forum Newbie
Posts: 5
Joined: Thu Dec 14, 2006 8:35 am

Post by tomharding »

THANKYOU!

You are some kind of buddah. I'm glad I found this forum now, seems less pretentious than places like devshed.
tomharding
Forum Newbie
Posts: 5
Joined: Thu Dec 14, 2006 8:35 am

Post 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
User avatar
Kieran Huggins
DevNet Master
Posts: 3635
Joined: Wed Dec 06, 2006 4:14 pm
Location: Toronto, Canada
Contact:

Post by Kieran Huggins »

you seem to have everything in order.. does this code fail you in some way?

Cheers,
Kieran
tomharding
Forum Newbie
Posts: 5
Joined: Thu Dec 14, 2006 8:35 am

Post 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 :D

Thanks, anyway haha.
Post Reply