Auto Increase in PHP for HTML Table - php

I'm trying to make a results table.
Here is a image of what the table looks like so far:
<?php
$row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");
while($sql = mysql_fetch_assoc($row)){
?>
<tr>
<td width="5%" align="center"><?php $rank=1; echo $rank;?></td>
<td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
<td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
</tr>
<?php } ?>
That is the code so far. I am trying to make it so the rank will auto increase. By the way, the rank is not coming from the database, but i just want it to start at 1 then increase. Please help me.

<?php
$row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");
$rank = 0; // default rank
while($sql = mysql_fetch_assoc($row))
{
$rank += 1; // increase
?>
<tr>
<td width="5%" align="center"><?php echo $rank;?></td>
<td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
<td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
</tr>
<?php } ?>

Your problem is the $rank variable is declared in the while loop
<td width="5%" align="center"><?php $rank=1; echo $rank; //this line ?></td>
So you need to move it out of the loop, because it will set to 1 again when the loop continue (that why is always display 1)
To fixed the problem you should do this
<?php
$row = mysql_query("SELECT * FROM table ORDER BY Votes DESC LIMIT 5");
$rank = 1; // declare rank here
while($sql = mysql_fetch_assoc($row)){
?>
<tr>
<td width="5%" align="center"><?php echo $rank++; // increase rank by 1 each times the loop run ?></td>
<td width="15%" align="center"><img src="/pictures/<?php echo $sql['Picture']; ?>.png" height="50%"></td>
<td width="7%" align="center"><?php echo $sql['Votes']; ?></td>
</tr>
<?php } ?>
Hope this help!

Related

how to stop loop from table which select from query?

image
Hello guys. I wanna ask how to minimize the second table?
because i wanna it just printed once no loop but i want all the value to compare with the first table
here is my code
<?php
$query1 = mysql_query("SELECT * FROM training_detail AS s JOIN subject AS t JOIN training AS u JOIN employee AS v WHERE v.id_employee = $id1 AND s.nik LIKE v.nik AND u.id_subject LIKE t.id_subject AND s.id_training LIKE u.id_training ");
$i=1;
while($row1 = mysql_fetch_array($query1))
{
$date = $row1['date'];
$subject1 = $row1['subject_name'];
?>
<table class="table table-bordered">
<tr>
A
<td class="table-bordered">No</td>
<td class="table-bordered">date</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject no</td>
<td class="table-bordered">revision no</td>
<td class="table-bordered">Trainer</td>
<td class="table-bordered">Institution</td>
</tr>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo date("j/F/Y", strtotime($date)); ?></td>
<td class="table-bordered"><?php echo $subject1; ?></td>
<td class="table-bordered"><?php echo $row1['subject_no']; ?></td>
<td class="table-bordered"><?php echo $row1['revision_no']; ?></td>
<td class="table-bordered"><?php echo $row1['trainer']; ?></td>
<td class="table-bordered"><?php echo $row1['institution']; ?></td>
</tr>
</br>
<table class="table table-bordered">
<tr>
B
<td class="table-bordered">No</td>
<td class="table-bordered">subject</td>
<td class="table-bordered">subject name</td>
<td class="table-bordered">subject no</td>
</tr>
<?php
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC ");
$x=1;
while($row2 = mysql_fetch_array($query2))
{
$subject2 = $row2['subject_name'];
if (strcasecmp($subject1, $subject2) != 0)
{
?>
<tr>
<td class="table-bordered"><?php echo $i; ?></td>
<td class="table-bordered"><?php echo $row2['subject'] ?></td>
<td class="table-bordered"><?php echo $subject2; ?></td>
<td class="table-bordered"><?php echo $row2['subject_no']; ?></td>
</tr>
<?php
}
$x++;
}
$i++;
}
?>
</table>
</table>
sorry about my pic's words is my own language
if you want to minimize the output
try to use limit
like so:
$query2 = mysql_query("SELECT * FROM header_job AS r JOIN subject AS q JOIN employee AS p WHERE q.id_subject LIKE r.id_header AND r.id_job LIKE p.id_job AND p.id_employee = $id1 ORDER BY q.id_subject ASC limit 1");

MySQL query - IF statement to generate two different rows

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_*

Force table to display 5 rows with or without data

I have to display a table of 5 rows fetching from MySQL database using PHP. From the code below, if I have 3 rows in database, it will display only 3 rows. But, I need to display 5 rows with 2 empty rows along with 3 fetched rows. If there are no records found in the database, I should display 5 empty rows. I need your help on that.
Note: I am generating a report using PHP and MySQL. From the above method, I can fix the table height and so report will generate without any overlaps.
CODE:
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}?>
As you are already counting the $row_count variable, you can add simple while loop, like this:
<?
while($row_count < 5){
?>
<tr>
<td > </td>
<td > </td>
<td > </td>
<td > </td>
</tr>
<?php $row_count++;
}?>
What is ? Is it needed?
Also check the #paxdiablo's answer about limit 5 option for your query.
This should do the trick.
<?php
$select= "select * from table where id=1";
$select2= mysql_query($select);
$select3= mysql_num_rows($select2);
$row_count = 1;
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr>
<?php $row_count++;
}
if ($row_count < 5){
for ($i=1; $i <= (5-$row_count); $i++){
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
<?php
}
}
?>
You appear to have two distinct problems (albeit related).
If you only want five rows even if your query returns twenty, you can either use the control variable $row_count to only output rows for the first five, or (preferably) just add limit 5 on to your query to get five rows or less.
The second problem is what to do if it returns less than five rows. In that case, use the control variable to output blank rows, something like adding the following to the end:
<?php
while ($row_count < 5) {
?>
<tr>
<td> </td>
<td> </td>
<td> </td>
<td> </td>
</tr>
<?php
$row_count++;
}
?>
while($row = mysql_fetch_assoc($select2)){
?>
<tr>
<td ><?php echo $row_count;?>.</td>
<td ><?php echo $rows['id']; ?></td>
<td ><?php echo $rows['name']; ?></td>
<td ><?php echo $rows['phone_number']; ?></td>
</tr> <?php }
if($select3<5){
for($i=1;$i<5-$select3;$i++){?>
<tr>
<td ></td>
<td ></td>
<td ></td>
<td ></td>
</tr>
<?php }
}

Wht the php query return a single row

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.

How remove rows in loop or any other method to get diplay as below

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.

Categories