Ok so I have this PHP page.
Anyone able to help me add in a count of rows?
<h2>All Open Incidents</h2>
<table class="table table-striped table-bordered table-head-bordered-bottom table-condensed">
<thead>
<tr>
<th class=span1>Ticket ID</th>
<th class=span2>Title</th>
<th class=span2>Submitter</th>
<th class=span2>Owner</th>
<th class=span2>Status</th>
<th class=span1>Created</th>
<th class=span1>Modified</th>
<th class=span1>SLA</th>
</tr>
</thead>
<tbody>
<?php
$query1 = "
SELECT HD_TICKET.ID as ID,
HD_TICKET.TITLE as Title,
HD_STATUS.NAME AS Status,
HD_PRIORITY.NAME AS Priority,
HD_TICKET.CREATED as Created,
HD_TICKET.MODIFIED as Modified,
HD_TICKET.CUSTOM_FIELD_VALUE10 as SLA,
S.FULL_NAME as Submitter,
O.FULL_NAME as Owner,
HD_TICKET.CUSTOM_FIELD_VALUE0 as Type
FROM HD_TICKET
JOIN HD_STATUS ON (HD_STATUS.ID = HD_TICKET.HD_STATUS_ID)
JOIN HD_PRIORITY ON (HD_PRIORITY.ID = HD_TICKET.HD_PRIORITY_ID)
LEFT JOIN USER S ON (S.ID = HD_TICKET.SUBMITTER_ID)
LEFT JOIN USER O ON (O.ID = HD_TICKET.OWNER_ID)
WHERE (HD_TICKET.HD_QUEUE_ID = $mainQueueID) AND
(HD_STATUS.NAME like '%Open%')
ORDER BY ID DESC
";
$result1 = mysql_query($query1);
$num = mysql_num_rows($result1);
$i = 0;
while ($i < $num)
{
$ID = mysql_result($result1,$i,"ID");
$Title = mysql_result($result1,$i,"Title");
$Status = mysql_result($result1,$i,"Status");
$Type = mysql_result($result1,$i,"Type");
$Created = mysql_result($result1,$i,"Created");
$Modified = mysql_result($result1,$i,"Modified");
$Priority = mysql_result($result1,$i,"Priority");
$Owner = mysql_result($result1,$i,"Owner");
$Submitter = mysql_result($result1,$i,"Submitter");
$SLA= mysql_result($result1,$i,"SLA");
$ID = stripslashes($ID);
$Title = stripslashes($Title);
$Status = stripslashes($Status);
$Type = stripslashes($Type);
$Created = stripslashes($Created);
$Modified = stripslashes($Modified);
$Priority = stripslashes($Priority);
$Owner = stripslashes($Owner);
$Submitter = stripslashes($Submitter);
$SLA = stripslashes($SLA);
$StatusSpan="";
if ($Status=="Stalled")
{
$StatusSpan="<span class='label label-warning'>$Status</span>";
}
$PriortySpan="";
if ($Priority=="High")
{
$PriortySpan="<span class='label label-important'><i class='icon-exclamation-sign icon-white'></i>High</span>";
}
if ($Priority=="Low")
{
$PriortySpan="<span class='label'>Low</span>";
}
if ($Priority=="Medium")
{
$PriortySpan="<span class='label'>Medium</span>";
}
if ($Priority=="Critical")
{
$PriortySpan="<span class='label'><i class='icon-exclamation-sign icon-white'></i>Critical</span>";
}
echo "<tr><td><a href='http://$KaceBoxDNS/adminui/ticket.php?ID=$ID' target='_blank'>$ID</a> $StatusSpan $PriortySpan</td> \n";
echo "<td>$Title</td> \n";
echo "<td>$Submitter</td> \n";
echo "<td>$Owner</td> \n";
echo "<td>$Status</td> \n";
echo "<td>$Created</td> \n";
echo "<td>$Modified</td> \n";
echo "<td>$SLA</td> \n";
echo "</tr> \n";
$i++;
}
echo "</tbody></table> \n";
?>
So basically, I want to count the number of results (8 for instance) and display
8 Results Found
At the moment I can get it to count, but it loops and puts "8 results found" 8 times, any help would be greatly apprecaited.
:)
Assuming that the number of results is in $num, you just need to add the line outside the while ($i < $num) loop, e.g. after the table close tag:
echo "</tbody></table>\n";
echo "<p>$num results found! Woohoo!</p>";
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
hi all i am trying to display array of data in a table .but the main task is i need to display all users that are under one supervisor, they may/may not have attempted the test .i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp
below is my code
controller:
if ($supervisor) {
$users_query="SELECT u.* ,v.value FROM usr_data u, udf_text v WHERE u.usr_id != 6 AND u.usr_id = v.usr_id " . $supervisor_condition . " GROUP BY u.usr_id";
// echo $users_query;
$user_records = $this->base_model->executeSelectQuery($users_query);
$final_dataa = [];
foreach ($user_records as $user) {
$user=(object)$user;
$user_test=" SELECT u.*,o.title,ta.*,tpr.workingtime, tpr.pass, tpr.tstamp, tpr.points, tpr.maxpoints, tm.minimum_level, tcr.mark_official,(tcr.reached_points/tcr.max_points)*100 as result,v.value,ta.submittimestamp,tcr.mark_official FROM usr_data u, object_data o,tst_tests tt,tst_active ta,tst_pass_result tpr, tst_result_cache tcr,udf_text v, tst_mark tm WHERE u.usr_id != 6 AND u.usr_id=ta.user_fi AND tt.obj_fi=o.obj_id AND v.usr_id=u.usr_id AND ta.test_fi=tt.test_id AND tt.test_id = tm.test_fi AND tcr.active_fi=ta.active_id AND tm.passed = 1 AND ta.active_id=tpr.active_fi AND tcr.active_fi=ta.active_id AND v.field_id='2' AND ta.user_fi = $user->usr_id GROUP by ta.submittimestamp";
// echo $user_test;
$tst_records=$this->base_model->executeSelectQuery($user_test);
if (count($tst_records)) {
foreach ($tst_records as $tst) {
$tst=(object)$tst;
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['submittimestamp'] = $tst->submittimestamp;
$dta['test_title'] = $tst->title;
$dta['division'] = $tst->value;
$mark_official = $tst->mark_official;
if ($mark_official == "passed" || $mark_official == "Passed" || $mark_official == "PASSED") {
$result_status = '<span class="label label-primary"> Completed </span>';
$completed = TRUE;
} else {
$result_status = '<span class="label label-danger"> Failed </span>';
$failed = TRUE;
}
$dta['status'] = $result_status;
$final_dataa[] = $dta;
}
}
else{
$dta['usr_id'] = $user->usr_id;
$dta['firstname'] = $user->firstname;
$dta['matriculation'] = $user->matriculation;
$dta['approve_date'] = $this->udfTextData(1, $user->usr_id);
$dta['department'] = $user->department;
$dta['job_title'] = $this->udfTextData(6, $user->usr_id);
$dta['test_title'] = $user->title;
$dta['division'] = $user->value;
$final_dataa[] = $dta;
}
}
}
$dataa['recordss'] = $final_dataa;
$this->load->view('supervisor', $dataa);
I am using if else condition if users have test else not like that .below is my view code
view:
<h3> Skill Matrix Report Process Based Training Record in LMS </h3>
<hr>
<?php $uniq_rec = array_unique($recordss,SORT_REGULAR);
// var_dump($uniq_rec);
$uniq_name = array_unique(array_column($recordss, 'firstname'));
$uniq_test = array_unique(array_column($recordss, 'test_title'));
?>
<table class="table" id="myTable">
<thead>
<tr>
<th>Employe NO</th>
<th>Employe Name</th>
<th>Date Joined</th>
<th>division</th>
<th>department</th>
<th>jobtitle</th>
<?php
foreach ($uniq_test as $row) {
?>
<th><?php echo $row?></th>
<?php
}
?>
</tr>
</thead>
<tbody>
<?php
foreach ($uniq_rec as $row) {
$row = (object)$row;
// var_dump($row);
$date_joined_y = substr($row->approve_date,0,4);
$date_joined_m = substr($row->approve_date,4,2);
$date_joined_d = substr($row->approve_date,6,2);
$date_joined = $date_joined_d."-".$date_joined_m."-".$date_joined_y;
?>
<tr>
<td><?php echo ucfirst($row->matriculation); ?></td>
<td><?php echo $row->firstname?></td>
<td> <?php echo ucfirst($date_joined); ?></td>
<td><?php echo ucfirst($row->division); ?></td>
<td><?php echo ucfirst($row->department); ?></td>
<td><?php echo ucfirst($row->job_title); ?></td>
<?php
foreach ($uniq_test as $uniq) {;
$status = "";
foreach ($recordss as $rec) {
if($uniq == $rec['test_title'] && $rec['firstname'] == $row->firstname){
echo "<td>".$rec['status'].$rec['submittimestamp']."</td>";
$status = "true";
}
}
if($status != "true"){
echo "<td></td>";
}
$status = "";
}
?>
but the main task is i need to display all users who are attempted test or not but those users are under one Supervisor
submittimestampbut the main task is i need to display all users who are attempted test or not but those users are under one Supervisor.i am using if else condition in controller but am getting error like
Message: Undefined index: status and Message: Undefined index:
submittimestamp
I want to append or insert a html input to the table's td. Do you know how?
This is my html form index.php:
<table class="stack">
<thead class="tblthead">
<tr>
<th>Filename</th>
<th width="400">Date/Time</th>
<th width="300">Action</th>
</tr>
</thead>
<tbody id="importable">
</tbody>
</table>
This is my backend.php, this is where i returned the data going to the frontend which is my index.php.
case 'filerec':
$arr =[
":userID" => $_SESSION['loggedIn_PH'][0]['user_id'],
];
$query = "SELECT * FROM file_rec_tbl WHERE user_id=:userID ORDER BY file_id DESC";
$stmt = $con -> prepare( $query );
$stmt -> execute( $arr );
$data = $stmt -> fetchAll();
$table = '';
for( $x = 0; $x < count($data); $x++ ) {
$table .= '<tr fileID="'.$data[$x]['file_id'].'">
<td>'.$data[$x]['file_name'].'</td>
<td>'.$data[$x]['file_datetime'].'</td>
<td>'.html('<input type="text"></input>')
</tr>';
}
exit ($table);
break;
I got an error to the last td. Can you please help me or suggest what might be a good solution to my problem.
Try may be your problem
$table = '';
for( $x = 0; $x < count($data); $x++ ) {
$table .= '<tr fileID="'.$data[$x]["file_id"].'">
<td>'.$data[$x]["file_name"].'</td>
<td>'.$data[$x]["file_datetime"].'</td>
<td><input type="text"></input></td>
</tr>';
}
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?
I am trying to apply css class for dynamic rows in php. But its not happening. Here is my code
please someone suggest me where i am going wrong. I have declared a counter for rows but not getting where to increment its count.
<table width='100%' border='0' cellspacing='0' cellpadding='2'>
<tr>
<td class='tddash'>10 latest unpaid customer invoices</td>
</tr>
<tr>
<td valign='top'>";
$SQL = "SELECT salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced,
SUM(salesorderdetails.unitprice*salesorderdetails.quantity*(1-salesorderdetails.discountpercent)/currencies.rate) AS ordervalue
FROM salesorders INNER JOIN salesorderdetails
ON salesorders.orderno = salesorderdetails.orderno
INNER JOIN debtorsmaster
ON salesorders.debtorno = debtorsmaster.debtorno
INNER JOIN custbranch
ON debtorsmaster.debtorno = custbranch.debtorno
AND salesorders.branchcode = custbranch.branchcode
INNER JOIN currencies
ON debtorsmaster.currcode = currencies.currabrev
WHERE salesorderdetails.completed=0
GROUP BY salesorders.orderno,
debtorsmaster.name,
custbranch.brname,
salesorders.customerref,
salesorders.orddate,
salesorders.deliverydate,
salesorders.deliverto,
salesorders.printedpackingslip,
salesorders.poplaced
ORDER BY salesorders.orderno";
$SalesOrdersResult1 = DB_query($SQL,$db);
echo "<table width='100%' celpadding='2' class='selection'><tbody>";
$TableHeader = "<tr><th> Customer </th><th>Order Date</th><th>Delivery Date</th><th>Delivery To</th><th>Order Total</th></tr> ";
$k = 0;
while ($row = DB_fetch_array($SalesOrdersResult1))
{
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
$FormatedOrderValue1 = locale_number_format($row['ordervalue'],$row['currdecimalplaces']);
//$TotalOrderValue = $array_sum($FormatedOrderValue1);
//$FormatedOrderValue1 = locale_number_format($myrow['ordervalue'],$_SESSION['CompanyRecord']['decimalplaces']);
$FormatedOrderDate = ConvertSQLDate($row['orddate']);
$FormatedDelDate = ConvertSQLDate($row['deliverydate']);
echo " <td> " . $row['name'] . " </td>";
echo " <td>$FormatedOrderDate</td><td>$FormatedDelDate</td><td> " . $row['deliverto'] . " </td><td>$FormatedOrderValue1</td> ";
}
//echo "<tr><td colspan='3'>Total</td><td colspan='2'>$TotalOrderValue</td></tr></tbody>";
//echo $array_sum($FormatedOrderValue1);
echo "</table>";
echo"</td>
</tr>
</table>
Try to put this inside your while loop.
if ($k == 1){
echo '<tr class="EvenTableRows">';
$k = 0;
} else {
echo '<tr class="OddTableRows">';
$k = 1;
}
If you want decorate your table, and you can use css use it. Don't use server side language
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Three issues with your code (four if you count the fact that you have a massive amount of HTML that should be moved outside of being echo'ed within php)
1) You have no $k++; to increment your $k value
2) You have a "tr" within the $row['name'] line that needs to be removed.
3) You need to move the entire if($k) block INSIDE of your while() loop.