如何为每个产品生成购买按钮

时间:2016-04-14 17:40:04

标签: php mysql

情况: 我有这个表,它显示了来自数据库的2行产品。

<table style ="position:absolute; left:0px">
    <tbody>
    <?php
    $i = 1;
    $columns = 2;
    $data1 = mysql_query("SELECT * FROM `products` WHERE categorie='1' AND nr_redeems='0' ")  or die(mysql_error());
    while($row1 = mysql_fetch_array($data1))
    {
        if($i == 1){ echo '<tr>'; }
        echo "<td>" 
                    . "<div class=''></div>" 
                    . "<div class='p1'>Some data about it.</div>"
                    . "<div class='p2'><b>Price: "
                    . $row1['nr_points']
                    . " points</b></div>" 
                    . "<button><img style='position:relative; top:0px; left:0px; width:396px; height:121px' src='../img/button.gif'></button>"
                    . "</td>";
        $i++;
        if($i == ($columns + 1)) { echo '</tr>'; $i = 1; }
    }
    ?>
    </tbody>
</table>

我在database.php中也有这个全局(只需提一下 - 从哪里看点和用户名)

$username = $_SESSION['username'];
$data = mysql_query("SELECT * FROM `users` WHERE username='$username' ")  or die(mysql_error());
 while($row= mysql_fetch_array( $data )) 
 { 
    $user = $row['username']; 
    $nick = $row['nickname']; 
    $email = $row['email']; 
    $points = $row['points']; 
 }  

我想做什么:在表格中会为每个产品生成更多按钮。当按下按钮时,我想检查用户是否有点获得该东西。如果他有所作为.. 我想通过更新产品使其无法供其他人使用并将记录添加到购买中来进行购买。 我还希望在“购买”时以产品的价格降低用户的积分。

$ points - 是用户已拥有的点数。 所以我想的是:

if ($points > $row1['nr_points']) // if user have enought points

do this...

$final_points = $points - $row1['nr_points'];  // to decrease user's points

$sql1 = "UPDATE users SET points='$final_points' WHERE username='$username'";
mysql_query($sql1) // update the new points value


$sql2 = "UPDATE products SET nr_redeems='1' WHERE id='$row1['id']'";
mysql_query($sql2) // make the product unavailable for others

$sql3 = "INSERT INTO purchases (id, username, productid)
VALUES ('the number for AUTO_INCREMENT ??', '$username', '$row1['id']')";

mysql_query($sql3)  // add the record into database

任何想法如何做到这一点......?我按下按钮时搜索了一些执行代码的方法,但我没有成功。

============================================ =================================

更新: 我找到了某种解决方案

这是购买页面的HTML代码:

<form method="POST" action="">
<input type="hidden" value="100" name="price">
<input type="hidden" value="1" name="productid">
<button type="submit" name="buy" ><img style='position:relative; top:0px; left:0px; width:396px; height:121px' src='../img/button.gif' </button>
</form> 

这是来自另一页的php代码:

 if (isset($_POST['buy'])) // get data from form I guess
{
   $id = $_POST['productid']; // get the product id and price sended by user when pressed on button
   $price = $_POST['price'];  

   $data112 = mysql_query("SELECT * FROM `products` WHERE id='$id' ")  or die(mysql_error());
   while($row112= mysql_fetch_array( $data112 )) 
   { 
      $nr_points = $row112['nr_points']; // get the price of product from database
   } 

   if ($nr_points == $price) // I want to check if the price from database is equal with price sended by user
   {
       if ($points < $price) // check if have enought points
       {
           echo "You don't have enought points"; // just testing
       }
        if ($points > $price) // check if have enought points
       {
           echo "You have enought points"; // just testing
       }
   }
}  

2 个答案:

答案 0 :(得分:0)

如果我得到了你想要的东西,因为你的while循环已经生成按钮

"<button><img style='position:relative; top:0px; left:0px; width:396px; height:121px' src='../img/button.gif'></button>"

只需将表格标签添加到每个按钮

 <form method="POST" action="">

“”

这样每个按钮都可以有自己的动作

答案 1 :(得分:0)

从PHP退一步,看看它在HTML中会是什么样子,你可能会有这样的事情:

<强> HTML

<div class='userBar'>
  <label>Name:</label><span id='user'>Nick</span>
  <label>Email:</label><span id='email'>nick@example.com</span>
  <label>Points:</label><span id='points'>100</span>
</div>
<h3>Store</h3>
<table style="position:absolute; left:0px">
  <tbody>
    <tr>
      <td>
        <div class=''></div>
        <div class='p1'>First Product Description.</div>
        <div class='p2'><b>Price: 20 points</b></div>
        <button id='button-1' class='buyButton' data-price='20'>Buy</button>
      </td>
      <td>
        <div class=''></div>
        <div class='p1'>Second Product Description.</div>
        <div class='p2'><b>Price: 25 points</b></div>
        <button id='button-2' class='buyButton' data-price='25'>Buy</button>
      </td>
    </tr>
    <tr>
      <td>
        <div class=''></div>
        <div class='p1'>Third Product Description.</div>
        <div class='p2'><b>Price: 15 points</b></div>
        <button id='button-3' class='buyButton' data-price='15'>Buy</button>
      </td>
      <td>
        <div class=''></div>
        <div class='p1'>Fourth Product Description.</div>
        <div class='p2'><b>Price: 25 points</b></div>
        <button id='button-4' class='buyButton' data-price='25'>Buy</button>
      </td>
    </tr>
  </tbody>
</table>

这可以通过PHP填充和创建,因此它将基于您的SQL查询。

使用jQuery(JavaScript框架),我们可以在页面中添加一些功能来构建一个小型用户界面。这允许我们控制按钮的工作方式,并从PHP发送到浏览器的结果HTML中读取数据。

以下是您可能想要使用的代码示例。

<强>的jQuery

var points = 0;
$(document).ready(function() {
  points = parseInt($("#points").text());
  $(".buyButton").click(function() {
    var price = parseInt($(this).data("price"));
    if (price > points) {
      alert("You do not have enough points.");
      return false;
    }
    points -= price;
    $("#points").html(points);
    $.post({
      url: '/echo/html/',
      data: {
        html: "Thank you for your purchase. You now have " + points + " Points.",
        user: $("#user").text(),
        points: points
      },
      success: function(response) {
        alert(response);
      }
    });
  });
});

您的代码最终会有所不同,并使用您网站上的特定详细信息,例如可以处理所需操作的PHP页面的URL路径。就像你在原帖中所拥有的一样。您的帖子中没有足够的信息可以为您的PHP代码提供进一步的建议。

以下是一个工作示例:https://jsfiddle.net/36cub58L/

您可以看到,当您单击“购买”按钮时,如果您有足够的积分,则会通过AJAX将数据发布到另一个处理购买的脚本。如果您没有足够的,该脚本会让您知道您无法购买。

所以这只是冰山一角。请注意,我不使用任何表单,这有助于确保用户不会破坏您的脚本。现在,聪明的用户将伪造POST请求并以较低的价格购买产品。如果你想允许这个,作为游戏的一部分,很酷!但您也可以使用$_SESSION变量和其他检查等内容添加代码,以确保用户无法执行此类操作。

请记住,这只是您可以做的一个例子。我怀疑你将会有更多的问题。将您的问题分成更小的部分。如果可以,一次解决一个元素。您将获得更快的响应速度。