1

Using this code:

<?php
$stmt = $pdo->prepare("SELECT pdimg1 FROM products WHERE pdcat LIKE 'fashion%'");
$stmt->execute();
$rows = $stmt->fetchAll();
$img1 = '';
foreach ($rows as $row) {
    $Rpdimg1 = $row['pdimg1'];
    $img1 .= $Rpdimg1;
}

UPDATED TO INCLUDE BELOW:
$stmt = $pdo->prepare("SELECT * FROM products WHERE pdcat LIKE 'collectibles%'");
$stmt->execute();
$rows = $stmt->fetchAll();
$img2 = '';
foreach ($rows as $row) {
    $Rpdimg2 = htmlspecialchars($row['pdimg1']);
    $img2 .= $Rpdimg2;
}

$cats = array(
            array(
                "title" => "FASHION",
                "img" => $img1
            ),
            array(
                "title" => "COLLECTIBLES & ART",
                "img" => $img2,
            )
        );

foreach ($cats as $cat): ?>
    <?php echo $cat["title"]; ?>
    <img src="images/<?php echo $cat["img"]; ?>">
<?php endforeach; ?>

I get this result displayed in Inspector Tools on Firefox:

<img src="images/shirt.jpgpants.jpg">

[But on the webpage, this displays as a broken image.]

But what I want is this result:

<img src="images/shirt.jpg">
<img src="images/pants.jpg">

[However, I would like the above results displayed as IMAGES, not as html text.]

Those two images are values echoed from my database. They are from my products table:

pdid | pdimg1
-----------------
  1  | shirt.jpg
  2  | pants.jpg

So basically

1) I'm selecting rows from my database table products

2) I'm inserting them as variables into an array (which is used for other purposes)

3) I'm using a foreach loop to echo out those variables from that array

4) Now the problem lies in that those IMAGE variables bunch together as a SINGLE WORD (shirt.jpgpants.jpg) instead of as separated values, and do not get encased in their OWN img tags, but are bunched together under the same img tag.

5) So I demonstrated above in bold text the result that I wanted. How would I achieve this? Thank you

3 Answers 3

2

Use an extra multidimensional array variable. like this

<?php
$int_data = array();
foreach ($rows as $row) { 

    $int_data[] = array(
        "title" => $row['title'];
        "image" => $row['pdimg1']; 
    );
}
?>

Now display the title and image using foreach loop

<?php
foreach ($int_data as $data) { ?> 
  Title : <?php echo $data['title]; ?> <br/>
  Image : <img src="path/<?php echo $data['image]; ?>"> <br/>
<?php
} ?>
Sign up to request clarification or add additional context in comments.

3 Comments

yeah I'm not sure why but this is not displaying anything. I'm getting the same result with Anat's code
This is strange.. I'm looking at the source code and it's empty
Before 1st loop, print the query output data like this print_r($rows);
0

you need to do array assignment inside loop, and bit reduced code like below:

<?php
$stmt = $pdo->prepare("SELECT pdimg1 FROM products WHERE pdcat LIKE 'fashion%'");
$stmt->execute();
$rows = $stmt->fetchAll();

$cats = array();
foreach ($rows as $row) {
    $cats[] = array(
    "title" => $row['title'];
    "img" => $row['pdimg1']; 
   ); // you can add as many column as you get from query
}

foreach ($cats as $cat): ?>
    <?php echo $cat["title"]; ?>
   <img src="images/<?php echo $cat["img"]; ?>">
<?php endforeach; ?>

1 Comment

okay so I found the issue was that the semicolon after the echoed out rows was unneeded. I omitted that and the code worked, so thank you for resolving this!
0
  <?php
     $stmt = $pdo->prepare("SELECT pdimg1 FROM products WHERE pdcat LIKE 'fashion%'");
     $stmt->execute();
     $rows = $stmt->fetchAll();
     foreach ($rows as $row) { ?>
        <img src="images/<?= $row['pdimg1']; ?>">
  <?php }  ?>

Comments

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.