In my sql database, I have a number stored, between 1 and 10. On the page I am working on, the number in the database is displayed on the screen. I would like to display a different image depending on whatever number is in the database. ie.- if 1, display 1.jpg, if 2, display 2.jpg and so on (instead of just displaying the actual number. Would some sort of if/else statement be used for this? If so, how should it be written? Thank you!
if($number==1) echo "<img src=\"the source here\">"; else if($number==2) echo "<img src=\"the source here\">"; else if($number==3) echo "<img src=\"the source here\">"; else if($number==4) echo "<img src=\"the source here\">"; else if($number==5) echo "<img src=\"the source here\">"; PHP: in this way
I would have an array of image paths and then have number indicate what the index is. $image[0] = ... $image[1] = ... . . . $image[10] = ... echo "<img src=\"" . $image[$number] . "\">";
sitefever, there is no need for an if statement or case statement. To answer exactly to you example you could do like this, if $number would be your variable holding the number from the DB echo '<img src="'.$number.'display.jpg">'; PHP:
printf( '<img src="%d.jpg" />', file_exists( sprintf( '%d.jpg', $number ) ) ? $number : 'not_found' ); PHP: make a not_found.jpg or something that is relevant to whatever you're doing.
I'm not evaluating the number, there is no point if they are all in a database and you know what the results are, I just prefer to use sprintf and printf over echo "this" . $sort . " of " . $business ; PHP: it just reads easier and you're less likely to make errors when writing code