如何扩展从MySQL导入的类别列表

时间:2015-07-30 20:25:33

标签: php mysql html5 xampp

我从phpMyAdmin中的两个不同的表中导入了一组不同的值,一个来自Category表(其中cat_id是PK),另一个来自Item表;其中(cat_id是FK)。

<?php
require ('config.php');

$que = "SELECT * FROM category";
$run = mysql_query($que);
$j = 0;
while($row = mysql_fetch_array($run))
{
    $cat_idd[$j] = $row['cat_id'];
    $cat_namee[$j] = $row['cat_name'];
    $j++;
}
for($i = 0;  $i < count($cat_idd);  $i++)
{
    $que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
    $result = mysql_query($que2);
    $run = mysql_num_rows($result);
    echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
}
?>

这是我到目前为止所做的:

Screenshot

现在我该如何扩展它?或者,例如,如何在同一页面或下一页上显示存储在名为Clothing的类别中的两个项目?由于此代码仅帮助我查看数据库中存储的项目数,

这是我的表格:

    <form name = "view" method = "POST" action ="cart.php">
      <table align = 'center' width = '100%' border = '4'>
      <tr bgcolor = 'yellow'>
      <td colspan = '20' align = 'center'><h2> Viewing all the Products </td>
</tr>
      <tr align = 'center'>
      <th>Item ID</th>
      <th>Name</th>
      <th>Price</th>
      <th>Select</th>
      </tr>

      <tr  class = "odd">


      <?php
        require ('config.php');

        $que = "SELECT * FROM category";
        $run = mysql_query($que);
        $j = 0;
      while($row = mysql_fetch_array($run))
       {
         $cat_idd[$j] = $row['cat_id'];
         $cat_namee[$j] = $row['cat_name'];
         $j++;
       }

我想过使用数组函数将值存储在一个类别Clothing(2) => 2

       function array_insert(&$array, $position, $insert)
       {
         if (is_int($position)) {
          array_splice($array, $position, 0, $insert);
       } else {
          $pos   = array_search($position, array_keys($array));
          $array = array_merge(
             array_slice($array, 0, $pos),
             $insert,
            array_slice($array, $pos)
        );
       }
       }

      $arr = array();
      $arr[] = 2;


      for($i = 0;  $i < count($cat_idd);  $i++)
      {
       $que2 =  "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]'";
       $result = mysql_query($que2);
       $run = mysql_num_rows($result);
       echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>";
     array_insert($arr, 0, "$run");
     // echo $arr[0];
      }
        $que2 = "SELECT * FROM item WHERE cat_id = '1'";      //instead of using '1' i wish to use the one user clicks on!
        $res2 = mysql_query($que2);
       while($row = mysql_fetch_array($res2))
        {
          $i_id = $row['item_id'];
          $i_namee = $row['item_name'];
          $i_price = $row['item_price'];


       ?>
       <td><?php echo $i_id; ?></td>
       <td><?php echo $i_namee; ?></td>
       <td><?php echo $i_price; ?></td>
       <td><input type="checkbox" name="addcart" value="<?php echo $item; ?>" onClick="return KeepCount()" />Tick</td>
</tr>
       <?php }  ?>    
       <br><br>
       </table>
       <input type = "hidden" name = "check">
       <button type=  "submit" onclick = "location.href = 'cart.php';" id = "cart" style="float:right; margin-top: 30px; margin-right: 10px;">Add to Cart</button>

上面的图像是我想要的结果,这个只针对服装(2)。我想让它在用户点击其他内容时更改,例如Shoes(1)。

2 个答案:

答案 0 :(得分:0)

您已发出正确的查询以获取有关某个项目的所有数据信息。但是,您没有获取每个字段的内容,而是仅选择计算受影响的行数。通过使用[a],您实际上可以获得每个字段的内容,并且可以显示结果。

根据您的代码考虑以下示例:

mysql_fetch_array()

for($i = 0; $i < count($cat_idd); $i++) { $que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' "; $result = mysql_query($que2); $run = mysql_num_rows($result); echo "<a href=''>".$cat_namee[$i]."(".$run.")</a><br>"; while($row = mysql_fetch_array($result)){ //Change ['item_name'] and ['item_description'] to match your DB schema. echo "item name: $row['item_name']"; echo "item description: $row['item_description']"; } } 是一个数组(包含关联和数字索引),包含您要查找的所有数据。在我的示例中,我假设$row表有一个名为itemsitem_name的字段。您的表可能具有不同的列名称,您需要更改它以匹配您的列名。

有关item_description的更多信息:http://php.net/manual/en/function.mysql-fetch-array.php

最后,您应该知道自PHP 5.5起不推荐使用mysql_fetch_array()扩展名。您应该强烈考虑转移到mysqlmysqli

有关PDO的更多信息:http://php.net/manual/en/book.mysqli.php

有关mysqli的更多信息:http://php.net/manual/en/book.pdo.php

答案 1 :(得分:0)

所以在花了差不多一两天之后我终于摆脱了这个问题而且我在这里发布了答案,所以如果有其他人来这里寻找它。

<?php
 require ('config.php');

 $que = "SELECT * FROM category";
 $run = mysql_query($que);
 $j = 0;
 while($row = mysql_fetch_array($run))
 {
    $cat_idd[$j] = $row['cat_id'];
    $cat_namee[$j] = $row['cat_name'];
  $j++;
 }
for($i = 0;  $i < count($cat_idd);  $i++)
    {
        $que2 = "SELECT * FROM item WHERE cat_id = '$cat_idd[$i]' ";
        $result = mysql_query($que2);
        $run = mysql_num_rows($result);
        echo "<a href='c.php?id=$cat_idd[$i]'>".$cat_namee[$i]."(".$run.")</a><br><br>";
    }

?>


<?php
 require('config.php');
 if(isset($_GET['id']))
 { 
    $cat_id = $_GET['id'];
    $que = "SELECT * FROM item WHERE cat_id = '$cat_id'";
    $run = mysql_query($que);
?>
<br><br>
<form name = "view" method = "POST" action ="cart.php">
<table align = 'center' width = '100%' border = '4'>
<tr>
<td colspan = '20' align = 'center'><h2> Viewing all the Products </h2></td>
</tr>

<tr align = 'center'>
<th>Item ID</th>
<th>Name</th>
<th>Price</th>
<th>Select</th>
</tr>

<tr align = 'center' class = "odd">
<?php

    while($row = mysql_fetch_array($run))
    {
        $i_id = $row['item_id'];
        $i_name = $row['item_name'];
        $i_price = $row['item_price'];
        ?>
<td><?php echo $i_id; ?></td>
<td><?php echo $i_name; ?></td>
<td><?php echo $i_price; ?></td>
<?php

$item = $i_name ." ". $i_price;  

?>                                                      
<td><input type="checkbox" name="addcart[]" value="<?php echo $item; ?> />Tick</td> 
</tr>
<?php  }?><input type = "hidden" name = "check">
<button type=  "submit" onclick = "location.href = 'cart.php';" id = "cart">Add to Cart</button> <?php }  ?>  
</table>