PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Aug 29, 2006 3:15 pm
My if /elseif statement does not work properly in the while loop. Can someone explain why?
Code: Select all
<?php
include 'includes/connect.php';
$query = "SELECT * FROM article, image WHERE article.a_id=image.a_id ORDER BY article.a_id DESC";
$result = mysql_query($query) or die('Error : ' . mysql_error());
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$a_id = $row['a_id'];
$title = $row['title'];
$abstract = $row['abstract'];
$date = $row['date'];
$path = $row['path'];
$iurl = $row['iurl'];
$none = $row['none'];
$organize = $row['organize'];
$date = date("F d Y");
$content = '<div id="entry">';
if (path == TRUE) {
$content .= '<img class="entry" src="' . $path . '" width="150" align="right">';
} elseif (iurl == TRUE) {
$content .= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
}
$content .= '<div id="entry-copy">';
more $content......
echo $content;
}
mysql_close();
?>
Last edited by
psurrena on Tue Aug 29, 2006 6:21 pm, edited 1 time in total.
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Tue Aug 29, 2006 3:16 pm
else if is two words, not one
Oren
DevNet Resident
Posts: 1640 Joined: Fri Apr 07, 2006 5:13 am
Location: Israel
Post
by Oren » Tue Aug 29, 2006 3:19 pm
blackbeard wrote: else if is two words, not one
It doesn't matter, both will work.
psurrena , it should be:
Not:
P.S I didn't read the rest of the code, so maybe there are more mistakes.
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Aug 29, 2006 3:21 pm
That didn't fix it. It seems to only check if path==true and then if it's true once it doesn't check for iurl
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Tue Aug 29, 2006 3:26 pm
If I understand your last post, what you need is two if statements. One to check path, and the other to check the iurl.
Also, inside your if statement, do you mean to check $path and $iurl?
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Aug 29, 2006 3:33 pm
Got it
Thank you, it has to be a variable since both would always be true if it just check the DB and both were true at least once. Right?
Code: Select all
if ($iurl == TRUE) {
$content .= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
} elseif ($path == TRUE) {
$content .= '<img class="entry" src="' . $path . '" width="150" align="right">';
}
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Tue Aug 29, 2006 3:37 pm
Change the elseif to else. You only use the elseif when you have two or more comparisons.
Code: Select all
if ($i ==1)
echo "true";
if ($i == 1)
echo "true";
else
echo "false";
if ($i == 1)
echo "I is 1";
else if ($i == 2)
echo "I is 2";
else
echo "I is not 1 or 2";
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Tue Aug 29, 2006 3:45 pm
Re-reading your post, I think what you need is something like this:
Code: Select all
if (!is_null($iurl)) {
$content .= '<img class="entry" src="' . $iurl . '" width="150" align="right">';
} elseif (!is_null($path)) {
$content .= '<img class="entry" src="' . $path . '" width="150" align="right">';
}
else {
// error handling
}
psurrena
Forum Contributor
Posts: 355 Joined: Thu Nov 10, 2005 12:31 pm
Location: Broolyn, NY
Post
by psurrena » Tue Aug 29, 2006 3:50 pm
Why would one use !is_null instead of ==True?
blackbeard
Forum Contributor
Posts: 123 Joined: Thu Aug 03, 2006 6:20 pm
Post
by blackbeard » Tue Aug 29, 2006 4:01 pm
In your specific case, I don't see an problem using == TRUE, for me, it's user preference.
However, you can have it where !is_null($var) evaluates to true, but ($var == TRUE) evaluates to false. As long as you know what to expect from the database, you can code it using your preferences.