Working with two URL variables

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Conor
Forum Newbie
Posts: 4
Joined: Sun Feb 13, 2005 1:55 pm

Working with two URL variables

Post by Conor »

Hey!

I have a page, where there is an option to view content in two options (contact sheet, or slideshow). The contact sheet has to be broken into two pages, page1 and page 2

At the minute I've got

Code: Select all

<?php
// get url varible for view (slideshow/contact sheet)
$view = $_GET['view'];
// get second url variable for page number of contact see
$page = $_GET['page'];

if ($view=="slideshow") {
echo "slideshow";
}
elseif ($view=="contact") {
echo "contact";
}
else {
echo "contact";
}
?>

As you can see, this will display either "contact" (*.php?view=contact) or "slideshow" (*.php?view=slideshow). However I want the URL to be like this - *.php?view=contact&page=x , so I'll be able to see contact page 1/2 depending on the URL

I've called the URL variable page, but don't have an idea how to work it into the above PHP. I was thinking something like this

Code: Select all

if ($view=="contact" AND $page=="1") {
echo "contact1";
}
elseif ($view=="contact" AND $page=="2") {
echo "contact2";
}
else {
echo "contact1";
}
Obviously the AND doesn't work, but I hope you get the idea.


Thanks
Conor
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

//if $_GET['page'] exists, use $_GET value, 
//if not assign it a default value (1)
$page = (isset($_GET['page']) ? $_GET['page'] : '1');

switch ($_GET['view'])
{
     case 'slideshow' :
     //do some stuff
     break;

     case 'contact' :
     //do some stuff 
     break;

     default :
     //if $_GET['view'] is neither, show stuff here
     break;
}
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Re: Working with two URL variables

Post by anjanesh »

Conor wrote: Obviously the AND doesn't work, but I hope you get the idea.
Conor
Replace AND with &&

Code: Select all

if ($_GET['view']=="slideshow")
 $result1 = "slideshow";
elseif ($_GET['view']=="contact")
 $result1 = "contact";
else
 $result1 = "contact";

if ($_GET['page']=="1")
 $result2 = "1";
elseif ($_GET['page']=="2")
 $result2 = "1";
 
echo $result1.$result2;
Or better replace ifelse with switch as Phenom suggested - its easier.
User avatar
scorphus
Forum Regular
Posts: 589
Joined: Fri May 09, 2003 11:53 pm
Location: Belo Horizonte, Brazil
Contact:

Post by scorphus »

Please consider this reference: Template security : To switch or not to switch?. I hope it'll be useful.

-- Scorphus
Conor
Forum Newbie
Posts: 4
Joined: Sun Feb 13, 2005 1:55 pm

Post by Conor »

I figured it out myself *gasp* :wink:

Code: Select all

<?php
// get url varible for view (slideshow/contact sheet)
$view = $_GET['view'];
// get second url variable for page number of contact see
$page = $_GET['page'];

if (($view=="contact") && ($page==1)) {
echo "contact1";
}
elseif (($view=="contact") && ($page==2)) {
echo "contact2";
}
elseif ($view=="slideshow") {
echo "slideshow";

else {
echo "contact1";
}
?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Code: Select all

if (($view=="contact") && ($page==1)) 
{
     echo "contact1";
}
elseif (($view=="contact") && ($page==2)) 
{
     echo "contact2";
}
elseif ($view=="slideshow") 
{
     echo "slideshow"; 
}
else
{
     echo "contact1";
}
You were missing Line 14's bracket though..
Post Reply