Skip to content
Advertisement

get the id of selected category php [closed]

<a href="report_product.php?id=<?php echo  $data['category']; ?>">

this line shows the name of category for example stationary..but this should be the id of selected category.is there any need to declare the id?$category_id = $_POST['category_id']; like this? i tried with this also but it gives an error of undefined index. through this link i want to show the list of products on another page associated with selected category i am on the learning phase and stuck on this issue kindly help me to sort out that

if(isset($_POST['search_category'])){ 
      // $category_id = $_POST['category_id'];   
      $records = mysqli_query($db,"SELECT DISTINCT category , 
      (SELECT COUNT(products.name) FROM products where category_id=categories.id) AS total_products,
      ( 
          SELECT SUM(quantity)  FROM productstock a
          LEFT JOIN products b ON a.product_id=b.id
          LEFT JOIN categories c ON b.category_id=c.id
          where c.deleted=0   AND b.category_id = categories.id
          GROUP BY category_id
      ) AS available_stock, 
      SUM(product_qty*orignalCost) AS SaleWise_cost,
      SUM(product_qty*saleprice) AS SaleWise_price,
      SUM(product_qty*saleprice) AS total_sale , 
      SUM((product_qty*saleprice)-(product_qty*orignalCost)) AS profit
               FROM categories
               INNER JOIN products ON  categories.id =  products.category_id
               INNER JOIN sales ON  sales.product_id = products.id
               INNER JOIN productstock ON  productstock.product_id = products.id
               WHERE categories.deleted=0
               GROUP BY category_id;"); // fetch data from database
      while($data = mysqli_fetch_array($records)) 
      {    
  ?>   
    <td><a href="report_product.php?id=<?php echo  $data['category']; ?>"><?php echo $data['category']; ?></a></td>   
    <td><?php echo $data['total_products']; ?></td>   
    <td><?php echo $data['available_stock']; ?></td>   
    <td><?php echo $data['SaleWise_cost']; ?></td>  
    <td><?php echo $data['SaleWise_price']; ?></td>  
    <td><?php echo $data['total_sale']; ?></td>  
    <td><?php echo $data['profit']; ?></td>  
    </tr> 
    <?php
      } }
    ?>
   </table>

Advertisement

Answer

You’re not selecting the category ID in you query add the ID column to the SELECT list.

For example, instead of

SELECT DISTINCT category, ...

Try doing

SELECT DISTINCT category, id ...

and then the ID should be returned in the data for use like…

<a href="report_product.php?id=<?php echo $data['id']; ?>"><?php echo $data['category']; ?></a>
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement