![]() |
Selecting from a position in an array...
Hi!
I have a mysql database with 4 rows containing arrays, including an unique ID field. I want to run a SELECT query to pull out the data from each array based on its position. E.g. I may want to select from the 4 arrays in my row the 3rd results: | ID | Name | Description | Price | 3 Car Red $100 My code so far looks like this: ($furnaccept is POST'd from the previous page) PHP Code:
Am I on the right lines? Help! :D |
Quote:
0,1,2 ... but my furn_ID row will look something like this: a:3:{i:0;s:1:"0";i:1;s:1:"2";i:2;s:1:"3";} Guess it's a case of converting the furn_ID data first?? |
you should go to the database only once not for every iteration of a loop...
write all your id's into a comma separated string PHP Code:
get all the records PHP Code:
|
Hi, thanks for the reply. Using this returns this:
PHP Code:
a:2:{i:0;s:17:"Brown with 4 legs";i:1;s:14:"Brown circular";} a:2:{i:0;s:1:"4";i:1;s:1:"1";} a:2:{i:0;s:3:"200";i:1;s:3:"400";} PHP Code:
a:2:{i:0;s:17:"Brown with 4 legs";i:1;s:14:"Brown circular";} a:2:{i:0;s:1:"4";i:1;s:1:"1";} a:2:{i:0;s:3:"200";i:1;s:3:"400";} What I want to show is: Chair Brown with 4 legs 4 200 Table Brown circular 1 400 So basically I want the SELECT command to look up an array's id then go to the database and pull out the value for that array position... |
is that the serialized data stored in the database?
you should be able to loop through the results and unserialize it |
Yes the data is serialized. The entire code currently is:
PHP Code:
|
prob easiest to get all the records and unserialize them and search for whatever record you are looking for
|
As a test I tried the following code:
PHP Code:
SHOW ME ROW 0 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Array a a a:2:{i:0;s:3:"200";i:1;s:3:"400";} SHOW ME ROW 1 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Array : : a:2:{i:0;s:3:"200";i:1;s:3:"400";} |
Had a thought... essentially I am trying to find the position of a value in an array. If i have the position (i.e. 0,1 or 0,3,4 etc) is there a way of saying
SELECT x from table where myID = $myID THEN... get position 0-> then position 1-> or get position 0-> then position 3-> then position 4-> Help! ? |
How do I got from getting data like this:
a:2:{i:0;s:5:"Chair";i:1;s:5:"Table";} into an array like this: [0]-> Chair [1]-> Table ? |
PHP Code:
|
Almost there! :) My entire PHP code:
PHP Code:
SHOW ME ROW 0 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Array ( [0] => Chair [1] => Table ) Array ( [0] => Brown with 4 legs [1] => Brown circular ) Array ( [0] => 4 [1] => 1 ) Array ( [0] => 200 [1] => 400 ) SHOW ME ROW 1 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Array ( [0] => Chair [1] => Table ) Array ( [0] => Brown with 4 legs [1] => Brown circular ) Array ( [0] => 4 [1] => 1 ) Array ( [0] => 200 [1] => 400 ) *** What I want... SHOW ME ROW 0 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Chair Brown with 4 legs 4 200 SHOW ME ROW 1 OF THE ARRAY: SELECT furn_item, furn_desc, furn_qty, furn_cost FROM dbQuotes WHERE id='2' and furn_ID IN (0,1) Table Brown circular 1 400 |
do you know how to loop through an array and echo the results?
|
why are you storing this information in a serialized format, it doesn't make any sense to me... just put it i there the way you want it to come out
|
Quote:
|
| All times are GMT +1. The time now is 05:03 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.