Jump to content

Java script to detect and load , no img


Barny74
Go to solution Solved by Barny74,

Recommended Posts

Hi,

 

I have been trying to code some javascript to detect when an image does not load , but having no luck as new to javascript.

 

I am trying to replace the below , if it does not load,

echo "<img src='".$row ['url_img']."' /><br />";

with an image called no_img.jpg

 

	if (isset($_GET['id'])) {
    $sql = "SELECT w.whisky_name
, price
, date
, avprice 
,url_img 
,whisky_id
FROM test_db t
JOIN 
    whisky w USING (whisky_id)
JOIN (
    SELECT whisky_id
    , AVG(price) as avprice 
    FROM test_db 
    WHERE whisky_id = :id
    GROUP BY whisky_id
    ) avcalc USING (whisky_id)
ORDER BY date DESC 
LIMIT 1";

    $stmt = $conn->prepare($sql);
    $stmt->execute( array( 'id' => $_GET['id']) );
    $row = $stmt->fetch();


    echo "<div class='details'>";
        echo "<br>";
        echo $row['whisky_name'];
        echo "<br><br>";
        echo "Average price based on all sales" ;
        echo"<br><br>";
        echo "<div class= 'price'>";
        echo " £";
        echo floor ($row ['avprice']);
        echo "</div>";
        echo "<br>";
        echo "<div class='img_border'>";
        echo "<img src='".$row ['url_img']."' /><br />";
        echo "<a href='records.php?id={$row['whisky_id']}' >Click here for more detailed results</a>";
        echo "<br>";
    echo "</div>";
}
	?>

Appreciate the help

Edited by Barny74
Link to comment
Share on other sites

i think the wording of your problem is perhaps causing unnecessary work in solving it.

 

are you trying to use a default image when you have no image defined in your database table, i.e. if $row['url_img'] is empty, use no_img.jpg as the image? if so, you would just put a conditional test in the php code that's producing the output.

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.