how to echo sum value in row - php

i want to echo sum of 1st script in to 2nd script
and both are same page i was try but not working
example
echo '<td>' . $row['status'] . '</td>';
echo '<td>' .$final_result4. '</td>';
how can i do this please help me to fix this issue thanks....
this is my 1st script
$res1 = (($basicsalary+$allowsalary)*$days*$yeardays1)/$yeardays/$monthdays;
$res2 = (($basicsalary1+$allowsalary1)*$days*$yeardays2)/$yeardays/$monthdays;
$res3 = (($basicsalary2+$allowsalary2)*$days*$yeardays3)/$yeardays/$monthdays;
$res4 = (($basicsalary+$allowsalary)*$days*$yeardays4)/$yeardays/$monthdays;
$res5 = (($basicsalary1+$allowsalary1)*$days*$yeardays5)/$yeardays/$monthdays;
$res6 = (($basicsalary2+$allowsalary2)*$days*$yeardays6)/$yeardays/$monthdays;
$res8 = 221/730*$miscamount +$otheramount;
$res7 = $startdays -$enddays;
$result1 = number_format((round($res1, 1)),3);
$result2 = number_format((round($res2, 1)),3);
$result3 = number_format((round($res3, 1)),3);
$result4 = number_format((round($res4, 1)),3);
$result5 = number_format((round($res5, 1)),3);
$result6 = number_format((round($res6, 1)),3);
$result7 = number_format((round($res7, 1)),3);
$result8 = number_format((round($res8, 1)),3);
$final_result = number_format(($result1 +$result2 +$result3 +$result4 +$result5 +$result6 ),3);
$final_result2 = number_format((round($final_result/730*$result7 ,1 )),3);
$final_result3 = number_format((round(($final_result2 +$result8) , 1)),3);
echo $final_result4 = number_format((round(($final_result -$final_result3) , 1)),3);
//Ending of php
?>
this is my 2nd script
echo "<span align='center' class='style2'>Over All Report For The Period Of $a to $b</span>";
echo "<div id='testTable' id='non-printable'><table class='hovertable' border='1' cellpadding='10'>";
echo "<tr> <th>Date</th><th>Id</th><th>Name</th> <th>Division</th><th>Payment</th><th>Status</th><th>Total</th></tr>";
// get results1 from database
$result1 = mysql_query("SELECT * FROM fullfsaccounts where date BETWEEN '$a' AND '$b' order by date ASC");
while($row = mysql_fetch_array($result1))
{
// echo out the contents of each row into a table
echo "<tr>";
echo '<td>' . $row['date'] . '</td>';
echo '<td>' . $row['cardno'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['division'] . '</td>';
echo '<td>' . $row['typecash'] . '</td>';
echo '<td>' . $row['status'] . '</td>';
echo '<td>' '</td>';
echo "</tr>";
//Increment the value of the Total_total variable
//by the salary value of one row till the while loop finishes
$Total_rows=$num=mysql_num_rows($result1);
$Total_total=$Total_total+$row['total'];
}
echo "</table>";
echo "<table class='hovertable' border='1' >";
echo "<tr width='100%'>";
echo '<td>Total</td>';
echo '<td>' . $Total_rows .'</td>';
echo '<td>' . $Total_total .'</td>';
echo "</tr>";
// close table>
echo "</table>";

in your code you have one basic error in line echo '' ''; your need
put echo '' . '';
I think you also do not explain well because if they are on the same page as you say you should not have any problems to call the value of the local variables of the page.
I advise you to put different -> echo 'pass'; or echo 'pass : '.resultxxx: in controls the flow of the page, I understand that if they are on the same page and do not get out and walk in it you should be able to pick up the value of the local variables without any problem.
Please explain more your problem if you need help.

Related

How to Use the If/else statement in PHP to filter mysql result

In the beginning, each person have 2 languages of messages (chimessage
& engmessage)saved to the database.
Each of them have selected a preferred language (either chi or eng).
In the display page, i'm trying to show each person's message with their preferred language.
eg, If person A prefer eng, the page will only show engmessage.
PS the display page will include both engmessage and chimessage for different people
I am wondering how can i do that, im thinking using the if/else statement.
PS I know iam using deprecated mysql,still trying to learn php, plz bear with me
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM table
WHERE language ='Chi' OR language ='Eng'");
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="200px">' . $row['people'] . '</td>';
echo '<td width="200px">' . $row['language'] . '</td>';
echo '<td>' . $row['chimessage'] ."<br />";
echo '<td>' . $row['engmessage'] ."<br />";
}
echo "</tr>";
echo "</table>";
?>
Expected result
People Language Msg
A Chi 物理治療
B Eng Physiotherapy
Change your code like this
<?php
include('connect-db.php');
$result = mysql_query("SELECT * FROM table
WHERE language ='Chi' OR language ='Eng'");
echo "<table>";
while($row = mysql_fetch_array( $result ,MYSQL_ASSOC)) {
echo "<tr>";
echo '<td width="100px">' . $row['id'] . '</td>';
echo '<td width="200px">' . $row['people'] . '</td>';
if($row['language'] == 'Chi')
{
echo '<td>' . $row['chimessage'] ."</td>";
}
else if($row['language'] == 'Eng')
{
echo '<td>' . $row['engmessage'] ."</td>";
}
echo "</tr>";
}
echo "</table>";
?>

when i search a letter with number it show error?

mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("corporate_personnel_order_for_2016") or die(mysql_error());
$search = $_GET['search'];
$result = mysql_query("SELECT * FROM obs WHERE OBS_NUMBER LIKE $search;") or die(mysql_error());
echo "<table><tr><th width=250>OBS Number</th><th width=250>Name</th><th width=250>Purpose</th><th width=250>Date of OB</th><th width=250>Destination</th></tr>";
while ($row = mysql_fetch_array($result)) {
echo '<table><tr>';
echo '<td width=250>' . $row['OBS_NUMBER'] . '</td>';
echo '<td width=250>' . $row['NAME'] . '</td>';
echo '<td width=250>' . $row['PURPOSE'] . '</td>';
echo '<td width=250>' . $row['DATE_OF_OB'] . '</td>';
echo '<td width=250>' . $row['DESTINATION'] . '</td>';
echo '</tr>';
echo "</table>";
when i search an entry with letter and number it cant shows an error but when I search a number only it shows to my table?
try this
$result = mysql_query("SELECT * FROM obs WHERE OBS_NUMBER LIKE '".$search."'") or die(mysql_error());
or this
$result = mysql_query("SELECT * FROM obs WHERE OBS_NUMBER LIKE '%".$search."%'") or die(mysql_error());

PHP While Loop: Second While Loop not showing data

I'm having trouble on While Loop. Here is my code:
<?php
//DISPLAY'S SKILL LIST
$showItemList = $con->query('SELECT * FROM items');
if ($showItemList->num_rows > 0) {
$x=1;
// output data of each row
while($row = $showItemList->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $x++ . '</td>';
echo '<td>' . $row['item_id'] . '</td>';
echo '<td>' . $row['item_name'] . '</td>';
echo '<td>' . $row['item_category'] . '</td>';
echo '<td>' . $row['item_costprice'] . '</td>';
echo '<td>' . $row['item_retailprice'] . '</td>';
echo '<td>' . $row['item_tax'] . '%</td>';
echo '<td>';
$showInHouseQty = $con->query('SELECT SUM(item_quantity) AS TotalQuantityInStock FROM item_inventory_inhouse WHERE item_id="'.$row['item_id'].'"');
while($row = $showInHouseQty->fetch_assoc()) {
echo $row['TotalQuantityInStock'];
}
echo '</td>';
echo '<td>';
$showPharmaQty = $con->query('SELECT SUM(item_quantity) AS TotalPharmaQty FROM item_inventory_inpharmacy WHERE item_id="'.$row['item_id'].'"');
while($row = $showPharmaQty->fetch_assoc()) {
echo $row['TotalPharmaQty'];
}
echo '</td>';
echo '</tr>';
}
}
?>
Well, the problem is, I can't get the result of $row['TotalPharmaQty'];
If you use the same variables in nested while loops as are in the parent while loop, they will overwrite. Change $row in the second and third while loops to $row2 and $row3 respectively, and it should work fine.

Mysqli_fetch_assoc not storing as a session variable

I am trying to store my outputted table from mySQL query into session variables and for use in another page. Here's what I have so far, but I do not get an output:
First page that displays the results of my search:
<?php
session_start();
$personid = ($_POST['personid']) ? $_POST['personid'] : $_GET['personid'];
if (empty($personid)) {
echo 'Please enter some search parameters';
} else {
$sql = "SELECT * FROM persons WHERE 1=1";
if ($personid)
$sql .= " AND personid='" . mysqli_real_escape_string($mysqli,$personid) . "'";
$total_records = mysqli_num_rows(mysqli_query($mysqli,$sql));
$sql .= " ORDER BY surname";
$loop = mysqli_query($mysqli,$sql)
or die ('cannot run the query because: ' . mysqli_error($mysqli));
echo "<table border='1' cellpadding='10'>";
echo "<tr> <th>Name</th> <th>Address</th> <th>City</th> <th>Province</th> <th>Postal Code</th> <th>Phone Number</th> <th>Email</th> <th></th></tr>";
while ($record = mysqli_fetch_assoc($loop)) {
echo "<tr>";
/* echo '<td>' . $record['firstname'] . $record['surname'] .'</td>';*/
echo "<td>$record[firstname] $record[surname]</td>";
echo '<td>' . $record['address'] . '</td>';
echo '<td>' . $record['city'] . '</td>';
echo '<td>' . $record['province'] . '</td>';
echo '<td>' . $record['postalcode'] . '</td>';
echo '<td>' . $record['phone'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo ("<td>Edit</td>");
echo "</tr>";
}
echo "</table>";
$_SESSION['animals']=$loop;
echo "<center>" . number_format($total_records) . " search results found</center>";
}
?>
Click here to see if sessions work
This then goes to this script:
<?php
session_start();
echo "The results";
while ($record = mysqli_fetch_assoc($_SESSION['animals'])) {
echo "<tr>";
echo "<td>$record[firstname] $record[surname]</td>";
echo '<td>' . $record['address'] . '</td>';
echo '<td>' . $record['city'] . '</td>';
echo '<td>' . $record['province'] . '</td>';
echo '<td>' . $record['postalcode'] . '</td>';
echo '<td>' . $record['phone'] . '</td>';
echo '<td>' . $record['email'] . '</td>';
echo ("<td>Edit</td>");
echo "</tr>";
}
What might the problem be? Thanks in advance.
Session are meant to store primitive data types.
Storing a resulstset into a session is a terrible idea and mostly likely bad for server ressources.
If you really need that data somewhere else in your app just query your database again which is not overly expensive.
I can see the problem that you are trying to store the mysqli resource object to the session.
$loop = mysqli_query($mysqli,$sql)
If you want to store the final html to the session, then create a single variable that holds final html string.
$html = "<table>";
while ($record = mysqli_fetch_assoc($loop)) {
$html .= $record['something'];
// Likewise add other variables to html string.
}
$html . = "</table>";
$_SESSION['animals'] = $html
But if you want the array that holds all the database result then you can do like this
$rows = array();
while ($record = mysqli_fetch_assoc($loop)) {
$rows[] = $record;
}
$_SESSION['animals'] = $rows;
Now you use this session value in another pages and iterate and create html.
Just as other website with session variables, using isset will make the script run smoothly
if (!isset($_SESSION['basket'])){
$_SESSION['basket'] = array();
}
Pascal
multiskillz

What is the wrong with this Mysql select ,php code?

When I tring to run this code any result not showing.Please help me
... some code is here...
$result = mysql_query("select * from dataform where date between '" . $d1 . "' and '" . $d2 . "'");
if (!$result) {
echo 'No result';
}
else{
echo $d1;
echo $d2;
echo "<table class=\"hovertable\">";
echo "
<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";
echo "<th>File No</th>";
echo "<th>Manufactor</th>";
echo "<th>Address</th>";
echo "<th>Supplier</th>";
echo "<th>Place Site</th>";
echo "<th>Tender Ref</th>";
echo "<th>Award No</th>";
echo "</tr>";
while ($row = mysql_fetch_array($result)) {
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";
echo "<th>" . $row["fileno"] . "</th>";
echo "<th>" . $row["manufacture"] . "</th>";
echo "<th>" . $row["address"] . "</th>";
echo "<th>" . $row["sup"] . "</th>";
echo "<th>" . $row["placesite"] . "</th>";
echo "<th>" . $row["tenderref"] . "</th>";
echo "<th>" . $row["awardno"] . "</th>";
echo "</tr>";
}
Try escaping your column DATE with backtick ` because it is a MySQL Data type. Another Suggestion is avoidusing string concatenation of your query because it is prone to mysql injection. Use PHP PDO or MYSQLi.
Example of using PDO:
<?php
$stmt = $dbh->prepare("SELECT * FROM dataform WHERE `date` BETWEEN ? AND ?");
$stmt->bindParam(1, $d1);
$stmt->bindParam(2, $d2);
$stmt->execute();
$result = $stmt->fetchAll(PDO::FETCH_ASSOC); //Fetch all results in form of associative array.
?>
Remember to always sanitize your inputs.
UPDATE 1
You are not checking for condition in your WHILE loop. The way you are doing now is you are assign a value which is wrong. Try using ==
instead of
while ($row = mysql_fetch_array($result))
{
}
change it to this
while ($row == mysql_fetch_array($result))
{
}

Categories