PHP 'IF Statement' comparing two tables in MYSQL

Posted on 2008-11-15
Medium Priority
Last Modified: 2013-12-13
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

Question by:flashjordan
LVL 17

Accepted Solution

psimation earned 2000 total points
ID: 22970003
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.id,t.tdstitle,t.subheading,t.supplier,t.dat,t.category,t.imageid,i.id,i.image FROM technicaldata as t, timages as i WHERE t.category="2" and t.imageid = i.id 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.


Author Closing Comment

ID: 31517213
That was great thanks... It was the selecting from two tables that was confusing me, but your clear explanasion did it... Perfect Thanks!

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

These days socially coordinated efforts have turned into a critical requirement for enterprises.
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month14 days, 8 hours left to enroll

839 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question