簡體   English   中英

如何遍歷$ _GET以獲取所有數據

[英]How to loop through $_GET to get all the data

我有一個大問題:當有人選擇多個產品時,例如他選擇了3個產品,就會進入購物車。 當他發送訂單(結賬)時,我只看到郵件中的第一個產品,而不是3個。

這是因為我在function.js中說過:

function fillInForm(){

    NumberOrdered = 0;
    NumberOrdered = readCookies("NumberOrdered");

    for (i = 1; i <= NumberOrdered; i++){
        NewOrder = "Order" + i;
        thisCookie = "";
        thisCookie = readCookies(NewOrder);;
        fields = new Array();
        fields = thisCookie.split("|");
        document.write("<input type=hidden name=\"Product ID " + i + "\" value=\"" + fields[0] + "\">");
        document.write("<input type=hidden name=\"Brand " + i + "\" value=\"" + fields[1] + "\">");
        document.write("<input type=hidden name=\"Model " + i + "\" value=\"" + fields[2] + "\">");
        document.write("<input type=hidden name=\"Price " + i + "\" value=\"" + fields[3] + "\">");
        document.write("<input type=hidden name=\"Amount products " + i + "\" value=\"" + fields[4] + "\">");
        document.write("<input type=hidden name=\"Total cost " + i + "\" value=\"" + fields[3] * fields[4] + "\">");
        document.write("<input type=hidden name=\" " + "\" value=\"" + "\">");

    }
}

當我使用var_dump($ _ GET); 它向我展示了一切,所以它給我看的三個產品有類似的東西

["Brand_1"]=>
["Brand_2"]=>
["Brand_3"]=>

在mail.php中,信息發送到我有:

$brand = $_GET["Brand_1"]; (<<WITH 1, one) 

因為

$brand = $_GET["Brand_i"]; (<<WITH i) does not work.

但理論上我需要

$brand = $_GET["Brand_i"] (<<WITH i)to get all the products............

我怎樣才能解決這個問題?

mail.php

$productid = $_GET["Product_ID_1"];
$brand = $_GET["Brand_1"];
$model = $_GET["Model_1"];
$price = $_GET["Price_1"];
$amount = $_GET["Amount_products_1"];
$totalcost = $_GET["Total_cost_1"];

$message .= 'Your order information: ' . '<br />';
$message .= 'Product ID: ' . $productid . "<br />" .
            'Brand: '. $brand . "<br />" .
            'Model: ' . $model . "<br />" .
            'Price per item: ' . $price . "<br />" .
            'Amount of item: ' . $amount . "<br />" .
            'Total cost: ' . $totalcost . "<br />" .
            'Order date and time: ' . $date;
$message .= '</body></html>';

你想要的可能是什么樣的

for ($i = 0; $i < $_GET['numberordered']; $i++)
{
    $brand = $_GET['Brand_'.$i];
    // ... rest of your code ...
}

你需要一個循環來迭代它們,你不能只使用Brand_i

$message .= 'Your order information: ' . '<br />';
$i = 1;
while (isset($_GET["Product_ID_".$i])) {
    $productid = $_GET["Product_ID_".$i];
    $brand = $_GET["Brand_".$i];
    $model = $_GET["Model_".$i];
    $price = $_GET["Price_".$i];
    $amount = $_GET["Amount_products_".$i];
    $totalcost = $_GET["Total_cost_".$i];

    $message .= 'Product ID: ' . $productid . "<br />" .
                'Brand: '. $brand . "<br />" .
                'Model: ' . $model . "<br />" .
                'Price per item: ' . $price . "<br />" .
                'Amount of item: ' . $amount . "<br />" .
                'Total cost: ' . $totalcost . "<br />" .
    $i++;
}

$message .= 'Order date and time: ' . $date;
$message .= '</body></html>';

澄清一下,空格和下划線不可互換。 如果你有這個字段:

<input type="hidden" name="brand 1" />

然后要在服務器上訪問它,你必須使用$_GET['brand 1']而不是$_GET['brand_1']

正如@GlitchMr指出的那樣,顯然字段名稱中的空格被PHP轉換為下划線。 我不知道這個功能。 你現在擁有的很好,但我強烈建議不要首先使用空格。

你也不能在字符串中使用i ,假設PHP會理解有很多東西,它應該替換為i數字。 通過在HTML中命名輸入來指示該字段是一個數組,如下所示:

<input type="hidden" name="brand[]" />

然后,您不必擔心使用Javascript對它們進行編號。 PHP將理解這是一個值數組,並且:

print_r($_GET['brand[]']);

將是一個數組,您可以使用foreach迭代編寫需要發送的電子郵件。

$length = count($_GET['brand']);
$fields = array('product_id[]', 'model[]', 'price[]', 'amount[]', 'total_cost[]');

foreach($fields as $field) {
    if($count($_GET[$field] !== $length) {
        die('Not all product arrays are the same length');
    }
}


for($i = 0; $i < $length; $i++) {
    $message .= 'Your order information: ' . '<br />';
    $message .= 'Product ID: ' . $_GET['product_id[]'][$i] . '<br />' .
                'Brand: '. $_GET['brand[]'][$i] . '<br />' .
                'Model: ' . $_GET['model[]'][$i] . '<br />' .
                'Price per item: ' . $_GET['price[]'][$i] . '<br />' .
                'Amount of item: ' . $_GET['amount[]'][$i] . '<br />' .
                'Total cost: ' . $_GET['total_cost[]'][$i] . '<br />' .
                'Order date and time: ' . $date. '<br />';
}

你需要使用字符串插值或連接來獲得你想要的東西。

你可以使用:

$brand = $_GET["Brand_{$i}"];

要么:

$brand = $_GET['Brand_' . $i];

在for循環中遍歷每個訂單。

您還可以使用PHP的內置功能自動創建產品數組。 在PHP中,當使用名為fieldName[]的字段提交表單時,值將轉換為數組。 但是,如果您需要以一種方式跳過一列中的值,它就不會這樣做。 但是,你可以指定一個索引, fieldName[1] ,然后解決這個問題(盡管你的索引從0開始,否則你可能需要一個for-each循環)。 您還可以使用語法products[0][brand]並獲取如下數組:

$_GET['products'] = array(
  0 => array(
    'brand' => 'My Brand',
    'sku' => '0123asd'
  )
);

長話短說 - 您可以使用相同的輸入名稱發送多個品牌。 ;)

HTML:

<input name="Brand[]" value="Cat" />
<input name="Brand[]" value="Dog" />
<input name="Brand[]" value="Shuttle" />

PHP:

foreach($_GET['Brand'] as $brand) {
    echo $brand;
}

結果:

CatDogShuttle

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM