I'm looking to upload a cvs into a db and then find how many times an instance of data appears to create a pick list.
I upload a cvs in to the db leaving me with
SKU and QUANTITY
I use to get the data from the db but I cant seam to find a way to group that data so their is only 1 sku for each item and a number of ordered items.
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("select * from pickcount");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['quan']; ?></td>
</tr>
<?php
}
?>
</table>
<table border="1" width="100%" id="table1">
<?php
$query = mysql_query("SELECT sku, SUM(quan) AS Sum_Of_Quan FROM pickcount GROUP BY sku");
while($fetch = mysql_fetch_array($query))
{
?>
<tr>
<td><?php echo $fetch['sku']; ?></td>
<td><?php echo $fetch['Sum_Of_Quan']; ?></td>
</tr>
<?php
}
?>
</table>
Try to replace your table structure with above structure and check the result
Replace your sql query to:
select sku, sum(quan) as sum_quan
from pickcount
group by sku
PS - you'll have to change
php echo $fetch['quan'];
to
php echo $fetch['sum_quan'];
Related
I have a mysql table with orders. I am trying to loop through the orders table and select individual client orders according to their user id and display the orders on their client accounts. However, my code below just prints the first row and repeats it endlessly jaming my browser every time.
what is wrong with this and how to i solve it
<?php
$records = $conn->prepare('SELECT * FROM orders WHERE user_id= :id');
$records-> bindParam(':id', $_SESSION['user_id']);
$records->execute();
$results=$records->fetch(PDO::FETCH_ASSOC);
$i=0;
while($i<=$results):
$i++;
?>
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td>#SJ<?=$results['id']; ?> </td><td><?=$results['academic_level']; ?></td><td ><?=$results['details']; ?></td>
</tr>
</table>
<?php
endwhile;
?>
Remove all $i related code. Just move your fetch statement to while condition, like the following:
while( $results=$records->fetch(PDO::FETCH_ASSOC))
You need to include the html code in the while loop because you want to display all of the orders.
I'm using this method, try this out.
Start while in first php section
<?php
$username = $_SESSION['username'];
$sql = $db->prepare("SELECT * FROM tableName WHERE username = '$username' ");
$sql->execute();
while($results=$sql->fetch(PDO::FETCH_ASSOC)){
?>
After that the html section coming:
<h3>Your Orders</h3>
<table >
<tr >
<th>Order Number</th><th>Academic Level</th><th>Order Details</th>Manage Order</th>
</tr>
<tr>
<td><?php echo $results['id']; ?> </td><td><?php echo $results['academic_level']; ?></td><td ><?php echo $results['details']; ?></td>
</tr>
</table>
and here the ending
<?php
}
?>
I'm trying to generate a list from database in a HTML table just like image below;
https://i.stack.imgur.com/61XLl.png
And here's what i did;
https://i.stack.imgur.com/lLsvF.png
And the code;
<table cellpadding="3" border="1" style="width:100%;margin-top:30px; margin-bottom:50px; font-size:12px">
<thead>
<tr>
<th>KURSUS</th>
<th rowspan="2">NAMA PENSYARAH</th>
<th rowspan="2">NO. SIRI</th>
</tr>
<tr>
<th>NAMA</th>
</tr>
</thead>
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();?>
<td><?php echo $rowname['user_name']; ?></td>
<td><?php echo $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']; ?></td>
<?php } ?>
</tr>
<?php
}
}
?>
</tbody>
</table>
Help me. Thank you in advance :)
Use this code. Concat user names and code with "br" tags in the second while loop and display them in "tds" after while loop.
<tbody align="center">
<?php
if($numrow>0)
{
while($row = $select->fetch_assoc()){
$code=explode("/",$row['po_code']);
$list=$connect->query("SELECT * FROM polist WHERE polist_poid='".$row['po_id']."' ORDER BY polist_bil ASC");
?>
<tr>
<td><?php echo $row['po_name']; ?></td>
<?php
$user_names = $codes = ''; // define empty variables
while($rowlist = $list->fetch_assoc()){
$name=$connect->query("SELECT * FROM user WHERE user_id='".$rowlist['polist_userid']."'");
$rowname=$name->fetch_array();
$user_names .= $rowname['user_name']."<br/>"; //concat to a single string
$codes .= $code[0]."/PO/".$code[1]." - ".$rowlist['polist_bil']."<br/>"; //concat to a single string
}?>
<td><?php echo $user_names;?></td>
<td><?php echo $codes;?></td>
</tr>
<?php
}
}
?>
</tbody>
Put the <td> outside the <?php while($rowlist = $list->fetch_assoc()){
Or get all your data before you start display html and store it in a multi-dimensional array. Then simply loop through the data array. That way you won't have as much php mixed with html also.
I am creating a php script to generate reports based on data stored in SQL, The query is here:
$sql = $adb->query("SELECT firstname, lastname, policynumber, isatype, isaname, startdate, unitvalue, numberofunits, currentamount, date, newunitvalue, newnumberofunits, newcurrentamount
FROM vtiger_isa, vtiger_addisa, vtiger_contactdetails
WHERE vtiger_isa.relatedclient = vtiger_addisa.addrelatedclient
AND vtiger_addisa.addrelatedclient = vtiger_contactdetails.contactid
AND vtiger_isa.relatedclient = $clientid
AND vtiger_isa.policynumber = $polnum
AND vtiger_addisa.addrelatedclient = $clientid
AND vtiger_addisa.newpolicynumber = $polnum
ORDER BY date ASC"
);
This performs fine as I have tested by using print_r($sql); and the results I want are there. Although when I am looping through the results they do not show. I have tested with different clientid's and the first result seems to missed out.
<b> New Figures:</b>
<table style="width:100%">
<tr>
<th>Date</th>
<th>Unit Value (P)</th>
<th>Number of Units</th>
<th>Total Value (£)</th>
</tr>
<?php
while ($sql->fetchInto($row)) {
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}?>
</table>
</body>
</html>
I am not using the fetchInto() method. I using the following example:
<?php
$sql = $adb->query("...");
foreach($sql as $row){
?>
<tr>
<td><?php echo $row['date'];?></td>
<td><?php echo $row['newunitvalue'];?></td>
<td><?php echo $row['newnumberofunits'];?></td>
<td><?php echo $row['newcurrentamount'];?></td>
</tr>
<?php
}
?>
This is working every time. Try it out.
i am developing a off-line chat application, i have two table 1. user details (cli_id,email, User name ) 2. chat table (c_from, c_to, subject, matter, image) now the problem is that i am taking the cli_id from the user table as from and to but when fetching the query it return a single row, my code looks like this
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($sql))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
You're overwriting $sql inside the loop, which replaces the result set in your outer loop with a result set which is already "emptied" by the time the code execution returns to the outer loop.
$sql variable changed inside the while loop. Use a different variable here:
$sql=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
try this :
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$selectChat=mysql_query("SELECT * FROM `chat` ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($selectChat))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?></td>
<td><?php echo $row['matter']; ?></td>
<td><?php
$chat_to =$row['c_to'];
$selectClient=mysql_query("SELECT * FROM `client` WHERE cli_id = $chat_to")or die(mysql_error());
while($qry=mysql_fetch_array($selectClient))
{
echo $qry['email'];
}
?></td>
</tr>
<?php } ?>
</table>
It is probably better for you to use a join in order to minimize the amount of database requests, this will also reduce the need for you to have a second query loop inside the first loop. Try the following code
<table width="100%" border="0">
<tr>
<td width="16%"><strong>From</strong></td>
<td width="23%"><strong>Subject</strong></td>
<td width="40%"><strong>Matter</strong></td>
<td width="21%"><strong>To</strong></td>
</tr>
<?php
include('connect.php');
$sql=mysql_query("SELECT * FROM `chat` LEFT JOIN 'client' on 'chat.c_to = client.cli_id' ORDER BY chat_id DESC")or die(mysql_error());
while($row=mysql_fetch_array($sql))
{
?>
<tr>
<td><?php echo $row['c_from']; ?></td>
<td><?php echo $row['subject']; ?> </td>
<td><?php echo $row['matter']; ?></td>
<td><?php echo $row['email'];?></td>
</tr>
<?php } ?>
</table>
You must need to rewrite the while statement that appears immediately after the main query
while($row=mysql_fetch_array($sql))
as
while($row=mysql_fetch_row($sql))
Hope this might help you.
Needed Display
Here my code and display
<table width="100%" cellspacing="0" cellpadding="0" summary="" id="box-table-a">
<thead>
<tr>
<th width="" scope="col"><strong>Item Description</strong></th>
<th scope="col" style="text-align:center;"><strong>Quantity</strong></th>
<th width="350" scope="col"><strong>Supplier Name</strong></th>
<th scope="col" style="text-align:center;"><strong>Unit Price Rs.</strong></th>
<th scope="col" style="text-align:center;"><strong>VAT Price Rs.</strong></th>
<th width="100" scope="col"><strong>Total Price Rs.</strong></th>
</tr>
</thead>
<tbody>
<?php
$get_data =mysql_query("SELECT supplier_add_quotaion_form.quotaion_request_id,supplier_add_quotaion_form.supplier_id,supplier_add_quotaion_form.supplier_add_quotaion_id,supplier_add_quotaion_request_item.* FROM supplier_add_quotaion_request_item,supplier_add_quotaion_form
WHERE supplier_add_quotaion_form.supplier_add_quotaion_id=supplier_add_quotaion_request_item.supplier_add_quotaion_id AND supplier_add_quotaion_form.quotaion_request_id='$id' ORDER BY quotation_item_id");
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<td><?php echo $item_description; ?></td>
<td align="center"><?php echo $quantity_required; ?></td>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
}
?>
</tbody>
When Duplicate description coming need to rowspan them or what ever method need to show display as first image..i try it get count and then rowspan according to it.but unable to get need display,don't know how to delete extra cell
Duplicate row count not fixed,only example show first image,it can be range 1-7 duplicate for one description(for one item 7 suppliers able to bids)
supplier_add_quotaion_form table structure
supplier_add_quotaion_request_item table structure
clerk_add_quotaion_request_item structure
You can do this in one pass,try this
<?php
$get_data =mysql_query("...");
$last_quotaion_request_id = -1;
while($row = mysql_fetch_array($get_data)){
$quotaion_request_id = $row['quotaion_request_id'];
$supplier_add_quotaion_id = $row['supplier_add_quotaion_id'];
$supplier_id = $row['supplier_id'];
$net_item_value = $row['net_item_value'];
$vat_item_value = $row['vat_item_value'];
$total_value = $row['total_value'];
$quotation_item_id = $row['quotation_item_id'];
$get_count = mysql_query("SELECT quotation_item_id FROM supplier_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
$count = mysql_num_rows($get_count);
$get_quantity = mysql_query("SELECT quantity_required,item_description FROM clerk_add_quotaion_request_item WHERE quotation_item_id='$quotation_item_id'");
while($rowB = mysql_fetch_array($get_quantity)){
$quantity_required = $rowB['quantity_required'];
$item_description = $rowB['item_description'];
}
?>
<tr>
<?php if($last_quotaion_request_id != $quotaion_request_id){ ?>
<td rowspan="<?php echo $count; ?>"><?php echo $item_description; ?></td>
<td rowspan="<?php echo $count; ?>" align="center"><?php echo $quantity_required; ?></td>
<?php } ?>
<td><?php echo $supplier_id; ?></td>
<td align="center"><?php echo $net_item_value; ?></td>
<td align="center"><?php echo $vat_item_value; ?></td>
<td align="center"><?php echo $total_value; ?></td>
</tr>
<?php
$last_quotaion_request_id = $quotaion_request_id;
}
?>
It'll probably be FAR easier to slurp your query results into a structured array, from which you can then easily retrieve the necessary counts to do your rowspans:
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[$row['item_description']][] = $row;
}
echo "start your table here";
foreach($data as $description => $items) {
echo "<tr><td rowspan=" . count($items) . ">";
foreach($items as $item) {
output item data here
}
}
This won't work as is, but should give you an idea of how to go about it.
you need to change the structure of table you are using. as per my guessing I'm giving a suggestion. keep the item description in a table and the quotation details in another table. and while fetching the data you can fetch using group by sql command.
your item table may be like this:
1. id
2. desc
3. clicks
and quotation table may be like this
1. id
2. item_id
3. supplier
4. quantity
5. vat
6. price
etc.