Codeigniter Pagination: How to properly do results numbering - php

How can I properly do the the numbering of the results? What i did was
<?php
if($results)
{
$x = 1;
foreach ($results as $data)
{
echo '<tr>';
echo '<td>' . $x++. '</td>';
echo '<td>' . $data->item_id . '</td>';
echo '<td>' . $data->item_name . '</td>';
echo '<td>' . $data->item_category . '</td>';
echo '<td>' . $data->item_costprice . '</td>';
echo '<td>' . $data->item_retailprice . '</td>';
echo '<td>' . $data->item_tax . '%</td>';
echo '</tr>';
}
}
?>
But it has the same numbering per page.

The numbering starts with 1 because thats the value you assign to it before the loop starts.
Assuming you pass the page number to your controller your could do the following:
$x = count($results) * ($pagenumber-1);//use count or some other type of count method
This will make the $x value 1 on the first page, 31 on the second page, 61 on the third page etc...
Then inside the loop change $x++ to ++$x otherwise the first page will be counted from 0 to 29.

Related

Parsing an Object with PHP

I have the following format:
{ "_id" : { "$oid" : "61f41a529210000060005487" }, "number" : "3", "name" : "Honduras", "description" : "1500m height farming, price per 200gr", "price" : 7, "stock" : 10, "stars" : "Rated with 4.0 stars by users." }
I need to parse a few entries like the above in PHP. I am using foreach for that purpose. Everything except {"_id": {"$oid": ... }} field seems to be parsed correctly. I get the following error:
Fatal error: Uncaught TypeError: Cannot access offset of type string on string
PHP script:
$products = json_encode($cursor['products']);
$products = json_decode($products, true) ;
foreach ($products as $product) {
$stringID = (string)$product['_id'];
echo '<tr>';
echo '<td>' . $stringID . '</td>';
echo '<td>' . $product['number'] . '</td>';
echo '<td>' . $product['name'] . '</td>';
echo '<td>' . $product['description'] . '</td>';
echo '<td>' . $product['price'] . '</td>';
echo '<td>' . $product['stock'] . '</td>';
echo '<td>' . $product['stars'] . '</td>';
echo '</tr>';
}
I have tried many solutions can't parse it though. Any suggestions?
$product is a string, not an object. You should json_decode($product) before to access to properties.
foreach ($products as $product)
{
$product = json_decode($product); // << decode here
$stringID = $product['_id']['$oid']; // << access to ID
// echo row :
echo '<tr>';
echo '<td>' . $stringID . '</td>';
echo '<td>' . $product['number'] . '</td>';
echo '<td>' . $product['name'] . '</td>';
echo '<td>' . $product['description'] . '</td>';
echo '<td>' . $product['price'] . '</td>';
echo '<td>' . $product['stock'] . '</td>';
echo '<td>' . $product['stars'] . '</td>';
echo '</tr>';
}

Creating HTML table with POST

I am trying to create a webpage that takes user input, posts it to a creation page and then creates another webpage displaying the data in a html table. I am using file_put_contents to create the webpage and I am getting an error whenever I try to include a loop to get the content from 3 td's to be output. Here is what I have so far. I will mention I posted earlier, however I deleted that as that post was confusing, lacked detail and examples.
This is the input field for user (deleteBox does not get posted)
The user will input: An ingredient, the amount and a note (txt is an empty variable)
<tr><td><input type='checkbox' name='deleteBox'></td><td>" + "<input type='text' name='ingredient[]' value='" + txt + "'/>" + "</td><td>" + "<input type='text' name='amount[]' value='" + txt + "'/>" + "</td> + <td>" + "<input type='text' name='note[]' value='" + txt + "'/>" + "</td></tr>
I've tried the below with both the for loop in a variable and directly entering it into the $strOut. I don't really know if this is how you're supposed to do it (clearly not, as it doesn't work -> gives error unexpected 'for' on line 42 [$tbl line]) and cannot find a work around. How would I make the array data display in the output for file_put_content so that it doesn't create errors?
$ingredient = $_POST['ingredient'];
$amount = $_POST['amount'];
$note = $_POST['note'];
$tbl = for ($i = 0; $i < count($ingredient); $i++) {
echo '<tr>';
echo '<td>' . $ingredient[$i] . '</td>';
echo '<td>' . $amount[$i] . '</td>';
echo '<td>' . $note[$i] . '</td>';
echo '</tr>';
}
$strOut = '<!DOCTYPE html>'
. '<table class="recipe-ingredients">'
. '<tr>'
. '<th class="table-th-large">Ingredient</th>'
. '<th class="table-th-medium">Amount</th>'
. '<th class="table-th-xl">Note</th>'
// User input tr goes here
. $tbl
. '</table>';
// Closing html tags after
file_put_contents("example.php", $strOut);
Any suggestions or work arounds would be much appreciated.
$tbl = '';
for ($i = 0; $i < count($ingredient); $i++) {
$tbl.= '<tr>';
$tbl.= '<td>' . $ingredient[$i] . '</td>';
$tbl.= '<td>' . $amount[$i] . '</td>';
$tbl.= '<td>' . $note[$i] . '</td>';
$tbl.= '</tr>';
}
Use this.
By the way, You forgot to put a closing </tr> in the heading row.

How to customize pagination in laravel to show a number of elements for each page

my application basically generate reports for some users
I fetch the rows from the database
every row contains a 'name' and 'work time' and others...
every user has multiple rows with the same name but a different work time value
I calculated the total of the 'work time' in the blade depending on the next row if its the same name I 'll keep adding work time, if the next row is for another user then I will show the total for the current user and start calculating the total for the next user
my problem is with pagination:
at the end of each page, the rows of the same user do not appear all of it
and my blade logic calculate only what appears on that page (see the picture)
notice here that other rows with the same name exist in the second page
and their total calculated separately
is there is any way to tell the paginator to devise rows on a condition besides that number ->paginate(15+condition) because I want all the rows for a specific user to stay on the same page.
this is the query:
$shifts = DB::table('driver_shifts')
->join('drivers','drivers.PermitNumber','=','driver_shifts.driver_badge_id')
->where ('driver_shifts.shift_start_time','>',$startDate)
->where ('driver_shifts.shift_start_time','<',$endDate)
->orderby('drivers.LastName')
->orderby('driver_shifts.shift_start_time')
->paginate(20)
->appends(request()->query());
this is the blade logic inside a table. Ps: I know it not a good practice but I coudn't find a better way to do it in the controller:
$total_time = 0.00;
$break_time = 0.00;
$work_time = 0.00;
for ($i = 0; $i < count($shifts); $i++)
{
$break_time_item = $shifts[$i]->total_time - $shifts[$i]->work_time;
echo '<tr>';
echo '<td>' . $shifts[$i]->FirstName . ' ' . $shifts[$i]->LastName . '</td>';
echo '<td>' . $shifts[$i]->vehicle_id . '</td>';
echo '<td>' . $shifts[$i]->shift_start_time . '</td>';
echo '<td>' . $shifts[$i]->shift_end_time . '</td>';
echo '<td>' . $shifts[$i]->total_time . '</td>';
echo '<td>' . $break_time_item . '</td>';
echo '<td>' . $shifts[$i]->work_time . '</td>';
echo '</tr>';
//calculation totals for the current driver
//dd($shifts[$i]->break_time);
$total_time = $total_time + $shifts[$i]->total_time;
$work_time = $work_time + $shifts[$i]->work_time;
if (isset($shifts[$i + 1]))
{
if ($shifts[$i]->LastName !== $shifts[$i + 1]->LastName)
{
//calculating break time
$break_time = $total_time - $work_time;
//converting minutes to houres
$total_time_hours = floor($total_time / 60);
$total_time_min = $total_time - ($total_time_hours * 60);
$work_time_hours = floor($work_time / 60);
$work_time_min = $work_time - ($work_time_hours * 60);
$break_time_hours = floor($break_time / 60);
$break_time_min = $break_time - ($break_time_hours * 60);
echo '<tr class="table-primary text-uppercase ">';
echo '<td colspan="4">' . $shifts[$i]->FirstName . ' ' . $shifts[$i]->LastName . ' TOTAL ' . '</td>';
if ($total_time_hours == 0 && $total_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $total_time_hours . "h:" . $total_time_min . "min" . '</td>';
}
if ($break_time_hours == 0 && $break_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $break_time_hours . "h:" . $break_time_min . "min" . '</td>';
}
if ($work_time_hours == 0 && $work_time_min == 0)
{
echo '<td>-</td>';
}
else
{
echo '<td>' . $work_time_hours . "h:" . $work_time_min . "min" . '</td>';
}
echo '</tr>';
$total_time = 0;
$break_time = 0;
$work_time = 0;
}
}

PHP - json - function call issue

I am having two functions printBefore(json1) and printAfter(json2).
Both are having json object as parameter.jsons are coming from database. They are parsing json and displaying it in correct format.
But it is taking more than 30 seconds.
When I removed functon and displays json as it is. It is not taking time more than second.
I am not able isolate problem. Please Help.
foreach ($records as $record) {
echo '<tr>';
echo '<td>' . $userNames['users'][$record->uid] . '</td>';
echo '<td>' . $userNames['users'][$record->uid] . '</td>';
echo '<td>' . $record->val . '</td>';
echo '<td>' . $record->mi. '</td>';
echo '<td>' . $record->created_on . '</td>';
echo '<td>' . printBefore($record->newData) . '</td>';
echo '<td>' . printAfter($record->oldData) . '</td>';
echo '</tr>';
}
Code for printAfter($oldData)
$oldData = json_decode($oldData, true);
if (isset($OldData['sub_category'])) {
$str = $str . "|Category : " . $this->category[$OldData['sub_category']] . "|";
}
code for PrintBefore($newData)
$newData = json_decode($newData, true);
if (isset($newData['sub_category'])) {
$str = $str . "|Category : " . $this->category[$newData['sub_category']] . "|";
}

mysql & php search highlighting

Wondering if someone could help give me a push in the right direction, I am building a search function (php and mysql) which will display search results and highlights keywords that the user has searched for. at the moment I grab the search criteria that the user has entered and query that against the database which works fine to get the desired results. the problem I have is
$highlight = preg_replace("/".$_GET['criteria']."/", "<span class='highlight'>".$_GET['criteria']."</span>", $_row['name']);
This will only highlight a phrase and not individual keywords. so for example if the document was called "Hello world" and the user typed this exactly it would highlight no problem however if the user typed "world hello" it will not highlight anything. I thought it would be a good idea to take the search criteria and use explode and check each word individually but this seems to fail as well. here is my query and how I am displaying results
$sql = "SELECT *
FROM uploaded_documents
WHERE dept_cat = 'procedures'
AND cat =:cat
AND keywords REGEXP :term ";
$result->execute(array(':cat' => $_GET['category'],':term' => $_GET['criteria']));
//display results
while($row = $stmt->fetch()){
$explode_criteria = explode(" ",$_GET['criteria']);
foreach($explode_criteria as $key){
$highlight = preg_replace("/".$key."/", "<span class='highlight'>".$key."</span>", $row['name']);
echo '<td><a target="_blank" href="'.$row['url'].'">'.$highlight.'</a></td>';
echo '<td>'.$row['version'].'</td>';
echo '<td>'.$row['cat'].'</td>';
echo '<td>'.$row['author'].'</td>';
echo '<td>'.$row['added'].'</td>';
echo '<td>'.$row['auth_dept'].'</td>';
echo '<td>';
}
}
For the sake of length I have omitted code here and tried to keep it minimal, I have been trying to base my work on the following post
highlighting search results in php/mysql
I think my first problem is the foreach loop in the while loop duplicating results but I cant think of a way around it.
Thanks in advance
In this block of code:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
foreach ($explode_criteria as $key)
{
$highlight = preg_replace("/" . $key . "/", "<span class='highlight'>" . $key . "</span>", $row['name']);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}
The loop is constantly referring to $row['name'], so the replacement is done, but the next time the loop happens it is replacing the next word on the original unmodified $row['name']
I think this should help you:
//display results
while ($row = $stmt->fetch())
{
$explode_criteria = explode(" ", $_GET['criteria']);
$highlight = $row['name']; // capture $row['name'] here
foreach ($explode_criteria as $key)
{
// escape the user input
$key2 = preg_quote($key, '/');
// keep affecting $highlight
$highlight = preg_replace("/" . $key2 . "/", "<span class='highlight'>" . $key . "</span>", $highlight);
echo '<td><a target="_blank" href="' . $row['url'] . '">' . $highlight . '</a></td>';
echo '<td>' . $row['version'] . '</td>';
echo '<td>' . $row['cat'] . '</td>';
echo '<td>' . $row['author'] . '</td>';
echo '<td>' . $row['added'] . '</td>';
echo '<td>' . $row['auth_dept'] . '</td>';
echo '<td>';
}
}

Categories