To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
PHPBuilder.com  
 

 

Go Back   PHPBuilder.com > PHP Help > Coding

Coding Help with PHP coding

Reply
 
Thread Tools Rate Thread Display Modes
Old 11-03-2009, 08:00 PM   #1
fr8
<?php echo ''.$TUX.''; ?>
 
Join Date: May 2003
Posts: 191
resolved [RESOLVED] Links with multiple varibles

Every time I add a sort option to my directory, all of my links grow by an ifelse statement. So the link code is getting a little out of hand.

Right now I only have three sortable options but have serveral more that I would like to add.

Im looking for a better way to handle the following. Any Ideas?


$date= new : old
$views=most
$rating=top
$channel=can be anything

PHP Code:
echo '<ol>
        <li>Sort By ></li>
        <li><a href="sort.php?date='
.($date == "new" ? "old" : "new").'&'; if(isset($_GET["channel"])){ echo 'channel='.$_GET["channel"].'&';} if(isset($views)){ echo 'views='.$views.'&';}if(iss    et($rating)){ echo 'rating='.$rating.'';} echo ' ">'.($date == "new" ? "Oldest" : "Newest").'</a></li>
        <li><a href="sort.php?'
;if(isset($date)){ echo 'date='.$date.'&';}if(isset($_GET["channel"])){ echo 'channel='.$_GET["channel"].'&';} echo 'views=most">Most Views</a></li>
        <li><a href="sort.php?'
;if(isset($date)){ echo 'date='.$date.'&';}if(isset($_GET["channel"])){ echo 'channel='.$_GET["channel"].'&';}  echo 'rating=top">Top Rated</a></li>
        </ol>
        '
;
edit: updated code.
__________________
I use Slackware 12.2 and only code with vim.

Last edited by fr8; 11-03-2009 at 08:12 PM.
fr8 is offline   Reply With Quote
Old 11-04-2009, 04:19 AM   #2
toxic_brain
Oops !
 
toxic_brain's Avatar
 
Join Date: Jun 2005
Location: Hell
Posts: 136
PHP Code:
if($_GET["date"] == "new"){
    
$date_sort_text = "Oldest";
    
$date_sort_param = "old";
}else{
    
$date_sort_text = "Newest";
    
$date_sort_param = "new";
}
echo
"<ol>
        <li>Sort By ></li>
        <li><a href=\"sort.php?"
. http_build_query( array_merge($_GET, array("date" => $date_sort_param)) ) . "\">$date_sort_text</a>
        <li><a href=
\"sort.php?" . http_build_query( array_merge($_GET, array("views" =>"most"))) .             "\">Most Views</a>
        <li><a href=\"sort.php?"
. http_build_query( array_merge($_GET, array("rating" =>"top"))) .            "\">Top Rated</a>
        </ol>"
;
or you could avoid code duplication by putting a function in place

PHP Code:
if($_GET["date"] == "new"){
    
$date_sort_text = "Oldest";
    
$date_sort_param = "old";
}else{
    
$date_sort_text = "Newest";
    
$date_sort_param = "new";
}
echo
"<ol>
        <li>Sort By ></li>
        <li><a href=\""
. getLink('date', $date_sort_param). "\">$date_sort_text</a>
        <li><a href=
\"" . getLink('views', 'most')         . "\">Most Views</a>
        <li><a href=\""
. getLink('rating', 'top')         . "\">Top Rated</a>
        </ol>"
;

function
getLink($key, $value){
    return
'sort.php?' . http_build_query( array_merge($_GET, array($key => $value)));
}
__________________
toxic_brain

Last edited by toxic_brain; 11-04-2009 at 04:29 AM.
toxic_brain is offline   Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT -4. The time now is 04:45 PM.






Acceptable Use Policy

internet.comMediabistrojusttechjobs.comGraphics.com

WebMediaBrands Corporate Info


Advertise | Newsletters | Feedback | Submit News

Legal Notices | Licensing | Permissions | Privacy Policy


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.