0

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.

4
  • 1
    looks to me like $size is never initialized. first appearance is $size = (int)substr($size,6,2); the variable is treated as empty string as the function argument, then 0 after the cast Commented Dec 8, 2013 at 19:48
  • it is further up $size = mysql_real_ecape($_POST['size']; Commented Dec 8, 2013 at 19:49
  • ok. I don't 100% follow the intent, but I think you'd likely get more help, and make things clearer for yourself, if you inserted a few print_rs or var_dumps (after the explodes would be a good starT) Commented Dec 8, 2013 at 19:53
  • I have actually found it now I missed a < in my html form, so the data wasn't sending or something Commented Dec 8, 2013 at 19:56

3 Answers 3

2

As I understand size should be in $current_size[0], not $current_size[1] like in your code.

Try this:

$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[0].'.';
    if($size === (int)$current_size[0]){
        $stock = $current_size[1];
        echo 'found it';
        $x = count($sizes);
    }else{
        $x++;
    }
    echo $size.'='.$current_size[0].'    ';
}
echo $stock;
Sign up to request clarification or add additional context in comments.

2 Comments

wow that was a stupid mistake, I mentioned earlier in a comment that I thought I solved it. Nope this did the trick. I was comparing the size with the quantity. Unfortunately for me I hadn't noticed.
sweet, I am glad I could help
1

Your problem is likely to be the strict comparison operator you're using in your if statement:

if($size === (int)$current_size[1]){

You've said in your comment:

$size = mysql_real_ecape($_POST['size']); // assumed this is a typo in your comment not code

Chances are, $size will be coming out of $_POST and mysql_real_escape_string (deprecated, use mysqli) as a string, and you're comparing it to an integer.

... strict comparison operators compare variable type as well as contents, so you're saying this in pseudo:

if( [string - size] is equal to [int - current size] )

So try this:

if((int)$size === (int)$current_size[1]){ // cast $size as integer also

...or

if($size == $current_size[1]){ // type juggle while comparing

Comments

1

you have the three = operator in your if statement

if($size === (int)$current_size[1]){

i don't know if using only two (==) instead would change anything. if not you could try to add the cast to (int) also for the $size variable

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.