Jump to content

Recommended Posts

Hi there

 

I've been working with some code to display a single

record on page. This all works fine and I'm able to pull what I

want from the database. My problems is trying to use that

data and turning it into something else like a link.

 

I have a field in the database called image url which

contains rows of image urls.

 

So here is the problem area of the code:

 

<?php
//////Displaying Data/////////////
$id=$_GET['id']; // Collecting data from query string
if(!is_numeric($id)){ // Checking data it is a number or not
echo "Data Error";
exit;
}

$fetch=mysql_query("select * from productfeeds where ProductID=$id ");
$row=mysql_fetch_object($fetch);
echo mysql_error();
echo "<table>";
echo "
<tr><td><b>ProductID</b></td><td>$row->ProductID</td></tr>
<tr><td><b>ProductName</b></td><td>$row->ProductName</td></tr>
<tr><td><b>ProductPrice</b></td><td>$row->ProductPrice</td></tr>

//problem area for me
<tr><td><b>Image</b></td><td>$row->ImageURL</td></tr>
echo "</table>";

 

I'm trying to edit this part of the code:

 

<tr><td><b>Image</b></td><td>$row->ImageURL</td></tr>

 

I've tried this:

 

<tr><td><b>Image</b></td><td><a href='{$row['URL']}'> <img src='{$row['ImageURL']}'></a>

 

and

 

<tr><td><b>Image</b></td><td><a href='$row['URL']'> <img src='$row['ImageURL']'></a>  //removed brackets

 

but I'm just getting errors. Can you guys help please?

 

Thank you very much.

 

 

Link to comment
https://forums.phpfreaks.com/topic/234753-creating-image-links-from-database/
Share on other sites

The code you have looks like it should work as is, you will need to assign the elements ( bit after the ->) to variables if you want to do things the way you are trying (so $img_url = $row->ImageURL), or change the mysql_fetch_object() to mysql_fetch_assoc() and use a while loop to run through the values.

 

if you just want to use the current code in the href this should work:

<tr><td><b>Image</b></td><td><a href='{$row->ImageURL}'> <img src='{$row->ImageURL}'></a>

 

assuming that you want to link to the image, but I don't see any other refference to a URL that you want to follow.

Hi there and thanks for reply:

 

If is try this: <tr><td><b>Image</b></td><td><a href='{$row['URL']}'> <img src='{$row['ImageURL']}'></a>

 

the error is returned as this:

( ! ) Fatal error: Cannot use object of type stdClass as array in C:\wamp\www\Test\product.php on line 28

Call Stack

# Time Memory Function Location

1 0.0032 374216 {main}( ) ..\product.php:0

 

If is try this: <tr><td><b>Image</b></td><td><a href='$row['URL']'> <img src='$row['ImageURL']'></a>  //removed brackets

 

 

the error is returned as this:

( ! ) Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in C:\wamp\www\Test\product.php on line 28

 

Inside the database I have a column called ImageURL and its just urls of the image path (external url)

Basically I want to hyper link that image with a url to wherever I want to point it.

 

Does this help in any way?

 

Thank you

The code you have looks like it should work as is, you will need to assign the elements ( bit after the ->) to variables if you want to do things the way you are trying (so $img_url = $row->ImageURL), or change the mysql_fetch_object() to mysql_fetch_assoc() and use a while loop to run through the values.

 

if you just want to use the current code in the href this should work:

<tr><td><b>Image</b></td><td><a href='{$row->ImageURL}'> <img src='{$row->ImageURL}'></a>

 

assuming that you want to link to the image, but I don't see any other refference to a URL that you want to follow.

 

Once again you have helped me solve my problem :)

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.