-1

I am pulling images and information from a mySQL database and displaying with a few PHP functions. At times all of the information isn't there and I need it to basically display:none; but I can't seem to get it - What am I missing? Here is my display function:

<?php if ($recipe->hassliderimage5 == true) { 
   $recipe->show_image_carousel5();
    } else {
    }

?>

And here is the PHP function calling it from the database -

if (trim(mysql_result($this->result,0,"imageCarousel5") != '')) {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}

Here is what I got to work for what I wanted - not sure if it is the best solution or not? I'm still kind of new to PHP.

<?php if ($recipe->hassliderimage5 == true) { ?>
      <div id="sliderimageFive" class="item">
            <?php   
                $recipe->show_image_carousel5();
            ?> 
       </div>    
<?php } ?>
jacob
  • 29
  • 5

3 Answers3

0

You seem to make the mistake to create your view before collecting the data that has to be displayed. A more logical application structure would be to fetch the data from the database, validate it and then create your view according to the amount and type of data you have.

Also, take a look at this: Why shouldn't I use mysql_* functions in PHP?

Community
  • 1
  • 1
chrisp
  • 569
  • 4
  • 24
0

I can't comment cause i have not enough reputation, so i try it with an answer.

why you are using the trim on mysql_result($this->result,0,"imageCarousel5") != '' this code will give you either true or false no string you have to trim

Maybe you want to trim the return of mysql_result and then check if its empty

if (trim(mysql_result($this->result,0,"imageCarousel5")) != '') {$this->hassliderimage5 = true;} else {$this->hassliderimage5 = false;}
Fritz
  • 831
  • 7
  • 23
0

Try this test:

<?php if (filter_var(recipe->hassliderimage5, FILTER_VALIDATE_BOOLEAN) == true) { 
   $recipe->show_image_carousel5();
} else {
   // do other
}
?>

if return true if the value of 'recipe->hassliderimage5' is "1", "true", "on" and "yes"

Enjoy your code!

Magicianred
  • 566
  • 2
  • 8