Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1282
  • Last Modified:

PHP if exists help

I have the following code that displays .jpg files for records in a database.  This works great except in the case where the _01.jpg file does not exist where is displays the ugly default missing graphic outline in the browser.  Can someone update this snippet so that I can see how to make it only display if the file exists?


if(mysql_num_rows($info) > 0){
			while($row = mysql_fetch_row($info)){
				$cat .= "<tr><td colspan='2' style='background-color:#000000' height='3'><td></tr>";
				$link = "$row[0]";
				$image = "<img width='400' height='300' src='".$datafd[1][$k+1][0]."$row[0].jpg' align='left' vspace='3' hspace='3' /><BR><img width='400' height='300' src='".$datafd[1][$k+1][0]."$row[0]_01.jpg' align='left' vspace='3' hspace='3' />";
				$pas = 1;
				$firstline = "";
				for ($i=0;$i<count($datafd[3][0]);$i++){
					$firstline .= "<td width='$width%'>";
					if (!(strcmp(trim($datafd[3][$k+1][$i]),"no")==0)){
						$firstline .= $row[$pas]."</td>";
						$pas++;
					}
					$firstline .= "</td>";
				}

Open in new window

0
pda4me
Asked:
pda4me
  • 2
1 Solution
 
hieloCommented:
try:
if(mysql_num_rows($info) > 0){
                  while($row = mysql_fetch_row($info)){
			   	$f1=$datafd[1][$k+1][0] . $row[0] . '.jpg');
				$f2=$datafd[1][$k+1][0] . $row[0] . '_01.jpg';
			    if( file_exists($f1) && file_exists($f2) )
			    {
                        $cat .= "<tr><td colspan='2' style='background-color:#000000' height='3'><td></tr>";
                        $link = "$row[0]";
                        $image = "<img width='400' height='300' src='".$datafd[1][$k+1][0]."$row[0].jpg' align='left' vspace='3' hspace='3' /><BR><img width='400' height='300' src='".$datafd[1][$k+1][0]."$row[0]_01.jpg' align='left' vspace='3' hspace='3' />";
                        $pas = 1;
                        $firstline = "";
                        for ($i=0;$i<count($datafd[3][0]);$i++){
                              $firstline .= "<td width='$width%'>";
                              if (!(strcmp(trim($datafd[3][$k+1][$i]),"no")==0)){
                                    $firstline .= $row[$pas]."</td>";
                                    $pas++;
                              }
                              $firstline .= "</td>";
                        }
				}
				

Open in new window

0
 
pda4meAuthor Commented:
I swapped out the code and only got a blank white page, no errors?
0
 
hieloCommented:
do you see the expected values for $f1 and $f2?

...
$f1=$datafd[1][$k+1][0] . $row[0] . '.jpg');
$f2=$datafd[1][$k+1][0] . $row[0] . '_01.jpg';
echo $f1. "<br" . $f2;
 if( file_exists($f1) && file_exists($f2) )
...
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now