Solved

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

Posted on 2009-05-16
10
182 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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 108

Assisted Solution

by:Ray Paseur
Ray Paseur earned 250 total points
Comment Utility
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
Comment Utility
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
How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

 

Author Closing Comment

by:TimDunton
Comment Utility
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
Comment Utility
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 108

Expert Comment

by:Ray Paseur
Comment Utility
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
Comment Utility
>> 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
Comment Utility
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

Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

Join & Write a Comment

Suggested Solutions

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
This article discusses how to create an extensible mechanism for linked drop downs.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

762 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now