Switch mysql query to compare results - php

I have a table containing several columns, one of which is a date column (data1).
The mysql query used is
SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()
We then take the data from each row, run some calculations and store this as a separate variables.
I would now like to compare this data with data from last week (in the same table), i.e. by changing the SELECT query.
Let me expand...
Query for getting this weeks data from table:
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
Now we run through extracting the data
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
(the above is just a snippet of the calculations we need to run to demonstrate).
Now we display the results based on this weeks data
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
I'd like to now change the $sqld query above to select rows in the table that fall into last week, run the same calculations above and display the results below so we can compare the two.
<h4> Totals for last week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
Is there any way of achieving this without copying everything and changing the $sqld query?

I think you can create a function for repeting data and call it with some parameter to change the query like below
somefunction("Previous");
somefunction();
function somefunciton($query = "current") {
if($query == "Current")
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 1 WEEK) and now()";
else
$sqld = "SELECT * from leads WHERE data1 between date_sub(now(),INTERVAL 2 WEEK) and (now(),INTERVAL 1 WEEK)";
$result = mysql_query($sqld) or die(mysql_error());
$num_rows = mysql_num_rows($result);
while($row = mysql_fetch_array($result))
{
$referred = $referred + $row['referred'];
$invalidated = $invalidated + $row['invalidated'];
$tobequalified = $tobequalified + $row['tobequalified'];
}
<h4>Totals for this week</h4>
<table class="table stat-table">
<tbody>
<tr>
<td class="value"><? echo $num_rows; ?></td>
<td class="full">Total leads</td>
</tr>
<tr>
<td class="value"><? echo $referred; ?></td>
<td class="full">Referred</td>
</tr>
<tr>
<td class="value"><? echo $invalidated; ?></td>
<td class="full">Invalidated</td>
</tr>
<tr>
<td class="value"><? echo $tobequalified; ?></td>
<td class="full">To be qualified</td>
</tr>
</tbody>
</table>
}

$result1 = mysql_query("SELECT * FROM leads");
$result2 = mysql_query("SELECT * FROM other table");
$arr1 = new Array();
$arr2 = new Array();
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr1['FirstName'] = $row['your column name'];
}
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$arr2['FirstName'] = $row['your column name'];
}
now you have your two arrays($arr1 and $arr2) and you compare as per you want...

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");

How to show mysql multi row / mysql_fetch_array results in a table?

I am trying to show mysql_fetch_array() results in a table.
I want to show guests name,their country and their agreed time who are traveling on a same date.
Following code works fine. The code fetches the row values and prints it.
$select_guests = mysql_query('SELECT name FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_guests, MYSQL_ASSOC)) { //visitor / guest loop starts here
echo $row['name'].'<br/>';
}
$select_country = mysql_query('SELECT country FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_country, MYSQL_ASSOC)) { //country of visitor / guest loop starts here
echo $row['country'].'<br/>';
}
$select_agreed_time = mysql_query('SELECT agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting guests for the same date
while($row = mysql_fetch_array($select_agreed_time, MYSQL_ASSOC)) { //visitor / guest agreed time loop starts here
echo $row['agreed_time'].'<br/>';
}
if there are 5 guests for a same date I am getting all of their names one below another when I execute the above code. Same I am getting there countries and agreed time too.
Now I want to show those results in a HTML table.
I tried several line of code but nothing works.
My HTML table should be as following:
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td class="text-left">Name 1</td>
<td class="text-left">Country 1</td>
<td class="text-left">Ag Time 1</td>
</tr>
<tr>
<td class="text-left">Name 2</td>
<td class="text-left">Country 2</td>
<td class="text-left">Ag Time 2</td>
</tr>
<tr>
<td class="text-left">Name 3</td>
<td class="text-left">Country 3</td>
<td class="text-left">Ag Time 3</td>
</tr>
<tr>
<td class="text-left">Name 4</td>
<td class="text-left">Country 4</td>
<td class="text-left">Ag Time 4</td>
</tr>
<tr>
<td class="text-left">Name 5</td>
<td class="text-left">Country 5</td>
<td class="text-left">Ag Time 5</td>
</tr>
</tbody>
</table>
How can create that table td s according to my mysql_fetch_array() ?
The above table structure is for 5 guests found or resulted by mysql_fetch_array()
First of all I think you dont need 3 different queries for your solution..
<table class="table-fill">
<thead>
<tr>
<th class="text-left">Name</th>
<th class="text-left">From</th>
<th class="text-left">Agreed Time</th>
</tr>
</thead>
<?php
$result = mysql_query('SELECT name,country,agreed_time FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
?>
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
<?php
}
?>
</table>
Firstly, you should use mysqli.
Secondly, shouldn't be sending so many queries to the database for information you can get in one query;
$select_guests = $mysqli->query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error());
Next, you want to fetch the number of rows.
$rows = $mysqli
You should also look into the PHP for function;
for ($i = 1; $i < $rows; $i++) {
$thisRow = $select_guests->fetch_row()
echo
' <tr>
<td class="text-left">'.$select_guests['name'].'</td>
<td class="text-left">'.$select_guests['country'].'</td>
<td class="text-left">'.$select_guests['time'].'</td>
</tr>
'; //This last line is to insert a line break and indent (for tidy HTML)
}
Give this a go, hopefully I've helped you.
I haven't completely solved it for you though, in order to change over to mysqli, you will need to make a few small changes which you can find in the mysqli link I sent you. The benefits are worth it.
$select_all = mysql_query('SELECT * FROM van_sharing WHERE date = "'.$serch_text.'"') or die(mysql_error()); // query for getting all details for the same date
while($row = mysql_fetch_array($select_all , MYSQL_ASSOC)) {//loop starts here
<tr>
<td>
<?php echo $row['name']; ?>
</td>
<td>
<?php echo $row['country'];?>
</td>
<td>
<?php echo $row['agreed_time']; ?>
</td>
</tr>
}

Handling three while loops in PHP MYSQL

I'm new to PHP and struck with a while loop. Please throw some lights here.
I'm showing some information related to books vs author vs isbn number.
There are two tables: book and author. The book name and ISBN number comes from book table. The author name comes from author table.
Here is my poor php code(please dont laugh)
<?php
mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("test") or die(mysql_error());
$result1 = mysql_query("SELECT book_name FROM book where status='1'") or die(mysql_error());
$result2 = mysql_query("SELECT FName FROM author") or die(mysql_error());
$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
?>
<table style="margin-top:40px" border="1" width="100%">
<tr style="background-color:#909F51">
<td>Book List</td>
<?php while($row1 = mysql_fetch_array( $result1 )) { ?>
<td><?php echo $row1['book_name']; ?></td>
<?php } ?>
</tr>
<?php while($row2 = mysql_fetch_array( $result2 )) { ?>
<tr>
<td><?php echo $row2['author_name']; ?></td>
<?php while($row3 = mysql_fetch_array( $result3 )) { ?>
<td><?php echo $row3['book_isbn_number']; ?></td>
<?php } ?>
</tr>
<?php } ?>
</table>
I'm not able to loop through the third while condition. I know the basic code structure is wrong.
Could someone help me out in getting the result.
Tables:
book
b_id
book_name
isbn_number
status
author
a_id
author_name
Expected Output:
<table width="100%" border="1">
<tr>
<td></td>
<td>Author Name 1</td>
<td>Author Name 2</td>
</tr>
<tr>
<td>Book Name 1</td>
<td>ISBN Number 1</td>
<td>ISBN Number 2</td>
</tr>
<tr>
<td>Book Name 2</td>
<td>ISBN Number 3</td>
<td>ISBN Number 4</td>
</tr>
</table>
You are using $row3['isbn_number'] when the column you are selecting is "book_isbn_number". Try changing that line to be <td><?php echo $row3['book_isbn_number']; ?></td>. The loops themselves, while using the old, deprecated mysql_* functions, should be working fine.
<td><?php echo $row3['isbn_number']; ?></td>
replace it with
<td><?php echo $row3['book_isbn_number']; ?></td>
$result3 = mysql_query("SELECT book_isbn_number FROM book where status='1'") or die(mysql_error());
Should be
$result3 = mysql_query("SELECT isbn_number FROM book where status='1'") or die(mysql_error());**
OR
<td><?php echo $row3['isbn_number']; ?></td>
Should be
<td><?php echo $row3['book_isbn_number']; ?></td>
Whichever fits your need

Auto Increase in PHP for HTML Table

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!

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