简体   繁体   English

如何在每行上添加按钮

[英]How can I add a button on every line

I'm using php and mysql to do a simple shopping cart. 我正在使用php和mysql做一个简单的购物车。 I can display the whole database, but how can I add a button on each line; 我可以显示整个数据库,但是如何在每行上添加一个按钮; for example, "add to cart"? 例如“添加到购物车”?

    <?php

$link = mysqli_connect("rerun","potiro","pcXZb(kL","poti");
if (!$link)
   die("Could not connect to Server");


$query_string = "select * from products";
$result = mysqli_query($link,$query_string);

$num_rows = mysqli_num_rows($result);

if ($num_rows > 0 ) {
    print "<table border=0>";
    while ( $a_row = mysqli_fetch_row($result) ) {
         print "<tr>\n";
         foreach ($a_row as $field)
             print "\t<td>$field</td>\n";
         print "</tr>";
    }
    print "</table>";
}

mysqli_close($link);
?>

Add a new column after the $field column and just linked up the Add to cart button to your Add to Cart page and just put the product id so that If you press the Add to cart button on that row, the product id on that row will be confirmed and by using this product id you can get the product information in the cart page and confirm the order. 在$ field列之后添加一个新列,然后将“ 添加到购物车”按钮链接到“ 添加到购物车”页面 ,只需放置产品ID,以便如果您在该行上按“ 添加到购物车”按钮 ,则该行上的产品ID将被确认,并使用此产品ID,您可以在购物车页面中获取产品信息并确认订单。

if ($num_rows > 0 ) {
    print "<table border=0>";
    while ( $a_row = mysqli_fetch_row($result) ) {
         print "<tr>\n";
         foreach ($a_row as $field)
             print "\t<td>$field</td>\n";
             print "\t<td><a href='cart.php?id=$your_product_id'>Add to cart</a></td>\n";
         print "</tr>";
    }
    print "</table>";
}

Such a button would be crafted with a combination of HTML and CSS; 这样的按钮将结合HTML和CSS来制作; you merely need to use PHP to echo out the code in the loop. 您只需要使用PHP在循环中echo显代码即可。

while ( $a_row = mysqli_fetch_row($result) ) {
     print "<tr>\n";
     foreach ($a_row as $field)
         print "\t<td>$field</td>\n";
         print "\t<td><a href='process.php?id=$field'>Add to cart</a></td>\n";
     print "</tr>";
}

Just add another column on each row. 只需在每行上添加另一列。

print "<table border=0>";
while ( $a_row = mysqli_fetch_row($result) ) {
     print "<tr>\n";
     foreach ($a_row as $field)
         print "\t<td>$field</td><td><button>Add to cart</button>\n";
     print "</tr>";
}
print "</table>";

In foreach loop added code is 在foreach循环中添加的代码是

    foreach ($a_row as $field)
       print "\t<td>$field</td><td><button>Add to cart</button>\n";//Add button here or any thing you want just add it
  }

I hope this is helps you 希望对您有帮助

Thanks 谢谢

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM