问题描述:

This is my url:

http://example.com/shop-login.php?itemCount=1&item_name_1=+product1&item_quantity_1=1&item_price_1=23.52&item_options_1=color%3A+Color%2C+pid%3A+5&Shipping_Option=EMS-Express

The following php command:

echo $_GET['item_name_1'];

is giving value as product1 correctly.

But the following code produces no output on for loop

$i = 1;

for ($i = 1; $i < $item_count; $i++) {

$item_name_.$i = $_GET["item_name_".$i];

echo $item_name_.$i .'<br />' ;

$item_quantity_.$i = $_GET["item_quantity_".$i];

echo $item_quantity_.$i .'<br />' ;

$item_price_.$i = $_GET["item_price_".$i];

echo $item_price_.$i .'<br />' ;

$item_options_.$i = $_GET["item_options_".$i];

echo $item_options_.$i .'<br />' ;

}

I want to get values like item_name_1, item_name_2 in url using $_GET[] ????

网友答案:

When approaching tasks in programming, you should ask yourself what's the best data-structure for your task. In this case I believe an array is a better approach. So consider using a multi-dimensional array items.

Also notice that your loop condition is $i < $item_count and in your example $item_count will be equals 1 and 1 isn't < than 1.

$item_count = (int) $_GET['itemsCount'];
$i = 1;
$items = array();

for ($i = 1; $i <= $item_count; $i++) {
    $items[$i]['name'] = $_GET["item_name_".$i];
    echo $items[$i]['name'] .'<br />' ;
    $items[$i]['quantity'] = $_GET["item_quantity_".$i];
    echo $items[$i]['quantity'] .'<br />' ;
    $items[$i]['price'] = $_GET["item_price_".$i];
    echo $items[$i]['price'] .'<br />' ;
    $items[$i]['options'] = $_GET["item_options_".$i];
    echo $items[$i]['options'] .'<br />' ;
}
网友答案:

Instead of $item_name_.$i, use ${'item_name_'.$i}. Read more on variable variables here.

Also check the value of $item_count. If it is less than or equal to 1, the for loop will not execute.

Here's the syntax-correct code:

$i = 1;
for ($i = 1; $i <= $item_count; $i++) {
    ${'item_name_'.$i} = $_GET["item_name_".$i];
    echo ${'item_name_'.$i} .'<br />' ;
    ${'item_quantity_'.$i} = $_GET["item_quantity_".$i];
    echo ${'item_quantity_'.$i} .'<br />' ;
    ${'item_price_'.$i} = $_GET["item_price_".$i];
    echo ${'item_price_'.$i} .'<br />' ;
    ${'item_options_'.$i} = $_GET["item_options_".$i];
    echo ${'item_options_'.$i} .'<br />' ;
}

You may find that using a multi-dimensional array would be easier in the context that you're using

$i = 1;
$items = array();
for ($i = 1; $i <= $item_count; $i++) {
    $items[$i] = array(
        'name' => $_GET['item_name_'.$i],
        'quantity' => $_GET['item_quantity_'.$i],
        'price' => $_GET['item_price_'.$i],
        'options' => $_GET['item_options_'.$i],
    );
    // Outputting statements here
}
网友答案:

Use an array something like below

$arr = array();
for ($i = 1; $i < $item_count; $i++) {
    if(isset($_GET["item_name_".$i]))
        $arr['item_name'][] = $_GET["item_name_".$i];
    if(isset($_GET["item_quantity_".$i]))
        $arr['item_quantity'][] = $_GET["item_quantity_".$i];
    if(isset($_GET["item_options_".$i]))
        $arr['item_options'][] = $_GET["item_options_".$i];
}
print_r($arr);
相关阅读:
Top