Forum Moderators: coopster

Message Too Old, No Replies

insert query results into hyperlink

one field in table of query results converted to hyperlink?

         

crackpipe

4:32 pm on Aug 13, 2009 (gmt 0)

10+ Year Member



A couple of days scratching my head over this one. I've got three columns I return from a MySQL query that I put into table format for as many records as the search returns. This works flawlessly and I won't waste space here with the table formatting. This is how I return the query:


$result = mysql_query("SELECT * FROM Items");
while ($row = mysql_fetch_array($result)) {
echo "<tr><td>">".$row{'item_name'}."</td><td>".$row{'item_pages'}."</td><td>".$row{'item_desc'}."</td></tr>";
}

You can see the three columns I return from the table are 'item_name', 'item_pages', and 'item_desc'. All works fine and it makes a table with as many rows as there are results.

The problem starts now I've attempted to add clickability to my results for "item_name" so I can simply click on any 'item_name' result and retrieve that file; 'item_name' stores complete file names in the database. The first, intermediate step was to see if I could insert a dead-end hyperlink, with 'item_name, and keep the formatting within the table:


while ($row = mysql_fetch_array($result)) {
echo "<tr><td><a href=\"#\">".$row{'item_name'}."</a></td><td>".$row{'item_pages'}."</td><td>".$row{'item_desc'}."</td></tr>";
}

This worked quite well for displaying an underlined text version of the file name ("item_name"'s value) in each row of the table. Of course, when clicked, the link didn't do anything but reload the page, since the link just went to '#'. So the final step, replacing '#' with the value in 'item_name' had to be taken. But inserting "item_name"'s value into the 'a href="' side of the hyperlink has not worked any way I've crafted it. When I get to this place...
 
while ($row = mysql_fetch_array($result)) {
echo "<tr><td><a href=\".$row{'item_name'}.\">".$row{'item_name'}."</a></td><td>".$row{'item_pages'}."</td><td>".$row{'item_desc'}."</td></tr>";
}

...the entire link disappears. Someone here probably knows the proper code? Thanks.

jatar_k

5:45 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld,

have you looked at the html it creates via view source? usually thse are html errors and the first clue is looking at the final product.

andrewsmd

9:49 pm on Aug 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



jatar_k is correct check out the html source code. My guess is that item_name is nothing. If the link is disappearing I bet when your echoing $row{'item_name'} it's null which phps ingenious brain parses it to "" automatically for you (develop in .net for a while and you will learn to appreciate little things like that). I bet in your source you have something like <tr><td><a href=""></a></td>... and thus no link. Just a guess though.

crackpipe

2:20 am on Aug 14, 2009 (gmt 0)

10+ Year Member



I appreciate the welcome and the responses. Your pointer to "view source" was the kicker -- I knew about 'view source', but was unaware source would appear as generated by the PHP, not as coded in the PHP. Apparently, there is a different format for displaying variables in HTML than in PHP. I'll just show the relevant portion of the solution here.

Previously (didn't create hyperlink):


echo "<tr><td><a href=\".$row{item_name}.\">".$row{'item_name'}."</a></td>

Currently (creates proper hyperlink):


echo "<tr><td><a href=\"$row[item_name]\">".$row{'item_name'}."</a></td>

That is, in HTML, columns within an array appear to require square brackets around their name (eg.[item_name]) to display the field's value, whereas in PHP, we seem to need braces and apostrophes around the column's name (eg. {'item_name'}). Thanks for the 'view source'pointer which allowed me to get there.

On a side note, if anyone here feels strongly that an excellent database engineering program exists in either a US or Euro university, I would love to hear about it. Thanks.

jatar_k

12:57 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



actually the braces are wrong, column names, as you put it, which is actually just standard array notation

in your case $row is an array, as returned from mysql_fetch_array or like function

you can access the parts of this array as such

$row['column_name']

though really that isn't 100% true as what is returned from a query is not always a column name

andrewsmd

1:21 pm on Aug 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I use the DB.php file that comes with PEAR. If the web page showed the php then it wouldn't a be server side programming language, it would have to be client side like JS.