embed href in php output

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

embed href in php output

Post 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.
Last edited by Benjamin on Tue Jul 26, 2011 1:00 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Re: embed href in php output

Post 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>";
		}		
	}
(#10850)
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: embed href in php output

Post 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>";
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: embed href in php output

Post by greyhoundcode »

Pipped at the post :P
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

Re: embed href in php output

Post 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?
Last edited by Benjamin on Tue Jul 26, 2011 1:01 pm, edited 1 time in total.
Reason: Added [syntax=php|sql|css|javascript] and/or [text] tags.
User avatar
greyhoundcode
Forum Regular
Posts: 613
Joined: Mon Feb 11, 2008 4:22 am

Re: embed href in php output

Post 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.
crowegreg
Forum Newbie
Posts: 13
Joined: Tue Jul 19, 2011 2:48 pm

Re: embed href in php output

Post by crowegreg »

Thanks.I'll study the tutorials!
Post Reply