Jump to content

How to make a image a link when it is outputted using PHP?


usman07

Recommended Posts

I have done this, and the page appears and when I place the cursor over the image a hand appears so it shows the image being a link, but now when I click the image, it comes up with this:

echo "<a href={$row['url']}><img class='image1' src='{$row['image_path']}' alt='{$row['summary']}'> </a> <br />";

but now when I click the image, it comes up with the message 'There was a problem etc...' from this code below.

}
else{
echo "There was a problem, please click<a href='index.php'> Here </a>to return to the main page and try again";

Link to comment
Share on other sites

  • Replies 84
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

Posted Images

Thats to weird, I added the quotation marks:

"<a href='{$row['url']}'><img class='image1' src='{$row['image_path']}' alt='{$row['summary']}'> </a> <br />";

I can see that the image is now a link but it just doesn't take me to the page that its meant to with the link in the database.

Link to comment
Share on other sites

Adding the escaped quotations makes my image disappear a blue sqaure question mark appears. this is what I have:

echo "<a href=\'{$row['url']}\'><img class='image1' src=\'{$row['image_path']}\' alt=\'{$row['summary']}\'> </a> <br />";

Link to comment
Share on other sites

You add double quotes for the html not php and you have to escape them for php, your going to have to play with it but basiclly

 

so it would be something like href=\"{$row['url']}\" and the same for src and alt

 

I use single quotes so echo '<a href='.$row['url'].'></a>';

 

Like I said, play with it.

Link to comment
Share on other sites

my echo statement:

echo "<a href=\'{$row['url']}\'><img class='image1' src=\'{$row['image_path']}\' alt=\'{$row['summary']}\'> </a> <br />";

The PHP code above is meant to get the url from the database right, from the field name 'url'?

 

Should the url be from my web directory or the actually website website, iv tried both and still does not work.

 

Image of my database which includes |id|property id|image_path|url

 

Why is it still not working?

post-130660-13482403503538_thumb.png

Link to comment
Share on other sites

You were using the table called `property`for this info and another table `images` for the property image.  I don't see where these tables are defined in your code on the first post.  Maybe I missed it.  Are you using a different table structure now?  Are you actually getting results with $row?

Link to comment
Share on other sites

Yeah mate i'm using a different table structure, yeah i'm getting all the results. The image has turned into a link but the link does not go to where I want it to go. And I have the url link in my database.

 

Here's the full code of echo's:

$qry = $sql->sqlStart.$sql->stmt.'Group By property.id';
$results = mysql_query($qry) or die (mysql_error()."<br />|-|-|-|-|-|-|-|-|-|-|-|-<br />$qry");
if(mysql_num_rows($results) < 1){
	die ("Sorry, No Results Match Your Search.");
}
while($row = mysql_fetch_assoc($results)){
	echo '<div class="container" style="float:left;">';
	echo '<div class="imageholder" style="float:left;">';
	echo "<a href=\'{$row['url']}\'><img class='image1' src=\'{$row['image_path']}\' alt=\'{$row['summary']}\'> </a> <br />";
	echo '</div>';
	echo '<div class="textholder" style="font-family:helvetica; font-size:14px; float:left; padding-top:10px;">';
	echo "{$row['summary']}";
	echo "<span style=\"color:#63be21;\"><br><br><b>{$row['bedrooms']} bedroom(s) {$row['bathrooms']} bathroom(s) {$row['receptions']} reception room(s)</b></span>";
	if($row['parking'] != null){
		echo "<span style=\"color:#63be21;\"><b> {$row['parking']} parking space(s)</b></span>";
	}

	echo '</div>';
	echo '<div style="clear:both"></div>';
}

}
else{
echo "There was a problem, please click<a href='index.php'> Here </a>to return to the main page and try again";
}

 

So when I click the link of the image, it echo's this:

echo "There was a problem, please click<a href='index.php'> Here </a>to return to the main page and try again";

 

Link to comment
Share on other sites

Save this as test.php (make sure to include your database connection) then point your browser to it

<?php
/* put your database connection here */
$query = "SELECT * FROM images";
$result = mysql_query($query);
while ($row = mysql_fetch_array($result)) {
<?
<a href="<?php echo $row['url']; ?>"><img src="<?php echo $row['image_path']; ?>" alt=""></a><hr>
<?php
}
?>

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.


×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.