I have a for loop that checks numbers stored in a database. The data is stored as a string and then split in php. The code below should only return the amount of stock for that that size. It looks as though it should work. The data is stored in the database like this:
size,quantity_*_size,quantity: and so on
I start by splitting the array:
$result = mysql_fetch_assoc($get);
$sizes = explode('_*_',$result['sizes']);
Pretty straight foreward, this is then lopped through and split
for($x = 0; $x < count($sizes); $x){
$current_size = explode(',',$sizes[$x]);
if($size === (int)$current_size[1]){
$stock = $current_size[1];
//do something with matched data
$x = count($sizes);
}else{
$x++;
}
}
But it doesn't ever match the data. I've been at this for hours now and I've got nothing. This is the whole thing:
$result = mysql_fetch_assoc($get);
$sizes = explode('_*_',$result['sizes']);
//check stock of each size
$size = (int)substr($size,6,2); //origal string from POST Size :x
$stock = 0;
for($x = 0; $x < count($sizes); $x){
$current_size = explode(',',$sizes[$x]);
echo $size.'='.$current_size[1].'.';
if($size === (int)$current_size[1]){
$stock = $current_size[1];
echo 'found it';
$x = count($sizes);
}else{
$x++;
}
echo $size.'='.$current_size[1].' ';
}
echo $stock;
If it helps this is the data from the database as its stored: 4,10_*6,14*_8,0_*10,3*_12,0_*_14,2
data is formated to match the explode function but its changed in here.
$sizeis never initialized. first appearance is$size = (int)substr($size,6,2);the variable is treated as empty string as the function argument, then0after the castprint_rs orvar_dumps (after the explodes would be a good starT)