sstoveld Posted September 23, 2009 Share Posted September 23, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/ Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 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; }?> Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923739 Share on other sites More sharing options...
sstoveld Posted September 23, 2009 Author Share Posted September 23, 2009 Quote 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? Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923751 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 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; }?> Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923759 Share on other sites More sharing options...
sstoveld Posted September 23, 2009 Author Share Posted September 23, 2009 Quote 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? Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923768 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 no you could get the country code from the database and add that into a link, replace $countrycode with the country code variable and countryname with the country name variable. <a href="country.php?code=$countrycode">$r->countryname</a> Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923772 Share on other sites More sharing options...
sstoveld Posted September 23, 2009 Author Share Posted September 23, 2009 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? Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923805 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 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>"; }?> Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923810 Share on other sites More sharing options...
sstoveld Posted September 23, 2009 Author Share Posted September 23, 2009 Quote 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>'); } Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923817 Share on other sites More sharing options...
nuttycoder Posted September 23, 2009 Share Posted September 23, 2009 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>'); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923822 Share on other sites More sharing options...
sstoveld Posted September 23, 2009 Author Share Posted September 23, 2009 Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923837 Share on other sites More sharing options...
nuttycoder Posted September 24, 2009 Share Posted September 24, 2009 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 Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923850 Share on other sites More sharing options...
sstoveld Posted September 24, 2009 Author Share Posted September 24, 2009 Quote 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 Quote Link to comment https://forums.phpfreaks.com/topic/175263-solved-need-help-using-_get-function-linking-to-a-new-page/#findComment-923875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.