分别打印会话数组数据以供进一步使用

时间:2019-01-09 16:16:55

标签: php html arrays

就像标题一样,我设法将会话数组传递到其他文件,并使用“ echo join”和“ print_r”进行打印。 我想单独打印它们,以后再使用。例如,我想将天数*价格相加并显示出来。 关于ot或im的任何想法都做错了,必须更改更多内容。

这是代码(来自pastebin)

cars.php :(这是我基于阵列的购物车)

https://pastebin.com/Q9euzvG7

summarise.php(这里我正在使用会话传递数组。)

<?php
      $array["shopping_cart"] = $_SESSION["shopping_cart"];
      foreach ($array["shopping_cart"] as $value) {
      print_r($value);
      echo join(',',$value);
      echo  $_SESSION["shopping_cart"]["quantity"];
?>

https://pastebin.com/CFhuAt3M

PS。很抱歉编辑代码。

更新:

Table from sql database

和购物车数组:

   if(isset($_POST["add_to_cart"]))
{
  if(isset($_SESSION["shopping_cart"]))
 {
     $item_array_id = array_column($_SESSION["shopping_cart"], "item_id");
     if(!in_array($_GET["id_samochodu"], $item_array_id))
     {
        $count = count($_SESSION["shopping_cart"]);
        $item_array = array(
            'item_id'           =>  $_GET["id_samochodu"],
            'item_name'         =>  $_POST["hidden_name"],
            'item_brand'        =>  $_POST["hidden_brand"],
            'item_price'        =>  $_POST["hidden_price"],
            'item_quantity'     =>  $_POST["quantity"]
        );
        $_SESSION["shopping_cart"][$count] = $item_array;
    }
    else
    {
        echo '<script>alert("Już dodałeś ten samochód do koszyka") 
</script>';
    }
  }
 else
 {
     $item_array = array(
        'item_id'           =>  $_GET["id_samochodu"],
        'item_name'         =>  $_POST["hidden_name"],
        'item_brand'        =>  $_POST["hidden_brand"],
        'item_price'        =>  $_POST["hidden_price"],
        'item_quantity'     =>  $_POST["quantity"]
     );
     $_SESSION["shopping_cart"][0] = $item_array;
 }

if(isset($_POST["add_to_cart"])) { if(isset($_SESSION["shopping_cart"])) { $item_array_id = array_column($_SESSION["shopping_cart"], "item_id"); if(!in_array($_GET["id_samochodu"], $item_array_id)) { $count = count($_SESSION["shopping_cart"]); $item_array = array( 'item_id' => $_GET["id_samochodu"], 'item_name' => $_POST["hidden_name"], 'item_brand' => $_POST["hidden_brand"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["shopping_cart"][$count] = $item_array; } else { echo '<script>alert("Już dodałeś ten samochód do koszyka") </script>'; } } else { $item_array = array( 'item_id' => $_GET["id_samochodu"], 'item_name' => $_POST["hidden_name"], 'item_brand' => $_POST["hidden_brand"], 'item_price' => $_POST["hidden_price"], 'item_quantity' => $_POST["quantity"] ); $_SESSION["shopping_cart"][0] = $item_array; }

1 个答案:

答案 0 :(得分:0)

问题非常模糊,我可以想象以下内容可以为您指明正确的方向:

<?php

$_SESSION["shopping_cart"] = [
    [
        'item_id'           =>  "id_samochodu1",
        'item_name'         =>  "hidden_name1",
        'item_brand'        =>  "hidden_brand1",
        'item_price'        =>  "hidden_price1",
        'item_quantity'     =>  "quantity1"
    ],
    [
        'item_id'           =>  "id_samochodu2",
        'item_name'         =>  "hidden_name2",
        'item_brand'        =>  "hidden_brand2",
        'item_price'        =>  "hidden_price2",
        'item_quantity'     =>  "quantity2"
    ]
]; 

$dictionary = [
    'item_id' => "id",
    'item_name' => "name", 
    'item_brand' => "brand", 
    'item_price' => "price",
    'item_quantity' => "quantity" 
];

echo '<table id="shopping-cart-content>'."\n";
foreach($_SESSION["shopping_cart"] as $entry) {
    echo '<tr class="item">'."\n";
    foreach($entry as $key => $val) {
        echo sprintf(
            '<td class="prompt">%s</td><td class="value">%s</td>', 
            htmlentities($dictionary[$key]), 
            htmlentities($val))."\n";
    }
    echo '</tr>'."\n";
}
echo '</table>'."\n";

这将是基于相同输入数据但不需要字典的另一种数据可视化方法:

echo '<table id="shopping-cart-content>'."\n";
foreach($_SESSION["shopping_cart"] as $entry) {
    echo '<tr class="item">'."\n";
    echo sprintf('<td class="prompt">id</td><td>%s</td>', htmlentities($entry['item_id']))."\n";
    echo sprintf('<td class="prompt">name</td><td>%s</td>', htmlentities($entry['item_name']))."\n";
    echo sprintf('<td class="prompt">brand</td><td>%s</td>', htmlentities($entry['item_brand']))."\n";
    echo sprintf('<td class="prompt">price</td><td>%s</td>', htmlentities($entry['item_price']))."\n";
    echo sprintf('<td class="prompt">quantity</td><td>%s</td>', htmlentities($entry['item_quantity']))."\n";
    echo '</tr>'."\n";
}
echo '</table>'."\n";

这两种方法的输出相同:

<table id="shopping-cart-content>
<tr class="item">
<td class="prompt">id</td><td class="value">id_samochodu1</td>
<td class="prompt">name</td><td class="value">hidden_name1</td>
<td class="prompt">brand</td><td class="value">hidden_brand1</td>
<td class="prompt">price</td><td class="value">hidden_price1</td>
<td class="prompt">quantity</td><td class="value">quantity1</td>
</tr>
<tr class="item">
<td class="prompt">id</td><td class="value">id_samochodu2</td>
<td class="prompt">name</td><td class="value">hidden_name2</td>
<td class="prompt">brand</td><td class="value">hidden_brand2</td>
<td class="prompt">price</td><td class="value">hidden_price2</td>
<td class="prompt">quantity</td><td class="value">quantity2</td>
</tr>
</table>