jonMEGA Posted April 17, 2003 Share Posted April 17, 2003 Hello I would like to have the last column in this table to be clickable ( <a href=" ">link</a> ) Here is what the php code looks like: <?php /* Connecting, selecting database */ $link = mysql_connect("localhost", "user", "password") or die("Could not connect"); /*print "Connected successfully";*/ mysql_select_db("mymovies") or die("Could not select database"); /* Performing SQL query */ $query = "SELECT * FROM `movies` ORDER BY `title` ASC"; $result = mysql_query($query) or die("Query failed"); /* Printing results in HTML */ print "<table border=1>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link); Link to comment Share on other sites More sharing options...
0 Quboid Posted April 17, 2003 Share Posted April 17, 2003 You loop through each field in a row with a foreach() so you can't catch the last entry easily. The simpliest way to adapt this is to change the foreach() so it stores the key as well as the value and checks the key against the field name for the last column, but a better way would be to re-write without using a foreach(). The simple way is as follows: foreach ($line as $col_key=>$col_value) { ? ? ? ? ? ?if ($col_key == "imdbEntry") ? ? ? ? ? ? ? ?print "\t\t<td><a href=\"$col_value\">$col_value</a></td>"; ? ? ? ? ? ?else ? ? ? ?? ? ? ? ?print "\t\t<td>$col_value</td>\n"; ? ? ?? ?} $col_key will hold the key. I'm calling the last column in the database imdbEntry, of course you replace this with the actual name. EDIT: Fixed code, should actually work now (!) Link to comment Share on other sites More sharing options...
0 jonMEGA Posted April 17, 2003 Author Share Posted April 17, 2003 :) It works http://jonmega.dyndns.org/movies/index2.php :) But I would like to have the links show in a new window so I tried foreach ($line as $col_key=>$col_value) { if ($col_key == "imdbEntry") print "\t\t<td><a href=\"$col_value\" TARGET="_blank">$col_value</a></td>"; else print "\t\t<td>$col_value</td>\n"; } and it gives me an error. Link to comment Share on other sites More sharing options...
0 Quboid Posted April 17, 2003 Share Posted April 17, 2003 You need to escape the target tag. At present, it interprets the first " in "_blank" as the end of the sentence to be printed, therefore it thinks the writing after it is an error. Add backslashes \ before each quote like I did in the href=\"$col_value\" bit to tell PHP to print quotes rather than end the sentence. It won't print out the \, don't worry. Link to comment Share on other sites More sharing options...
0 jonMEGA Posted April 17, 2003 Author Share Posted April 17, 2003 Thanks :) http://jonmega.dyndns.org/movies/index3.php Now I need to research how to have every other line a different color. BTW what editor or IDE do you use to code PHP ? Link to comment Share on other sites More sharing options...
0 Quboid Posted April 17, 2003 Share Posted April 17, 2003 I typically do all my site work in Macromedia's Dreamweaver MX. It has a good HTML editor, handy built in FTP client and good colour highlighting. If I just have a quick job, like fixing a typo bug, I use Editplus 2. Each line in a different colour ... are you familiar with style sheets? On the page PC Friendly's Product Page, I used a simple and elegent method with 2 styles, each the same but with a different background colour. One called something like "cell1" and the other "cell2". At the end of for() loop that displays each line, I have this if statement to alternative the value of $c (c for colour) between one and two for each line (before the for(), I set it to 1): if ($c == 1) $c = 2; else $c = 1; Then when I'm print the cell, I do: print "<td class=\"cell$c\">content</td>"; This gives each cell on a row the same style and changes the style between rows. If you're not familar with styles, you could do the same thing with $c but use if statements inside the for() loop and print a background colour based on that. Not as elegent, but it will work perfectly. Link to comment Share on other sites More sharing options...
Question
jonMEGA
Hello I would like to have the last column in this table to be clickable (
)
Here is what the php code looks like:
<?php /* Connecting, selecting database */ $link = mysql_connect("localhost", "user", "password") or die("Could not connect"); /*print "Connected successfully";*/ mysql_select_db("mymovies") or die("Could not select database"); /* Performing SQL query */ $query = "SELECT * FROM `movies` ORDER BY `title` ASC"; $result = mysql_query($query) or die("Query failed"); /* Printing results in HTML */ print "<table border=1>\n"; while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) { print "\t<tr>\n"; foreach ($line as $col_value) { print "\t\t<td>$col_value</td>\n"; } print "\t</tr>\n"; } print "</table>\n"; /* Free resultset */ mysql_free_result($result); /* Closing connection */ mysql_close($link);Link to comment
Share on other sites
5 answers to this question
Recommended Posts