Page 1 of 1
php coding for www.mysite.com/page?id=100 etc
Posted: Thu May 19, 2005 6:09 am
by dylan001
Sorry if this has already been covered in the forum already, i spent hours looking for it with no success.
I'm looking to set up a website which will mainly use data from a MySQL database, what i'm looking to do is have a few page templates which will simply display the data in my table:
For example:
index page:
Please select a country:
country 1 --> country id=1
country 2 --> country id=2
country 3 --> country id=3
upon clicking on the link for say country 2 the template for regions will be loaded with the data from the database for id=2:
region 2a
region 2b
region 2c
Sorry, i've no idea what this kind of structure/template is called and would be grateful for any help (tutorials, demos, references) that anyone can offer
Hope it makes sense
CHEERS
Posted: Thu May 19, 2005 6:47 am
by John Cartwright
Code: Select all
$sql = "SELECT * FROM `regiona`,`regionb`,`regionc` FROM `table_name` WHERE `country` = '".$_GET['id']."' LIMIT 1";
This will fetch 3 columns, (regiona, regionb, regionc) from your table (table_name) where the column named country holds the country id's.
Please post your database structure, that would help alot.
Posted: Thu May 19, 2005 7:14 am
by dylan001
Sorry
My databases has two tables
COUNTRIES:
----------------------
ID | COUNTRY
----------------------
01 | ENGLAND
----------------------
02 | SCOTLAND
----------------------
REGIONS:
---------------------------------------
GOBAL ID | ID | REGION
---------------------------------------
01 | 01 | LONDON
---------------------------------------
02 | 01 | LIVERPOOL
---------------------------------------
03 | 02 | GLASGOW
---------------------------------------
04 | 02 | EDINBURGH
---------------------------------------
BASICALLY WHAT IM TRYING TO ACHIVE IS:
ON ONE PAGE HAS A LIST OF THE COUNTRIES I.E.
ENGLAND
SCOTLAND
WHICH WILL LINK TO THE CITIES FROM EACH COUNTRY - BUT I DON'T WANT A NORMAL LINK TO A PAGE LIKE .com/englishcities.php
I AM AIMING FOR .com/cities.php?id=(id for england)
I hope that is a bit clearer.
Thanks for the response.
Posted: Thu May 19, 2005 8:16 am
by phpScott
dylan001 wrote:
I AM AIMING FOR .com/cities.php?id=(id for england)
you got it.
Code: Select all
on your <a href="e;cities.php?countryId=idForEngland"e;>England<a>
then in cities.php
Code: Select all
if(isset($_GET['countryId']))
{
$cId = $_GET['countryId'];
$sql="SELECT * FROM REGIONS WHERE `id`= $cId";
//submit query and do what you want with the results.
}
is that what you are looking for?
Posted: Thu May 19, 2005 8:48 am
by dylan001
Thanks this looks exatly like what i'm after.
I've tried your suggestion and i'm getting an error message, but I think it's an error on my part trying to output the data!!! I'm fairly new to this... I don't know if you'll be able to help
my countries page script is:
Code: Select all
$db = mysql_connect($hostname,$username,$password);
mysql_select_db("dylan001_list1" ,$db);
$sql = mysql_query("SELECT `ID` , `C1` FROM `COUNTRIES` WHERE 1 AND `ID` > 0 ORDER BY 'C1'ASC" ,$db);
echo ("<table border ='0'>");
echo ("<tr> Select a country <td></td></tr>");
while ($tablerows = mysql_fetch_row($sql))
{
echo("<tr><td><a href='/region.php?ID=$tablerows[0]'>$tablerows[1]</a></td></tr> ");
}
echo "</table>";
mysql_close($db);
?>
which i think is successfully passing over the suggested Countryid "ID" to the reigion script below
Code: Select all
$db = mysql_connect($hostname,$username,$password);
mysql_select_db("dylan001_list1" ,$db);
if(isset($_GET['ID'])){
$cId = $_GET['ID'];
$sql="SELECT * FROM REGIONS WHERE `ID`= $cId";
}
echo ("<table border ='0'>");
echo ("<tr><td> LIST OF REGIONS</td></tr>");
while ($tablerows = mysql_fetch_row($sql))
{
echo("<tr><td><a href='/country?GID=$tablerows[0]'>$tablerows[0]</a></td></tr> ");
}
echo "</table>";
mysql_close($db);
?>
Thanks for the help.
Posted: Thu May 19, 2005 9:08 am
by phpScott
I don't think it matters to much but this is the way that I always do my queries
Code: Select all
$sql = "SELECT * FROM `REGIONS` WHERE `ID` = '$cId'";
$result = mysql_query ($sql) or die (mysql_error());
while ($table = mysql_fetch_assoc($result))
{
echo "<tr><td>".$table['id']."</td><td>".$table['name']."</td></tr>\n";
}
notice the $sql variable that holds the text of my query then I mysql_query the $sql variable.
I just think it keeps the seperation a little nicer.
Also the 'or die' bit is important in case there are any errors in your query.
so in your case on the second page you never submit your query using mysql_query.
Posted: Thu May 19, 2005 9:28 am
by dylan001
THANK YOU.
Thats working brilliantly, thanks again for your help!
See what you mean about the nicer presentation, think i'll adopt it from now on.
Cheers.