Handling url variables with php

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
mikeb
Forum Commoner
Posts: 60
Joined: Tue Jul 08, 2003 4:37 pm
Location: Dublin, Ireland

Handling url variables with php

Post 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
graphicmd
Forum Newbie
Posts: 8
Joined: Mon Jul 28, 2003 11:37 pm

Post 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;
?>
User avatar
DuFF
Forum Contributor
Posts: 495
Joined: Tue Jun 24, 2003 7:49 pm
Location: USA

Post 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;
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
Post Reply