/    Sign up×
Community /Pin to ProfileBookmark

Pagination issue prob easy fix…

Hey all,

I got a paging function which isn’t doing what i want it to do… it works fine on other pages but i dont know how to make it work with a mysql join in my php script heres the code i’ve got :-

Its displaying the right amount of page numbers but its still displaying all the images

[code=php]

$max = 2; //amount of images per page

$p = $_GET[‘p’];

if(empty($p))

{

$p = 1;

}

$limits = ($p – 1) * $max;

//view the image!

if(isset($_GET[‘act’]) && $_GET[‘act’] == “view”)

{

$id = $_GET[‘id’];

$query2 = “SELECT likes.imgid, COUNT(likes.imgid), img.name, img.tagged FROM likes, img WHERE likes.imgid = img.imgid GROUP BY likes.imgid ORDER BY COUNT(likes.imgid) DESC”;

$result2 = mysql_query($query2) or die(mysql_error());

while($info = mysql_fetch_array( $result2 ))
{

$szImgId = $info[‘imgid’];

//Outputs the images
echo “<li><a href=”all_pics.php?id=$szImgId”><img src=/”.$thumb.”>”.”<p>” .$info[‘tagged’].”</p></a></li>” ;

}

}else{

//view all the images in rows

$sql = mysql_query(“SELECT likes.imgid, COUNT(likes.imgid), img.name, img.tagged FROM likes, img WHERE likes.imgid = img.imgid GROUP BY likes.imgid ORDER BY COUNT(likes.imgid) DESC LIMIT “.$limits.”,$max”) or die(mysql_error());

//the total rows in the table

$totalres = mysql_result(mysql_query(“SELECT COUNT(likes.imgid) AS tot FROM likes, img WHERE likes.imgid = img.imgid”),0);

//the total number of pages

$totalpages = ceil($totalres / $max);

while($info = mysql_fetch_array( $result2 ))
{

$szImgId = $info[‘imgid’];

//Outputs the images

echo “<li><a href=”all_pics.php?id=$szImgId”><img src=/”.$thumb.”>”.”<p>” .$info[‘tagged’].”</p></a></li>” ;

}

for($i = 1; $i <= $totalpages; $i++){

//this is the pagination link

echo “<a href=’index.php?p=$i’>$i</a>|”;
}
}

[/code]

any ideas what to change?

thanks in adavnced

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@tirnaApr 14.2010 — If I understand you correctly, it sounds like your script is not displaying the correct images when the sql query getting the images involves joining tables. Otherwise the correct images are displayed.

If that is so, then that suggests to me that you have a problem with the sql query.

The first thing to do would be, if you don't have a debugger, is to insert something like:

[CODE]echo $query; die();[/CODE] immediately after where you create the query in order to see what the query that is about to be run actually is. [U]Make sure the structure and syntax of $query is correct.[/U]

Also, for complicated queries, I first create them and test them in a MySql GUI (I use SQLyog) where it is much easier to run the query and see the results to debug the query.

Once the query returns the correct rows in the GUI, I then transfer the query to my PHP script.
×

Success!

Help @swl7 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.5,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,

tipper: @Samric24,
tipped: article
amount: 1000 SATS,
)...