IF Statement & Two Tables With Different Echoes


Posted on 16th Feb 2014 07:03 pm by admin

I'm retrieving two tables in a single query using UNION ALL, like so:

Code: $query = "SELECT * FROM film UNION ALL SELECT * FROM people ORDER BY id DESC LIMIT 5 ";
Now each of those tables, FILM and PEOPLE create links to two different pages for the different information stored in the said tables. Here are the two different echoes for the tables:

FILM:
Code: <li><a href="filmography.php?title='.$title.'"><img src="images/thumbnails/'.$image.'.jpg" alt="" title="'.$title.' ('.$year.')"></a></li>
PEOPLE:
Code: <li><a href="biography.php?name='.$name.'"><img src="images/thumbnails/'.$img.'.jpg" alt="" title="'.$name.' ('.$occupation.')"></a></li>
Is there an IF statement, or anything else for that matter, which will identify which table each row comes from and then assign the necessary link to that row, like so?:

Quoteif (row belongs to (FILM)) {
echo '<li><a href="filmography.php?title='.$title.'"><img src="images/thumbnails/'.$image.'.jpg" alt="" title="'.$title.' ('.$year.')"></a></li>';
}
else echo '<li><a href="biography.php?name='.$name.'"><img src="images/thumbnails/'.$img.'.jpg" alt="" title="'.$name.' ('.$occupation.')"></a></li>';
Here's my poor attempt at managing it (obviously doesn't work):
Code: <?php

mysql_connect(localhost,user,pw);
@mysql_select_db(db) or die( "Unable to select database");

$query = "SELECT * FROM film UNION ALL SELECT * FROM people ORDER BY id DESC LIMIT 5 ";
$result = mysql_query( $query ) or die(mysql_error());

while ($row = mysql_fetch_array($result))
{
extract($row);

$field_array = mysql_fetch_array($result);
if (!in_array(`img`, $field_array)) {

echo '<li><a href="filmography.php?title='.$title.'"><img src="images/thumbnails/'.$image.'.jpg" alt="" title="'.$title.' ('.$year.')"></a></li>';

}
else echo '<li><a href="biography.php?name='.$name.'"><img src="images/thumbnails/'.$img.'.jpg" alt="" title="'.$name.' ('.$occupation.')"></a></li>';

}
?>

No comments posted yet

Your Answer:

Login to answer
348 Like 25 Dislike
Previous forums Next forums
Other forums

Upload file and add HTML
Hey everyone,

I have a client has meeting agendas and minutes that should be posted online. I

How many users online in the last 24-hours using time()?
I'm fairly new to PHP. I have a MySQL table called 'users' and a field called 'loggedin' which is in

PHP5 - AJAX help
I've been following the tutorial on w2schools (http://www.w3schools.com/php/php_ajax_database.asp) a

ALV sum various columns problem
Hi all,
I'm doing a report on ABAP for a timesheet of the current month. The output i want is s

Just a white page
Okay so, my website, when I click SignUp on it it takes me to /join.php but its a complete white pag

printing links
Hi every1

i am connecting to a table in sql and the looping through with a while ($ var = my

using variables in another page
I have a test database set up on localhost. I have a form that I can type a name into, hit the butto

shift numbers with paging
Hi

How would i be able to display 10 numbers 1 2 3 4 5 6 7 8 9 10
and when I click next

Displaying an image using echo command
I had done a query on my database, one of the fields being an image reference to a directory where a

Table sorting
Hi,

I'm trying to modify the following in order to make the output table sorted alphabeticall

Sign up to write
Sign up now if you have flare of writing..
Login   |   Register
Follow Us
Indyaspeak @ Facebook Indyaspeak @ Twitter Indyaspeak @ Pinterest RSS



Play Free Quiz and Win Cash