Passing variables with pagination - iterating through unique id per link


Posted on 16th Feb 2014 07:03 pm by admin

Hi everyone,
I have seen a few topics like this one. Still cant find the specific thing, so I am asking - does anyone know this?

I am trying to combine a mysql query with this code I found at php-mysql-tutorial.com.

Essentially this is the rub. The pages are working fine, but I am flumoxxed as to how to apply the $imgid to a page (viewing one image per page).

The imgid are in numerical order but do not begin at one, and there may be gaps - eg img 3, 5, 7,8 ,9 and 10 on a 6 page pagination.
I can pass the $page variable because it is a simple increment iteration, but how do I apply the changing $imgid value? so that pp1 has an imgid of 3 passed, pp2 is 5, pp3 is 7 etc. I expect it is something to do with combining a query and an array and applying it, but I'm having a brain-strain at that point.

Gratefully, here is my code stripped of excess ( I hope).

<?php

// back button depending on which gallery is parent
if(!isset($_REQUEST['random'])) {

$file= "gallery";

$varid = 0;
} else {
$file= "random";
$varid = 1;
}

echo "<fieldset><div id="back_but"><a href="gallery.php?blog=$file"> <br /></a></div>";

$url = "blog=picture&pics=picdetail&";

// how many rows to show per page
$rowsPerPage = 1;

// by default we show first page
$pageNum = 1;

// if $_GET['page'] defined, use it as page number
if(isset($_GET['page']))
{
$pageNum = $_GET['page'];
}

// counting the offset
$offset = ($pageNum - 1) * $rowsPerPage;


$query = "SELECT imgid, title, year, description, thumburl, imgurl, medium, style, clientname FROM images LEFT JOIN mediums ON images.mediumid = mediums.mediumid LEFT JOIN imgstyle ON images.styleid = imgstyle.styleid LEFT JOIN clients ON images.clientid = clients.clientid WHERE imgid = $imgid".
" LIMIT $offset, $rowsPerPage";

echo $query;
$result = mysql_query($query);
while($row=mysql_fetch_array($result, MYSQL_ASSOC))

{
$imgid = $row['imgid'];
$title = $row['title'];
$year = $row['year'];
$description = $row['description'];
$thumburl = $row['thumburl'];
$medium = $row['medium'];
$style = $row['style'];
$client = $row['clientname'];
}
?>

<fieldset><?php

// how many rows we have in database
$query = "SELECT COUNT(imgid) AS numrows FROM images WHERE tarot = $varid";
$result = mysql_query($query) or die('Error, query failed');
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

// how many pages we have when using paging?
$maxPage = ceil($numrows/$rowsPerPage);

// print the link to access each page
$self = $_SERVER['PHP_SELF'];
$nav = '';

for($page = 1; $page <= $maxPage; $page++)
{
if ($page == $pageNum)
{
$nav .= " $page "; // no need to create a link to current page
}
else
{

$nav .= " <a href="$self?$url&page=$page">$page</a> ";
}
}

// creating previous and next link
// plus the link to go straight to
// the first and last page

if ($pageNum > 1)
{
$page = $pageNum - 1;
$prev = " <a href="$self?$url&page=$page">[Prev]</a> ";

$first = " <a href="$self?$url&page=1">[First Page]</a> ";
}
else
{
$prev = '&nbsp;'; // we're on page one, don't print previous link
$first = '&nbsp;'; // nor the first page link
}

if ($pageNum < $maxPage)
{
$page = $pageNum + 1;
$next = " <a href="$self?$url&page=$page">[Next]</a> ";

$last = " <a href="$self?$url&page=$maxPage">[Last Page]</a> ";
}
else
{
$next = '&nbsp;'; // we're on the last page, don't print next link
$last = '&nbsp;'; // nor the last page link
}

// print the navigation link
echo $first . $prev . $nav . $next . $last;



?></fieldset>

No comments posted yet

Your Answer:

Login to answer
184 Like 30 Dislike
Previous forums Next forums
Other forums

Line break?
Hi, I'm new to the forum and new to php. I'm not sure if I'm using the correct terminology so here i

Number Format
Hi All,

I have number '000000000050085' I want to format it to 500.85 Can any one give me

Send current URL in URL with a $_GET variable problem.
Hello community,

I have created a function that gets the current variable of a page, example:

User registration and login
I don't know whether this is the correct forum, if not i apologise but am pretty desperate at the mo

Pro-rata Billing in Periodic Billing for Rental
Hi guys,

We have a situation, where a rental contract starts at the mid of the month say

form class help (oop php5)
Hidy Ho Neighbors,

I'm forcing myself to learn oop/classes for php5. It seems like a good id

Displaying a record from mysql in a simple swf file
Hi,

I have a mysql database containing information I would like to display in my swf.

update sql when refresh - php
hi
I have made a table (attachement)

the users can update the sql database using + or x bu

How to Handle more than one submit button in single form?
Hi
I have one PHP file which contains one Form. In this form there are two Submit type Buttons

how to validate date using javascript
I need to validate date in textbox using javascript..
The date is must be not greater than TODAY

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash