Jump to content

echo an array to show selected images from mysql


Go to solution Solved by PNewCode,

Recommended Posts

Hello. I have 2 different questions running at one time today. My apologies if that is annoying. For this task, I am wanting to show the images that are from an array of different file names. What I have now shows all of them in the echo. I even understand why it's doing that. Where I'm puzzled and stuck is how to limit it to what is only showing in the entry.

Right now, there is  only "1.png", "6.png" in the database (one row, one column named emotes2)

And it's showing all 8 instead of just those 2 (like I said, I know why it is, but I don't know how to fix it.. see below)

image.png.32dc37c761cf74a5de5301aedc29d14d.png

 

What I tried to do is change the array to

$emotes2s = array(" '. $row["emotes2"] .' ");

////// OR //////

$emotes2s = array( echo $row["emotes2"]; );

And several variations of that, but no luck.



Here is my code

$emotes2s = array("1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png");

foreach ($emotes2s as $emotes2) {

echo "<img src='img-picker/images/$emotes2' width='35'/>";

}

} else {
echo '';
}

 

You mention a database, but there's no database in your code. You've got an array containing every image, and you're looping through them and displaying each one. The code is working as expected; if it's not doing what you want then you need to reconsider your expectations. What is it exactly you're trying to do?

@maxxd I'm sorry. I didn't show the database because it's connecting fine so I thought it would be overkill in the post. I'll show it below.

Yes, the code is working as expected for as it is, like I said I know why it's doing what it's doing. But I'm trying to alter it so it only displays what is entered in the database.
In the column called "emotes2", there is an entry 
I'm trying to make the code I have display only the two image file names (as images) instead of all 8 images

All 8 are "1.png", "2.png", "3.png", "4.png", "5.png", "6.png", "7.png", "8.png"

In the database entry there is "1.png", "6.png"

So I'm trying to echo only "1.png", "6.png" images on the page instead of all 8

And if the entry said 2.png, 3.png, 7.png then only show those 3 file names as images. (it will change as different users select various different ones for different posts)

Here is the database info (note that I'm using * because there are 20+ other columns that the rest of the page is using the different columns for in various other scripts. The page entirety is very long) this issue is only for the column emotes2
 

  $hostname_l = "localhost";
  $username_l = "deleted for posting";
  $password_l = "deleted for posting";
  $dbname_l = "deleted for posting";
  
   $conn_l = mysqli_connect($hostname_l, $username_l, $password_l, $dbname_l);
  if(!$conn_l){
    echo "Database connection error".mysqli_connect_error();
  }
  $user_id = $_SESSION['id']; 
  $sql_l = "SELECT * FROM users WHERE id = '$user_id'";

 

  • Solution

For anyone else that may come to see this, here is the solution. Credit goes to a friend that I just got lucky enough to rarely see online

 

if (!empty($row["emotes2"])) {
    $selectedImages = $row["emotes2"]; // This should be a string like "3.png", "4.png", "5.png"
    $imageArray = explode(", ", $selectedImages); // Convert the string to an array
    
    foreach ($imageArray as $emotes2) {
        echo "<img src='img-picker/images/" . trim($emotes2, ' "') . "' width='35'/>";
    }
} else {
    echo '';
}

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.