vixtran 10-18-2002, 04:50 AM ok, I want to enter in table data and have it displayed as a link. I am halfway new to this and I am trying to see if I can list and item as a hyperlink through php. There are many items in the table each with its own hyperlink. Can anyone help?
thanks
Jeewhizz 10-18-2002, 02:57 PM How is it stored?!
THere are two ways i can imagine this being done, the efficient and the inefficient ways!
Efficient
you have a table (call it links) with two fields... href_www and href_words
in href_www we have the webaddress like http://www.codingforums.com and in href_words we have the words to describe the link e.g. Coding Forums
Then use the following code (assuming that you have already got a connection to the dbase through $connect)
$connect = mysql_connect('username','pass','localhost');
$query = "SELECT * FROM `links` ORDER BY `href_words` ASC";
$result = mysql_db_query($db,$query,$connect);
while(list($www,$words)=mysql_fetch_array($result))
{
echo "<a href=\"$www\">$words</a>";
}
That's the easy way....
the other way is if you stored the link html (e.g. <a href="http://www.codingforums.com/">Coding Forums</a>) in a field... in which case you could do this:
$connect = mysql_connect('username','pass','localhost');
$query = "SELECT * FROM `links`";
$result = mysql_db_query($db,$query,$connect);
while(list($link)=mysql_fetch_array($result))
{
echo $link;
}
Hope this helps
Jee
mordred 10-18-2002, 03:51 PM Just want to add my 2 cents:
1. 'SELECT * ' is not an efficient way to talk to a database. This statement will give you everything back from the table regardless what you actually need from it. Also, it makes SQL statements unreadable.
2. Don't forget a primary key, I suggest implementing a ID field of the type int, so you have a unique identifier.
3. mysql_db_query() is deprecated. Use mysql_select_db() to connect to the database and send the query with mysql_query().
4. If you don't need the numerical indices of the fields of each record, you can also use mysql_fetch_assoc(). It's faster.
vixtran 10-18-2002, 06:30 PM OK,hmm heres what I am doing. I am grabing information from a database. This information is in the form of
map_name
map_type
date_sub
map_sta
date_pst
fin_scor
when I pull the information it is listed in a straight line left to right row by row. Currently I am not using an unique ID, but can if it makes it more efficient.
I want the Map_name to be a link to the review.
I am kind of new at this, so this simple task may be just buzzing me by.This is the current way I am accessing my DB
$db_name = "map_review";
$table_name = "map_rev";
$connection = @mysql_connect("localhost", "user", "pass") or die("Couldn't Connect.");
$db = @mysql_select_db($db_name, $connection) or die("Couldn't select database.");
$sql = "
SELECT map_name, map_type, date_sub, map_sta, date_pst, fin_scor FROM $table_name
ORDER BY map_name";
$result = @mysql_query($sql, $connection) or die("Couldn't execute query.");
while ($row = mysql_fetch_array($result)) {
thanks for all your help.
mordred 10-18-2002, 08:50 PM That's not dificult at all. The only thing you have to do is to create a string and have the right db values inserted at the right places. Let's suppose that "map_name" contains the URL portion of the link, and "map_sta" the title of the link (like http://www.codingforums.com and Codingforums).
while ($row = mysql_fetch_array($result)) {
$str = '<a href="' . $row['map_name'] . '">' . $row['map_sta'] . '</a><br />';
echo $str;
}
That will print out your db values as hyperlinks. I hope that's what you asked for.
I noticed that you used quite short, very condensed looking field names. You are free to use longer ones that have more meaning in them, but maybe they make perfect sense to you and not me just because I'm not aquainted with your project.
And having a unique ID is very helpful in case you want to retrieve a specific recordset, because in the WHERE part of the SQL statement you just have to write 'WHERE id = 23456' and that makes looking it up by MySQL as fast as possible. Comparing against whole text values does not achieve the same level of performance. Also, you will have a nice way to construct relationships between tables someday later, because MySQL as a RDBMS is much more capable at doing things than a simple flatfile.
vixtran 10-18-2002, 10:11 PM thank you very much...the ID, I have to assign them physically for each record correct. It mayseem easy to you all lol, but I am just learning as you can see. I appreciate your help very much..
ok one more, maybe I am listing this wrong, it lists it as a hyperlink, however no matter what I put it, it only takes me to the
locahost root folder
thank you
mordred 10-19-2002, 08:57 AM Originally posted by vixtran
the ID, I have to assign them physically for each record correct. It mayseem easy to you all lol, but I am just learning as you can see.
Exactly for this purpose MySQL supplies you with a very versatile feature. It's called AUTO_INCREMENT, and is a special attribute you can assign to a table's field. Every new record gets an automagically generated ID which is greater one than the previous record's id. You can also add an AUTO_INCREMENT column later; in this case the ids are also generated as a sequence list for you.
http://www.mysql.com/doc/en/example-AUTO_INCREMENT.html
ok one more, maybe I am listing this wrong, it lists it as a hyperlink, however no matter what I put it, it only takes me to the
locahost root folder
Hmh... without actually seeing what HTML output you received, I guess that your links start all with "/" - which points to the document root folder of your server. Show us your code and we'll see if we can fix that too.
vixtran 10-20-2002, 06:49 AM well actually this is where the long way is working...the hyperlink/name method actually placed in the data base.
I tried using while ($row = mysql_fetch_array($result)) {
$map_location '<a href="' . $row['map_link'] . '">' . $row['map_name'] . '</a><br />';
$map_type = $row['map_type'];
$date_sub = $row['date_sub'];
$map_sta = $row['map_sta'];
$date_pst = $row['date_pst'];
$fin_scor = $row['fin_scor'];
$display_name .= "<p><strong>$map_location</strong></p>";
$display_type .= "<p><strong>$map_type</strong></p>";
$display_dsub .= "<p><strong>$date_sub</strong></p>";
$display_msta .= "<p><strong>$map_sta</strong></p>";
$display_dpst .= "<p><strong>$date_pst</strong></p>";
$display_fscor .= "<p><strong>$fin_scor</strong></p>";
}
and called out each ind. display in a cell where i wanted it. Maybe the long way i dont know, but like I said I'm new :)
when i used <? echo "$display_name"; ?> all i get is hyperlink to the root folder
shouldn't i just be able to type in http://www.xxxxxxx.net in the links part and have that called ??? im lost lol..
http://www.truefighters.net/map_reviews/Reviews.php
mordred 10-20-2002, 02:14 PM Originally posted by vixtran
$map_location '<a href="' . $row['map_link'] . '">' . $row['map_name'] . '</a><br />';
If that's not an error from cut'n-pasting, then it's probably the root of your problem, because this line should read:
$map_location = '<a href="' . $row['map_link'] . '">' . $row['map_name'] . '</a><br />';
Though I'm astonished you don't mentioned any error messages... but it would make sense, since $map_location is empty, so is your hyperlink.
Please note that's not what I wanted to know - I asked specifically for the HTML output. The corresponding PHP source code is nice to see, but I can't guess the values of your variables from afar. They could be empty or containing sth. totally different, who knows? But often it helps to see the HTML code generated because that shows you what is actually printed out.
|
|