0

I am trying to display my images in a for loop in the form tag using PHP but it does not seem to work. I have also tried doing a return statement but nothing still appears.

<?php

$data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");

function displayCheckboxes(){
  for ($i=0; $i<count($data); $i++) {
  echo "<img src='".$data[$i] . "<br>";
  }
}
?>

  <body>
    <main id ="main">
      <form id="pics" action="process.php" method="get">
        <label>Name: </label>
        <?php echo displayCheckboxes();?>
      </form>
    </main>
  </body>
6
  • 1
    you don't have <?php ?> tags on your php code. You write twice jpg extension and you call php echo function twice. I think you need add <input type="checkbox" value="" /> if you want display some checkbox in your form. Commented May 7, 2020 at 17:19
  • 1
    Hello my friend. Your code snippet is not valid, because you don't end your php block after the function. Is this your real code? Commented May 7, 2020 at 17:21
  • 1
    Use Harish's approach. Foreach is better than for in this situation because you don't need a condition for the loop (simpler) nor do you have use for the counter variable. Commented May 7, 2020 at 17:25
  • @threeside Yes, I am going to add my checkboxes after I can display my images Commented May 7, 2020 at 17:26
  • @gview soryy my code is kind of a mess. the data array and the function belong together in a .php file, and the <body> codes belong in another file. Commented May 7, 2020 at 17:27

5 Answers 5

2

Maybe Like This:

<?php

 $data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");


function displayCheckboxes(){
  global $data;
  for ($i=0; $i<count($data); $i++) {
     echo "<img src='".$data[$i]."'>" . "<br>";
  }
}

displayCheckboxes();

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

Comments

1
function displayCheckboxes($img_array){
  foreach($img_array as $img) {
    echo "<img src='".$img."'>'" . "<br>";
  }
}

and

echo displayCheckboxes($data);

There is several errors with your function : - $data is not inside the function - .jpg is already in the image name.

1 Comment

I gave you +1 for being the first to see the problem with the original code was the missing parameter. With that said, there was no reason to change he name of the parameter from $data.
1

this should work,call the function with $data

$data = array("images/architecture-57e8d34a48_640.jpg",
"images/gateway-arch-57e2d64548_640.jpg",
"images/horseshoe-bend-57e6d6434f_640.jpg",
"images/lake-irene-57e6d24a4d_640.jpg",
"images/silhouette-57e8d5444e_640.jpg");

/* Write your displayCheckboxes() function here */
displayCheckboxes($data);

function displayCheckboxes($data){
  for ($i=0; $i<count($data); $i++) {
  echo "<img src='".$data[$i]."'.jpg>'" . "<br>";
  }
}
?>

Comments

1
<?php

$data = array(
    "images/architecture-57e8d34a48_640.jpg",
    "images/gateway-arch-57e2d64548_640.jpg",
    "images/horseshoe-bend-57e6d6434f_640.jpg",
    "images/lake-irene-57e6d24a4d_640.jpg",
    "images/silhouette-57e8d5444e_640.jpg"
);

?>

<body>
    <main id ="main">
        <form id="pics" action="process.php" method="get">
            <label>Name: </label>
            <?php foreach($data as $image): ?>
                <img src="<?= $image; ?>"></br>
            <?php endforeach; ?>
        </form>
    </main>
</body>

This is an alternative way. Looks much cleaner and readable. And I actually don't think we need a function for printing HTML.

I would say NEVER put HTML inside PHP echo unless it is compulsory. The reason I am stating this is if you put HTML inside PHP, the code becomes easily messy. It becomes hard to understand the logic unless you are the one who coded. Particularly, if you are working with some designers or in future, they will have a hard time making even little changes.

Even I think using <?= than <?php echo is better.

3 Comments

I agree with using foreach. I also like the use of the php alternative syntax foreach to keep the presentation separate from the logic. This is the same reason people use template classes like smarty, twig and blade.
@gview I agree with you.
Thank you @HarishST. and @gview!
1

You are trying to give an extension ".jpg" of the images that you previously gave to it in the path of images in the array "$data".

For exemple:
We will take the first element in the array $data with index = 0 "$data[0]" (images/architecture-57e8d34a48_640.jpg):

In PHP looks like -> echo "<img src='".$data[0]."jpg'>" . "<br>"; ,but in HTML looks like -> <img src='images/architecture-57e8d34a48_640.jpg.jpg'>

You shold have to delete the extension ".jpg" from your echo to seem like:

echo "<img src='".$data[$i]."'>" . "<br>";

1 Comment

sorry about that, I forgot to remove that part when I posted the question. Thank you

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.