Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Find the right 'if' for the code 1

Status
Not open for further replies.

sinbadly

Technical User
Mar 16, 2006
126
GB
This is an example of the way I get a list of articles to appear on the webpage. I use a small image beside each brief piece of text.

However, sometimes there isn't an image.

How can I say within this code then, if there isn't an image, go straight onto the text, please?

In different coding, I can use
if ($myrow["fieldname"])
but that arrangement doesn't work with this format.

Code:
$result = mysql_query("SELECT id, head, pfolder, px, blurb
etc etc ...")

if ($myrow = mysql_fetch_assoc($result))	
{
do
{

printf("<h1>%s </h1> 
<img src=\"../images/%s/
%s					
3.jpg\" align=\"left\" title=\"
%s.\" alt=\"
%s.\">
%s \n ... 
<br><span class=\"aim\"><a href=\"%s?id=%s\" title=\"Link to the article.\">
Article  %s &nbsp; &raquo;</a> 
</span> <br>
<div id=\"date2\"> %s</div><br>
<hr align=\"left\" color=\"#FFFFFF\" width=\"250\" size=\"1\"><br>\n",
$myrow["head"], 
$myrow["pfolder"], 
$myrow["px"],
$myrow["blurb"]  etc etc
 
Is the database pulling information about a picture that may or may not be on the filesystem? If so, then before printing out the img tag, you could run file_exists() to check if the file exists in the specified location on the server. If it does, print the img tag, if it does not, then don't.

I do not know why you would have information in the database on a picture if there is no picture in the filesystem though.
 
I would have a default picture link in the DB for when there is no picture, that way there is no checking involved.

Anyway To do what you want you would need to change how you are outputting the information. Instead of using printf in a huge string, with %s interspersed in it, you could turn it into blocks, so you can check before outputting each block.
Code:
if(file_exists($myrow['px'])){
echo "<img src='../images/$myrow['pfolder']/$myrow['px']" . "3.jpg'";
}
then move on with the rest of the output.

----------------------------------
Ignorance is not necessarily Bliss, case in point:
Unknown has caused an Unknown Error on Unknown and must be shutdown to prevent damage to Unknown.
 
Thanks, vacunita, that seems to be the way to go. Much obliged to you.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top