I'm trying to implement pagination for a search result. The following code works perfectly:
echo "<p>" . $data['meta']['total'] . " properties found. (search " . $data['meta']['searchId'] . ")</p>\n";
$pages = $data['meta']['total'] / $count;
$pages = ceil($pages);
echo "<p>" . $pages . "</p>\n";
However, if I add in the following, I get a timeout:
$page = 1;
echo "<p>";
while ($page <= $pages); {
echo $page++ . " ";
}
echo "</p>\n";
No doubt I'm missing something obvious.
Your error is here:
while ($page <= $pages); {
//^ See this empty statement here!
echo $page++ . " ";
}
Your while loop loops trough a empty statement so no statement is going to increment $page. So your curly brackets are just a normal code block, in order to get your while loop working just remove the semicolon like this:
while ($page <= $pages) {
echo $page++ . " ";
}
Related
I want to manipulate the end output of a while loop.
In this example, i am using number 5 in place of a variable.
<?php $i=1; while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
$i++;
} ?>
giving output of
col-md-2
col-md-2
col-md-2
col-md-2
col-md-2
---> I want to show 4 here by applying the below math >>
12-(5-1)*round(12/5)
Can you please help me with this. is it possible?
A help will be very much appreciated.
An alternative is to use a for() loop and end it one earlier and then output an extra column after...
$count = 5; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . round(12/$count) . "<br>";
}
echo "col-md-" . (12-($count-1)*round(12/$count)) . "<br>";
Which gives...
col-md-2<br>col-md-2<br>col-md-2<br>col-md-2<br>col-md-4<br>
One option could be to check if the variable $i equals 5 before it gets incremented:
$i=1;
while($i<=5) {
echo "col-md-" . round(12/5) . "<br>";
if ($i === 5) {
echo "col-md-" . (12-(5-1)*round(12/5));
}
$i++;
}
If it is always at the end, you can just add it after the while loop:
echo "col-md-" . (12-(5-1)*round(12/5));
This is the final code, which solved my issue.
$count = 1; // Number of columns
for ( $i = 1; $i < $count; $i++ ) {
echo "col-md-" . floor(12/$count) . "<br>";
}
if (($count == 5 || 7 || 8 || 9 || 10 || 11)) {
echo "col-md-" . (12-($count-1)*floor(12/$count)) . "<br>";
}
Thanks to #nigel and #thefourthbird.
hello guys i need to find the differences between two different loops which is summation loop and totals loop as shown in controller code below
function summary() {
echo '<table>';
$summation=$this->select_model->sum_income("date_of_income BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($summation as $sum){
echo '<tr><td>Total Income</td> <td >'.$sum['total'].'</td></tr>';
}
$totals=$this->select_model->sum_expenditure("date_of_expenditure BETWEEN '" . $start . "' AND '" . $last . "'");
foreach($totals as $total){
echo '<tr><td>Total Expenditure</td><td ><strong>'.$total['total'].'<</td></tr>';
}
//display differences here:
echo '<tr><td>'.**$sum['total']-$total['total']**.'</td></tr>'
echo '</table>';
}
You would have to create the variables outside of the loops and add (or subtract) as the loop progresses.
$sum = 0;
$total = 0;
foreach... {
$sum += $sum;
}
foreach... {
$total += $total;
}
echo $sum - $total;
If you're adding negative numbers, the math will work without having to do anything specific.
I am having problems printing out a timetable using the results from a SQL statement and some HTML layout with PHP.
I have the times of the classes along the top of the page.
I am trying to put the days of the week along the side of the page and then check if the result of the SQL statement (containing a module) should be printed in the specific day and time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th></th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
while($row = mysqli_fetch_array($result1)) {
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "$printday";
for($t = 9; $t < 17; $t++) {
if($row['Day'] == $d && $row['Time'] == $t){ //fix this area so that it moves along
echo "<td>" . $row['ModuleName'] . "<br/>\n " .$row['Location'] . "</td>";
} //if
else {
echo "<td></td>";
} //else
} //for 2
echo "</tr>";
} //for 1
} //while
The problem is that I am printing out Monday-Friday 3 times as there are 3 $row results. Any idea how I could get this to work.
Your looping through your data in the wrong spot, you first need to put that in an array so you can loop through it for each day/time.
//Print out the top of the array to display the times
echo "<div><table class='table table-bordered'><thead><tr><th>Day</th><th>9-10</th><th>10-11</th><th>11-12</th><th>12-13</th><th>13-14</th><th>14-15</th><th>15-16</th><th>16-17</th></tr></thead><tbody>'";
//Now loop through the result of the SQL statement that contains the modules associated with the selected course
$courses = array();
while($row = mysqli_fetch_array($result1)) {
$courses[] = $row;
} //while
for($d = 1; $d < 6; $d++){
$printday = $days[$d];
echo "<tr><td>" . $printday . "</td>";
for($t = 9; $t < 17; $t++) {
unset($course_row);
foreach($courses as $course){
if($course['Day'] == $d && $course['Time'] == $t){ //fix this area so that it moves along
$course_row .= $course['ModuleName'] . "<br/>\n " .$course['Location'] . "<br/>\n<br/>\n";
} //if
}
if(isset($course_row)){
echo "<td>" . $course_row . "</td>";
} else {
echo "<td> </td>";
} //else
} //for 2
echo "</tr>";
} //for 1
Here is how the index will show the links for pagination:
total record = 40
per_page = 2
and now for the link generating:
<?php
if ($pagination->total_pages() > 1) {
if ($pagination->has_previous_page()) {
echo "<a href='index.php?page=";
echo $pagination->previous_page();
echo "&refone=" . $refone ."'>« PREVEOUS</a> ";
}
for ($i = 1; $i <= $pagination->total_pages(); $i++) {
if ($i == $page) {
echo " <span class=\"selected\">{$i}</span> ";
} else {
echo " <a href='index.php?page=" . $i . "&refone=" . $refone ."'>" . $i . "</a> ";
}
}
if ($pagination->has_next_page()) {
echo " <a href='index.php?page=";
echo $pagination->next_page();
echo "&refone=" . $refone."'>NEXT »</a> ";
}
}
?>
metion code will generate the links for pagination but the problem is it is showing many links
for example:
we have 40 record in each page we need to show 2 records so it will generate 20 links( for ($i = 1; $i <= $pagination->total_pages(); $i++) {) here is the code which will calculate for the links but I want to echo only 8 links the rest should be hid like
1-2-3-4-5-6-7-8-Next
prev-2-3-4-5-6-7-8-9-next
but its showing all
I found my answer::
here I need to change the code for the for statement:
for ($i = $page - $per_page; $i <= $page + $per_page; $i++){
this will work for me.
When I check this url for page pagination, pagination worked and show page result in page and page=+1:
mydomain/search.php?page=1
mydomain/search.php?page=2
But when I check with this url:
mydomain/search.php?page=-1
mydomain/search.php?page=-2
I see this error:
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '-20, 10' at line 1
I print result using this pagination code:
// If number of results is bigger than the maximum number
// of search results set in config we start the pagination
if ( $results > $conf['search_results'] )
{
// Calculate the first number of page to show
// This makes the list of pages numbers smaller
if ((($page*$conf['search_results'])-($conf['search_results']*5)) >= 0)
$first=($page*$conf['search_results'])-($conf['search_results']*5);
else
$first=0;
// Calculate the last element of the pagination list
if ((($page*$conf['search_results'])+($conf['search_results']*6)) <= $results)
$last =($page*$conf['search_results'])+($conf['search_results']*6);
else
$last = $results;
# $i=$first/$conf['search_results'];
// Previous link
if ($page > 0)
{
$pagenum = $page - 1;
echo ' <a style="float:left;" href="' . URL . '/search.php?page=' . $pagenum . '&' . $session->fetch('listingsearchvariablespage') . '">PRE</a> | ';
}
// Middle pagination
for ( $step = $first; $step < $last; $step=$step+$conf['search_results'] )
{
if ( $i == $page )
{
$pagenum = $i+1;
echo ' <span class="warning">' . $pagenum . '</span> | ';
$i++;
}
else
{
$pagenum = $i+1;
echo ' ' . $pagenum . ' | ';
$i++;
}
}
// Next link
if ($page - (($results / $conf['search_results']) - 1) < 0)
{
$pagenum = $page+1;
echo ' <a style="float:right;" href="' . URL . '/search.php?page=' . $pagenum . '&' . $session->fetch('listingsearchvariablespage') . '">NEXT</a>';
}
}
Now, how do can I fix this error for Negative number and prevent any attack?
I'd recommend something simple like:
if($page < 1)
$page = 1;
LIMIT -20,10 is invalid syntax - you can't have a negative offset. You should clamp the value to the valid range of pages.