Jump to content

[SOLVED] Need help using $_GET function linking to a new page


sstoveld

Recommended Posts

hey guys, i just had some help fixing a problem i ran into earlier here:

http://www.phpfreaks.com/forums/index.php/topic,270260.0.html

 

now, im still on the same project here, but ran into another problem.

 

first of all, here's the link to the site:

http://gamera.caset.buffalo.edu/~sstoveld/a2.php

 

what i want to do is make each country a link, which links to country.php, but displays it's own seperate information for each country. i have all the information already in the database.

 

what i want to do it use the $_GET function to link to each different country by their country code (in the database).

 

each country has their own country code already in the database. so say if you click on Afghanistan, the link would be:

http://gamera.caset.buffalo.edu/~sstoveld/country.php?code=AFG

 

how can i do this? here's my code:

<?php
// Query the Database to select Countries in Asia //
$queryasia = mysql_query('SELECT * FROM Country WHERE Continent = "Asia" ORDER BY Name');
$queryeurope = mysql_query('SELECT * FROM Country WHERE Continent = "Europe" ORDER BY Name');
$queryna = mysql_query('SELECT * FROM Country WHERE Continent = "North America" ORDER BY Name');
$queryafrica = mysql_query('SELECT * FROM Country WHERE Continent = "Africa" ORDER BY Name');
$queryoceania = mysql_query('SELECT * FROM Country WHERE Continent = "Oceania" ORDER BY Name');
$queryantarctica = mysql_query('SELECT * FROM Country WHERE Continent = "Antarctica" ORDER BY Name');
$querysa = mysql_query('SELECT * FROM Country WHERE Continent = "South America" ORDER BY Name');



// Make an HTML table to hold the data for Asia //
?>

<h2>Asia</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryasia)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

// Make an HTML table to hold the data for Europe //
?>
</table>

<h2>Europe</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryeurope)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

<h2>North America</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryna)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

<h2>Africa</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryafrica)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

<h2>Oceania</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryoceania)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

<h2>Antarctica</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryantarctica)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

<h2>South America</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($querysa)) {
	echo('<tr><td>' . $row['Name'] . '</td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

?>
</table>

 

any help is appreciated, thanks

 

Link to comment
Share on other sites

on country.php get the data from the url then do a query based on the data

 

 

<?php
$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
while($r=mysql_fetch_object($s)){
//echo your data
echo $r->Name;
}?>

Link to comment
Share on other sites

on country.php get the data from the url then do a query based on the data

 

 

<?php
$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
while($r=mysql_fetch_object($s)){
//echo your data
echo $r->Name;
}?>

 

im sorry, i dont understand what you mean :S how do i get the data from the url? ugh i have pretty much no idea what im doing heh. where would i be putting in that query that you wrote there? does that go somewhere in my first script on my page, or does that go somewhere into the new country.php page? sorry im pretty clueless here... :(

 

also, how can i make each country into a link using php?

Link to comment
Share on other sites

okay where ever you pass anything in the url such as country.php?code=asia anything after the ? becomes a variable to use it you need to add it to a variable

 

$code = $_GET['code'];

 

now $code contains 'asia'

 

so on the first script change the country name into a link like this:

 

<a href="country.php?code=asia">Asia</a>

 

then in country.php

 

first connect to your database then get the variables from the url and make them safe from sql injection using mysql_real_escape_string

 

$code = $_GET['code'];

$code = mysql_real_escape_string($code);

 

then your ready to do a query based on the country like this:

 

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());

 

so the full page may look like this:

 

<?php
//connect to db first

$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
while($r=mysql_fetch_object($s)){
//echo your data
echo $r->Name;
}?>

 

Link to comment
Share on other sites

okay where ever you pass anything in the url such as country.php?code=asia anything after the ? becomes a variable to use it you need to add it to a variable

 

$code = $_GET['code'];

 

now $code contains 'asia'

 

so on the first script change the country name into a link like this:

 

<a href="country.php?code=asia">Asia</a>

 

then in country.php

 

first connect to your database then get the variables from the url and make them safe from sql injection using mysql_real_escape_string

 

$code = $_GET['code'];

$code = mysql_real_escape_string($code);

 

then your ready to do a query based on the country like this:

 

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());

 

so the full page may look like this:

 

<?php
//connect to db first

$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$s = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
while($r=mysql_fetch_object($s)){
//echo your data
echo $r->Name;
}?>

 

ok im starting to understand this a bit, but the problem is, there are about 250+ countries in the database, does that mean i would have to change each into a link manually?

 

like i want the user to be able to click on any of the countries listed, which would take them to a page that shows the info about that country.

 

example: http://flywheel.caset.buffalo.edu/315/world.php

 

when the user click on say, Afghanistan, it takes them to this page:

 

http://flywheel.caset.buffalo.edu/315/country.php?code=AFG

 

do i have to create each link individually?

Link to comment
Share on other sites

hmm im still not quite getting it. when i try this the link still reads out country.php?code=$code...

 

how do i get the $code part to actually display the correct country code?

// Query the Database to select Countries //
$queryasia = mysql_query('SELECT * FROM Country WHERE Continent = "Asia" ORDER BY Name');
$queryeurope = mysql_query('SELECT * FROM Country WHERE Continent = "Europe" ORDER BY Name');
$queryna = mysql_query('SELECT * FROM Country WHERE Continent = "North America" ORDER BY Name');
$queryafrica = mysql_query('SELECT * FROM Country WHERE Continent = "Africa" ORDER BY Name');
$queryoceania = mysql_query('SELECT * FROM Country WHERE Continent = "Oceania" ORDER BY Name');
$queryantarctica = mysql_query('SELECT * FROM Country WHERE Continent = "Antarctica" ORDER BY Name');
$querysa = mysql_query('SELECT * FROM Country WHERE Continent = "South America" ORDER BY Name');

// Make an HTML table to hold the data for Asia //
?>

<h2>Asia</h2>

<table>
<tr>
    	<td><strong>Name</strong></td>
        <td><strong>Population</strong></td>
        <td><strong>GNP</strong></td>
        <td><strong>Government</strong></td>
    </tr>

<?php
while ($row = mysql_fetch_array($queryasia)) {
	echo('<tr><td>' . '<a href="country.php?code=$code">' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

// Make an HTML table to hold the data for Europe //
?>

 

and then once it takes you to the new page, how do i get it to show the correct information about the correct country?

 

Link to comment
Share on other sites

if you have the country code in your database then use that in the link or just the country name :<a href="country.php?code=".$row['Name'].">

 

then next page is where you query the database based on the country

 

 

<?php
$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$queryasia = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
echo "<table>";
while ($row = mysql_fetch_array($queryasia)) {
      echo('<tr><td>' . '<a href="country.php?code=$code">' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
   }
echo "</table>";
}?>

Link to comment
Share on other sites

if you have the country code in your database then use that in the link or just the country name :<a href="country.php?code=".$row['Name'].">

 

then next page is where you query the database based on the country

 

 

<?php
$code = $_GET['code'];
$code = mysql_real_escape_string($code);

$queryasia = mysql_query("SELECT * FROM Country WHERE Continent ='$code' ORDER BY Name")or die(mysql_error());
echo "<table>";
while ($row = mysql_fetch_array($queryasia)) {
      echo('<tr><td>' . '<a href="country.php?code=$code">' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
   }
echo "</table>";
}?>

 

when i try that, i just get a blank page, no errors or anything :S

 

is there something wrong with this piece of code?

while ($row = mysql_fetch_array($queryasia)) {
	echo('<tr><td>' . '<a href="country.php?code=".$row['Code']."> ' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
}

Link to comment
Share on other sites

had the wrong quotes

 

<?php
while ($row = mysql_fetch_array($queryasia)) {
      echo('<tr><td>' . '<a href="country.php?code='.$row['Code'].'> ' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
   }
?>

Link to comment
Share on other sites

had the wrong quotes

 

<?php
while ($row = mysql_fetch_array($queryasia)) {
      echo('<tr><td>' . '<a href="country.php?code='.$row['Code'].'> ' . $row['Name'] . '</a></td><td>' . $row['Population'] . '</td><td>' . $row['GNP'] . '</td><td>' . $row['GovernmentForm'] . '</td></tr>');
   }
?>

 

ahh thanks haha, got that all sorted out.

 

im working on the country.php page now, but having issues...

 

i keep getting this error: Fatal error: Cannot use object of type stdClass as array in /home/sstoveld/public_html/country.php on line 43

$s = mysql_query("SELECT * FROM Country WHERE Code ='$code' ORDER BY Name")or die(mysql_error());
while($row=mysql_fetch_object($s)){

// Fill the tables with the Data // 
?>

<table>
<tr>
    <td><strong>Local Name:</strong></td><?php echo('<td>' . $row['LocalName'] . '</td>') ?>
    </tr><tr>
    <td><strong>Code:</strong></td><?php echo('<td>' . $row['Code'] . '</td>') ?>
    </tr><tr>
    <td><strong>Two Character Code:</strong></td><?php echo('<td>' . $row['Code2'] . '</td>') ?>
    </tr><tr>
    <td><strong>Continent:</strong></td><?php echo('<td>' . $row['Continent'] . '</td>') ?>
    </tr><tr>
    <td><strong>Reigon:</strong></td><?php echo('<td>' . $row['Reigon'] . '</td>') ?>
    </tr><tr>
    <td><strong>Surface Area:</strong></td><?php echo('<td>' . $row['SurfaceArea'] . '</td>') ?>
    </tr>
</table>

<?php
}
?>

 

line 43 would be the one with LocalName

 

Link to comment
Share on other sites

it's because your using mysql_fetch_object at the top but using array notation when calling the items such as $row['Name'] when using mysql_fetch_object use the -> notation so $row->name.

 

or change mysql_fetch_object to mysql_fetch_array

thank you for all your help! i am finally finished. i appreciate it.

 

here's the final thing: http://gamera.caset.buffalo.edu/~sstoveld/a2.php

 

i know its ugly, but im too tired to bother and make it look nice :P

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.