PHP: Display number of links based on variable - php

I have a variable $h, which counts the number of properties in my foreach. I then have a number stored in $nb_elem_per_page to limit the number of properties shown per page.
<?php $sum_total = ceil($h / $nb_elem_per_page); ?>
I then use the above to work out how many pages these will need. For example $nb_elem_per_page is currently 12. So if $h was 123 it would need 11 pages.
Is there a way for me, using the number from $sum_total create links for the number here, such as:
http://www.website.co.uk/properties/search/?bed=4&go=1
http://www.website.co.uk/properties/search/?bed=4&go=2
http://www.website.co.uk/properties/search/?bed=4&go=3
So it outputs the number of links based on the number in $sum_total, but on each one the end number goes up 1 each time as above? This would then be my pagination.

for ($i = 1; $i < $sum_total; $i++) {
echo "{$i}";
}
Or whatever you would like.
You can refer to php's documentation here : http://php.net/manual/en/control-structures.for.php

Related

Generating rows and columns within a given range of numbers - PHP

I'm trying to build a little program to automatize a little task; in it, one can write two values (as inputs in a form) and then, in another page, numbers will be written in rows and columns with all the numbers within the range given.
The thing is... There will (almost always) be 500 numbers in the range, so I want the result to be displayed in 10 columns of 50 rows each, something like this image:
I wanted the result page (the one with all the numbers) to THEN give the possibility to export this as xml or pdf, but that's another story..
By now, I just would like to get the display right.
I'm using HTML and PHP on XAMPP. I've been able to get the first column... but can't get to the generations of the others, like this (and so on until 50):
So far, this is what I have, I'll put here my PHP code, because I don't think the HTML is relevant (but will post it too if it's needed) - (The values are passed by POST method)
<?php
$cont = 0;
$i = 0;
$arrayValues = range($valueFrom, $valueTo);
if($cont <= 50){
echo "<tr>";
for($i = 0; $i < 50; $i++){
echo "<p>$arrayValues[$i]</p>";
$cont++;
}
echo "</tr>";
}
?>
Also, using something like this --> Use PHP to Generate HTML table with static Cell Count of MySQL Data and worked ok, but I would the numbers displayed in columns... so not in rows (horizontally), so..
YES | NO
1 4 7 | 1 2 3
2 5 8 | 4 5 6
3 6 9 | 7 8 9
That's why I stopped trying with tables and just used a tag for the result.
EDIT:
So, given the "start" value and "end" value for a range, I would like to be able to print all those values in 10 columns of 50 rows each (so when it reaches 50 rows in the first column, it automatically moves to a new column and so on), and vertically (like the little diagram I tried to put above this, in the "YES" side)
Any ideas or guidance will be appreciated! :)
Have a nice rest of the week!
You can do this by splitting your range up into chunks and indexing into the resulting array of columns in a nested loop.
<table>
<?php
$valueFrom = 1;
$valueTo = 500;
$rowCount = 50;
$columns = array_chunk(range($valueFrom, $valueTo), $rowCount);
$columnCount = count($columns);
for ($row = 0; $row < $rowCount; $row++) {
echo '<tr>';
for ($column = 0; $column < $columnCount; $column++) {
$number = $columns[$column][$row] ?? '';
echo "<td>$number</td>";
}
echo '</tr>';
}
?>
</table>

Dynamic pagination by counting the number of rows and a pre-defined result per page (rpp) with PHP

I whipped up some code to count the number of rows from my database, and through a little script, display "Page : 1 2 3 4 5" etc.
Here is my code:
$totalRows = mysql_num_rows
$rpp = 20
$totalPages = ceil($totalRows/$rpp);
$i;
for (i=0; i<totalPages; i++){
echo "Page: " . "<a href='index.php?page=\"$i\"rpp=20>\"$i\"</a>";
}
Does this look good? Do I need anything else?
EDIT 1: Added ceil() to round up. No more missing results :-)!
I think you need to round $totalPages to the next highest integer, ie.
$totalPages = ceil($totalRows/$rpp);

How i generate lotto number using ajax and PHP

I want to generate the lotto number like generating here
http://www.nationallottery.co.za/lotto_home/NumberGenerator.asp
may i know what will be the logic or way to generate the lotto number using PHP,mysql and Ajax.
I will be thankful of you.
Sample Example:
$generated = array();
while (count($generated) < 6)
{
$no = mt_rand(1, 49);
if(!array_search($no, $generated))
{
$generated[] = $no;
}
}
echo implode(" : ", $generated);
You just need to generate random numbers.
Create multiple random numbers and style them however you want them. That site appears to have replaced text numbers with images which are probably programatically coded. If you want multiple rows like they offer, just make a form like they have and return the correct number of rows. Shouldn't be too hard
The php function you are looking for is mt_rand()
Use it to generate an integer between two given values, something like this:
<?php
for($i = 1; $i <= 6; $i++){
echo mt_rand(1,45).' ';
}
?>

php calculation solution for pagination

I have a variable $total which is the total number of results and $page which is the page number. The result is limited to 12 per page.
Suppose if $total is 24, the script may return 1 and 2 for $page=1 and $page=2 respectively. It should also return 1 if the input number is less than 1 (negative or zero) or if the number is greater than 2
Again, suppose if $total is 25, the script may return 1, 2 and 3 for $page=1, $page=2 and $page=3 respectively. It should also return 1 if the input number is less than 1 (negative or zero) or if the number is greater than 1
Here's one way to calculate it:
// Assuming you have the $total variable which contains the total
// number of records
$recordsPerPage = 12;
// Declare a variable which will hold the number of pages required to
// display all the records, when displaying #recordsPerPage records on each page
$maxPages = 1;
if($total > 0)
$maxPages = (($total - 1) / $recordsPerPage) + 1;
// $maxPages now contains the number of pages required. you can do whatever
// it is you need to do with it. It wasn't clear from the question..
return $maxPages;
Further, if you wanted to generate an array containing the indexes of each available page you could just do this:
$pages = array();
for($i = 1; $i <= $maxPages; i++)
{
array_push($pages, $i);
}
print_r($pages);

SQL - adding result page numbers based on number of results?

I have a SQL query that separates the results into groups of 25:
$page = 0; // 1, 2, 3
$perPage = 25;
....
ORDER BY c.date DESC LIMIT " . ( $page * $perPage ) . ", " . $perPage);
I'm trying to figure out how I write something that loops through the number of $page returned and then echos a number back to the screen. Ie. Page 1, Page2, Page3 - based on $page.
I will assume the search PHP page is driven throught GET parameters like page like a.php?page=5 or something.
In that case, try something of this sort:
$total = //total no. of rows. if using mysql use: mysql_numrows($resource);
$total = ceil($total/$perPage);
for ($i=0; $i<$total; $i++){
echo 'Page ' . ($i+1) . '';
}
This is just to give you a kick start. You can obviously, take this ahead in the way you would like.
What you are doing is called Pagination. Just google for PHP Pagination, and you will end up with lots of results. And most of them are just too easy to use.
Try Zend Paginator: http://framework.zend.com/manual/en/zend.paginator.html

Categories