/    Sign up×
Community /Pin to ProfileBookmark

Not sure how to include this properly in a for loop

Okay I have a For loop that spits out page numbers for navigation and its as follows:

[code=php] <?php
for ($page=0; $page<=$totalPages_Products; $page++) {
$label = $page + 1; // Because for some reason its starts on page 0, and that doesn’t make sense to an enduser.
$pagenum = $_GET[‘pageNum_Products’];
if ($page == $pagenum) {
$active = ‘ class=”currentpage”‘;
}
if ($_GET[‘Category’] != “”) {
$CatURL = “&Category=” . $_GET[‘Category’];
}
if ($totalPages_Products > 0) {
echo “<li><a$active href=’product-listing.php?pageNum_Products=”.$page.$CatURL.”‘>”.$label.”</a></li>”;
}
}
?>
[/code]

You’ll notice this line:

[code=php]if ($page == $pagenum) {
$active = ‘ class=”currentpage”‘;
}[/code]

Here’s where I’m having the trouble, and I figure its because I’m putting this in the wrong place, but I don’t know where else to put it.

Basically what its doing wrong, is when I get to that page it will set that one to the current page class.. but then any page numbers that come after that as well.. so if there’s 4 pages, and I’m on page 2, it’ll set the current page class to 2, 3, 4 & 5…

How else can I do this, so it’ll select just 2 or whatever page its on..

to post a comment
PHP

2 Comments(s)

Copy linkTweet thisAlerts:
@scragarNov 18.2008 — Okay I have a For loop that spits out page numbers for navigation and its as follows:

[code=php] <?php
for ($page=0; $page<=$totalPages_Products; $page++) {
$label = $page + 1; // Because for some reason its starts on page 0, and that doesn't make sense to an enduser.
$pagenum = $_GET['pageNum_Products'];
if ($page == $pagenum) {
$active = ' class="currentpage"';
}
if ($_GET['Category'] != "") {
$CatURL = "&Category=" . $_GET['Category'];
}
if ($totalPages_Products > 0) {
echo "<li><a$active href='product-listing.php?pageNum_Products=".$page.$CatURL."'>".$label."</a></li>";
}
}
?>
[/code]


You'll notice this line:
[code=php]if ($page == $pagenum) {
$active = ' class="currentpage"';
}[/code]


Here's where I'm having the trouble, and I figure its because I'm putting this in the wrong place, but I don't know where else to put it.

Basically what its doing wrong, is when I get to that page it will set that one to the current page class.. but then any page numbers that come after that as well.. so if there's 4 pages, and I'm on page 2, it'll set the current page class to 2, 3, 4 & 5...

How else can I do this, so it'll select just 2 or whatever page its on..[/QUOTE]

few rather simple fixes are available, try this edit:
[code=php]
<?php

for ($page=0, $pagenum = $_GET['pageNum_Products']; $page<=$totalPages_Products; $page++){

if($page == $pagenum){
$active = ' class="currentpage"';
}else{
unset($active);
}
if ($_GET['Category'] != "") {
$CatURL = "&Category=" . $_GET['Category'];
}
if ($totalPages_Products > 0) {
echo "<li><a$active href='product-listing.php?pageNum_Products=".$page.$CatURL."'>".$label."</a></li>";
}
}
?>
[/code]
Copy linkTweet thisAlerts:
@glitchgirlauthorNov 18.2008 — THANK YOUUUUUUU! Lol, I was feeling mighty dumb, now I feel just a bit dumber, but with the right answer! ahah! Thanks again =P
×

Success!

Help @glitchgirl 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.16,
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: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

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