I've started to writing my engine of forum, just to refresh php skills.
My problem is that, when I make a board and one theard, I have double queries.
I know why, I have "foreach in different foreach" but I cannot find a way to sort it out, can you guys help me how would you do it? To avoid this big number of queries?
An example, SMF forum main page has only 12 queries (NO MATTER OF NUMBERS BOARDS AND THEARDS).
My page have 12 already, but I have only 2 categories, 5 boards, 4 theards, 1 user.
If I add one board, and one theard I have +2 queries.
<?php
if(!defined('USED'))
exit;
# Name of page!
$page = 'Forum';
$db = new Database();
$array = $db -> select("SELECT `id`,`name` FROM `".PREFIX."category`");
if(count($array) > 0)
{
$container .= '';
foreach($array as $category)
{
$container .= '<div class="panel panel-primary" id="category-'.$category['id'].'">
<div class="panel-heading">
<h3 class="panel-title">
'.$category['name'].'
</h3>
</div>
<table class="table table-bordered table-striped table-hover">
<thead>
<tr>
<th class="col-md-9">Board Name</th>
<th class="col-md-3">Latest</th>
</tr>
</thead>';
$array2 = $db -> select("SELECT `id`,`id_category`,`name`,`info` FROM `".PREFIX."boards` WHERE `id_category`=".$category['id']."");
if(count($array2) > 0)
{
foreach($array2 as $board)
{
$container .='<tbody>
<tr>
<td>
<strong>'.$board['name'].'</strong><br/>
<small>'.$board['info'].'</small>
</td>';
$array3 = $db -> select("SELECT `id`,`id_board`,`title`,`created_by`,`created_date` FROM `".PREFIX."theards` WHERE `id_board`=".$board['id']." ORDER BY `created_date` DESC LIMIT 1");
if(count($array3) > 0)
{
foreach($array3 as $theards)
{
$array4 = $db -> select("SELECT `id`,`name` FROM `".PREFIX."users` WHERE `id`=".$theards['created_by']."");
foreach($array4 as $user)
{
$created_by = $user['name'];
}
$container .='<td>
'.$theards['title'].'<br/><small>'.$created_by.', <abbr class="timeago" title="'.$theards['created_date'].'">less than a month ago</abbr></small>
</td>';
}
}
else
{
$container .= '<td>
Nothing is here!
</td>';
}
$container .=' </tr>
</tbody>';
}
}
else
{
$container .='<tbody>
<tr>
<td>Nothing is here!</td>
<td>...</td>
</tr>
</tbody>';
}
$container .='</table>
</div>';
}
}
else
{
Site::Error("You need to add a category first!");
}
And my select:
public function select($query) {
$rows = array();
$result = $this -> query($query);
if($result === false) {
return false;
}
while ($row = $result -> fetch_assoc()) {
$rows[] = $row;
}
global $db_count;
$db_count = !isset($db_count) ? 1 : $db_count + 1;
return $rows;
}
Ive added $db_count at the end of the file to check numbers of queries.
Instead of running one query to get a list of IDs, then looping across those IDs to get another set of IDS, then looping across those IDs to get a set of data, how about writing one query, using a JOIN to line up the three tables?
Related
I want to query a database by making use of a whereMonth clause in laravel using a for loop but I am getting an empty array. If I replace $m in whereMonth clause with an integer like 7 it pulls the data.
<table class="table table-bordered table-hover expenses-report" id="expenses-report-table">
<thead>
<tr>
<th class="bold">Category</th>
<?php
for ($m=1; $m<=12; $m++) {
echo ' <th class="bold">' .date('F', mktime(0,0,0,$m,1)) . '</th>';}
?>
<th class="bold">year ({{\Carbon\Carbon::now()->format('Y')}})</th>
<?php
?>
</tr>
</thead>
<tbody>
#foreach ($categories as $category)
<tr>
<td>
{{$category->name}}
<?php
for ($m = 1; $m <= 12; $m++) {
if (!isset($netMonthlyTotal[$m])) {
$netMonthlyTotal[$m] = array();
}
$expense_results = $expenses->whereMonth('date',$m)
->whereYear('date', \Carbon\Carbon::now()->format('Y'))
->where('category', $category->id)
->get();
print_r($expense_results);
$total_expenses = array();
echo '<td>';
foreach ($expense_results as $expense) {
$expense = $expenses->where('id', $expense->id)->first();
$total = $expense->amount;
$totalTaxByExpense = 0;
// Check if tax is applied
if ($expense->tax != 0) {
$totalTaxByExpense+= ($total / 100 * $expense->tax);
}
$taxTotal[$m][] = $totalTaxByExpense;
$total_expenses[] = $total;
}
$total_expenses = array_sum($total_expenses);
// Add to total monthy expenses
array_push($netMonthlyTotal[$m], $total_expenses);
if (!isset($totalNetByExpenseCategory[$category->id])) {
$totalNetByExpenseCategory[$category->id] = array();
}
array_push($totalNetByExpenseCategory[$category->id], $total_expenses);
// Output the total for this category
if (count($categories) <= 8) {
echo $total_expenses;
} else {
// show tooltip for the month if more the 8 categories found. becuase when listing down you wont be able to see the month
echo '<span data-toggle="tooltip"
title="' . date('F', mktime(0, 0, 0, $m, 1)) . '">' . $total_expenses . '</span>';
}
echo '</td>';
}
?>
</td>
<td class="bg-odd">
{{array_sum($totalNetByExpenseCategory[$category->id])}}
</td>
</tr>
#endforeach
</tbody>
</table>
I basically want to return results matching the search query including the whereMonth
Can you replace this part
$query->whereMonth('date', $m);
with this
$query->whereMonth('date', (string) $m);
And then try again. I guess its not accepting integers.
I was able to get it to work by making use of Raw query
$rawQuery ="SELECT amount, id,tax FROM tblexpenses WHERE MONTH(date) = $m AND YEAR(CURRENT_DATE) AND category=$category->id GROUP BY id";
$expense_results = DB::select(DB::raw($rawQuery));
Though I would appreciate it if someone can help to translate this query to laravel
Bellow are my code, i get four column from these, but the result shows every one data will loop in four time in one column then it appear again in new column with the same data.
<?php
$tampil_data = $this->in_elektronik_model->tampil_data(); //load data
foreach ($tampil_data as $tampildata) {
for ($i = 0; $i < 4; $i++) {
$table->addRow(500); //make new row
for ($j = 0; $j < 4; $j++) {
$table->addCell(2000)->addText(htmlspecialchars($tampildata->elektronik_nama)); //content of table
}
}
}
model
function tampil_data() {
$this->db->where('elektronik_status_aktif', 0);
$tampil = $this->db->get('in_elektronik');
if ($tampil->num_rows() > 0) {
return $tampil->result();
} else {
return array();
}
}
How to make table with four column, where the content of table will appear from right column to left and appear just one data in one time, after four column are full, it makes new row?
You can refer this code. This is the way i have done table in view page
<table border="1">
<tr role="row">
<th width="5%">Featured Id</th>
</tr>
<?php
if(!empty($results))
{
foreach ($results as $row) {
?><tr>
<td class=" "><?php echo $row->fet_id; ?></td>
</tr>
<?php }}
?>
</table>
I am currently having problem with my total. Well, I have 3 different tables. billing_payments, billing_entry, and services.
My problem is, I can't get the total of each SERVICES. I provided a screenshot so that you'll get what I mean.
Here is the code of the report. I have added some comments to indicate where the problem starts and ends.
<table class="table table-striped table-hover">
<thead>
<tr>
<th>#</th>
<th>PATIENT NAME</th>
<th>CASE</th>
<?php
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
echo '<th>'.$service->service_name.'</th>';
}
?>
<th>MEDICAL SUPPLIES</th>
<th>PHILHEALTH</th>
<th>DISCOUNT</th>
<th>GROSS</th>
<th>NET</th>
<tr>
</thead>
<tbody>
<?php
$x = 1;
$billquery = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($billquery->result() as $data) {
echo '<tr>';
echo '<td>'.$x++.'</td>';
echo '<td>'.$data->patientname.'</td>';
echo '<td>'.$data->session_id.'</td>';
//SERVICES
$servicesquery = $this->db->get('services');
foreach ($servicesquery->result() as $service) {
$this->db->where('billing_serviceid', $service->service_id);
$this->db->where('billing_patientid', $data->patient_id);
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$billing = $this->db->get('billing_entry');
foreach ($billing->result() as $bill) {
echo '<td>'.$bill->billing_amount.'</td>';
}
}
//MEDICAL SUPPLIES
$this->db->where('billing_id', $data->billing_id);
$this->db->where('billing_servicename', 'MEDICAL SUPPLIES');
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td>'.$med->billing_amount.'</td>';
}
//PHILHEALTH
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_philhealth');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_philhealth.'</td>';
}
//DISCOUNT
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_discount');
$philhealth = $this->db->get('billing_entry');
foreach ($philhealth->result() as $phil) {
echo '<td class="bg-info">'.$phil->billing_discount.'</td>';
}
//GROSS
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$gross = $this->db->get('billing_entry');
foreach ($gross->result() as $gr) {
echo '<td class="bg-warning">'.$gr->billing_amount.'</td>';
}
echo '<td class="bg-danger">'.$data->total_amount.'</td>';
echo '</tr>';
}
echo '<tr>';
echo '<td colspan="3" style="text-align:right"><strong>TOTAL:</strong></td>';
//PROBLEM STARTS HERE
//TOTAL PER SERVICES
$quer = $this->db->get('services');
foreach ($quer->result() as $service) {
$totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totserv->result() as $servdata) {
$id = $servdata->id;
$this->db->where('billing_id', $servdata->billing_id);
$this->db->where('billing_serviceid', $service->service_id);
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
echo '<td class="bg-success">'.$med->billing_amount.'</td>';
}
}
}
//PROBLEM ENDS HERE
//TOTAL NET
$totalamt = $this->db->query('SELECT SUM(total_amount) AS totalamount FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totalamt->result() as $data) {
echo '<td>'.$data->totalamount.'</td>';
}
echo '</tr>';
?>
If you look at your image, it's clear to see you're outputting each value from the columns (in the green box) in your final row (350, zero, 350, 485, 485, 485, zero, 15, 300)
You can fix this by adding a placeholder variable to calculate the total for each service, e.g.:
foreach ($quer->result() as $service) { // you want to output one <td> per service, so the <td> should be printed at the end of this foreach
$serviceTotal = 0; // initialize the total for each service
$totserv = $this->db->query('SELECT * FROM `billing_payments` WHERE (`date` BETWEEN "'.$this->input->get('from').'" AND "'.$this->input->get('to').'")');
foreach ($totserv->result() as $servdata) {
$id = $servdata->id;
$this->db->where('billing_id', $servdata->billing_id);
$this->db->where('billing_serviceid', $service->service_id);
$this->db->select_sum('billing_amount');
$medsup = $this->db->get('billing_entry');
foreach ($medsup->result() as $med) {
$serviceTotal = $serviceTotal + $med->billing_amount
}
}
echo '<td class="bg-success">'. $serviceTotal .'</td>';
}
This way you will get one per service, and the $serviceTotal variable will contain the sum of each individual line item for that particular service.
You have more efficient options open to you as well - firstly, you could calculate the individual totals when you are getting them in the services code block, e.g.:
$serviceTotals = array(); // a holder for all the totals
foreach ($servicesquery->result() as $service) {
$this->db->where('billing_serviceid', $service->service_id);
$this->db->where('billing_patientid', $data->patient_id);
$this->db->where('billing_id', $data->billing_id);
$this->db->select_sum('billing_amount');
$billing = $this->db->get('billing_entry');
foreach ($billing->result() as $bill) {
$serviceTotals[$service->service_id] = $serviceTotals[$service->service_id] + $bill->billing_amount; //this updates the total for each service ID
echo '<td>'.$bill->billing_amount.'</td>';
}
}
You could then loop through this array to output each total.
Ideally, you should also consider calculating all these values in your controller, which would call the queries you need from your model(s), and then packaging that data in your $data variable. The controller can then pass all of that to your view. CodeIgniter has a lot of good resources for explaining the Model-View-Controller (MVC) pattern (it's a bit outside of the scope of this question to go into it here), and using CI without implementing the MVC pattern is going to make it harder to make use of most of the documentation and support for CI on the net.
My table has got 4 categories with unique id. And all I am doing is just querying the database and including a foreach loop which is getting the cateogry ids based on which subcategories are being fetched. Since my table has got 4 categories . So running the loop has to return the div 4 times which I will echo out in the view. That is the concept I had. But it is returning only once. So the div main_div gets printed only for once. I don't understand what is wrong here.
In my Model
public function getSubCategory(){
$sub_div = null;
$query_main_cat = $this->db->query("SELECT * FROM main_category");
foreach($query_main_cat->result_array() as $row){
$mc_id = $row['id'];
$query_get_sub = $this->db->get_where("sub_category", array('main_cat_id' => $mc_id));
foreach ($query_get_sub->result_array() as $row_sub_cat) {
$sub_cat_id = $row_sub_cat['id'];
$sub_cat_name = $row_sub_cat['sub_name'];
$sub_cat_mc_id = $row_sub_cat['main_cat_id'];
$sub_div .= '<div class="col-sm-6 col-md-4">
<div class="table-responsive">
<table class="table table-striped text-center">
<th class="text-center">'.$sub_cat_name.'</th>
<tbody>
<tr><td>Hello</td></tr>
<tr><td>Hello</td></tr>
</tbody>
</table>
</div>
</div>';
}
$main_div = '<div class="tab-pane" id="tab'.$mc_id.'">
'.$sub_div.'
</div>';
return $main_div;
}
}
In my controller
public function load_view(){
$this->load->model("get_category");
$data['sub_category'] = $this->get_category->getSubCategory();
$this->load->view('index', $data);
}
In view
echo $sub_category;
This should return the div main_div 4 times. since I have got 4 rows in my table. But only is showing for once. Need some help here.
You get only once hello, because you prematurely return out of the loop.
return "hello";
You should not use return in the loop, but accumulate the string and return it at the end.
$text = '';
foreach($query_main_cat->result_array() as $row){
$text .= "hello";
}
return $text;
I have a php code similar to below. I want to make just last row bold. Is it possible?
<table border = "5">
<?php
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) ) {
echo "<tr><td>".$rows[0]." <td>".$rows[1]."
<td>".$rows[2]." <td>".$rows[3]."
<td>".$rows[4]." <td>".$rows[5]." ";
}
}
</table>
You can use mssql_num_rows() to get the total number of rows returned by the query before you loop through them, and then (with the use of a counter), see if the current row is the last one and then apply any styling you want:
<table border = "5">
<?php
if (isset($startdate) && isset($enddate) ){
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
$numRows = mssql_num_rows($result);
if ($numRows > 0) {
$counter = 0;
while ($rows = mssql_fetch_array($result) ) {
$style = ($counter++ == ($numRows - 1)) ? ' style="font-weight: bold;"' : '';
?>
<tr>
<td<?=$style;?>><?=$rows[0];?></td>
<td<?=$style;?>><?=$rows[1];?></td>
<td<?=$style;?>><?=$rows[2];?></td>
<td<?=$style;?>><?=$rows[3];?></td>
<td<?=$style;?>><?=$rows[4];?></td>
</tr>
<?
}
}
}
?>
</table>
I find that copying the data to an array (or better yet object) is much better, then outputting the data at the end makes these sorts of things easier.
in this case, if you drop the data into an array, you can then use lovely things like count() to see how many rows there are etc. This lets you do a simple check in the display loop to see if it is the last element in the array.
This should work:
<?php
if (isset($startdate) && isset($enddate))
{
$res=array();
$query = "storedprocedure '$startdate','$enddate','field1'";
$result = mssql_query($query);
while ($rows = mssql_fetch_array($result) )
{
$res[]=$rows;
}
}
?>
<table border = "5" id="1">
<?php
$numberRows=count(%res);
for($i=0;$i<$numberRows;$i++)
{
$boldStart="";
$boldEnd="";
if($i==($numberRows-1))
{
$boldStart="<B>";
$boldEnd="</B>";
}
echo "<tr><td>".$boldStart.$rows[$i][0].$boldEnd." </td><td>".$boldStart.$rows[$i][1].$boldEnd."
</td><td>".$boldStart.$rows[$i][2].$boldEnd." </td><td>".$boldStart.$rows[$i][3].$boldEnd."
</td><td>".$boldStart.$rows[$i][4].$boldEnd." </td><td>".$boldStart.$rows[$i][5].$boldEnd." </td>";
}
?>
</table>
Just use CSS:
t1 tr:last-child{font-weight:bold;}