Forum Moderators: coopster

Message Too Old, No Replies

Controlling My Database Results

if statement

         

applealps

1:51 pm on Aug 16, 2005 (gmt 0)

10+ Year Member



I'm trying to create an if statement that will allow me control how information from my database is shown.

If theme02 is empty then I only want theme01 so show up, else both of them.


if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme01']}";
}

I manage to get by adapting free php scripts etc., but I'm no expert, so I'd appreciate any help.

Thanks

jatar_k

5:32 pm on Aug 16, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Welcome to WebmasterWorld applealps,

is that not working? Are you getting an error message?

if we know how it is misbehaving we can help explain how to fix it.

applealps

8:44 pm on Aug 16, 2005 (gmt 0)

10+ Year Member



Hi jatar :)

I don't get an error message, the page loads as it should, but no themes come up. This is the code in full. Maybe I'm using $themes wrongly?


$query = "SELECT * FROM films";
$result = mysql_query($query);

if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme01']}";
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

echo " $themes";

mcibor

9:27 pm on Aug 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you echo the output?

if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme01']}";
}
echo $themes;

The code should be working if not, then try this:
if(empty($row['theme02'])) {
$themes = $row['theme01'];
}
else {
$themes = $row['theme01']." ".{$row['theme01']};
}
echo $themes;

Best regards
Michal Cibor

applealps

10:06 pm on Aug 16, 2005 (gmt 0)

10+ Year Member



When I do this on it's own, there is a value output

if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme02']}";
}

echo $themes;

But when I try to put it in the whole code, which is this


$query = "SELECT * FROM films";
$result = mysql_query($query);

if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme02']}";
}

while($row = mysql_fetch_array($result, MYSQL_ASSOC))

echo "
<div class=ficinfo><a href=\"javascript:Start('../_films/{$row['link']}')\";>{$row['title']}</a><br>
[b]$themes[/b] - {$row['rating']} - {$row['length']}mins - {$row['date']} - by {$row['director']}<br>
{$row['summary']}</div>";

no value for $themes shows up.

I probably should have put the whole code in my first post. I think I have things in the wrong order and place. I'm happy that the if statement technically works though, it's the first php thing I've ever written from scratch. :)

mcibor

11:16 am on Aug 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You mixed the grammar - you don't assign value to $row before checking it. It should be:
$query = "SELECT * FROM films";
$result = mysql_query($query);

while($row = mysql_fetch_assoc($result)) {
if(empty($row['theme02'])) {
$themes = "{$row['theme01']}";
}
else {
$themes = "{$row['theme01']} {$row['theme02']}";
}

echo "
<div class=ficinfo><a href=\"javascript:Start('../_films/{$row['link']}')\";>{$row['title']}</a><br>
$themes - {$row['rating']} - {$row['length']}mins - {$row['date']} - by {$row['director']}<br>
{$row['summary']}</div>"; }


Best regards
Michal Cibor

applealps

12:57 pm on Aug 17, 2005 (gmt 0)

10+ Year Member



Thanks Michal! :D Now that you show me it looks so glaringly obvious.