sql - Creating Stats page. Php -


i trying create stat page within website display products belonging trader, amount of each product sold , total price of product has been sold.

my code @ moment creates while loop loops through database, displays items multiple times (instead of once each product) , total amount of each product sold isn't set product, showing total amount every product sold.

here select statement:

      $querytest = "select * product                     inner join trader                     on product.stall_id=trader.stall_id                     inner join order_items                     on product.product_id=order_items.product_id                     trader.username='". $_session['username'] ."'";        $testresult = oci_parse($connection, $querytest);       oci_execute($testresult) 

here while loop. wish output products in database once. outputting them numerous times.

    while($row = oci_fetch_assoc($testresult)) {       $dname = $row ['name'];       $dprodesc = $row ['product_desc'];       $dimageid = $row['imageid'];       $dprice = $row['price'];        $price = $row['price'];        $prodord = $row['productquantity'];        $totalprodord = $totalprodord + $prodord;        $totalprodprice = $totalprodord * $dprice;        $quantity = $row['quantity'];        $subtotal = ($price * $quantity);        $total = $total + $subtotal;        $stock = $stock + $quantity;                  echo  '<td> <img src="' . $dimageid.  '" height="50" width="50"/></td>';             echo  '<td>' .$dname. '</td>';             echo  '<td>' .$dprodesc. '</td>';             echo '<td>' .$totalprodord. '</td>';             echo '<td>&pound; ' .$dprice.  '</td>';                   echo '<td>&pound; ' . $totalprodprice . '</td>';'</tr>';     }         ob_flush();       ?> 

my database tables:

create table trader(     trader_id number (5) not null,     trader_name varchar2 (20),     firstname varchar2 (10) not null,     lastname varchar2 (10) not null,     dob date not null,     username varchar2 (15) not null,     password varchar2 (32) not null,     phone varchar2 (10),     email varchar2 (30) not null,     stall_id number (5) references stall(stall_id),     primary key (trader_id));  create table product(     product_id number (5) not null,     name varchar2 (30) not null,     product_desc varchar2 (50),     imageid varchar2 (35),     price number(5,2) not null,     quantity number (10) not null,     stall_id number (5) references stall(stall_id),     product_type_id number (5) references product_type(product_type_id),     primary key (product_id));  create table order_items(     order_items_id number (5) not null,     totalprice number (5,2) not null,     productquantity number (5) not null,     day_id number (5) references collection_day(day_id),     time_id number (5) references collection_time(time_id),     customer_id number (5) references customer(customer_id),     product_id number (5) references product(product_id),     order_id number (5) references customer_order(order_id),     primary key (order_items_id)); 

can please me stats showing correctly?

thanks

you need sum numbers in query, or have sum numbers , skip identical products in code. doing neither.

try replace query this:

select prod.product_id, prod.name,  prod.quality-sum(orders.quality) stock_left,  avg(orders.price) sold_price,  sum(orders.price * orders.quality) sold_amount,  sum(orders.quality) total_sold  product prod inner join order_items orders on prod.prod_id = orders.prod_id  inner join trader  on prod.stall_id = trader.stall_id trader.username='". $_session['username'] ."'" 

you can rid of calculations in loop.


Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -