Page 1 of 1

embed href in php output

Posted: Tue Jul 26, 2011 11:08 am
by crowegreg
I worked on this until late into the night, and I can't figure it out!
The following code is the output of a mysql query

Code: Select all

	if(mysql_num_rows($result) > 0) {
		while($row = mysql_fetch_assoc($result)) {
			echo "<tr>";
			echo "<td>" . $row['cardID'] . "</td><td>" . $row['bill_last_name'] . "</td>";
			echo "</tr>";
		}		
	}
I need to embed a href associated with the ['cardID'] output. So if this table has 4 records displayed, I'd like for the user to click on the desired 'cardID', and use the selected cardid for a query to display all the data associated with that cardID on another page.

Re: embed href in php output

Posted: Tue Jul 26, 2011 12:02 pm
by Christopher

Code: Select all

	if(mysql_num_rows($result) > 0) {
		while($row = mysql_fetch_assoc($result)) {
			echo "<tr>";
			echo "<td>" . $row['cardID'] . "</td><td><a href=\"http://mysite.com/editrecord.php?cardID={$row['cardID']\">" . $row['bill_last_name'] . "</a></td>";
			echo "</tr>";
		}		
	}

Re: embed href in php output

Posted: Tue Jul 26, 2011 12:03 pm
by greyhoundcode
You are already placing the query results in amongst HTML markup. All you need to do now is add the additional markup required to create a link.

Code: Select all

echo "<a href='$myLink'>Click me</a>";

Re: embed href in php output

Posted: Tue Jul 26, 2011 12:04 pm
by greyhoundcode
Pipped at the post :P

Re: embed href in php output

Posted: Tue Jul 26, 2011 12:24 pm
by crowegreg
Thank you!! Changing my code to the following gets me almost there:

Code: Select all

	if(mysql_num_rows($result) > 0) {
		while($row = mysql_fetch_assoc($result)) {
			echo "<tr>";
			echo "<td>" . $row['cardID'] . "</td><td><a href=\"test.php?cardID={$row['cardID']}\">" . $row['bill_last_name'] . "</td>";
			echo "</tr>";
		}		
	}
My two remaining questions are, two fields being displayed, cardID and bill_last_name. When I place the cursor over the cardID display, it does not change. When I place the cursor over the bill_last_name, the hyperlink works their. Why does it only work on the one field? Second, on test \"test.php?cardID={$row['cardID']}\", I understand after parsing that statement will look like test.php?cardID=00002. Within test.php, how do I use cardID=0002?

Re: embed href in php output

Posted: Tue Jul 26, 2011 1:58 pm
by greyhoundcode
Basically, to turn something into a hyperlink it needs to be wrapped up inside an anchor element, just like you tried to do in the second column. Also note that you appear to be missing the end tag </a> in the second column (around $row['bill_last_name'], that is).

Code: Select all

<!-- Opening tag -->          <!-- Text in between -->    <!-- Closing tag -->
<a href="http://website.com"> Please click on this link!  </a>
There are some good tutorials to help you learn HTML here. To answer your second question you will need to inspect the $_GET array from within test.php, more on that here.

Re: embed href in php output

Posted: Tue Jul 26, 2011 6:14 pm
by crowegreg
Thanks.I'll study the tutorials!