Php MySQL - Hyperlink in Php not functioning correctly

I have a MySQL table made up of several fields, 2 of which are for the url, one field is labelled ‘prefix’ which contains ‘http://’ and the other field contains the remaining section of the url i.e. www.midchesh.ac.uk or ‘shop.niace.org.uk/adultslearning-free-sample.html’.

I have echoed the info out onto the screen but now want to connect the ‘prefix’ and ‘url’ fields from the database to enable the user to click on the link and go to the destination.

Unfortunately at the moment when I click a link it keeps the current url in the address bar and just tags the new url to the end which obviously causes an error.

The script below is the php I currently have, I would be grateful if someone could advise as to where I have made the error.

Thanks

$query = “SELECT * FROM resources”;
$result = mysql_query($query) or die(mysql_error());
echo ‘

Websites and Journals

’;
while ($row = mysql_fetch_array($result))
{
echo $row['id'] . '<br />';
echo $row['type'] . '<br />';
echo "<a href=/" . $row['prefix'] . $row['url'] . ">" . $row['prefix'] . $row['url'] . "</a>";
echo '<br />' . $row['notes']. '<br /><br /><hr /><br />';
echo "<br />";

}

Thanks again for any help.

Hi there,

Try this code I modified for you:

[php]<?php

$query = “SELECT * FROM resources”;
$result = mysql_query($query) or die(mysql_error());
echo ‘

Websites and Journals

’;
while ($row = mysql_fetch_array($result))
{

echo $row[‘id’] . ‘
’;
echo $row[‘type’] . ‘
’;
echo “<a href=’”.$row[‘prefix’].$row[‘url’]."’>".$row[‘prefix’].$row[‘url’]."";
echo ‘
’ . $row[‘notes’]. ‘



’;
echo “
”;
}[/php]

Hope this code helps.

Thanks for that OpzMaster, works great. At the risk of being cheeky I am trying to fit " target="_blank" in their as well but when I place it where I think it should go things fall apart again.

[php]
echo $row[‘id’] . ‘
’;
echo “<a href=’” . $row[‘url’] . ‘target="_blank’ . “’>” . $row[‘url’] . “”;
echo ‘
’ . $row[‘login’] .’
’;
echo $row[‘notes’].’



’;
echo “
”;
[/php]

Thanks for any help.

You really need to take care of your quotes. You mismatched single and double quotes. If you get into these problems, just try to be consistent. You choose one method and keep an eye on standards.

[php]echo “{$row[‘id’]}
”;
echo “<a href=”{$row[‘url’]}" target="_blank">{$row[‘url’]}";
echo “
{$row[‘login’]}
”;
echo “{$row[‘notes’]}



”;
echo “
”;[/php]

All variables are included with {} which allows you to use your assoc array directly. As you can see, single quotes are only used for the array elements. Double quotes for tag attributes but make sure you escape them correctly. Whatever you do, don’t mismatch your single and double quotes.

Hope this helps.

Thanks Richard, yes it did solve that issue however now due to your comments regarding escaping I am flummoxed by the following line of code

[php]
echo “<a href=’”.$row[‘prefix’].$row[‘url’]."’>".$row[‘prefix’].$row[‘url’]."";
[/php]

I don’t seem to be able to get the correct syntax on this line.

Thanks in advance for any help

If you are not sure how to do this, just first start by writing the correct HTML, something like

<a href="PREFIXURL">PREFIXURL</a>

Then echo and escape it.

[php]echo “<a href=“PREFIXURL”>PREFIXURL”;[/php]

Then insert the variables.

[php]echo “<a href=”{$row[‘prefix’]}{$row[‘url’]}">{$row[‘prefix’]}{$row[‘url’]}";[/php]

This method works every time :smiley:

Thanks a lot Richard, been trying to sort it since the post but didn’t get anywhere, the way you suggest to write it does make sense.

;D

Sponsor our Newsletter | Privacy Policy | Terms of Service