Solved

Can I apply urls stored in mysql as links in dreamweaver?

Posted on 2009-05-16
10
188 Views
Last Modified: 2013-12-12
Hello experts, I am working on a website in Dreamweaver CS4 using wamp/

I have a dynamic table of data from mysql relating to news articles that the website's users may be interested in viewing. I would like users to be able to click on the row or a word or two such as 'read more' and navigate to the third party website where they can view the full article.

The columns I have within the database are:

news_id (primary)
news_section (I would like to separate the news into four categories)
news_date (the date the article was written)
news_source (the site that published the article)
news_headline
news_intro (the first paragraph)
news_link (the URL I wish to link to)

How do I go about using the URL that is being held and applying it to the either the entire row or to the text 'read more'.

As I said, this is all new to me.

Your help would be appreciated"
0
Comment
Question by:TimDunton
  • 4
  • 3
  • 3
10 Comments
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24404072
Hi, Tim.  I'm not sure I can help with the Dreamweaver part of this, but I can show you the general guidance that would get this information from the DB into the HTML.
<?php // RAY_mysql_example.php
error_reporting(E_ALL);
 
// IMPORTANT PAGES FROM THE MANUALS
// MAN PAGE: http://us2.php.net/manual/en/ref.mysql.php
// MAN PAGE: http://us2.php.net/manual/en/mysql.installation.php
 
 
// CONNECTION AND SELECTION VARIABLES FOR THE DATABASE
$db_host = "localhost"; // PROBABLY THIS IS OK
$db_name = "??";        // GET THESE FROM YOUR HOSTING COMPANY
$db_user = "??";
$db_word = "??";
 
// OPEN A CONNECTION TO THE DATA BASE SERVER
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-connect.php
if (!$db_connection = mysql_connect("$db_host", "$db_user", "$db_word"))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB CONNECTION: ";
   echo "<br/> $errmsg <br/>";
}
 
// SELECT THE MYSQL DATA BASE
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-select-db.php
if (!$db_sel = mysql_select_db($db_name, $db_connection))
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>NO DB SELECTION: ";
   echo "<br/> $errmsg <br/>";
   die('NO DATA BASE');
}
// IF WE GOT THIS FAR WE CAN DO QUERIES
 
 
 
// CREATING AND SENDING A SELECT QUERY AND TESTING THE RESULTS
// MAN PAGE:http://us2.php.net/manual/en/function.mysql-query.php
// APPLY THE APPROPRIATE WHERE AND LIMIT CLAUSES TO THIS QUERY
$sql = "SELECT news_headline, news_link FROM my_table";
$res = mysql_query($sql);
 
// IF mysql_query() RETURNS FALSE, GET THE ERROR REASONS
if (!$res)
{
   $errmsg = mysql_errno() . ' ' . mysql_error();
   echo "<br/>QUERY FAIL: ";
   echo "<br/>$sql <br/>";
   die($errmsg);
}
// IF WE GET THIS FAR, THE QUERY SUCCEEDED AND WE HAVE A RESOURCE-ID IN $res SO WE CAN NOW USE $res IN OTHER MYSQL FUNCTIONS
 
 
 
// DETERMINE HOW MANY ROWS OF RESULTS WE GOT
// MAN PAGE: http://us2.php.net/manual/en/function.mysql-num-rows.php
$num = mysql_num_rows($res);
if (!$num)
{
   echo "<br/>QUERY FOUND NO DATA: ";
   echo "<br/>$sql <br/>";
}
else
{
   echo "<br/>QUERY FOUND $num ROWS OF DATA ";
   echo "<br/>$sql <br/>";
}
 
 
 
// ITERATE OVER THE QUERY RESULTS SET
while ($row = mysql_fetch_assoc($res))
{
 
// INJECT COLUMN NAMES AND VALUES INTO MY NAMESPACE
   extract($row);
 
// CREATE THE LINKS
   echo "<br/><a target=\"news\" href=\"$news_link\">$news_headline</a\n";
}

Open in new window

0
 

Author Comment

by:TimDunton
ID: 24404200
Hi Ray, thanks for taking the time to respond...

I already have the database connection set up and the queries are working fine. I can also display the data.

What I'm struggling with is this:

1) At the moment the URLs are stored in a varchar field in the database and so when they are displayed they are simply text. Is this ok?

2) I don't actually want to display the URL, rather apply the URL as a link to one/all of the fields in the row. So for example, a user can look at the headline, read the intro and if he/she wants to read more, can simply click and be directed to the original article (without having to view or click on the actual URL)

As I mentioned I really am very new to this, your help is appreciated.

Thanks
   
0
 
LVL 70

Accepted Solution

by:
Jason C. Levine earned 250 total points
ID: 24404208
Hi Tim,

If I read your question correctly, all you need to do is echo the URL being held in the database into an <a> construct in your HMTL:

<a href="<?php echo $row_YourRecordSet['news_link'] ?>">Read more</a>

The easiest way to build the link above is to type "Read more" in Design View then select it.  In the Properties Panel, click the lightning bolt icon next to the link box to map the link to the database field.
0
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 110

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
ID: 24404265
Hi, Tim.  See line 79 in the code snippet above.  That would (almost) do what you suggest, if you fix my typo!.
echo "<br/><a target=\"news\" href=\"$news_link\">$news_headline</a>\n";

Open in new window

0
 

Author Comment

by:TimDunton
ID: 24404298
Thanks guys, I'm going to have a little play around and see where I'm at. I'll get back to you asap
0
 

Author Closing Comment

by:TimDunton
ID: 31582263
Thanks so much guys. I almost feel guilty using experts exchange, the advice is so valuable and succinct. I promise however that I spend hours trying to work things out for myself before asking a question here.

I have no training and have really thrown myself in at the deep end so I can only say thanks again, I'd be pulling my hair out by now if it wasn't this site and the experts who share their knowledge. Be lucky!
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 24404660
Thanks for the kind words, Tim.

A bit of explanation between Ray's answer and mine.  Ray is a REAL php coder and I fake it convincingly with the help of Dreamweaver.  In the grand scheme of things, Ray's answer is actually better as it shows you the steps to you will need in order to really learn how PHP does things while my answer addresses the Dreamweaver interface more than it does PHP code.

Good luck with your site and feel free to keep asking questions of us.  Don't feel guilty :)
0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 24407216
Tim: Thanks for the points; it's a good question.  

Let me echo what jason1178 says: One of the best attributes in a programmer is laziness!  Think about it... Lazy programmers rely on examples and advice.  Lazy programmers avoid repeating their mistakes and save time debugging, etc.  Personally I have found EE to be a great resource for the lazy pursuit of my programming goals.  One should never feel guilty about this sort of thing.

But if you want to put a little work into your PHP education, consider getting this excellent book.
http://www.sitepoint.com/books/phpmysql1/

Best regards, ~Ray
0
 
LVL 70

Expert Comment

by:Jason C. Levine
ID: 24409074
>> One of the best attributes in a programmer is laziness!

Oddly enough I was just telling my database dev that he needs to get lazier instead of constantly trying to reinvent the wheel :)
0
 

Author Comment

by:TimDunton
ID: 24410068
Sorry chaps, only just noticed your final comments. I appreciate the pointers and I'll definitely take a look at the book. Thanks Ray & Jason, keep up the good work!
0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As a database administrator, you may need to audit your table(s) to determine whether the data types are optimal for your real-world data needs.  This Article is intended to be a resource for such a task. Preface The other day, I was involved …
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question