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
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Thu Oct 20, 2005 5:09 pm
Hey everyone
I'm new to the forum. I have tried everything that I know to solve this problem.
I'm making a clothing catalog. The problem I'm having is when I serialize an array of shirt sizes and quantities, I can't get a clean look at it. There's got to be something wrong with the following code or my logic.
Code: Select all
//this is what the serialized array looks like when echoed
//a:7:{s:2:"xs";i:30;s:2:"sm";i:31;s:2:"md";i:32;s:2:"lg";i:33;s:2:"xl";i:32;s:3:"xxl";i:31;s:4:"xxxl";i:30;}
$result = mssql_query("SELECT array FROM arrays WHERE id='8'");
$serialarray = mssql_fetch_array($result);
echo "ATTEMPT TO TRAVERSE THE SERIALIZED ARRAY<br>";
$a = $serialarray;
$a = unserialize($a);
// Key = shirt size, Value = quantity
while (list($key, $value) = each($a))
{
echo "Key: ". $key ." > Value: ".$value."<br>";
}
// should print out this:
//Key: xs > Value: 30
//Key: sm > Value: 31
//Key: md > Value: 32
//Key: lg > Value: 33
//Key: xl > Value: 32
//Key: xxl > Value: 31
//Key: xxxl > Value: 30
Last edited by
dallasx on Thu Nov 10, 2005 3:25 pm, edited 1 time in total.
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 2:10 pm
After looking at the above code, i think when the array is pulled from the db, the craziness begins there. The values before the array was stored in the database are set. I insert it into the database then pull it out and array values are lost.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Oct 21, 2005 3:21 pm
var_dump($serialarray) .. see what comes up..
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 3:33 pm
feyd wrote: var_dump($serialarray) .. see what comes up..
Ok, I'll try that right now.
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 3:51 pm
feyd wrote: var_dump($serialarray) .. see what comes up..
First... ignore the code above. I started a new file.
Ok... before I serialize it and insert it into the database, I get this:
Code: Select all
$mens_sizes["Small"] = 10;
$mens_sizes["Medium"] = 15;
$mens_sizes["Large"] = 20;
var_dump($mens_sizes);
OUTPUT:
array(3) { ["Small"]=> int(10) ["Medium"]=> int(15) ["Large"]=> int(20) }
Then, after I retrieve the serialized array from the db
OUTPUT:
array(1) { ["array"]=> string(58) "a:3:{s:5:"Small";i:10;s:6:"Medium";i:15;s:5:"Large";i:20;}" }
when I unserialize it...
Code: Select all
$dbarray = unserialize($dbarray);
var_dump($dbarray);
OUTPUT:
bool(false)
Last edited by
dallasx on Fri Oct 21, 2005 3:59 pm, edited 1 time in total.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Oct 21, 2005 3:57 pm
I'm going to guess you don't understand what the problem is after that illustration?
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 4:00 pm
feyd wrote: I'm going to guess you don't understand what the problem is after that illustration?
Heh... I have no clue.
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Oct 21, 2005 4:02 pm
mssql_fetch_array() returns an array of all the fields you select, whether one or two hundred.
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 4:10 pm
feyd wrote: mssql_fetch_array() returns an array of all the fields you select, whether one or two hundred.
Nice... it obviously worked...
The way I see it is that in the fetched array, the array I want is actually stored as a key and the data I want is stored as a string value?
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Fri Oct 21, 2005 9:23 pm
yep.
dallasx
Forum Contributor
Posts: 106 Joined: Thu Oct 20, 2005 4:55 pm
Location: California
Post
by dallasx » Fri Oct 21, 2005 9:25 pm
Well I'll be a monkey's uncle...