foreach循环未在html表中添加另一行

时间:2019-05-24 00:19:29

标签: php html curl

我有一项任务要设置一个用于股票的购物车。 API查询有效,我可以向购物车中添加数量。 foreach循环在HTML表中显示1行。 如果我对库存报价执行另一个API查询,并为第二个报价添加数量,则该表不会显示第二行。

我正在运行XAMPP,PHP和MYSQL的当前版本。该项目是如何使用SESSION变量在页面之间存储数据的练习。

这是我的browser.php文件


<?php
session_start();

require 'head.php';
require 'nav.php';

if(!isset($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
}

if(isset($_GET['clear']))
{
    $_SESSION['cart'] = array();
}

if(isset($_GET['stockID']))
{
    $stockID = $_GET['stockID'];
    $quan = $_GET['quan'];
    $name = $_GET['name'];
    $symbol = $_GET['symbol'];
    $price = $_GET['price'];

    if(!isset($_SESSION['cart'][$stockID]))
    {
        $_SESSION['cart'][$stockID]['quan'] = $quan;
        $_SESSION['cart'][$stockID]['name'] = $name;
        $_SESSION['cart'][$stockID]['symbol'] = $symbol;
        $_SESSION['cart'][$stockID]['price'] = $price;      
    }
    else
    {
        $_SESSION['cart'][$stockID]['quan'] += $quan;
    }
}

if(isset($_GET['quote']))
{
    $stock = $_GET['quote'];

    //base URL
    $service_url = "https://api.iextrading.com/1.0/stock";
    //add endpoint
    $service_url .= "/$stock/batch?types=quote";
    //add search val

    //useful for debug - uncomment if needed
    //echo $service_url."<br>";

    //Use CURL to fetch the response
    $curl = curl_init($service_url);

    //had to set up several options to make this work
    $user_agent = 'Mozilla/5.0 (Windows NT 6.1; rv:8.0) Gecko/20100101 Firefox/8.0';
    curl_setopt($curl, CURLOPT_URL, $service_url);
    curl_setopt($curl, CURLOPT_USERAGENT, $user_agent);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_TIMEOUT, 10);

    $curl_response = curl_exec($curl);
    if ($curl_response === false) 
    {
        $info = curl_getinfo($curl);
        curl_close($curl);
        die('error occurred during curl exec. Additional info: ' . var_export($info));
    }
    curl_close($curl);

    $decoded = json_decode($curl_response, 1);

    if(!empty($decoded))
    {
        $symbol = $decoded['quote']['symbol'];  
        $name = $decoded['quote']['companyName'];
        $price = $decoded['quote']['latestPrice'];      
        /*
        echo $symbol."<br>";
        echo $name."<br>";
        echo $price."<br>";
        */
        echo"<pre>";
        print_r($_SESSION['cart']);
        echo"</pre>";       

        echo "<h1>Stock Portfolio</h1>";
        echo "<h2>Browsing Stock Quotes</h2>";

        echo 
        "
        <form>
            <table >
                <tr>
                    <th>Stock Symbol</th>
                    <th>Company Name</th>
                    <th>Price per share </th>
                    <th>Quantity</th>
                </tr>           
                <tr>
                    <td>$symbol</td>
                    <td>$name</td>
                    <td>$price</td>
                    <td>
                        <form action='{$_SERVER['PHP_SELF']}' method='GET'>
                            <input type='text' name='quan' id='quan'>
                            <input type='hidden' name='stockID' id='stockID' value='1'>
                            <input type='hidden' name='name' id='name' value='$name'>
                            <input type='hidden' name='symbol' id='symbol' value='$symbol'>
                            <input type='hidden' name='price' id='price' value='$price'>
                            <input type='submit' value='Add To Cart' >
                        </form>
                    </td>                   
                </tr>               
            </table>
        </form>     
        ";
    }
    else 
    {
        echo "No listing found";
    }
}
?>

    <form action = 'browse.php' method = 'GET' class = 'quote'>
            Stock Symbol <input name = 'quote'>
            <input type = 'submit'>

        </form>

<?php
require 'foot.php';
?>

这是我的cart.php文件


p
session_start();

require 'head.php';
require 'nav.php';

if(!isset($_SESSION['cart']))
{
    $_SESSION['cart'] = array();
}

if(isset($_GET['clear']))
{
    $_SESSION['cart'] = array();
}

if(isset($_GET['stockID']))
{
    $stockID = $_GET['stockID'];
    $quan = $_GET['quan'];

    $_SESSION['cart'][$stockID]['quan'] = $quan;

    if($_SESSION['cart'][$stockID]['quan'] == 0)
    {
        unset($_SESSION['cart'][$stockID]);
    }   
}
?>
    <?php
        echo "<pre>";
        print_r($_SESSION['cart']);
        echo "</pre>";
    ?>

<h1>Stock Portfolio</h1>
<h2>Shopping Cart for Stock Quotes</h2> 

<form>
    <table >
        <tr>
            <th>Stock Symbol</th>
            <th>Company Name</th>
            <th>Price </th>
            <th>Quantity</th>
            <th>Subtotal</th>
        </tr>
<?php
        $total = 0;
        foreach($_SESSION['cart'] as $key => $val)
        {
            $sub = $val['quan']*$val['price'];
            $total += $sub;
            $sub = "$ " . number_format($sub, 2);
            $total = "$ " . number_format($total, 2);

            echo "
            <tr>
                <td>{$val['symbol']}</td>
                <td>{$val['name']}</td>
                <td>{$val['price']}</td>
                <td>
                    <form action='{$_SERVER['PHP_SELF']}'>
                        <input type='text' name='quan' id='quan' value='{$val['quan']}'>
                        <input type='hidden' name='stockID' id='stockID' value='$key'>                      
                        <input type='submit' value='Update Quantity'>                       
                    </form>
                </td>
                <td>$sub</td>
            </tr>
            ";
        }   // foreach
?>  
        <tr>
            <td colspan = '4'>Grand Total</td>
            <td><?php echo $total?></td>
        </tr>

    </table>
</form>
<form action = 'browse.php' method = 'GET' class = 'quote'>
    Stock Symbol <input name = 'quote'>
    <input type = 'submit'> 
</form>

<?php
require 'foot.php';
?>

I get the table view in both pages after querying for the first stock quote. I add a quantity and click on the add quantity button and it will show the first query in the cart. But when I perform a second query and try to add that to the cart, nothing shows in the table.

0 个答案:

没有答案