Infinate scrolling on tablets while pulling content with a Json - php

Im using the scrollExtend plugin to call a Json script which will spit out my content.
This works fine on desktops and works well however my site is responsive and I will need this functionality to work on tablets and mobile devices aswell.
I have conducted a lot of test and I have narrowed it down to my Json script:
<?php
$parent_query = bb_query("SELECT f_children
FROM t_articles
WHERE f_id= '85' and (f_removed IS NULL or f_removed ='0')
");
$parent_row = bb_fetch_assoc($parent_query);
$children = explode(':',$parent_row['f_children']);
$i= 1;
$rowsperpage = 6;
$totalpages = count($children);
$_SESSION['highlights_current_page']++;
if (isset($_SESSION['highlights_current_page']) && is_numeric($_SESSION['highlights_current_page'])) {
$currentpage = (int) $_SESSION['highlights_current_page'];
}
if ($currentpage > $totalpages) {$currentpage = $totalpages;}
if ($currentpage < 1) {$currentpage = 1;}
$offset = ($currentpage-1) * $rowsperpage;
$x=0;
##take off earlier articles
while($x < $offset){
array_shift($children);
$x++;
}
##take off later articles
$children = array_slice($children, 0, $rowsperpage);
echo $_SESSION['highlights_current_page'];
echo "current:".$currentpage;
echo "total:".$totalpages;
echo "offset:".$offset;
foreach($children as $child)
{
$child_query = bb_query("
SELECT *
FROM t_articles
WHERE f_id = {$child}
and (f_removed IS NULL or f_removed ='0')
AND f_live ='1'
");
while ($row = bb_fetch_assoc($child_query)){
//data for echoing the articles
}
}
?>
When it is displayed on desktops and you scroll down it will then echo the next 6 articles and then if you continue the next 6 and so on. but for tablets it will echo the next 6 articles out and then it when you scroll down it will echo the same 6 articles over and over.
Here is my math for the blocks on desktops:
First echoed block of articles: $_SESSION['highlights_current_page']:2 current:2 total:20 offset:6
second echoed block of articles: $_SESSION['highlights_current_page']:3 current:3 total:20 offset:12
third echoed block of articles: $_SESSION['highlights_current_page']:4 current:4 total:20 offset:18
$_SESSION['highlights_current_page'] is the number of how many blocks it has echoed. e.g: the first six echoed would be 2 because six articles start off as displayed are classed as 1.
On tablets it will repeat:
First: $_SESSION['highlights_current_page']:2 current:2 total:20 offset:6
Second: $_SESSION['highlights_current_page']:2 current:2 total:20 offset:6
Third: $_SESSION['highlights_current_page']:2 current:2 total:20 offset:6
I'm rather new to programming for tablets is there something that I've added that wont work, or can anyone see the issue with my maths?
Thank you in advance

Related

Splitted pages highlighting with php possible?

I've got a page that gets the latest news from my database.
There are a lot of rows so i split the pages.
It works just fine but how can i highlight on which page they are?
Here is the code
$pagesplit = $pdo->prepare("SELECT COUNT(id) FROM News");
$pagesplit->execute();
$row = $pagesplit->fetch(PDO::FETCH_NUM);
$records = $row[0];
$pages = ceil($records / 20);
for ($i=1; $i<=$pages; $i++) {
echo "<a href='news.php?p={$i}'>{$i}</a> ";
};
What is the best way too highlight these splitted pages?
First you need to get what page they're on...
$_GET['p']
Then it's a matter of checking that against the loop counter to see if there is a match.
if ($i == $_GET['p']) {
// You're on this page
}

How to hide the next button when no pages to dispaly

Can someone help me please? I am sure it is easy for you guys. I am battling to find a solution on how to hide the next link when there are no pages to display my code is as follows:
if (!isset($_GET['page']) or !is_numeric($_GET['page'])) {
$page = 0;
} else {
$page = (int)$_GET['page'];
}
$pages_query=mysql_query ("SELECT COUNT * FROM hardware");
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
echo 'Next<p>';
$prev = $page - 3;
//only print a "Previous" link if a "Next" was clicked
if ($prev >= 0) {
echo 'Previous';
}
You can use mysql_num_rows($result) to get the number of records in hardware:
$result = mysql_query("SELECT * FROM hardware LIMIT $page, 3");
$record_count = mysql_num_rows($result);
if ($record_count > 1)
echo 'Next';
in your if statement check if the $page is greater than 0 then according to the outcome of the value of $page write your code. you can use another if statement in the first if statement and make it detect the situation and decide what to do. The other thing is if the user clicked next then the user is on the second page so your previous should appear if $prev is higher than 1 it should make it
something along the lines of:
$itemsPerPage = 3;
$sql = "SELECT * FROM hardware";
$result = mysql_query($sql);
$count = mysql_num_rows($result);
$pageCount = $count/$itemsPerPage;
if($pageCount > $page) { //Are there more pages worth of items stored, than we're currently looking at?
echo 'next';
}
You want to be using OFFSET in your SQL syntax, as well as LIMIT.
LIMIT limits the number of rows returned.
OFFSET tells it to start a number of rows into the result set.
You need to limit to the number of items you want on a page. and offset by that number*page.
Hopes this helps.

echo a certain number of results per page in php

I have a query and loop that displays products depending on an id. Sub-Category id in this case. The code is as follows:
<div id="categoryproducts">
<?php
$productsGet = mssql_query("SELECT * FROM Products WHERE SubCatID = ".$_GET['scid']."");
while ($echoProds = mssql_fetch_array($productsGet)) {
?>
<div class="productbox">
<div class="productboximg">
<img src="<?php echo $echoProds['ProdThumb']; ?>" height="58" width="70" alt="" />
</div>
<div class="productboxdtl">
<h3><?php echo $echoProds['Title']; ?></h3>
<p><?php echo $echoProds['Synopsis']; ?></p>
</div>
<div class="productboxprc">
Price <strong>£<?php echo $echoProds['Price']; ?></strong>
</div>
<div class="productboxmore">
</div>
</div>
<?php
}
?>
<div id="shoplistpagesbot" class="shoplistpages">
Results Pages: 1 2 [Next »]
</div>
I am unsure how to display a certain number of products per page, as shown there is a mechanism for changing between pages, I need to somehow code that after a certain number of products, say 5 for example, the remainder are displayed on the next page.
Can anyone suggest how to do this? Or point me in the correct dirrection as to which functions I should be looking into.
Sorry if it isn't very clear, I am new to PHP. The DB im using is a MS SQL one not MySQL
Depends what version of MSSQL you are using.
I'm not a user of MSSQL, but apparently the SQL Server 2000 uses the TOP command, whilst SQL Server 2005 uses the BETWEEN command. A Google search should provide you with several tutorials on each but I am going to assume that you are using the 2005 version.
To return the items on page number $page_number, the general algorithm is :
// The most common way to specify which page to display is a GET variable. There
// are others ways and if you'd prefer them, just set $page_number to get the
// number from there instead. Don't forget to filter all data from the GET array
// as a user may try to insert harmful data, such as XSS attacks.
$page_number = $_GET['page'];
$scid = $_GET['scid'];
// Calculate the range of items to display.
$min = $page_number * $items_per_page;
$max = $min + $items_per_page;
$sql = "SELECT * FROM Products WHERE SubCatID = \"{$scid}\" BETWEEN {$min} AND {$max}";
To get the remaining number of items, you'll need a separate database query returning the total number of items.
$sql = "SELECT COUNT(*) FROM Products WHERE SubCatID = \"{$scid}\"";
// Using the same $max as before as it is the number of items on the page plus
// total items on previous pages, but we'll redefine it here just in case.
$max = ($page_number * $items_per_page) + $items_per_page;
// Assume that $total_rows is the number returned from executing the count query.
$remaining_items = $total_rows - $max;
Now to generate links to all the other pages.
$current_page = $_GET['page'];
$total_pages = $total_rows / $items_per_page;
if($current_page != 1) {
$previous = $current_page - 1;
echo "Previous";
}
for($i = 1; $i <= $total_pages; $i++) {
echo "{$i}";
}
if($current_page != $total_pages) {
$next = $current_page + 1;
echo "Next";
}
$pagenumber = $_GET['pagenumber'];
$recordsperpage = 30;
$first = ($pagenumber*$recordperpage)-$recordperpage;
$last = $pagenumber*$recordsperpage;
$productsGet = mysql_query("SELECT * FROM Products WHERE SubCatID = '".$_GET['scid']."' LIMIT $first,$last");
Have this at the top of the page and pass a GET parameter in your links e.g. browse.php?pagenumber=1;
You can calculate the number of pages by dividing total rows in the recordset by your $recordsperpage variable.
Then just use a simple for loop to output the navigation links e.g.:
for($i = 1; $i <= $totalpages; $i++) {
echo "<a href='browse.php?pagenumber=$i'>$i</a>";
}
Where $totalpages is the result of dividing total rows in the recordset by your $recordsperpage variable.
Hope this helps
Here it is for MS SQL:
SELECT TOP 10 *
FROM (SELECT TOP 20 * FROM products ORDER BY ID) as T
ORDER BY ID DESC
Basically are selecting the top 10 records from the top 20 in reverse order here. Hence you get the second 10 in the recordset.
Replace the 10 with the number of records per page and the 20 with the $last variable above.
Hope this clears it up

Pagination with PHP and MySQL

I've tried a load of different code snippets that are out there for using pagination in my result set. I've got the same problem in each case. Using LIMIT on the end of the query I can display the first page correctly along with the navigation links for the correct number of pages (so the code doing the calculations with number of rows returned and number to display on each page is right), but when you click to follow the link to any of the other pages then they're blank. So how do I get the updated query to run again and display my results on the subsequent pages?
This is one piece of code that I've tried:
$rowsperpage = 10;
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) { $currentpage = (int) $_GET['currentpage']; }
else { $currentpage = 1; }
// if current page is greater than total pages...
if ($currentpage > $totalpages) { $currentpage = $totalpages; }
// if current page is less than first page...
if ($currentpage < 1) { $currentpage = 1; }
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
// run the query
$sql = "SELECT * FROM table WHERE keyword LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $link) or trigger_error("SQL", E_USER_ERROR);
// while there are rows to be fetched...
while ($list = mysql_fetch_assoc($result))
{
//echo data for each record here
}
// building pagination links
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'><<</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'><</a> ";
} // end if
Not permitted to show rest of navigation code due to too many hrefs, but the links appear correctly. The problem seems to be how the query is carried over to the next page.
You have to use limit offset, rowcount.
Typically for nth page
offset= (n-1)*pagesize and rowcount=pagesize

PHP displaying data in a 5 by 3 table with pagination that contains thumbs and other info

I am making a website that will show night club revelers events and night establishments in a big city. Events could number in the hundreds. I would like to have data displayed in a 5 by 3 table that contains thumbs and other information with paging.
For example:
Thumb_event1 Thumb_event2 Thumb_event3 Thumb_event4 Thumb_event5
event_name1 event_name2 event_name3 event_name3 event_name5
Date Date Date Date Date
Thumb_event6 Thumb_event7 Thumb_event8 Thumb_event9 Thumb_event10
event_name6 event_name7 event_name8 event_name9 event_name10
Date Date Date Date Date
Thumb_event11 Thumb_event12 Thumb_event13 Thumb_event14 Thumb_event15
event_name11 event_name12 event_name13 event_name14 event_name1
Date Date Date Date Date5
Records 1 to 15 of 36 First Previous Next Last
How does one achieve this? Any help will be greatly appreciated. I have made PHP code before to display data in a similar fashion but without pagination.
This was to display some picture thumbs only some time back:
<?php
echo "\n<table>";
$i = 5;
while ($pictures = mysql_fetch_assoc($pictures_result))
{
if($i==5) echo "\n\t<tr>";
echo "<td width='100' height='100' align='center' valign='middle'><a href = 'picture_view.php?picture_id=$pictures[picture_id]'>
<img src='User_Images/$pictures[picture_thumb_url]' border='0'/></a></td>\n";
$i--;
if($i==0) {
echo "\n\t</tr>\n\t<tr>";
$i = 5;
}
}
if($i!=5) echo "\n\t\t<td colspan=\"$i\"></td>\n\t</tr>";
echo "\n</table>";
?>
But I have no idea how to go about making pages if I set a limit of 15 per page. Thanks in advance.
Well, you definitely need to know the number of pages that you will have in total, so that you can do something like this:
define('ITEMS_PER_PAGE', 15);
// do a count query here
$num_items = mysql_result($result);
$num_pages = ceil($num_items / ITEMS_PER_PAGE);
$cur_page = (isset($_GET['p']) && (intval($_GET['p']) > 0)) ? intval($_GET['p']) : 1;
if ($cur_page > $num_pages)
die('This page does not exist.');
$limit_clause = 'LIMIT '.(($cur_page - 1)*ITEMS_PER_PAGE).','.ITEMS_PER_PAGE;
// Add this to the end of your query
// YOUR QUERY GOES HERE
// YOUR THUMBNAIL CODE GOES HERE
// Now create the links
$links = array();
if ($cur_page > 1)
{
$links[] = 'First page';
$links[] = 'Previous page';
}
if ($cur_page < $num_pages)
{
$links[] = 'Next page';
$links[] = 'Last page';
}
So what you do is you calculate the number of overall pages (based on how many items you have) and the page you're currently on and use that to show links to previous or later pages (if necessary).
I hope that helps. Ask away if there are more questions.
EDIT: I didn't realize you did not yet have the query with LIMIT clause. So, to add the code mentioned in the other answer, I added the code how you have to adapt your query.
You should change your mysql query - add LIMIT directive. For example, LIMIT 10, 15 will return 15 rows starting from 10th. And you should change first parameter of LIMIT (offset) depending on your current page (you can pass it in GET)

Categories