Get the total from foreach loop + mysql while loop - php

I am getting the $_post array and running a query on each iteration, then trying to get the total points accummulated, it seems to be overwriting the total with the last point iteration. How do I fix this?
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$total_donations = $row['points'];
}
}
}
$full_total += $total_donations;
echo $full_total;

You have to insert the $full_total in the foreach loop like this
$full_total = 0;
foreach($postid as $key => $value){
$array = explode(',', $value);
if($value[0]!=''){
$id = $array[0];
$query = "SELECT * FROM products WHERE id = '$id'";
$result = mysqli_query($dbc, $query);
while ($row = mysqli_fetch_array($result)) {
echo '<tr valign="bottom">';
echo '<td>' . stripslashes($row['rangeCode']) . '-' . stripslashes($row['pointsType']) . '</td>';
echo '<td>' . stripslashes($row['category']) . '</a></td>';
echo '<td>' . stripslashes($row['itemDesc']) . '</a></td>';
echo '<td class="middle">' . stripslashes($row['points']) . '</a></td>';
echo '</tr>';
$full_total += $row['points'];
}
}
}
echo $full_total;

Related

PHP foreach loop doesnt itterate over objects, and won't start

Whenever i try the following loop
if (is_object($ueberweisung)) {
print_r($ueberweisung);
foreach ($ueberweisung as $item)
{
echo '<h1>test</h1>';
echo '<tr>';
//echo '<td>' . $item['ibansender'] . '</td>';
echo '<td>' . $item->$u->getBetrag() . '</td>';
echo '<td>' . $item->u->getBicempfaenger() . '</td>';
echo '<td>' . $item->u->getBicempfaenger() . '</td>';
echo '<td>' . $item->u->getZahlungsreferenz() . '</td>';
echo '<td>' . $item->u->getVerwendungszweck() . '</td>';
echo '<td>' . $item->u->getBetrag() . '</td>';
echo '<td>' . $item->u->getDatum() . '</td>';
echo '<td>';
echo '</tr>';
}
}
it wont jump into that for each loop even though it is definitely a object and is not null$, and i don't know why.
$ueberweisung is filled by another class which is connected to a mysql database
public static function getUberweisungperIban($iban)
{
$db = Database::connect();
$sql = "SELECT * FROM ueberweisung WHERE ibansender = ?";
$stmt = $db->prepare($sql);
$stmt->execute(array($iban));
// fetch dataset (row) per ID, convert to Credentials-object (ORM)
$ueberweisung = $stmt->fetchObject('Ueberweisung');
//$ueberweisung = $stmt->fetchAll();
//return $ueberweisung;
Database::disconnect();
//return $ueberweisung;
return $ueberweisung !== false ? $ueberweisung : null;
}

Invalid argument supplied for foreach php pdo in hostinger

i hope you understand my problem, i don't know to much english, but i'll try. I've a page in a hostinger and when i generate the code to show the rows, gives me this error:
Warning: Invalid argument supplied for foreach()
Connection code:
public static function conexion() {
try {
$con = new PDO('mysql:host=localhost;dbname=example', 'usuario', 'clave');
return $con;
} catch (PDOException $e) {
return false;
}
}
And:
include 'functions.php';
$con = Functions::conexion();
$sql = 'SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC';
foreach ($con->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['IDP'] . '</td>';
echo '<td>'. $row['Name'] . '</td>';
echo '<td>'. $row['Description'] . '</td>';
echo '<td style="color: #333;">'. $row['Price'] . '</td>';
echo '<td>' . '<a class="btn" href="update.php?id='.$row['IDP'].'">Edit</a>' . '</td>';
echo '</tr>';
But when i load in localhost there is not any problem, shows the rows.
That is because, you are still setting the host as
$con = new PDO('mysql:host=localhost;dbname=example', 'usuario', 'clave');
change host name to the hostingers provided name
$con = new PDO('mysql:host=hostinger_host_name;dbname=example', 'usuario', 'clave');
hope this helps
try this
<?php
$sql = $con->prepare("SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC'");
$sq->execute();
$results = $sql->fetchall(PDO::FETCH_ASSOC);
if (count($results > 0)) {
foreach ($con->query($sql) as $row) {
echo '<tr>';
echo '<td>' . $row['IDP'] . '</td>';
echo '<td>' . $row['Name'] . '</td>';
echo '<td>' . $row['Description'] . '</td>';
echo '<td style="color: #333;">' . $row['Price'] . '</td>';
echo '<td>' . '<a class="btn" href="update.php?id=' . $row['IDP'] . '">Edit</a>' . '</td>';
echo '</tr>';
}
}
?>
Add this line before foreach loop
$sql = 'SELECT * FROM Products p, Prices ps, ProductsPrices pc where p.IDP=pc.IDP AND ps.ID=pc.ID ORDER BY p.IDP ASC';
$pdoStatement = $con->query($sql);
$data = $pdoStatement->fetchAll();
foreach($data as $row){
//...
}

Returning records from database in to the table with php

I have this script where I am for first time returning records in to the table. It works quite fine but, it is returning only last added record. Can you look at it, because I cant find what is wrong.
$query = 'SELECT character_id, alias, real_name, alignment
FROM comic_character
ORDER BY ' . $order[$o];
$result = mysql_query($query, $db) or die (mysql_error($db));
if (mysql_num_rows($result) > 0) {
echo '<table>';
echo '<tr><th>Alias</th>';
echo '<th>Real Name</th>';
echo '<th>Alignment</th>';
echo '<th>Powers</th>';
echo '<th>Enemies</th></tr>';
$odd = true;
while ($row = mysql_fetch_array($result)) {
echo ($odd == true) ? '<tr class="odd_row">' : '<tr class="even_row">';
$odd = !$odd;
echo '</td><td>' . $row['alias'] . '</td>';
echo '<td>' . $row['real_name'] . '</td>';
echo '<td>' . $row['alignment'] . '</td>';
$query2 = 'SELECT power FROM comic_power p
JOIN comic_character_power cp
ON p.power_id = cp.power_id
WHERE cp.character_id = ' . $row['character_id'] . '
ORDER BY power ASC';
$result2 = mysql_query($query2, $db) or die(mysql_error($db));
if (mysql_num_rows($result2) > 0) {
$powers = array();
while ($row2 = mysql_fetch_assoc($result2)) {
$powers[] = $row2['power'];
}
echo '<td>' . implode(',', $powers) . '</td>';
} else {
echo '<td>none</td>';

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.

how to echo sum value in row

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.

Categories