How to expand the list of categories imported from MySQL

I imported a set of different values ​​from two different tables into phpMyAdmin, one from the Category table (where cat_id is PK) and the other from the Item table; where (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>";
}
?>

      

Here's what I have so far:

Screenshot

Now how do I expand it? or, for example, how to show two items that are stored in the Clothing category on the same page or the next? Since this code helps me to view the number of items stored in the database,

Here is my form:

    <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++;
       }

      

I was thinking of using an array function to store the value inside the ie category 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>

      

The above image is the result of what I want, this character is specific to clothing only (2). I want it to change when the user clicks on something else like Shoes (1).

+1


source to share


2 answers


So after spending almost a day or two, I finally got my way out of this and I'm posting an answer here, so in case anyone else here looking for it.



<?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>

      

0


source


You have submitted the correct request to get all the information about the file. However, instead of getting the contents of each field, you chose to only count the number of affected rows. By using mysql_fetch_array()

, you actually get the content of each field and you can display the results.

Consider the following example based on your code:

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

is an array (which is associative and numerically indexed) containing all the data you are looking for. In my example, I am assuming the table items

has a field named item_name

and item_description

. Your table probably has different column names and you will need to change it to match the column names.



More information on mysql_fetch_array()

: http://php.net/manual/en/function.mysql-fetch-array.php

Finally, you should know that the extension has been mysql

deprecated since PHP 5.5. You should strongly consider switching to mysqli

or PDO

.

More information on mysqli

: http://php.net/manual/en/book.mysqli.php

More information on PDO

: http://php.net/manual/en/book.pdo.php

0


source







All Articles