Right now i have this piece of code running:
$count = 0;
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
$count++;
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "<td>";
$price = $prices[$count]* $number;
echo "€ ", $price;
echo "</td>";
echo "</tr>";
$count++;
}
}
But now i added a 3rd row (my first row is just the <th>'s) beneath that one looking like this:
echo "<tr>";
echo "<td>";
echo "Totaal";
echo "</td>";
echo "<td>";
echo $totalproducts;
echo "</td>";
echo "<td>";
echo $totalprice;
echo "</td>";
echo "</tr>";
As you can see i placed 2 $total... in this row where as you can guess need to come the total products ordered and total price of those products.
My problem is that i don't really know how to let it sum up all the $price and place this in $totalprice and sum up all $number and place it inside the $totalnumber. I know it's a basic question but it has been a long time ago since i actually worked with php. And i'm stuck on this.
I appreciate any help on this matter, thanks in advance!
Try this...
$totalprice=0;
$count = 0;
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
$count++;
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "<td>";
$price = $prices[$count]* $number;
$totalprice = $totalprice+ $price;
echo "€ ", $price;
echo "</td>";
echo "</tr>";
$count++;
}
}
and
echo "<tr>";
echo "<td>";
echo "Totaal";
echo "</td>";
echo "<td>";
echo $total_products;
echo "</td>";
echo "<td>";
echo $totalprice;
echo "</td>";
echo "</tr>";
i hope you want this
first set the variable $total_products and $total_price
$total_products = 0; // set to 0 initially
$total_price = 0; // set to 0 initially
$count = 0;
foreach ($products as $key => $product){
$number = isset($_POST[$key])?$_POST[$key]:'';
if (!$number){
$count++;
echo "";
} else {
echo "<tr>";
echo "<td>";
echo $product;
echo "</td>";
echo "<td>";
echo $number;
echo "</td>";
echo "<td>";
$price = $prices[$count]* $number;
echo "€ ", $price;
echo "</td>";
echo "</tr>";
$total_products += 1; // update total_products +1
$total_price += $price; // update total_price +$price
$count++;
}
}
and put them here like
echo "<tr>";
echo "<td>";
echo "Totaal";
echo "</td>";
echo "<td>";
echo $total_products;
echo "</td>";
echo "<td>";
echo $total_price;
echo "</td>";
echo "</tr>";
Related
I am trying to display a table from database using two nested foreach loops with some criteria. here is the code-
echo "<div class='container'>";
echo "<table class='table table-hover table-bordered table-condensed' style='width:95%' align='center'>";
echo "<tr>";
echo "<th>";
echo "Edit";
echo "</th>";
echo "<th>";
echo "Delete";
echo "</th>";
echo "<th>";
echo "Sl. No.";
echo "</th>";
echo "<th>";
echo "Group";
echo "</th>";
echo "<th>";
echo "Component";
echo "</th>";
echo "<th>";
echo "Quantity";
echo "</th>";
echo "</tr>";
if($rslt->rowCount() > 0)
{
foreach($rslt as $item)
{
foreach($rslt3 as $item3)
{
/*echo $item3['component'];
if($item3['component']===$item['component'])
{
if($Qty>=$item3['Qty'])
{
$item3[Qty]=$item3[Qty]-$item[Qty];
*/
//will implement this after the second loop starts working
$id = $item['entry_id'];
$Qty = $item['Qty'];
$group_ID = $item['group_ID'];
$component = $item['component'];
$vendor_ID = $item['vendor_ID'];
echo "<tr>";
echo "<td>";
echo "<a href='production_edit.php?id=$id&Qty=$Qty&group_ID=$group_ID&component=$component&vendor_ID=$vendor_ID'>Edit</a>";
echo "</td>";
echo "<td>";
echo "<a href='production_delete.php?id=$id&vendor_ID=$vendor_ID'>Delete</a>";
echo "</td>";
echo "<td>";
echo $item['entry_id'];
echo "</td>";
echo "<td>";
echo $item['group_ID'];
echo "</td>";
echo "<td>";
echo $item['component'];
echo "</td>";
echo "<td>";
echo $item['Qty'];
echo "</td>";
echo "</tr>";
}
}
}
echo "</table></div><br>";
}
Now the problem here is when I use the second foreach loop the table displays the first entry in each table row... I am curious where I am at fault with this second foreach loop.. Thanks in advance
My controller function:
function index()
{
$data['user_clicks'] = $this->common_model->get_userClicks();
$this->load->view('common/header');
$this->load->view('content_pages/user_clicks',$data);
$this->load->view('common/footer');
}
My model
function get_userClicks()
{
$this->db->select('agent_id,click_type,click_count');
$this->db->select_sum('click_count');
$this->db->from('daily_clicks');
$this->db->group_by('click_type,agent_id');
$this->db->order_by('agent_id');
$q = $this->db->get();
return $q->result_array();
}
this is i am getting, what i want is to get the click_count of phone click, email click,offline click with respect to their columns and their total clicks is already done.
the data base image is the last one klindly help me get through it.
Thank you
Try this,
$resarr=count($resultarray);
<table>
<tr>
<th>Agent_id</th>
<th>phone_click</th>
<th>email_click</th>
<th>offline_click</th>
<th>total_click</th>
</tr>
for($i=1; $i<100; $i++){ //agent id
for($j=1; $j<=$resarr; $j++) //number of records
{
echo "<tr>";
if($arr['user_clicks'][$j]['agent_id'] == $i)
{
if(!empty($arr['user_clicks'][$j]['agent_id']){
echo "<td">
echo $arr['user_clicks'][$j]['agent_id'];
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='phone'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='email'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}else
{
echo "<td">
echo '';
echo "</td>";
}
if($arr['user_clicks'][$j]['click_type']=='offline'){
echo "<td">
echo $arr['user_clicks'][$j]['click_type'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
if(!empty($arr['user_clicks'][$j]['click_count']){
echo "<td">
echo $arr['user_clicks'][$j]['click_count'];
echo "</td>";
}
else
{
echo "<td">
echo '';
echo "</td>";
}
}
echo "</tr>";
}
}
Hello guys i'm in need of php experts advice.
Here's my problem. A user has 2 inputs the start and the end this are all integers. this will able to identify odd and even. i have solve already odd and even and almost done. main problem ex. start value 1 end 5. 1 is odd it should be displayed on odd in table. but the problem is it is found in even table. initial value is the problem. the rest was good.
here's my code
<?php
$firstnum = $_POST['first_input'];
$secondnum = $_POST['second_input'];
$counter = 0;
echo "<table border='1'>";
if ($firstnum < $secondnum) {
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter <= $secondnum ; $counter++) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
} elseif ($firstnum > $secondnum) {
# code...
//first num is < second num
echo "<tr>"; //first tr
echo "<th>"; echo "Even numbers"; echo "</th>";
echo "<th>"; echo "Odd numbers"; echo "</th>";
echo "</tr>";
for ($counter=$firstnum; $counter >= $secondnum ; $counter--) {
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
} else {
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
}
}
echo "</table>";
?>
Your issue is you have invalid html resulting from your if/else blocks.
If your if you have
<tr>
<td><td>
and in your else you have
<td></td>
</tr>
Both of these need full row/cell tags
<tr>
<td></td>
<td></td>
</tr>
So your code should look like
if ($counter % 2 == 0){
echo "<tr>";
echo "<td>";
echo $counter;
echo "</td>";
echo "<td>";
echo "</td>";
echo "</tr>";
} else {
echo "<tr>";
echo "<td>";
echo "</td>";
echo "<td>";
echo $counter;
echo "</td>";
echo "</tr>";
}
I have a table that should look like the following:
http://hell-rider.de/Fotos/img/beispiel-1.JPG
I have the following PHP script:
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<th>";
echo $row{'date'};
echo "</th>";
echo "<th>";
echo '' . $row{'ownerName1'} .'';
echo "</th>";
echo "<th>";
foreach ($allgemeinxml->result->rowset->row as $type){
echo '' . $type{'typeName'} .'';
}
echo "</th>";
echo "<th>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</th>";
echo "<th>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</th>";
echo '</tr>';
}
}
echo "</table>";
The actual output of my script is the following, however:
http://hell-rider.de/Fotos/img/beispiel2.JPG
How do I have to change my script to populate the third column correctly (and not with MetallurgyTradeLaboratory OperationLaboratory OperationLaboratory OperationResearchCybernetics)?
<th> tags are for header cells, you should be using <td> data cells.
$types = array();
foreach ($allgemeinxml->result->rowset->row as $type){
$types[] = $type{'typeName'};
}
$type_index = 0;
foreach ($xml->result->rowset->row as $row){
if($row{'refTypeID'} == 42){
echo '<tr>';
echo "<td>";
echo $row{'date'};
echo "</td>";
echo "<td>";
echo '' . $row{'ownerName1'} .'';
echo "</td>";
echo "<td>";
echo $types[$type_index];
echo "</td>";
echo "<td>";
echo '<p class="minus">' . $row{'amount'} .' ISK</p>';
echo "</td>";
echo "<td>";
echo '<p class="kontostand">' . $row{'balance'} . ' ISK</p>';
echo "</td>";
echo '</tr>';
}
$type_index++;
}
echo "</table>";
I don't know exactly what the use of if($row{'refTypeID'} == 42){ is, so you either want $type_index++; in the place I've put it above, or just above the closing brace }. For this to work well, $allgemeinxml->result->rowset->row and $xml->result->rowset->row need the same number of elements.
I have the following code:
<?php
require "dbconn.php";
$register="SELECT * from register WHERE username = '". $_SESSION['username']."'";
$re = $connect->query($register);
$numrow = $re->num_rows;
echo "<table>";
echo "<tr>";
echo"<th>Username</th>";
echo"<th>Forename</th>";
echo"<th>Surname</th>";
echo"<th>Course</th>";
echo"<th>Subject</th>";
echo"<th>Level</th>";
echo"<th>Date</th>";
echo"<th>Time</th>";
echo"</tr>";
$count = 0;
while ($count < $numrow)
{
$row = $re->fetch_assoc();
extract($row);
echo "<tr>";
echo "<td>";
echo $username;
echo "</td>";
echo "<td>";
echo $firstName;
echo "</td>";
echo "<td>";
echo $surname;
echo "</td>";
echo "<td>";
echo $course;
echo "</td>";
echo "<td>";
echo $subject;
echo "</td>";
echo "<td>";
echo $level;
echo "</td>";
echo "<td>";
echo $date;
echo "</td>";
echo "<td>";
echo $time;
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
It shows the records from the database as a form. I have the following button:
<FORM METHOD="LINK" ACTION="register.php">
<INPUT TYPE="submit" VALUE="Go back to register"></INPUT></form>
No matter what DIV I put the button in, or what CSS I give it. The button always stays ABOVE the form from the database and I cannot get it below the table.
Any ideas?
Firstly, you did not close your table with a </table> tag; it's important that you do.
Then there's METHOD="LINK" which doesn't exist and isn't a valid method.
You either want to use method="post" or method="get".
See this answer on Stack about it:
https://stackoverflow.com/a/11582427/