PHP 'IF Statement' comparing two tables in MYSQL

I am relatively new to PHP, I understand some and other bits confuse me... I have two tables n a database one called timages (Where I keep images) and one called technical data (where I store data)
On adding the data to the database, if there is no image for the particular data being added I auto upload a default gif image in its place.
When the data is called, the imageid in the technocaldata calls the reference for the image by id in timages.
My problem is that in technicaldata I store an imageid that relates to timages, so that I can call the technicaldata image as requested. what I need to do is have an IF Statement (think) that if the image that is called by the imageid is a gif, it doesnot display the links.
What I can do is call the timages type as stored and then checj if it is a gif and then have the IF replace the coe in the script.

I have attached the code where I call the technicaldata whee I have added to if statements to replace the links if they are needed or not. It is the val [6} in the IF staement that I need to compare to:
SELECT type FROM timages WHERE id="'.$val[6].'"
Then either display nothing if $val[6] = to a gif or if not display the two links $tdsdownload $tdslink

This is probably a bit confusing! Any help would be much a[[reciated
include 'admin/connect.php';
$res=mysql_query('SELECT id,tdstitle,subheading,supplier,dat,category,imageid FROM technicaldata WHERE category="2" ORDER BY tdstitle DESC',$dbh) or die(mysql_error());
for($run=0; $run<$num; $run++)
if ($val[6]=='0') $tdsdownload='&nbsp;'; else $tdsdownload='<a href="imagedownload.php?a='.$val[6].'"><img src="images/download.gif" alt="Download TDS" width="30" height="22" border="0"></a>';
if ($val[6]=='0') $tdslink='&nbsp;'; else $tdslink='<a href="image.php?a='.$val[6].'&TB_iframe=true&height=760&width=730" rel="sexylightbox"><img src="images/tds.gif" alt="View TDS" width="30" height="22" border="0"></a>';
echo '<table width="820" border="0" cellspacing="0" cellpadding="0" bgcolor="'.$col.'">
  <td height="40" align="right" valign="middle"><table width="820" height="22" border="0" cellspacing="0" cellpadding="0">
      <td width="303" align="left" class="mainTXT12LEFT"><a href="image.php?a='.$val[6].'&TB_iframe=true&height=760&width=730" rel="sexylightbox"><span class="Formtitles13">'.$val[1].'</span></td>
      <td width="327" align="left" class="mainTXT12LEFT">'.$val[2].'</td>
      <td width="110" height="22" align="left"><span class="mainTXT12LEFT">'.$val[4].'</span></td>
      <td width="41" align="left">'.$tdsdownload.'</td>
      <td width="39" align="center" class="maineleven_BK">'.$tdslink.'</td>
      <td height="1" align="center" valign="top"><img src="images/greenspacer.gif" width="820" height="1"></td>

Open in new window

Who is Participating?
OK, first, to get the image from the timage tabl;e in the same query, change your SQL on line 4 to this:

$res=mysql_query('SELECT,t.tdstitle,t.subheading,t.supplier,t.dat,t.category,t.imageid,,i.image FROM technicaldata as t, timages as i WHERE t.category="2" and t.imageid = ORDER BY t.tdstitle DESC',$dbh) or die(mysql_error());

#this assumes your timages table has an "id" collumn that matches the imageid collumn in technicaldata, and that you store the image in a collumn called "image" in the timages table AND that you "store" only the image name and not the actual image as a blob.

So, now your SQL will return all the data as previous but in addition, you will laos now have the link between the tecnical data you queried and it's associated image from the timages table, as well as the image name.

Now, you need to first get the image name from your query - insert this on line 9 of your code:

$imagename= $val['image'];

Now we need to check if the image is a .gif or something else:

$extension = substr($imagename,-3);
This returns the last three characters of the filename.

Now we can check to see if it matches "gif" or "GIF" or "Gif" to then either display or not.

if (($extension == "gif") || ($extension == "GIF") || ($extension == "Gif)) {

} else {

 You can ajust your code on lines 10 and 11 using this.

flashjordanAuthor Commented:
That was great thanks... It was the selecting from two tables that was confusing me, but your clear explanasion did it... Perfect Thanks!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.