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
Handling url variables with php
Moderator: General Moderators
by url varable do you mean a query string?
Code: Select all
<?php
if (isset($_GETї'name'])) {
echo ("Hello " . $_GETї'name'] );
}else{
?>
<a href ="your_page.php?name=graphic">Click Here</a>
<?php
}
?>Its pretty easy, heres an example that I'm using:
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:
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
<?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();
?>Code: Select all
<a href='$PHP_SELF?order=name'>Name:</a>Code: Select all
<a href='$PHP_SELF?order=name&method=ASC'>Name:</a>
OR
<a href='$PHP_SELF?order=name&method=DESC'>Name:</a>- twigletmac
- Her Royal Site Adminness
- Posts: 5371
- Joined: Tue Apr 23, 2002 2:21 am
- Location: Essex, UK