how to combine rows data of same id in one row - php

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

Related

simple else with while in loop not working

My code is working as for my needs. But the only thing bugging me is
the "else" is not working. When i search for a correct record the
record will appear and it was running fine. But if i Incorrectly
search a record nothing will happen. i am expecting "Records not Found" will echo but nothing happen.
}else{
echo "Records not found";
}
This is the whole code.
<?php
$conn = mysqli_connect("localhost", "root", "", "my1stdb") or die("could not connect");
$set = $_POST['search'];
if ($set) {
$show = "SELECT * FROM users where email='$set'";
$result = mysqli_query($conn, $show);
while ($rows = mysqli_fetch_array($result)) {
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
} else {
echo "Records not found";
}
?>
</table>
You need to use mysqli_num_rows() along with mysqli_fetch_assoc():-
<?php
$conn=mysqli_connect("localhost","root","","my1stdb") or die("could not connect");
$set = $_POST['search'];
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show) or die(mysqli_error($conn));
if(mysqli_num_rows($result)>0){ // check data present or not
while($rows=mysqli_fetch_assoc($result)){ // for lighter array due to associative indexes only
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
}else{
echo "Records not found";
}
}else{
echo "Please insert search term";
}
?>
</table>
Note:- Your code is wide-open for SQL INJECTION. to prevent from it use prepared statements
Reference:-
mysqli prepared statements
PDO prepared statements
You could count the number of results returned.
if($set) {
$show="SELECT * FROM users where email='$set'";
$result=mysqli_query($conn,$show);
$recordCount = 0;
while($rows=mysqli_fetch_array($result)){
$recordCount++;
echo "Registrant Found";
echo "<tr>";
echo "<td>";
echo $rows['username'];
echo "</td>";
echo "<td>";
echo $rows['fullname'];
echo "</td>";
echo "<td>";
echo $rows['password'];
echo "</td>";
echo "<td>";
echo $rows['email'];
echo "</td>";
echo "</tr>";
echo "<br/>";
}
if($recordCount==0){
echo "Records not found";
}
}

Why does PHP nested foreach loop displays only first row from sql database

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

A simple php script error result with tables

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

How to generate HTML table with nested loop?

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.

Calculating total price php

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 "&#8364 ", $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 "&#8364 ", $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 "&#8364 ", $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>";

Categories