Hi everyone, I've been a reader for a long time, now I've joined up, as I'm fairly stumped on this one!!

I'm currently teaching myself mysql and php at work, and have a problem. I am using php to produce a listing of restaurants on a page, and I would like to do the following at the end of each line:

1) If the website field is not blank, display the url (or even just "Website")

2) Make the URL Clickable, and open in a new window.

I've managed to produce the list, however my code has gone south somewhere as the "website" field is displaying even when there is no entry in the database. Also when the link is present it's clickable, but opening in the same window. Can someone help me and tell me where I've gone wrong?

<?php
					mysql_connect(HOST,USER,PASS);
@mysql_select_db("db303278079") or die( "Unable to select database");
$query="
SELECT * FROM restaurants";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$rest_name=mysql_result($result,$i,"rest_name");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"website");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$area=mysql_result($result,$i,"area");
echo "<strong>$rest_name</strong>, $address, $telephone, ($cuisine_type), $area, <A HREF=$website>'Website'</a><br >";
$i++;
}
?>

Thanks in advance for your help :)

cwarn23 commented: good question... +4

Recommended Answers

All 4 Replies

hi,

you pretty much have it right there by calling the $website variable inside of the href, but your basically saying its text becuase its inside the echo statement.

try something like this.....

{?>
<A HREF=<?php $website ?> >'Website'</a><br >
<?php   }  ?>

Perhaps this:

<?php
mysql_connect(HOST,USER,PASS);
@mysql_select_db("db303278079") or die( "Unable to select database");
$query="SELECT * FROM restaurants";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
for ($i=0; $i<$num; $i++) {
$rest_name=mysql_result($result,$i,"rest_name");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"website");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$area=mysql_result($result,$i,"area");
$website=(empty($website))?'':", <A HREF=$website>'Website'</a>";
echo "<strong>$rest_name</strong>, $address, $telephone, ($cuisine_type), $area$website<br >";
}
?>
commented: helps a lot to show me where I'm going wrong, thanks for the help!! +0

Hi everyone, I've been a reader for a long time, now I've joined up, as I'm fairly stumped on this one!!

I'm currently teaching myself mysql and php at work, and have a problem. I am using php to produce a listing of restaurants on a page, and I would like to do the following at the end of each line:

1) If the website field is not blank, display the url (or even just "Website")

2) Make the URL Clickable, and open in a new window.

I've managed to produce the list, however my code has gone south somewhere as the "website" field is displaying even when there is no entry in the database. Also when the link is present it's clickable, but opening in the same window. Can someone help me and tell me where I've gone wrong?

<?php
					mysql_connect(HOST,USER,PASS);
@mysql_select_db("db303278079") or die( "Unable to select database");
$query="
SELECT * FROM restaurants";
$result=mysql_query($query);
$num=mysql_numrows($result);
mysql_close();
$i=0;
while ($i < $num) {
$rest_name=mysql_result($result,$i,"rest_name");
$address=mysql_result($result,$i,"address");
$telephone=mysql_result($result,$i,"telephone");
$website=mysql_result($result,$i,"website");
$cuisine_type=mysql_result($result,$i,"cuisine_type");
$area=mysql_result($result,$i,"area");
echo "<strong>$rest_name</strong>, $address, $telephone, ($cuisine_type), $area, <A HREF=$website>'Website'</a><br >";
$i++;
}
?>

Thanks in advance for your help :)

line7

mysql_numrows()

should be

mysql_num_rows()

and the mysql_close() better to put at the end of the script.
Also check if website is a valid url first, if its not opening properly

commented: thanks for the help, really shows me where I'm going wrong, much appreciated!! :) +0

thanks for the help guys, it's a slow learning process but I'm getting there!! hopefully one day I can be the one answering the threads!!! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.