1

Sorry for my English. I'm learning PHP and I try to create a small member area, I think it's a good way to learn. In my member area, some members are "verified" and some are not.

How can I display different pics based on data stored in Mysql using PHP? What I want is to display "Picture1" if "1" is the value stored in a MySQL column and "Picture2" if "2" is the value, etc... You are "unverified member" so you see picture 1, verified member see picture 2...

I know how to SELECT data using MySQLi but I can't find what I have to do next?

Thank you.

3
  • http://php.net/manual/en/control-structures.if.php Commented Apr 10, 2018 at 3:29
  • fetch the rows first, then based on the fetched row, use an if statement Commented Apr 10, 2018 at 3:30
  • If you need help with prepared statements for your SQL queries, you can check out this nifty tool which helps you learn... wbr.bz/QueryPro/index.php?query_type=prepared_select If you need help with the image logic, my answer below should help you out :) Commented Apr 10, 2018 at 4:00

4 Answers 4

2

Lets assume that you have a database named as db and table as tb with columns name and verify. Name is string and verify is Boolean. Also verify stored 1 if user is verified and 0 it isn't.

so, you can do it by iteration statements (either by using if else or by switch statements)

<?php

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "db";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
}

$query="SELECT * FROM `tb`";
$sql=$conn->query($query);

if($sql->num_rows>0)
{
while($row=$sql->fetch_assoc())
{
//display name
echo $row['name'];

//check if user is verified or not

//using if else statement
if($row['verify']=="0")
{
echo '<img src="./picture1.jpg">';
}
else
{
echo '<img src="./picture2.jpg">';
}

//using switch case 
switch($row['verify'])
{
case(0):
{
echo '<img src="./picture1.jpg">';
}
case(1);
{
echo '<img src="./picture2.jpg">';
}
}

//remember i have used both methods hence it will show the image twice , i did it just you possible ways , you choose any one from it.(if else or switch case)
}
}

$conn->close();

?>  
Sign up to request clarification or add additional context in comments.

Comments

2

There's a few ways. If it will always follow a number system... use this:

$picture = "Picture" . $verified; 
// example: $picture = "Picture1", if $verified = 1

Lots of ways to concatenate...

$pic = "Picture$verified.png"; 
$picture = "Picture" . $verified . ".jpeg"; 

It will just depends on how you have it setup..

switch($verified){
 case 1: 
  $picture = "Picture1";
  break;
 case 2: 
  $picture = "Picture2"; 
  break; 
 case default:
  $picture = "Picture1"; 
  break;
}

Another way...

if($verified == 1){ $picture = "Picture1"; } else { $picture = "Picture2"; }

The reason for the switch statement, or this if/else statement is that it can account for verified being equal to 0, null, or another non 1 or 2 value that may be stored in the database for whatever reason. With the first example, if $verified (verified column = to 1 or 2) has an odd value, the picture# may not actually be real.

Now, of course this is just to handle the different ways. You would have to make sure it lines up properly with your actual image...

$picture = "Picture1"; // would be dynamic to code above
$ext = ".png"; // optional, could be added into the $picture variable above easily too 
$filePath = "/images/user_uploads/"; // make sure path is correct
$filePathName = $filePath . $picture . $ext;
// see? We combine path/pictureName/extension to create an image URL
if(file_exists($filePathName) !== false){ // make sure exists 
 $imageCode = "<img src='$filePathName' alt='Not Found'>"; 
} else {
 // file does not exist! ut oh! 
}

Comments

1

It depends on how your images map to the values in your database, but assuming you have a simple relationship like:

  • 1: /images/1.jpg
  • 2: /images/2.jpg

Simply grab the data from your database and map it to a variable (in my example it's $image), and then concatenate it with the filepath inside an echo statement inside of an <img src> attribute:

<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>

This will output:

<img src="/images/1.jpg"/>
<img src="/images/2.jpg"/>

Based on the chosen image.

Now you just have to set that variable to the right value based on your conditional:

<?php

if ($authenticated) {
  $image = 1;
}
else {
  $image = 2;
}

?>

<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>

Or more succinctly with a ternary:

<?php ($authenticated ? $image = 1 : $image = 2) ?>
<img src="<?php echo '/images/' $image . '.jpg'; ?>"/>

Comments

0

Try this:

<img src="<?php echo "/pictures/Picture{$number}.jpg"; ?>" />

3 Comments

This will work, but it assumes a few things... 1) $number is never null/blank/anything incorrect. 2) The file still exists (remember, relative paths can be wonky, especially if migrating to different server).. Should use file_exists() to check when using files that have paths or names constructed from any logic related to user data... and 3) It assumes that all images will be in the same path, same extension, etc for every verified status.
@Nerdi.org, I think all of that may be a bit too much for the OP who just wanted a simple answer without all the sugar. But you are correct- I did not include that in my answer.
Yeah, you're right - I try to answer for the future Googlers too. My biggest annoyance when using SO to learn bits and pieces of code was always that answers were always so tailored to 1 specific OP...Many times there may be thousands of people that stumble upon an answer, while the OP only read it once ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.