Jump to content

Have a problem in my code


adamjones

Recommended Posts

Ok. So I have this code. The idea of it is to change the background of my website, for special occasions;

 

<?php
mysql_connect("localhost", "wowdream_dreams", "pass") or die(mysql_error());
mysql_select_db("wowdream_dreams") or die(mysql_error());

$result = mysql_query("SELECT * FROM theme") or die(mysql_error());  

while($row = mysql_fetch_assoc($result)) {
   if($row['theme'] == christmas) {
  echo"images/christmas_bg.gif";
} if($row['theme'] == easter) {
  echo"images/easter_bg.gif";
  } if($row['theme'] == halloween) {
  echo"images/halloween_bg.gif";
  } if($row['theme'] == valentines) {
  echo"images/valentines_bg.gif";
  } if($row['theme'] == newyear) {
  echo"images/newyear_bg.gif";
  } if($row['theme'] == event) {
  echo"images/event_bg.gif";
}else{
echo"images/normal_bg.gif";
}
}
?>

 

Only problem is, if any of the 'if's are set in the database, eg. halloween or easter, it doesn't show a background, but if anything other than an if is set in the database, eg. '123456', then it will show my 'normal_bg'.

Link to comment
https://forums.phpfreaks.com/topic/130167-have-a-problem-in-my-code/
Share on other sites

Try this instead:

<?php
mysql_connect("localhost", "wowdream_dreams", "pass") or die(mysql_error());
mysql_select_db("wowdream_dreams") or die(mysql_error());

$result = mysql_query("SELECT * FROM theme") or die(mysql_error());  
$valid_themes = array('christmas','easter','halloween','valentines','newyear','event');

while($row = mysql_fetch_assoc($result)) {
  if (in_array($rw['theme'],$valid_themes)
     echo 'images/' . $rw['theme'] . 'valentines_bg.gif';
else
     echo "images/normal_bg.gif";
}
?>

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.