Jump to content

Recommended Posts

Hi,

 

I am trying to have an image show only if it exists.

 

I've managed to get the image to show if it is there and if not, shows a graphic that says NO IMAGE AVAILABLE.

 

What I am trying to is if there is no image show nothing.

 

//This bit in the code below is causing my problems

<img src=\"$thumbnail\">

 

If I take out the else statement. I get a broken image graphic on the page, which of course would be there because it is looking for <img src=\"$thumbnail\">

 

I am stumped as to how to get around this.... Thankyou

 

while($row = mysql_fetch_array($result, MYSQL_NUM))
{

	list($id, $title, $date, $description, $thumbnail) = $row;

	if ($thumbnail) {
		$thumbnail = WEB_ROOT . 'images/' . $thumbnail;
	} else {
		$thumbnail = WEB_ROOT . 'images/noimage.png';
	}	

	$content .= "<li><b><a href=\"item.php?id=$id\">$title</a>
	<br>$date</b><br>
	$description<br>

//This bit is causing my problems
<img src=\"$thumbnail\">

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/
Share on other sites

 

Thankyou for your quick reply.

 

I have tried numerous ways of using file_exists.

(I've been working on this little issue for about a week now)

 

Tried this

if (file_exists$row[thumbnail]) {
   <img src=\"$thumbnail\">;
} else {
    echo "";
}

 

All that happens is that no image show and the result just echo code out.

eg ; } else { echo ""; } 

 

My host is still on php4

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-867716
Share on other sites

<?php
if (file_exists$row[thumbnail]) {
   <img src=\"$thumbnail\">;
} else {
    echo "";
}
?>

there is a lot wrong with that code

 

1. file_exists is a function you have to use ()  and not directly paste value you want to check behind it.

 

2. $row[thumbnail] is an array only you are not using quotes you can only leave the quotes when the array keys are whole numbers 0,1,2,3,4, etc.

write it like so:

<?php
$row['thumbnail']; 
//or double quotes
$row["thumbnail"];
?>

 

3. 

 <img src=\"$thumbnail\">;

either close the php tags or echo your html between quotes. you cant mix html and php like that

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-867826
Share on other sites

<?php
if (file_exists$row[thumbnail]) {
   <img src=\"$thumbnail\">;
} else {
    echo "";
}
?>

there is a lot wrong with that code

 

1. file_exists is a function you have to use ()  and not directly paste value you want to check behind it.

 

2. $row[thumbnail] is an array only you are not using quotes you can only leave the quotes when the array keys are whole numbers 0,1,2,3,4, etc.

write it like so:

<?php
$row['thumbnail']; 
//or double quotes
$row["thumbnail"];
?>

 

3. 

 <img src=\"$thumbnail\">;

either close the php tags or echo your html between quotes. you cant mix html and php like that

 

thankyou have tried that now and removed the else statement. Also changed from $row to just get $id of thumbnail

If I do this

$id['thumbnail'];

if (file_exists$id['thumbnail']; 
{  echo '<img src=\"images/$thumbnail\">'
}

I get Parse error: parse error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

If I do this

$id['thumbnail'];

if (file_exists$id["thumbnail"]; 
{  echo '<img src=\"images/$thumbnail\">'
}

I get Parse error: parse error, unexpected '"', expecting T_STRING or T_VARIABLE or T_NUM_STRING

 

If I do this

if (file_exists$id[$thumbnail]; 
echo '<img src=\"images/$thumbnail\">'

I get

if (file_exists2;

echo '<img src="images/thumbnail2.jpg">'

actually printed out onto the web document , if the image exists is shows, if it doesn't it still shows broken image.

So with the if(file_exists  being printed out that usually means that I have enclosed in <?php ?>

but it is.

 

 

This is my entire code for this section.

<?php
ini_set('display_errors', 1);
ini_set('error_reporting', E_ALL | E_STRICT);
include_once ("library/config.php");

$id = $_GET['id'];

?>
<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
<?php

// if no id is specified, list the available tbl_news
if(!isset($_GET['id']))
{
$self   = $_SERVER['PHP_SELF'];
$query  = "SELECT id, title, DATE_FORMAT(`Date`, '%D %M %Y') AS `date`, news_description, thumbnail FROM tbl_news ORDER BY date DESC LIMIT 10";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 

//The news list

$content =  '<ol>';


while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title, $date, $news_description, $thumbnail) = $row;

	$content .= "<li><b><a href=\"news.php?id=$id\">$title</a>
<br>$date</b><br>
$news_description<br>


if (file_exists$id[$thumbnail]; 
 echo '<img src=\"images/$thumbnail\">'
<br>
<br></li></b>\r\n";
}

$content .= '</ol>';

$title   = 'News';
} else {
////Everything after this is working nicely

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-868106
Share on other sites

With the following:

1. file_exists is a function you have to use ()  and not directly paste value you want to check behind it.

I meant:

file_exists($id["thumbnail"]);

 

 

Another thing, in the following code:

$content .= "<li><b><a href=\"news.php?id=$id\">$title</a>
   <br>$date</b><br>
   $news_description<br>
   
      
   if (file_exists$id[$thumbnail]; 
    echo '<img src=\"images/$thumbnail\">'
   <br>
   <br></li></b>\r\n";

 

notice how everything is highlighted in red? It makes it more clear that all of it gets crammed as a string in the variable $content.

Your if statement and file_exist should not be inside a string.

 

End the string after $news_description<br>

 

like so:

$content .= "<li><b><a href=\"news.php?id=$id\">$title</a>
   <br>$date</b><br>
   $news_description<br>";

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-868230
Share on other sites

notice how everything is highlighted in red? It makes it more clear that all of it gets crammed as a string in the variable $content.

Your if statement and file_exist should not be inside a string.

 

End the string after $news_description<br>

 

like so:

$content .= "<li><b><a href=\"news.php?id=$id\">$title</a>
   <br>$date</b><br>
   $news_description<br>";

 

thanks I ended the string. Now I am getting a parse error, unexpected T_VARIABLE on line 41 which is

if (file_exists$id[$thumbnail];

I really can not understand why this isn't working

 

<?php
$content =  '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title, $date, $news_description, $thumbnail) = $row;



	$content .= "<li><b><a href=\"news1.php?id=$id\">$title</a>
	<br>$date</b><br>
	$news_description<br>";

if (file_exists$id[$thumbnail];
    echo '<img src=\"images/$thumbnail\">'
?>

 

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871657
Share on other sites

You are not listening to him, file exists is a function...

as he has said use it like so:

if (file_exists($id["$thumbnail"]));

 

just copy that code delete ure existing code with the if statement on the line and paste that.

 

please listen this time,

Shergold.

 

Thankyou, I did listen and tried that, and I now do not have the T_VARIABLE error. But now I have an error a couple of lines down where there is a <br> tag. parse error, unexpected '<'

 

I will keep on working on it and see if I can figure it out. I'm probably missing a semi-colon or something.

 

 

Which

if (file_exists ($row["$rss_thumbnail"]));
    echo '<img src=\"images/$rss_thumbnail\">';
    
<br>
	<br></li></b>\r\n";
}

$content .= '</ol>';

$title   = 'News';
} else {

 

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871671
Share on other sites

ok, your problem is that you have placed the html code in php without any tags around them.

 

the

  <br>

        <br></li>  and the rest have no starting " or echo.

 

it should be like so:

 

echo "<br><br></li></b>\r\n";

 

also you have placed a semi-colon ";" at the end of the if statement,

take the semi colon of like so:

if (file_exists ($row["$rss_thumbnail"]))

 

Shergold.

 

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871679
Share on other sites

although if the

<br><br></li></b>\r\n

is meant to be in the if statement above it use the following code, this has all the fixes in so you can just copy it and paste it over existing:

 

if (file_exists ($row["$rss_thumbnail"])){
    echo '<img src=\"images/$rss_thumbnail\">';
    echo "<br><br></li></b>\r\n";
   }

 

If you have any more problems just ask,

shergold.

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871683
Share on other sites

ok, your problem is that you have placed the html code in php without any tags around them.

 

the

  <br>

        <br></li>  and the rest have no starting " or echo.

 

it should be like so:

 

echo "<br><br></li></b>\r\n";

 

also you have placed a semi-colon ";" at the end of the if statement,

take the semi colon of like so:

if (file_exists ($row["$rss_thumbnail"]))

 

Shergold.

 

 

 

Thankyou very much...I have no errors now,

But I now have no images, in the source view of the page, it doesn't show echoing the <img src

 

I've also figured out I have to us $id instead of $row or I get undefined index. (don't understand that)

<?php
$content =  '<ol>';


while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title, $date, $news_description, $thumbnail) = $row;

///////Shows image  if exists otherwise shows default image ..working		
//		if ($rss_thumbnail) {
//			$rss_thumbnail = WEB_ROOT . 'images/' . $thumbnail;
//		} else {
//			$rss_thumbnail = WEB_ROOT . 'images/no-image-small.png';
//		}	

	$content .= "<li><b><a href=\"news1.php?id=$id\">$title</a>
	<br>$date</b><br>
	$news_description<br>";

if (file_exists ($id["$thumbnail"]))
echo '<img src=\"images/$thumbnail\">';    

echo "<br><br></li></b>\r\n";
}

$content .= '</ol>';

$title   = 'News';
} else {
?>

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871698
Share on other sites

Why not ditch the list function? You're making it more complicated for yourself then you really need.

 

remove

list($id, $title, $date, $news_description, $thumbnail) = $row;

 

The $row var is an array in your loop, you can see the thumbnail value like so(if the fieldname is thumbnail that is):

<?php
while($row = mysql_fetch_array( $result )) {
  // in case your fieldname for your thumbnail is indeed thumbnail.
  echo $row['thumbnail'];
}
?>

 

you can check what values are stored in the $row array like so:

<?php
while($row = mysql_fetch_array( $result )) {
    echo "<pre>",print_r($row),"</pre>";
}
?>

 

And what does your query look like and what are the field names you wish to use? Can you post the complete code including the sql query?

 

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871813
Share on other sites

Why not ditch the list function? You're making it more complicated for yourself then you really need.

 

remove

list($id, $title, $date, $news_description, $thumbnail) = $row;

 

The $row var is an array in your loop, you can see the thumbnail value like so(if the fieldname is thumbnail that is):

<?php
while($row = mysql_fetch_array( $result )) {
  // in case your fieldname for your thumbnail is indeed thumbnail.
  echo $row['thumbnail'];
}
?>

 

you can check what values are stored in the $row array like so:

<?php
while($row = mysql_fetch_array( $result )) {
    echo "<pre>",print_r($row),"</pre>";
}
?>

 

And what does your query look like and what are the field names you wish to use? Can you post the complete code including the sql query?

 

 

Why not ditch the list function? You're making it more complicated for yourself then you really need.

 

The reason I have the $rss_thumbnail to $thumbnail in the code in the forum is that I have been researching this for days and there seems to be no solution, google, this forum . I've been comming to this forum for years and I usually can always find an answer because some has asked it before. So I thought it would be easier for others to search out for a solution if I put $thumbnail.

 

 

 

I am also no Guru, this code has been adapted from my articles code, which I did a few years ago with a tutorial, it works nicely, So I am attempting to have a create feed on my homepage. This is how I learn. So except for this image issue it all works.

I have working code that will display a thumbnail, and I could make sure every news item had a thumbnail, but this is not what I am trying to achieve. I even thought of making an invisible.gif so it wouldn't show. That would only be a work around.

 

Just show the image only if it exists.

 

Why not ditch the list function? You're making it more complicated for yourself then you really need.

 

The reason I'm using the list function is because of adapting this from my articles code, all of that works, I understand how I could change that, I'm sure that there is a whole lot of edits that could be done in this code.

In the code as it is now, I have no thumbnails showing and this is echoing in the html source a number of times

<br/><br></li></b>\r\n

 

<?php
error_reporting(E_ALL);
include_once ("library/config.php");

$id = $_GET['id'];

?>
<link href="styles.css" rel="stylesheet" type="text/css" media="screen" />
<?php

// if no id is specified, list the available tbl_news
if(!isset($_GET['id']))
{
$self   = $_SERVER['PHP_SELF'];
$URL = www.mysite.com;
$query  = "SELECT id, title, DATE_FORMAT(`Date`, '%D %M %Y') AS `date`, news_description, thumbnail  FROM tbl_news ORDER BY date DESC LIMIT 10";
$result = mysql_query($query) or die('Error : ' . mysql_error()); 


// create the rss news list

$content =  '<ol>';


while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title, $date, $news_description, $thumbnail) = $row;

///////Shows image  if exists otherwise shows default image ..working		
//		if ($thumbnail) {
//			$thumbnail = WEB_ROOT . 'images/rss/' . $thumbnail;
//		} else {
//			$thumbnail = WEB_ROOT . 'images/no-image-small.png';
//		}	

	$content .= "<li><b><a href=\"news1.php?id=$id\">$title</a>
	<br>$date</b><br>
	$news_description<br>";

if (file_exists ($id["$thumbnail"]))
echo '<img src=\"images/$thumbnail\">';    

echo "<br><br></li></b>\r\n";
}

$content .= '</ol>';

$title   = 'News';
} else {
// get the info from database

//This code is what is shown after clicking on the news link on homepage
$query   = "SELECT id, title, DATE_FORMAT(`Date`, '%D %M %Y') AS `date`, news_description, rss_image, content FROM tbl_news WHERE id=".$_GET['id'];
$result  = mysql_query($query) or die('Error : ' . mysql_error()); 
$row     = mysql_fetch_array($result, MYSQL_ASSOC); 

$title   = $row['title'];
$content = $row['content'];
$rss_image = $row['rss_image'];
$date = $row['date'];


}
   	
?>

<body>
<table class="whitelink" width="100%" BGCOLOR="#FFFFFF">
  <tr> <td> 
      <h1 align="center">
        <?php echo $title;?>
      </h1>
      <b><a type="application/rss+xml" href="http://localhost/testing/rss.php">Subscribe 
      to the Fairy Art News Feed <img src="feed-icon-16.gif" width="16" height="16" border="0"></a></b><br>
      <br>
      <p><img src="images/rss/<?php echo $rss_image; ?>" class="floatleft" alt="Fairy Art <?php echo $title;?>">
      <br>
      <?php echo $date; ?>
<br>
  <?php echo $content;?>
    </p><br>
    
      <?php

// when displaying an article show a link
// to see the article list
if(isset($_GET['id']))
{ 
?>
      <?php
}
?>
    </td>
  </tr> </table>
</body>
</html>

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-871873
Share on other sites

It does make more sense now since you posted more of the code. How ever it is messy though and has vulnerabilities.

What I see is that you're basically are trying to put 2 pages in  1  which are:

[*]a page with all records listed

[*]a detail page(showing only details of one record)

 

Is this correct?

 

But anyway looking back at the previous code which is just a lot of bullocks (and which I am sorry to say I suggested due to being either too tired to see it or just too early without having a proper cup of coffee thrown down my throat)

file_exists($id["thumbnail"]);

That was no good since the $id var wasn't an array to start with. it should be:

file_exists($thumbnail);

 

or use the row instead

file_exists($row["thumbnail"]);

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-872235
Share on other sites

What I see is that you're basically are trying to put 2 pages in  1  which are:

[*]a page with all records listed

[*]a detail page(showing only details of one record)

 

Is this correct?

 

Yes this is correct and works!

 

But anyway looking back at the previous code which is just a lot of bullocks (and which I am sorry to say I suggested due to being either too tired to see it or just too early without having a proper cup of coffee thrown down my throat)

 

No sorry to say it's not the coffee, it is the code. I can understand code that is written like this.

I switched my site over to php in 2000, and have learnt this on a need to know.

I have successfully fluked most functionality I have added and have learnt a lot this way.

 

When I am a rich and famous artist I'll have staff and I'll outsource! (Actually it will probably be my children)

 

I tried your last suggestion, and a few variations on it.

I had to put the if statement in brackets, I don't know why but it stopped all the parse errors.

I still see a broken image.

The browser is still expecting the image.

 



list($id, $title, $date, $news_description, $thumbnail) = $row;

if (file_exists ($row["thumbnail"])) {
$thumbnail = WEB_ROOT . 'images' . $thumbnail;
}

$content .= "<li><b><a href=\"news1.php?id=$id\">$title</a>
<br>$date</b><br>
$news_description<br>

//The browser is still expecting the image here because this is where I want to display it.
//So image is broken if there is no image.
<img src=\"$thumbnail\">

 

Going over all this again and the helpful posts, the suggestions should be working.

 

 

So I have this work around now, so if the image exists it show otherwise clear.gif displays

which gives me what I want displayed on the page.

But not using the file_exists which I still want to know how to implement.

 

 



$content =  '<ol>';
while($row = mysql_fetch_array($result, MYSQL_NUM))
{
list($id, $title, $date, $news_description, $thumbnail) = $row;

if ($thumbnail) {
$thumbnail = WEB_ROOT . 'images/' . $thumbnail;
} else {
$thumbnail = WEB_ROOT . 'images/spacer.gif';
}

$content .= "<li><b><a href=\"news1.php?id=$id\">$title</a>
<br>$date</b><br>
$news_description<br>

<img src=\"$thumbnail\">

<br><br></li></b>\r\n";
}
$content .= '</ol>';

 

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-872376
Share on other sites

Do note that you need to pass the complete url (path + name) of the image location to the function. Just the name of the image is not enough.

 

What is the location of the image? and have you tried to echo out the path and the image name to see if it is correct?

echo WEB_ROOT . 'images/' . $row['thumbnail'];

 

if that is the correct path try:

if (file_exists (WEB_ROOT . 'images/' . $row['thumbnail'])) {

 

You might also want tocheck if the slashes are included I notice you forgot them throughout your code on several places.

Link to comment
https://forums.phpfreaks.com/topic/164500-show-image-if-exists/#findComment-872606
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.