hello guys i need to find the differences between two different loops which is summation loop and totals loop as shown in controller code below
function summary() {
echo '<table>';
$summation=$this->select_model->sum_income("date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($summation as $sum){
echo '<tr><td>Total Income</td> <td >'.$sum['total'].'</td></tr>';
}
$totals=$this->select_model->sum_expenditure("date_of_expenditure BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($totals as $total){
echo '<tr><td>Total Expenditure</td><td ><strong>'.$total['total'].'<</td></tr>';
}
//display differences here:
echo '<tr><td>'.**$sum['total']-$total['total']**.'</td></tr>'
echo '</table>';
}
You would have to create the variables outside of the loops and add (or subtract) as the loop progresses.
$sum = 0;
$total = 0;
foreach... {
$sum += $sum;
}
foreach... {
$total += $total;
}
echo $sum - $total;
If you're adding negative numbers, the math will work without having to do anything specific.
Related
I'm probably making a very basic error but I'm quite new to this.
I have a table where I need to edit what is displayed in each box using variables but I'm having trouble with getting the outputs into the table. Experimentation helped me work out the first box but I can't get the second one working because I think the function is written incorrectly. I need a conditional loop that displays all even numbers between 10 and 20 (the code below doesn't have anything to do with even numbers at the moment I'm just trying to get it to work)
<?php
$random = rand() . srand(3034);
function loop() {
for ($i = 10; $i <= 20; $i++) {
$loop = $i;
return $loop;
}
}
echo "<table border='1'>
<tr>
<td>Box 1 - ".$random."</td>
<td>Box 2 - ".$loop."</td>
</tr>
</table> ";
?>
Any help is much appreciated.
You need to loop on the tags itself, because the return condition in the loop breaks it to only 1 iteration.
So you should do it like this:
echo "<tr>
<td>Box 1 - ".$random."</td>";
for ($i = 10; $i <= 20; $i++) {
echo "<td>Box 2 - ".$i."</td>";
}
echo"</tr>";
Additionally to my comment here is some more code:
<?php
$random = rand() . srand(3034);
function loop($randomNumber) {
for ($loop = 10; $loop <= 20; $loop++) {
echo
'<tr>' .
'<td>Box 1 - ' . $randomNumber . '</td>' .
'<td>Box 2 - ' . $loop . '</td>' .
'</tr>';
}
}
echo
'<table border="1">' .
loop($random) .
'</table>';
Try this:
<?php
function loop(){
$return = '';
for($i = 10; $i <=20; $i++){
$random = rand() . srand(3034);
if($i%2==0){
$return.='<tr>
<td>Box 1 - '.$random.'</td>
<td>Box 2 - '.$i.'</td>
</tr>';
}
}
return $return;
}
echo '<table>'.loop().'</table>';
For example :
$monthly_bill = 700
$paid_amount = 2100
Now I want to use a php loop which will print me 700, 700 and 400 to each line. How can I do this ?
I am doing following code but not working :
<?php
$paid_amount = 1800;
$monthly_bill = 700;
$counting_month = round($paid_amount / $monthly_bill);
for ($x=1; $x<=$counting_month; $x++) {
echo $insert = $paid_amount - $monthly_bill;
$paid_amount = $insert;
echo '<br/>';
}
Don't use for loop. Those run a fixed number of times, and your loop would keep running until well after the money runs out.
Use a do instead, and have its termination clause be when the money runs out:
$remaining = 1800;
$monthly = 700;
do {
echo $monthly;
$remaining -= $monthly;
if ($remaining < $monthly) {
$monthly = $remaining;
}
} while ($remaining > 0);
<?php
$amount = 1800;
$monthlybill = 700;
for ($i=0; $i<$amount/$monthlybill; $i++) {
echo "paid for " . ($i+1) . " month =" . $monthlybill . "<br>\n";
$amount -= $monthlybill;
}
if ($amount>0) {
echo "paid for " . ($i+1) . " month =" . $amount;
}
I am having problems printing out a timetable using the results from a SQL statement and some HTML layout with PHP.
I have the times of the classes along the top of the page.
I am trying to put the days of the week along the side of the page and then check if the result of the SQL statement (containing a module) should be printed in the specific day and time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "$printday";
for($t = 9; $t < 17; $t++) {
if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
} //if
else {
echo "<td></td>";
} //else
} //for 2
echo "</tr>";
} //for 1
} //while
The problem is that I am printing out Monday-Friday 3 times as there are 3 $row results. Any idea how I could get this to work.
Your looping through your data in the wrong spot, you first need to put that in an array so you can loop through it for each day/time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
$courses[] = $row;
} //while
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "<tr><td>" . $printday . "</td>";
for($t = 9; $t < 17; $t++) {
unset($course_row);
foreach($courses as $course){
if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
$course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
} //if
}
if(isset($course_row)){
echo "<td>" . $course_row . "</td>";
} else {
echo "<td> </td>";
} //else
} //for 2
echo "</tr>";
} //for 1
I'm trying to implement pagination for a search result. The following code works perfectly:
echo "<p>" . $data['meta']['total'] . " properties found. (search " . $data['meta']['searchId'] . ")</p>\n";
$pages = $data['meta']['total'] / $count;
$pages = ceil($pages);
echo "<p>" . $pages . "</p>\n";
However, if I add in the following, I get a timeout:
$page = 1;
echo "<p>";
while ($page <= $pages); {
echo $page++ . " ";
}
echo "</p>\n";
No doubt I'm missing something obvious.
Your error is here:
while ($page <= $pages); {
//^ See this empty statement here!
echo $page++ . " ";
}
Your while loop loops trough a empty statement so no statement is going to increment $page. So your curly brackets are just a normal code block, in order to get your while loop working just remove the semicolon like this:
while ($page <= $pages) {
echo $page++ . " ";
}
I am trying to make a function in PHP which writes out a table, and looks in the database to find what cells should have info. the grid will always be the same size, but the content may be in different places.
I've gotten it to be able to look in the database, though it seems to only highlight the first cell, rather than the correct coordinates.
require("sql.php");
$sql = <<<SQL
SELECT *
FROM `maps`
WHERE `objpresent` = 1
SQL;
if(!$result = $db->query($sql)){
die('There was an error running the query [' . $db->error . ']');
} // ran the query
$xobj = array();
$yobj = array();
while($row = $result->fetch_assoc()){
//echo $row['x'] . $row['y'] . $row['object'] . '<br />';
$xobj[] += $row['x'];
$yobj[] += $row['y'];
}// get the rows
//find whether the row is obstructed
for($a=0; $a<=20-1; $a++) //rows (y)
{
for($i=0; $i<=25-1; $i++) //cols (x)
{
echo "<td>"; //between these write the apt content
// if (empty($xobj[$i]) || empty($yobj[$a]) ){
// echo '0';
//} //detect whether there is even a record for this space
if(!empty($xobj[$i]))
{
if(!empty($yobj[$a]))
{
echo $xobj[$i]; //debug
if($xobj[$i] == $i)
{
//echo $xobj[$i];
echo "A";
}
}
}
//echo "<td><img src='emptysym.png'></img></td>";
echo "</td>"; //add textual descriptions for now, add icons later
}
echo "</tr>";
}
this is my current(though rather messy) code.
if there is a row with the column x saying 2, and the column y saying 3, then it should put a letter at (2,3.
is it possible to fix this, or is there a better method for this?
Use a 2-dimensional array whose indexes are the x and y values from the database:
$xyobj = array();
while($row = $result->fetch_assoc()){
$xyobj[$row['x']][$row['y']] = true;
}
Then your output loop should be:
for ($y = 0; $y < 20; $y++) {
echo '<tr>';
for ($x = 0; $x < 25; $x++) {
echo '<td>';
if (isset($xyobj[$x][$y])) {
echo 'A';
}
echo '</td>';
}
echo '</tr>';
}