Here is my code:
<table width="50%" border="2" bordercolor="green">
<tr>
<th>ItemName</th>
<th>Balance</th>
</tr>
<?php
if($qw="select DISTINCT(itemname) from details where DATE(date)<='$date' order by date desc"){
$qq = mysqli_query($con,$qw);
while($r=mysqli_fetch_array($qq,MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $r['itemname']; ?></td>
<td><?php echo $r['balance']; ?></td>
</tr>
<?php
}
}
?>
</table>
The given code is fetch product itemname but Balance is not fetch from the database.
Your query does not include the 'balance' field.
Change your query to include it:
select DISTINCT itemname, balance from details where ...
Related
I have obtained following table as a result of this MySQL statement
select entry.id as id,
entry.payorder,
entry.bank,
entry.PFMS_SCHEME,
entry.name,
entry.vendor,
tally_head.head,
ledgers.amount,
entry.po,
entry.Description,
bank_details.Name as name,
entry.sum as sum
from entry join bank_details on entry.name=bank_details.id
join ledgers on ledgers.id=entry.id
join tally_head on ledgers.ledger=tally_head.id
group by tally_head.head
result i got
These are the mysql tables from which i am retrieving data
entrybank tableledgertally
the final output i want
the view i want
The HTML and PHP Code I used`
<table class="table">
<thead>
<tr>
<th>Voucher No </th>
<th>Payorder </th>
<th>Bank </th>
<th>PFMS_SCHEME </th>
<th>Party Name </th>
<th> For</th>
<th> Po/agmt/inv N.o </th>
<th>Ledger</th>
<th>Ledger Amount</th>
<th>Description/ Narration</th>
<th>sum</th>
</tr>
</thead>
<tbody>
<?php
$sql=mysqli_query($con,"select entry.id as cid,entry.payorder,entry.bank,entry.PFMS_SCHEME,entry.name,entry.vendor,tally_head.head,ledgers.amount,entry.po,entry.Description,bank_details.Name as name,entry.sum as sum from entry join bank_details on entry.name=bank_details.id join ledgers on ledgers.id=entry.id join tally_head on ledgers.ledger=tally_head.id group by tally_head.head ");
$cnt=1;
while($row=mysqli_fetch_array($sql))
?>
<tr>
<td><?php echo htmlentities($row['cid']);?></td>
<td><?php echo htmlentities($row['payorder']);?></td>
<td><?php echo htmlentities($row['bank']);?></td>
<td><?php echo htmlentities($row['PFMS_SCHEME']);?></td>
<td><?php echo htmlentities($row['name']);?></td>
<td><?php echo htmlentities($row['vendor']);?></td>
<td><?php echo htmlentities($row['po']);?></td>
<td><?php echo htmlentities($row['head']);?></td>
<td><?php echo htmlentities($row['amount']);?></td>
<td><?php echo htmlentities($row['Description']);?></td>
<td><?php echo htmlentities($row['sum']);?></td>
<td>
</td>
</tr>
<?php
$cnt++;
?>
</tbody>
</table>
Trying to fetch all results in PHP from MySQL database, but it is leaving the first query.
My MySQL table is in the below image: .
My Code:
<?php
$irn = "33857";
$stmt = $user_home->runQuery('SELECT * FROM invoice WHERE Inv = :inv ORDER BY Sr ASC ');
$stmt->bindParam(':inv',$irn);
$stmt->execute();
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
?>
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="4" align="center">
<h1>Company</br><span style="font-size: 75%;">Number</span></h1>
</td>
</tr>
<tr>
<td colspan="2">
<h3><span style="float: left;"><?php echo $rowc['Customer']; ?> (<?php echo $rowc['Inv']; ?>)</span></h3>
</td>
<td colspan="2">
<h3><span style="float: right;"><?php echo $rowc['Date']; ?></span></h3>
</td>
</tr>
<tr>
<th>Sr.</th>
<th>Item</th>
<th>Qty</th>
<th>Amount</th>
</tr>
<?php
$i = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Item; ?></td>
<td>5</td>
<td>200</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><strong><?php echo getIndianCurrency(225); ?>Only</strong></td>
<td><strong>Total:</strong></td>
<td><strong>225</strong></td>
</tr>
</table>
<?php
}
?>
The result when I execute the above code is in the below image:
There are three queries with invoice number 33857, but only two are displayed (leaving the first one)!
I need all to be displayed as per invoice number.
Please help me from sorting out the error/code I made or I left.
The problem in your code is with the line:
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
in the beginning of your code you're fetching the first row.
You have to update your while loop, like this
<?php
$i = 0;
while($rowc)
{
extract($rowc);
$i++;
?>
<!-- Your HTML/PHP code for the table -->
<?php
$rowc=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
So, the idea is to fetch the row at the end of the while loop, in order to not lose your first row.
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'];
I have some issues with below script, essentially what I'm trying to achieve is to grab different product and prices and generate a table which works fine. However, some of the products do have an extra charge, in this case the product will use three rows (price, extra charge and total sum). I'm trying to get the IF statement to work as follows: if the extra charge = 0 then it should only make a single row in the table, if more then 0 it should produce the 3 row version.
Someone have any idea what I'm doing wrong? Thanks in advance!
<?php
$page=basename($_SERVER['PHP_SELF']); /* Returns PHP File Name */
$page_name=str_replace(".php","",$page);
mysql_connect(localhost,$dbuser,$dbpass);
#mysql_select_db($database) or die( "Unable to select database");
$query= ("SELECT * FROM table e
JOIN validdate1 r ON e.datevalid1=r.id
JOIN validdate2 d ON e.datevalid2=d.id
WHERE productpage='$page_name'
ORDER BY productname,price2");
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();?>
<table>
<thead>
<tr>
<th class="headerdesc">Description</th>
<th class="headerprice1 rates1">2015</th>
<th class="headerprice2 rates2">2016</th>
</tr>
</thead>
<?php $i=0;while ($i < $num)
{
$productname = mysql_result($result,$i,"productname");
$price1=mysql_result($result,$i,"price1");
$price2=mysql_result($result,$i,"price2");
$extracharge1=mysql_result($result,$i,"extracharge1");
$extracharge2=mysql_result($result,$i,"extracharge2");
$daterange1=mysql_result($result,$i,"daterange1");
$daterange2=mysql_result($result,$i,"daterange2");
if ($extracharge1 > "0") {
echo " <tr>
<td class="desc"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>
<tr>
<td class="extra">Extra Charge**</td>
<td class="price1 rates1">$<? echo $extracharge1; ?></td>
<td class="price2 rates2">$<? echo $extracharge2; ?></td>
</tr>
<tr class="lastrow">
<td class="totalprice"><strong>Total price</strong></td>
<td class="total rates1"><strong>$<? echo $price1+$extracharge1; ?></strong></td>
<td class="total rates2"><strong>$<? echo $price2+$extracharge2; ?></strong></td>
</tr>";
} else {
echo " <tr class="lastrow">
<td class="extra"><?php echo $productname; ?></td>
<td class="price1 rates1">$<? echo $price1; ?></td>
<td class="price2 rates2">$<? echo $price2; ?></td>
</tr>";
}
?>
<?php $i++;}?>
</table>
You have several error in code, change it like this, on the top below query
$num=mysql_numrows($result); will be
$num=mysql_num_rows($result);
don't close the connection since you are still performing queries below
//mysql_close(); comment it out and move it to bottom
and here you need this
if (mysql_num_rows($extracharge1) > 0 )
You are comparing string with a resource in your code
note: Don't use mysql_* functions its deprecated, use PDO or mysqli_*
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.