开发者

for loop and if loop is refusing to print a varriable outside the loop?

i am facing a problem that after i created the jQuery post, i was able to receive all the data but as a one peace, so when i began rephrasing them i succeed until the final part which was the inserting into the database, where inside the for loop and if loop i am getting the value but when i wanted to start inserting them into the database i am getting null values, below is the for loop and if loop

if ($action == "insert")
{
    $fields = explode("&",$data);

    foreach($fields as $field)
    {
        $field_key_value = explode("=",$field);
        $key = urldecode($field_key_value[0]);
        $value = urldecode($field_key_value[1]);

        $id = $row['id'];

        $date1 = date("d/n/Y");

        foreach ($cart->get_contents() as $item)
        {
            $item_id    = $item['id'];
            $item_name  = $item['name'];
            $item_price = $item['price'];
            $item_qty   = $item['qty'];
            $item_ids       = explode("-",$item_id);

            for($i = 0; $i < count($item_ids); $i++)
            {
                $item_idn = join("",$item_ids);
            }

            if($key == $item_id."id")
            {
                $ids = $value;
                echo $ids."\r\n";
            }
            elseif($key == "Small".$item_idn)
            {
                $small= $value;
                echo $small."\r\n";
            }
            elseif($key == "large".$item_idn)
            {
                $large= $value;
                echo $large."\r\n";
            }
            elseif($key == "medium".$item_idn)
            {
                $medium= $value;
                echo $medium."\r\n";
            }
            elseif($key == "xlarge".$item_idn)
            {
                $xlarge= $value;
                echo $xlarge."\r\n";
            }
            elseif($key == "qty".$item_idn)
            {
                $qty = $value;
                echo $qty."\r\n";
            }
            elseif($key == "Total".$item_idn)
            {
                $subtotal = $value;
              开发者_如何转开发  echo $subtotal."\r\n";
            }
            elseif($key == "finaltotal")
            {
                $finaltotal = $value.",";
                $final = explode(",",$finaltotal);

                for($i = 0; $i < count($final); $i++)
                {
                    $totalf = $final[$i];
                    break 3;
                }
            }
        }
    }
}


From jQuery docs:

The .serialize() method creates a text string in standard URL-encoded notation

So on the PHP side you'll get a similar string like this:

a=1&b=2&c=3&d=4&e=5

There's no need to explode &'s (or any other hocus-pocus) you can easily access your submitted variables like:

$a = $_POST['a']; //1

Of course when you submit your data via $_GET, you need to use $_GET.


It appears as if you have a number of issues. If your jQuery is posting to your PHP script you can get all your keys/values through the $_POST array. You could get $finaltotal like this $finaltotal = $_POST['finaltotal']. If your jQuery is not POSTing, but sending them as a GET request, you can get the same value like so $finaltotal = $_GET['finaltotal'].

Also, regarding your big if/elseif block of code, I would recommend using a switch statement instead if you are going to keep your code as is.

switch($key)
{
    case 'finaltotal':
        //do stuff
        break;
    default:
        //do default
        break;
}


First of all, you either can use $_GET/$_POST for the submited data, as fabrik pointed out, or (if for some reason you really only have your data in $data) can use the built in function parse_str.

parse_str($data, $fields);
foreach($fields as $key => $field) {
  foreach ($cart->get_contents() as $item) {
    if($key == ...) {
      ...
    }
  }
}

And if I correctly understand what you are doing here, you need to move the break 3; out of your for loop:

elseif($key == "finaltotal") {
  $finaltotal = $value.",";
  $final = explode(",",$finaltotal);

  for($i = 0; $i < count($final); $i++){
    $totalf = $final[$i];
  }
  break 3;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜