flyboeing Posted April 19, 2010 Share Posted April 19, 2010 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 [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
F1Fan Posted April 19, 2010 Share Posted April 19, 2010 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>'; } ?> Quote Link to comment Share on other sites More sharing options...
flyboeing Posted April 19, 2010 Author Share Posted April 19, 2010 Thank you for your reply. If there is no URL in that field. it is just empty. The problem for me is, I don't know how I can combine the two scripts (your script into mine) Quote Link to comment Share on other sites More sharing options...
F1Fan Posted April 19, 2010 Share Posted April 19, 2010 Post your code. Quote Link to comment Share on other sites More sharing options...
flyboeing Posted April 19, 2010 Author Share Posted April 19, 2010 <? 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> Quote Link to comment Share on other sites More sharing options...
F1Fan Posted April 19, 2010 Share Posted April 19, 2010 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? Quote Link to comment Share on other sites More sharing options...
flyboeing Posted April 19, 2010 Author Share Posted April 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
F1Fan Posted April 19, 2010 Share Posted April 19, 2010 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. Quote Link to comment Share on other sites More sharing options...
flyboeing Posted April 19, 2010 Author Share Posted April 19, 2010 Thanks! Can you help me with that? I am not very familiar with that Quote Link to comment Share on other sites More sharing options...
F1Fan Posted April 19, 2010 Share Posted April 19, 2010 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> Quote Link to comment Share on other sites More sharing options...
flyboeing Posted April 19, 2010 Author Share Posted April 19, 2010 Thank you so much for you help! This is what I wanted. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.