HTML / PHP / SQL - 从数据库到网站匹配值

时间:2017-02-08 02:20:15

标签: php html sql

我正在开设一个网站,其中包含付款计划,每个计划的价格不同。有了这个,我用SQL创建了一个数据库,所以如果需要,我可以改变计划的价格,所以它会自动更新到网站上。

使用W3Schools上的示例,我创建了一个应该获取产品名称和价格的基本文件。但是,由于有三种不同的计划类型,我不确定如何定义(在index.php中)每种产品和价格。每个产品都有一个主键,我没有将其添加到PHP文件中,所以如果有人能就如何处理这个问题给我任何建议,我将非常感激。

想法是改变一行,例如

<p class="plan-price">&pound;14.95 <span>

这样的东西
<p class="plan-price">&pound;'.$SESSION_PRICE['price'].' <span>

price.inc.php

<?php
include '../dbh.php';

$sql2    = "SELECT * FROM products";
$result2 = mysqli_query($conn, $sql2);

if(mysqli_num_rows($result2) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $_SESSION_PRICE['productTitle'] = $row['productTitle'];
        $_SESSION_PRICE['price'] = $row['price'];
    }   
}
?>

index.php(只是必填部分)

<section class="section-plans">
    <div class="row">
        <h2>Order Today.</h2>
    </div>
    <div class="row">
        <div class="col span-1-of-3">
            <div class="plan-box">
                <div>
                    <h4>Premium</h4>
                    <p class="plan-price">&pound;14.95 <span> / Month </span></p>
                </div>
                <div>
                    <ul>
                        <li><i class="ion-ios-checkmark icon-small"></i>Priority Acccess</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>24/7 Online Support</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>Access Anywhere</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>Unlimited Speed</li>
                    </ul>
                </div>
                <div>
                    <a href="#" class="btn btn-full">Sign Up Now</a>
                </div>
            </div>
        </div>
        <div class="col span-1-of-3">
            <div class="plan-box">
                <div>
                    <h4>Pro</h4>
                    <p class="plan-price">&pound;9.95 <span> / Month </span></p>
                </div>
                <div>
                    <ul>
                        <li><i class="ion-ios-checkmark icon-small"></i>Worldwide Access</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>24/7 Online Support</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>Access Anywhere</li>
                        <li><i class="ion-close-circled  icon-small"></i>Unlimited Speed</li>
                    </ul>
                </div>
                <div>
                    <a href="#" class="btn btn-ghost">Sign Up Now</a>
                </div>
            </div>
        </div>
        <div class="col span-1-of-3">
            <div class="plan-box">
                <div>
                    <h4>Lite</h4>
                    <p class="plan-price">&pound;4.95 <span> / Month </span></p>
                </div>
                <div>
                    <ul>
                        <li><i class="ion-ios-checkmark icon-small"></i>Worldwide Access</li>
                        <li><i class="ion-ios-checkmark icon-small"></i>24/7 Online Support</li>
                        <li><i class="ion-close-circled icon-small"></i>Access Anywhere</li>
                        <li><i class="ion-close-circled  icon-small"></i>Unlimited Speed</li>
                    </ul>
                </div>
                <div>
                    <a href="#" class="btn btn-ghost">Sign Up Now</a>
                </div>
            </div>
        </div>
    </div>
</section>

2 个答案:

答案 0 :(得分:0)

我不确定你是否应该命名一个变量$ _SESSION_PRICE,因为它看起来应该是一个超全局变量。也许尝试这样的事情:

if(mysqli_num_rows($result2) > 0) {
    while($row = mysqli_fetch_assoc($result)) {
        $session_price['productTitle'] = $row['productTitle'];
        $session_price['price'] = $row['price'];
    }   
}

然后,为了将PHP值传入HTML,请在PHP块中回显它。试试这个:

<p class="plan-price">&pound; <?php echo $session_price['price']; ?><span>

您需要在index.php文件中包含price.inc.php文件,以便能够访问在那里创建的变量。所以在你需要之前的某个地方有一个这样的语句:

include 'price.inc.php';

现在,所有这一切的问题在于您循环抛出SQL查询返回的结果并重复分配相同的索引:

while($row = mysqli_fetch_assoc($result)) {
        $session_price['productTitle'] = $row['productTitle'];
        $session_price['price'] = $row['price'];
} 

如果您要获取MULTIPLE产品,则需要将它们存储起来:

在循环时编辑

$i = 0;

while($row = mysqli_fetch_assoc($result)) {
  $session_price[$i]['productTitle'] = $row['productTitle'];
  $session_price[$i++]['price'] = $row['price'];
} 

然后,您需要动态创建HTML以循环显示EACH产品,如下所示:

<?php 

  foreach ($session_price as $individual_item) {
    $item_name = $individual_item['productTitle'];
    $price = $individual_item['price'];
?>

  <div class="col span-1-of-3">
    <div class="plan-box">
        <div>
          <h4><?php echo $item_name; ?> </h4>
          <p class="plan-price">&pound;<?php echo $price; ?><span> / Month </span></p>
        </div>
        <div>
            <ul>

              <?php 

                if ($individual_item['productTitle'] == 'Premium') {
                  $access = 'Priority Access';
                } else {
                  $access = 'Worldwide Access';
                }

              ?>

                <li><i class="ion-ios-checkmark icon-small"></i><?php echo $access; ?></li>
                <li><i class="ion-ios-checkmark icon-small"></i>24/7 Online Support</li>
                <li><i class="ion-ios-checkmark icon-small"></i>Access Anywhere</li>
                <li><i class="ion-ios-checkmark icon-small"></i>Unlimited Speed</li>
            </ul>
        </div>
        <div>
            <a href="#" class="btn btn-full">Sign Up Now</a>
        </div>
    </div>
  </div>

<?php 

} // end foreach loop

?>

答案 1 :(得分:0)

你的

<p class="plan-price">&pound;'.$SESSION_PRICE['price'].' <span>

应该添加echo

<p class="plan-price">&pound;<?php echo $SESSION_PRICE['price'] ?> <span>