Current location: Hot Scripts Forums » Programming Languages » PHP » Help making a certain field into a hyperlink from an echoed MySQL table


Help making a certain field into a hyperlink from an echoed MySQL table

Reply
  #1 (permalink)  
Old 05-11-06, 02:16 PM
cynebald cynebald is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Help making a certain field into a hyperlink from an echoed MySQL table

Below is a truncated version of the MySQL table. The main focus is the title field. (I also have a question about the id field.)

Code:
+---+-----------------------------+----+------+
|id |title                        |year|rating|
+---+-----------------------------+----+------+
|1  |Robin Hood: Prince of Thieves|1991|PG-13 |
+---+-----------------------------+----+------+
|2  |A Perfect World              |1993|PG-13 |
+---+-----------------------------+----+------+
|3  |Lolita                       |1997|R     |
+---+-----------------------------+----+------+
Below is the PHP code I use to output the table's fields, minus the id field, into an HTML table for organized display on a web browser.

PHP Code:

<?php

$connect 
mysql_connect("localhost""root""password")
    or die(
"Could not connect: " mysql_error());

mysql_select_db("site");

$results mysql_query("SELECT title, year, rating FROM movies ORDER BY title")
    or die(
"Could not connect: " mysql_error());

while (
$row mysql_fetch_assoc($results)) {
    echo 
"\t<tr>\n";
    foreach (
$row as $value) {
        echo 
"\t\t<td>";
        echo 
$value;
        echo 
"</td>\n";
    }
    echo 
"\t</tr>\n\n";
}
echo 
"</table>";
?>
So, to my question. How could I hyperlink the movie titles to another page automatically? I'd like page to be named after the id; for example, Robin Hood: Prince of Thieves would hyperlink to 1.php, A Perfect World would hyperlink to 2.php, and Lolita would hyperlink to 3.php. The only way I've thought of doing so is by inputting the actual HTML code into the title field.

Code:
INSERT INTO test (id, title, year, rating) 
VALUES (NULL , '<a href="3.php">Lolita</a>', '1997', 'R');
This way seems impractical, and ORDER BY title won't function as intended anymore. Also, is it possible to have the id start at 0001 instead of 1? When I input the zeros, they vanish after I've submitted.
Reply With Quote
  #2 (permalink)  
Old 05-11-06, 02:51 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
For your last question, you would need to change the definition of your ID field to include - INT(4) ZEROFILL

You can build and echo the title as a link, using the id as the page number, but it would require eliminating the foreach loop. Since, you only have three fields this might not be that bad. I notice too that your posted code does not include an opening TABLE tag.

However, since naming and storage schemes often need to change (perhaps you find that after there are a few hundred files that you want to divide them up into sub folders or that using the ID makes it too hard to keep track of which file goes with which title), I recommend adding a column to your table that would hold the link. In this way the link can be whatever you want.

For this second scheme, you would still order the results based on the title field, but you would SELECT the link field and echo it out. The link field could be saved fully formed (which would repeat the title name) or you can build the link that you echo in the table from the link field (which would only contain the link address) and the title (which would provide the link display name.)
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #3 (permalink)  
Old 05-11-06, 07:54 PM
cynebald cynebald is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
OK, here is what I have now.

Code:
<table>

	<tr>
		<td>Title</td>
		<td>Year</td>
		<td>Rating</td>
	</tr>
PHP Code:

<?php

$connect 
mysql_connect("localhost""root""")
    or die(
"Could not connect: " mysql_error());

mysql_select_db("site")
    or die(
"Could not connect: " mysql_error());

$result mysql_query("SELECT id, title, year, rating FROM movies ORDER BY title")
    or die(
"Could not connect: " mysql_error());

while (
$row mysql_fetch_array($result)) {
    echo 
"\t<tr>\n";
    echo 
"\t\t<td><a href=".$row[id].">".$row[title]."</a></td>\n";
    echo 
"\t\t<td>".$row[year]."</td>\n";
    echo 
"\t\t<td>".$row[rating]."</td>\n";
    echo 
"\t</tr>\n\n";
}
?>
Code:
</table>
I've almost got it! Two questions: How do I append .php after ".$row[id].", and how do I encapsulate the two between double quotations?


Here is an example of what the above code outputs.
Code:
	<tr>
		<td><a href=3>Lolita</a></td>
		<td>1997</td>
		<td>R</td>
	</tr>
Here is an example of what I want.
Code:
	<tr>
		<td><a href="3.php">Lolita</a></td>
		<td>1997</td>
		<td>R</td>
	</tr>
Reply With Quote
  #4 (permalink)  
Old 05-11-06, 09:43 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
You are essentially building a string. The following will work -
PHP Code:

echo "\t\t<td><a href=\"".$row['id'].".php\">".$row['title']."</a></td>\n"
To get a double-quote within the string, put the \ to escape it. The .php is just part of the string.
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
  #5 (permalink)  
Old 05-11-06, 10:03 PM
cynebald cynebald is offline
Newbie Coder
 
Join Date: Jul 2005
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Thank you, mab. You've helped me a great deal.
Reply With Quote
  #6 (permalink)  
Old 05-11-06, 10:20 PM
mab's Avatar
mab mab is offline
Community VIP
 
Join Date: Oct 2005
Location: Denver, Co. USA
Posts: 2,674
Thanks: 0
Thanked 0 Times in 0 Posts
Quote:
Originally Posted by cynebald
Thank you, mab. You've helped me a great deal.
Thanks for saying so. It is a pleasure to help someone who knows their code, is only missing a little piece to make it all work, and can take an idea and incorporate it into what they already have .
__________________
Error checking, error reporting, and error recovery. If your code does not have these to get it to tell you why it is not working, what makes you think someone in a programming forum will be able to tell you why it is not working???
Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Mass Emailing from MySQL table field instill PHP 1 04-07-05 11:25 AM
Multiple form fields saved in one mysql field?? cebuy PHP 4 10-15-04 12:08 PM
Disable form fields to be submitted RickyRod JavaScript 2 05-24-04 10:15 AM
Newbie MySQL fccolon PHP 2 03-16-04 10:54 AM
How to see if 'yes' is in a field of a mysql table? x2z PHP 2 11-08-03 08:25 AM


All times are GMT -5. The time now is 07:12 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.