Page 1 of 1

Working with two URL variables

Posted: Thu Mar 31, 2005 12:07 pm
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

Posted: Thu Mar 31, 2005 12:19 pm
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;
}

Re: Working with two URL variables

Posted: Thu Mar 31, 2005 12:22 pm
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.

Posted: Thu Mar 31, 2005 12:39 pm
by scorphus
Please consider this reference: Template security : To switch or not to switch?. I hope it'll be useful.

-- Scorphus

Posted: Thu Mar 31, 2005 1:08 pm
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";
}
?>

Posted: Thu Mar 31, 2005 1:11 pm
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..