Page 1 of 1

Handling url variables with php

Posted: Tue Jul 29, 2003 4:16 pm
by mikeb
Can anyone give me the syntax for handling a url variable using php. I'm getting quite far with php.(A newbie moving from CF and begrugingly admitting that php perhaps rocks ever so slightly!). I am creating a new site and forcing myself to do it entirely in php! The site doesn't have to be published 'til January '04 so I'm not under severe pressure at least.

Anyway, I'm creating a record listing from a db and I want the customer to be able to click on column heads in the results table to re-sort the listing by various fields such as surname, date etc. The page will call itself each time and I'm using a dynamic sort variable within the sql query, but my php book gives no examples of a url variable being used in an action page. A rough idea will suffice just to get me going,

cheers

Mike

Posted: Tue Jul 29, 2003 4:32 pm
by graphicmd
by url varable do you mean a query string?

Code: Select all

<?php
if (isset($_GET&#1111;'name'])) &#123;
  echo ("Hello " . $_GET&#1111;'name'] );
&#125;else&#123;
  ?>
  <a href ="your_page.php?name=graphic">Click Here</a>
  <?php
&#125;
?>

Posted: Tue Jul 29, 2003 5:38 pm
by DuFF
Its pretty easy, heres an example that I'm using:

Code: Select all

<?php
$order = $_GET['order'];  //get order value from URL
$method = $_GET['method'];  //get method value from URL
include("header.php");
@mysql_connect(localhost,$username,$password);  //connect
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM tablename WHERE something='$something' ORDER BY '$order' $method";  //query database using the variables from the URL
$result=mysql_query($query) or die("Error: " . mysql_error());
$num=mysql_numrows($result);
mysql_close();
?>
After this just get the information from the database and display it on the page. Then at the top of each column you can have links such as this:

Code: Select all

&lt;a href='$PHP_SELF?order=name'&gt;Name:&lt;/a&gt;
then you can take it further and have the user be able to sort the listing descending or ascending by using a link such as this:

Code: Select all

&lt;a href='$PHP_SELF?order=name&amp;method=ASC'&gt;Name:&lt;/a&gt;
OR
&lt;a href='$PHP_SELF?order=name&amp;method=DESC'&gt;Name:&lt;/a&gt;

Posted: Wed Jul 30, 2003 4:43 am
by twigletmac
Just a slight correction to the code above - $PHP_SELF won't work if register_globals is off so you should use $_SERVER['PHP_SELF'] instead.

Mac