Jump to content

How do I use the get function with an href?


Theonlybrains

Recommended Posts

HiI am fairly new to php, I have a table that has insect names, id, scientific name and descriptions. I am trying to present the table with insect Id name and scientific name. The insect description is too long to fit into the table I therefore want to create a link to a different page which will give the insect description then link back to the home page. I have been trying to find some info on using the get function to retrieve the data but no luck!this what my code looks like: /*create a new mysql query to select insects from a selected table*/$insects = mysql_query ("SELECT insect_id, insect_slug, insect_com, insect_science, insect_desFROM insectsORDER BY insect_com"); echo "<table border ='1'";echo "<h>insects</h>";echo "<tr><th>insect_slug</th><th>insect_com</th><th>insect_science</th></tr>"; /* create a while loop to get the rows of the table */while($row = mysql_fetch_array( $insects )) {$strInsect = $row ['insect_com'];//create a link to the insect description page$strLink = "<a href='insects-details.php?insect_des = ". $row['insect_com']. "'>".$strInsect."</a>"; // Print out the contents of each row into a tableecho "<tr><td>"; echo $row['insect_slug'];echo "</td><td>";echo $strLink;echo "</td><td>"; echo $row['insect_science']; echo "</td></tr>"; //echo "<li>".$strLink."</li>"; }echo "</table>"; ?>Thanking you in advance

Link to comment
Share on other sites

what problem are you facing? what is not working as expected? errors?

Link to comment
Share on other sites

PHP is a server side language and can not interact with the html.You can look at it like php is writing you a html file and then sends it to the browser. What you do is you create an other php file which disblays the description of your insect

<tr>	 <td><a href="descriptionpage.php?insect=<?php echo $row['insect_id']?>">Description</a></td></tr>

You can retive the insect key with this variable on you description page:

$_GET['insect']

Link to comment
Share on other sites

YOU HAVE TO SHOW HERE WHAT ERROR YOU ARE GETTING FROM YOUR SERVERMOST OF THE ERRORS DEPENDS UP ON THE DATA RESULTS SO WE NEED IT.. here is some tipsthe page must be a .php extension for run every command which was called as <? ..opening and ?> closing tagsand use die...() functions for look up is this query was valid....and use mysql_num_rows function before while () statement so can be check if row empty of not..

Edited by Net123
Link to comment
Share on other sites

Thanks ckrudelux,I have created the link page which seems to work fine, but I cant get my head round using the $_GET['insect'] , What I am trying to do is that if I click on any insect, the link should take me to the description of that particular insect. At the moment when I click I just land on the description page. Like I said I am fairly new to php and kind of having teething problems! create a while loop to get the rows of the table */while($row = mysql_fetch_array( $insects )) {$strInsect = $row ['insect_com'];$strDescription = $row['insect_des'];//create a link to the insect description page$strLink = "<a href='descriptionpage.php?insect_des = ". $row['insect_com']. "'>".$strInsect."</a>"; // Print out the contents of each row into a tableecho "<tr><td>"; echo $row['insect_slug'];echo "</td><td>";echo $strLink;echo "</td><td>"; echo $row['insect_science']; echo "</td></tr>"; this creates a table with three columns and the insect_com acts as a link to the the description page.My challenge now is how do I create the link so that it points to individual description of each insect rather than the whole page? thanks

Link to comment
Share on other sites

you need to pass the id to description page using the url.

 $strLink = "<a href='descriptionpage.php?insect_id = ". $row['insect_id']. "'>".$strInsect."</a>";

in descriptionpage.php insect_id will be available in $_GET['insect_id'] using that id you can now query your database to find data where it matches the id and you can show it up.

Link to comment
Share on other sites

Thanks guys for your help, I do not seem to understand where I am going wrong with this! I have created the link to my description page and that works fine. Now I just can get my head round to creating a query or a function to the retrieve individual insect description! this is what my description page code looks like:<?php // Make a connection to the host servermysql_connect("localhost","xxxxxx","xxxxxxxx") or die(mysql_error);mysql_select_db("khumbu") or die(mysql_error());/*create a new mysql query to select insects from a selected table*/$insects = mysql_query ("SELECT insect_id, insect_slug, insect_com, insect_science,insect_des FROM insects");$row = $_Get['insect_id']; echo "<table border ='1'";echo "<h1>Insect Description</h1>";echo "<tr><th>insect_des</th></tr>";echo "<tr><td>"; echo $row['insect_des'];echo "</td></tr>"; echo "</table>";?> I am getting an error that say undefined variable GetPlease help

Link to comment
Share on other sites

$row = $_Get['insect_id'];
$_GET is case sensitive as all variable identifier is. $_Get and $_GET are different. when you use $_Get['insect_id'] it tries to get element "insect_id" which does not exist. thats what the error means. also you need to put that id in where caluse so it will match the id which you are looking for. you are also just executing the query are not fetching the results.check the examples here http://w3schools.com/php/php_mysql_intro.asp
Link to comment
Share on other sites

If I am not mistaken, you want every insects to have their own get variables for the get method using href. Well you can use arrays. first declare a variable outside the while with zero value:$x = 0; then increment it at the end of your while:$x++; then display each of your items from your query inside the while, before the "$x++", adding the $x to the end of your variables:$strLink = "<a href='descriptionpage.php?insect_des$x = ". $row['insect_com']. "'>".$strInsect."</a>"; the variable will then look like this if you retrieve it: $_GET['insect_des0'];$_GET['insect_des1'];$_GET['insect_des2'];and so on... then you can use the same way retrieving it, or you can look for your own way.

Edited by roy0702
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...