Jump to content

Recommended Posts

I having an issue with a query output where is only outputing one product instead of all the products matching WHERE shelf='KDR'. Here is the code

<?php include_once('../../header.php'); ?>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script src="https://code.highcharts.com/highcharts.js"></script>
<script src="https://code.highcharts.com/highcharts-3d.js"></script>
<script src="https://code.highcharts.com/modules/exporting.js"></script>
<script type="text/javascript">
$(function () {
    $('#KDR').highcharts({
        chart: {
            type: 'pie',
            options3d: {
                enabled: true,
                alpha: 45
            }
        },
        title: {
            text: 'Current Inventory'
        },
        plotOptions: {
            pie: {
                innerSize: 100,
                depth: 45
            }
        },
        series: [{
            name: 'In Stock',
            data: [   
              <?php
$query_products = mysql_query("SELECT * FROM product, product_amount WHERE shelf='KDR'");
while($list_products = mysql_fetch_assoc($query_products))
{
$products['id'] = $list_products['id'];
$products['status'] = $list_products['status'];
$products['code'] = $list_products['code'];
$products['name'] = $list_products['name'];
        $products['amount'] = $list_products['amount'];
     $dump = array();
}
 
    $result1= $products['name'];
    $result2= $products['amount']
    ?>

['<?php echo $result1?>', <?php echo $result2?>], ] }] }); }); </script> <div class="row"> <H1>KDR</H1> <div id="KDR" style="width:50%; height:400px;"> </div> </div> <?php include_once('../../footer.php'); ?>

he querry appears to work, but when I echo the result I get:
['product 1 name', 'product 1 amount' ],

instead of :

['product 1 name', 'product 1 amount' ],
['product 2 name', 'product 2 amount' ],
['product 3 name', 'product 3 amount' ],
['product 4 name', 'product 4 amount' ],
['product 5 name', 'product 5 amount' ],
['product 6 name', 'product 6 amount' ],

 

attached are images of the tables structure

any ideas?

 

post-199776-0-15040800-1469632788_thumb.png

post-199776-0-76044900-1469632789_thumb.png

Link to comment
https://forums.phpfreaks.com/topic/301658-mysql-query-output/
Share on other sites

Every time the loop executes, the $products variable is being overridden. So the output will only contain the last record. You could try something like this:

$products = array();
while($list_products = mysql_fetch_assoc($query_products))
{
    $products[]['id'] = $list_products['id'];
    $products[]['status'] = $list_products['status'];
    $products[]['code'] = $list_products['code'];
    $products[]['name'] = $list_products['name'];
    $products[]['amount'] = $list_products['amount'];
    $dump = array();
}
 
And then use a foreach loop to output the products.
Link to comment
https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535161
Share on other sites

You need to look at your code. You move the fetched array into another array. And Repeat.

 

Why?

 

If you want to output the results, then output them instead of just copying them to the same array over and over and over again...

 

...assuming that's all the OP wants to do. But good point.

Link to comment
https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535165
Share on other sites

There are two other main interfaces that are part of PHP. The mysqlI one and PDO. Many people prefer PDO which has a small learning curve but is easy once you've used it a couple fo times, IMHO.

 

As told, the MySQL interface is so old that PHP has stopped supporting it since php 7. Use it at your own risk because once your host moves to that level your code will no longer work.

Link to comment
https://forums.phpfreaks.com/topic/301658-mysql-query-output/#findComment-1535177
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.