Jump to content

Only show image if URL is in DB


flyboeing

Recommended Posts

Hello,

I am a beginner with PHP and I am having a problem with showing data from a mysql database. In the database I have a table which contains (for example) 5 rows with data. Each row has an option of containing a URL to an image (some of them contain an image URL, some not). In my PHP I want to use I icon as a link for the image. The only problem is, when I am making the <a href="...."><img src="......"></a> in the resultpage (PHP file) the icon always shows up, even if the database doesn't contain an URL.

 

The icon only has to be shown when that row contains an URL.

 

Can someone help me with that?

 

My file is attached to this message. I am sorry if the file is not correct PHP, still beginning with it and I got it from the internet :shy:

 

[attachment deleted by admin]

Link to comment
Share on other sites

What is in that field if it is not a URL? If it is blank or null, you could just check to see it it's empty using the PHP empty() function. Like this:

 

<?php
if (empty($row['URLFieldName'])){
  // Do something here...
} else{
  echo '<a href="...."><img src="......"></a>';
}
?>

Link to comment
Share on other sites

<?
include("conn.php");
$name=$_POST["name"];
$departmentid=$_POST["department"];
$mstatus=trim($_POST["mstatus"]);
$dob=$_POST["dob"];
$wdate=$_POST["wdate"];
$city=$_POST["city"];
$email=$_POST["email"];
$academicid=$_POST["academic"];
$o=$_POST["o"];
$rest="";
$text="Search Keywords : ";

if($academicid!="")
  {
    if($rest=="")
  {
$rest.=" inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";
$text.="Academic = $academicid";
  }
else
  {
	$rest.=" $o  inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";
$text.=", Academic = $academicid";
  }
  }

if($name!="")
  {
if($rest=="")
  {
$rest.=" where fname like '$name%' or mname like '$name%' or lname like '$name%' ";
$text.="Name like $name ";
  }
else
  {
$rest.="$o where fname like '$name%' or mname like '$name%' or lname like '$name%' ";
$text.="Name like $name ";
  }
  }

if($departmentid!="")
  {
if($rest=="")
  {
$rest.=" where department='$departmentid' ";
$text.="Department = $departmentid";
  }
else
  {
$rest.=" $o department='$departmentid' ";
$text.=", Department = $departmentid";
  }
  }

if($mstatus!="")
  {
if($rest=="")
  {
$rest.="where mstatus='$mstatus' ";
$text.="Marital Staus = $mstatus";
  }
else
  {
$rest.=" $o mstatus='$mstatus' ";
$text.=", Marital Staus = $mstatus";
  }
  }

if($dob!="")
  {
if($rest=="")
  {
$rest.="where dob = '$dob' ";
$text.="Date of Birth = $dob ";
  }
else
  {
$rest.=" $o  dob = '$dob' ";
$text.=", Date of Birth = $dob ";
  }
  }

if($wdate!="")
  {
if($rest=="")
  {
$rest.="where wedding = '$wdate' ";
$text.="Wedding Date = $wdate";
  }
else
  {
$rest.=" $o  wedding = '$wdate' ";
$text.=", Wedding Date = $wdate";
  }
  }

if($city!="")
  {
if($rest=="")
  {
$rest.="where tcity = '$city' ";
$text.="City = $city";
  }
else
  {
$rest.=" $o  tcity = '$city' ";
$text.=", City = $city";
  }
  }


if($email!="")
  {
if($rest=="")
  {
$rest.="where email like '$email%'";
$text.="Email = $email";
  }
else
  {
$rest.=" $o email like '$email%' ";
$text.=", Email = $email";
  }
  }


if($rest!="")
  {
$rest=$rest;
  }
else
  {
//die("Enter Search Parameter<br><br><br><br><br><br><br><br><br><br><br>");
  }

$sql="select $empdetail.* from $empdetail $rest order by eta DESC";
$result=mysql_query($sql,$connection) or die(mysql_error());
$num=mysql_num_rows($result);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Wlink Employee Details</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<link href="../../style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../script.js"> </script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">

  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <table width="750" border="0" align="center" cellpadding="2" cellspacing="2">
        <tr>
          <td align="left">
<?
echo "<div align=right>$text</div>";
echo "<table width=700 border=0 cellpadding=0 cellspacing=0 align=center>\n
<tr>\n
<td> Total No of Records Found: $num   \n
<td>  \n

</tr>\n
<tr><table border=0 width=700 align=left class=allborder> \n
<tr> \n
<td><b>ETA</b></td><td><b>ETD</b></td><td><b>Type</b></td><td><b>Registration</b></td><td><b>Airline</b></td><td><b>Callsign</b></td><td><b>Orig</b></td><td><b>Dest</b></td>
<td><b>Date</td><td colspan=2><b>Info & Photo</b></td></tr>
<tr> \n";
$counter=0;
while($row=mysql_fetch_array($result))
{
$sn=$row['sn'];
$eta=$row['eta'];
$etd=$row['etd'];
$aircraft=$row['aircraft'];
$type=$row['type'];
$airline=$row['airline'];
$reg=$row['reg'];
$callsign=$row['callsign'];
$orig=$row['orig'];
$dest=$row['dest'];
$date=$row['date'];
$remarks=$row['remarks'];
$uitleg=$row['uitleg'];
$photo=$row['photo'];

$typesql="select sn,type from $academic where sn='$type'";
//echo $depsql;
$rAep=mysql_query($typesql,$connection) or die(mysql_error());
while($r=mysql_fetch_array($rAep)) {
	$typeid=$r['type'];
}

$aircraftsql="select sn,aircraft from $academic where sn='$aircraft'";
//echo $depsql;
$rAep=mysql_query($aircraftsql,$connection) or die(mysql_error());
while($r=mysql_fetch_array($rAep)) {
	$aircraftid=$r['aircraft'];
}

$airlinesql="select sn,airline from $department where sn='$airline'";
//echo $depsql;
$rAep=mysql_query($airlinesql,$connection) or die(mysql_error());
while($r=mysql_fetch_array($rAep)) {
	$airlineid=$r['airline'];
}


if($counter>=0) {
echo "
<tr>
<td>$eta</td><td>$etd</td><td><a href='#' title='ajax:type.php?sn=$sn'>$type</a></td><td>$reg</td><td>$airlineid</td><td>$callsign</td><td>$orig</td><td>$dest</td>
<td>$date</td><td><a href='#' ><img src='images/info.png' title='ajax:info.php?sn=$sn' border=0></img></a>
</td>


<td>

</td>
</tr>
\n";
 $counter++;
 }
 if($counter==2) {
 	echo "</tr>\n<tr>\n<td colspan=100%>Total No of Records Found: $num  \n</tr>\n<tr>\n";
	$counter=0;
 }
}
while ($row = mysql_fetch_array($result))
{
  if (trim($row['photo']) != '')
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }
}
echo "</table></table>\n";
?></td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

Link to comment
Share on other sites

OK, I assume you are referring to this line:

while ($row = mysql_fetch_array($result))
{
  if (trim($row['photo']) != '')
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }
}

 

So just do this:

while ($row = mysql_fetch_array($result))
{
  if (!empty($row['photo']))
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }
}

 

Also, are you aware that you're not using the $row['photo'] variable for the image source?

Link to comment
Share on other sites

Thanks.

What do you mean exactly? I am calling the photo with the $photo variable. Is this wrong? Because the piece of code that already was in the script, isn't working. I was trying to get it to work, but I don't know how. Maybe I am doing something wrong, placing the code at the wrong place or something like that.

Link to comment
Share on other sites

Ah, I see your problem. The problem is that you already called "while ($row = mysql_fetch_array($result))" earlier in your code. The first time you do that, you have emptied that result, so when you run it again, there's nothing left.

 

You need to run the query again (not recommended), move the contents of the second while loop into the first one, or save the rows into an array that you can use over and over. I suggest moving the contents on your second while loop so that it is inside the first while loop.

Link to comment
Share on other sites

Not sure which one you were asking about, but here's the array version.

<?
include("conn.php");
$name=$_POST["name"];
$departmentid=$_POST["department"];
$mstatus=trim($_POST["mstatus"]);
$dob=$_POST["dob"];
$wdate=$_POST["wdate"];
$city=$_POST["city"];
$email=$_POST["email"];
$academicid=$_POST["academic"];
$o=$_POST["o"];
$rest="";
$text="Search Keywords : ";

if($academicid!="")
  {
    if($rest=="")
  {



$rest.=" inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";



$text.="Academic = $academicid";
  }
else
  {




$rest.=" $o  inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";



$text.=", Academic = $academicid";
  }
  }

if($name!="")
  {
if($rest=="")
  {



$rest.=" where fname like '$name%' or mname like '$name%' or lname like '$name%' ";



$text.="Name like $name ";
  }
else
  {



$rest.="$o where fname like '$name%' or mname like '$name%' or lname like '$name%' ";



$text.="Name like $name ";
  }
  }

if($departmentid!="")
  {
if($rest=="")
  {



$rest.=" where department='$departmentid' ";



$text.="Department = $departmentid";
  }
else
  {



$rest.=" $o department='$departmentid' ";



$text.=", Department = $departmentid";
  }
  }

if($mstatus!="")
  {
if($rest=="")
  {



$rest.="where mstatus='$mstatus' ";



$text.="Marital Staus = $mstatus";
  }
else
  {



$rest.=" $o mstatus='$mstatus' ";



$text.=", Marital Staus = $mstatus";
  }
  }

if($dob!="")
  {
if($rest=="")
  {



$rest.="where dob = '$dob' ";



$text.="Date of Birth = $dob ";
  }
else
  {



$rest.=" $o  dob = '$dob' ";



$text.=", Date of Birth = $dob ";
  }
  }

if($wdate!="")
  {
if($rest=="")
  {



$rest.="where wedding = '$wdate' ";



$text.="Wedding Date = $wdate";
  }
else
  {



$rest.=" $o  wedding = '$wdate' ";



$text.=", Wedding Date = $wdate";
  }
  }

if($city!="")
  {
if($rest=="")
  {



$rest.="where tcity = '$city' ";



$text.="City = $city";
  }
else
  {



$rest.=" $o  tcity = '$city' ";



$text.=", City = $city";
  }
  }


if($email!="")
  {
if($rest=="")
  {



$rest.="where email like '$email%'";



$text.="Email = $email";
  }
else
  {



$rest.=" $o email like '$email%' ";



$text.=", Email = $email";
  }
  }


if($rest!="")
  {
$rest=$rest;
  }
else
  {



//die("Enter Search Parameter<br><br><br><br><br><br><br><br><br><br><br>");
  }

$sql="select $empdetail.* from $empdetail $rest order by eta DESC";
$result=mysql_query($sql,$connection) or die(mysql_error());
$num=mysql_num_rows($result);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Wlink Employee Details</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<link href="../../style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../script.js"> </script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">

  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <table width="750" border="0" align="center" cellpadding="2" cellspacing="2">
        <tr>
          <td align="left">
<?
echo "<div align=right>$text</div>";
echo "<table width=700 border=0 cellpadding=0 cellspacing=0 align=center>\n
<tr>\n



<td> Total No of Records Found: $num   \n



<td>  \n

</tr>\n
<tr><table border=0 width=700 align=left class=allborder> \n
<tr> \n
<td><b>ETA</b></td><td><b>ETD</b></td><td><b>Type</b></td><td><b>Registration</b></td><td><b>Airline</b></td><td><b>Callsign</b></td><td><b>Orig</b></td><td><b>Dest</b></td>
<td><b>Date</td><td colspan=2><b>Info & Photo</b></td></tr>
<tr> \n";
$counter=0;
$resultRows = array();
while($row=mysql_fetch_array($result))
{
  $resultRows[] = $row;
}


foreach ($resultRows as $row)
{

$sn=$row['sn'];



$eta=$row['eta'];



$etd=$row['etd'];



$aircraft=$row['aircraft'];



$type=$row['type'];



$airline=$row['airline'];



$reg=$row['reg'];



$callsign=$row['callsign'];



$orig=$row['orig'];



$dest=$row['dest'];



$date=$row['date'];



$remarks=$row['remarks'];



$uitleg=$row['uitleg'];



$photo=$row['photo'];



$typesql="select sn,type from $academic where sn='$type'";



//echo $depsql;



$rAep=mysql_query($typesql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$typeid=$r['type'];



}



$aircraftsql="select sn,aircraft from $academic where sn='$aircraft'";



//echo $depsql;



$rAep=mysql_query($aircraftsql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$aircraftid=$r['aircraft'];



}



$airlinesql="select sn,airline from $department where sn='$airline'";



//echo $depsql;



$rAep=mysql_query($airlinesql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$airlineid=$r['airline'];



}




if($counter>=0) {



echo "
<tr>
<td>$eta</td><td>$etd</td><td><a href='#' title='ajax:type.php?sn=$sn'>$type</a></td><td>$reg</td><td>$airlineid</td><td>$callsign</td><td>$orig</td><td>$dest</td>
<td>$date</td><td><a href='#' ><img src='images/info.png' title='ajax:info.php?sn=$sn' border=0></img></a>
</td>


<td>

</td>
</tr>
\n";



$counter++;



}



if($counter==2) {







echo "</tr>\n<tr>\n<td colspan=100%>Total No of Records Found: $num  \n</tr>\n<tr>\n";





$counter=0;



}
}
foreach ($resultRows as $row)
{
  if (!empty($row['photo']))
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }
}
echo "</table></table>\n";
?></td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

 

I still think you should move this part:

  if (trim($row['photo']) != '')
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }

into the other while loop, but I'm not sure where exactly you want it. Here's an example with it at the end.

<?
include("conn.php");
$name=$_POST["name"];
$departmentid=$_POST["department"];
$mstatus=trim($_POST["mstatus"]);
$dob=$_POST["dob"];
$wdate=$_POST["wdate"];
$city=$_POST["city"];
$email=$_POST["email"];
$academicid=$_POST["academic"];
$o=$_POST["o"];
$rest="";
$text="Search Keywords : ";

if($academicid!="")
  {
    if($rest=="")
  {



$rest.=" inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";



$text.="Academic = $academicid";
  }
else
  {




$rest.=" $o  inner join $academic on $empdetail.sn=$academic.empid and level='$academicid'";



$text.=", Academic = $academicid";
  }
  }

if($name!="")
  {
if($rest=="")
  {



$rest.=" where fname like '$name%' or mname like '$name%' or lname like '$name%' ";



$text.="Name like $name ";
  }
else
  {



$rest.="$o where fname like '$name%' or mname like '$name%' or lname like '$name%' ";



$text.="Name like $name ";
  }
  }

if($departmentid!="")
  {
if($rest=="")
  {



$rest.=" where department='$departmentid' ";



$text.="Department = $departmentid";
  }
else
  {



$rest.=" $o department='$departmentid' ";



$text.=", Department = $departmentid";
  }
  }

if($mstatus!="")
  {
if($rest=="")
  {



$rest.="where mstatus='$mstatus' ";



$text.="Marital Staus = $mstatus";
  }
else
  {



$rest.=" $o mstatus='$mstatus' ";



$text.=", Marital Staus = $mstatus";
  }
  }

if($dob!="")
  {
if($rest=="")
  {



$rest.="where dob = '$dob' ";



$text.="Date of Birth = $dob ";
  }
else
  {



$rest.=" $o  dob = '$dob' ";



$text.=", Date of Birth = $dob ";
  }
  }

if($wdate!="")
  {
if($rest=="")
  {



$rest.="where wedding = '$wdate' ";



$text.="Wedding Date = $wdate";
  }
else
  {



$rest.=" $o  wedding = '$wdate' ";



$text.=", Wedding Date = $wdate";
  }
  }

if($city!="")
  {
if($rest=="")
  {



$rest.="where tcity = '$city' ";



$text.="City = $city";
  }
else
  {



$rest.=" $o  tcity = '$city' ";



$text.=", City = $city";
  }
  }


if($email!="")
  {
if($rest=="")
  {



$rest.="where email like '$email%'";



$text.="Email = $email";
  }
else
  {



$rest.=" $o email like '$email%' ";



$text.=", Email = $email";
  }
  }


if($rest!="")
  {
$rest=$rest;
  }
else
  {



//die("Enter Search Parameter<br><br><br><br><br><br><br><br><br><br><br>");
  }

$sql="select $empdetail.* from $empdetail $rest order by eta DESC";
$result=mysql_query($sql,$connection) or die(mysql_error());
$num=mysql_num_rows($result);
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>

<title>Wlink Employee Details</title>

<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">


<link href="../../style.css" rel="stylesheet" type="text/css">
<script type="text/javascript" src="../../script.js"> </script>
<style type="text/css">
<!--
.style1 {color: #FFFFFF}
-->
</style>
</head>

<body>
<table width="775" border="0" align="center" cellpadding="0" cellspacing="0" bgcolor="#FFFFFF">

  <tr>
    <td><hr size="1" noshade></td>
  </tr>
  <tr>
    <td>
      <table width="750" border="0" align="center" cellpadding="2" cellspacing="2">
        <tr>
          <td align="left">
<?
echo "<div align=right>$text</div>";
echo "<table width=700 border=0 cellpadding=0 cellspacing=0 align=center>\n
<tr>\n



<td> Total No of Records Found: $num   \n



<td>  \n

</tr>\n
<tr><table border=0 width=700 align=left class=allborder> \n
<tr> \n
<td><b>ETA</b></td><td><b>ETD</b></td><td><b>Type</b></td><td><b>Registration</b></td><td><b>Airline</b></td><td><b>Callsign</b></td><td><b>Orig</b></td><td><b>Dest</b></td>
<td><b>Date</td><td colspan=2><b>Info & Photo</b></td></tr>
<tr> \n";
$counter=0;
while($row=mysql_fetch_array($result))
{



$sn=$row['sn'];



$eta=$row['eta'];



$etd=$row['etd'];



$aircraft=$row['aircraft'];



$type=$row['type'];



$airline=$row['airline'];



$reg=$row['reg'];



$callsign=$row['callsign'];



$orig=$row['orig'];



$dest=$row['dest'];



$date=$row['date'];



$remarks=$row['remarks'];



$uitleg=$row['uitleg'];



$photo=$row['photo'];



$typesql="select sn,type from $academic where sn='$type'";



//echo $depsql;



$rAep=mysql_query($typesql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$typeid=$r['type'];



}



$aircraftsql="select sn,aircraft from $academic where sn='$aircraft'";



//echo $depsql;



$rAep=mysql_query($aircraftsql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$aircraftid=$r['aircraft'];



}



$airlinesql="select sn,airline from $department where sn='$airline'";



//echo $depsql;



$rAep=mysql_query($airlinesql,$connection) or die(mysql_error());



while($r=mysql_fetch_array($rAep)) {





$airlineid=$r['airline'];



}




if($counter>=0) {



echo "
<tr>
<td>$eta</td><td>$etd</td><td><a href='#' title='ajax:type.php?sn=$sn'>$type</a></td><td>$reg</td><td>$airlineid</td><td>$callsign</td><td>$orig</td><td>$dest</td>
<td>$date</td><td><a href='#' ><img src='images/info.png' title='ajax:info.php?sn=$sn' border=0></img></a>";

  if (!empty($row['photo']))
  {
     echo "<a href='./photo/".$photo."'><img src='images/foto.png' border=0 /></img></a>";
  }

echo "</td>


<td>

</td>
</tr>
\n";



$counter++;



}



if($counter==2) {







echo "</tr>\n<tr>\n<td colspan=100%>Total No of Records Found: $num  \n</tr>\n<tr>\n";





$counter=0;



}
}
echo "</table></table>\n";
?></td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>

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.