0

I have an array called memberList which is filled with a list of members.

Now I am trying to use PHP to loop through the array and display it on the page.

<tr class='d1'><td><p class='normal-text5'><?=$memberList[$x]?></p></td></tr>

I wanted to loop through it using a for loop, instead of having to copy/paste the same code 100 times, displaying every name.

for ($x = 0; $x <= 99; $x++) {
echo <tr class='d1'><td><p class='normal-text5'><?=$memberList[$x]?></p></td></tr>
}

But then I remembered you can't execute PHP code in an Echo, so I was wondering how I could do this differently?

AOD Saenai
  • 37
  • 5
  • i am suggesting you to read about how to embed html in php.https://stackoverflow.com/a/18140338/1939258 – Adnan Haider Feb 23 '18 at 05:14
  • Possible duplicate of [How to write html code inside ](https://stackoverflow.com/questions/18140270/how-to-write-html-code-inside-php) – Adnan Haider Feb 23 '18 at 05:15

4 Answers4

0

the problem is in your code

echo <tr class='d1'><td><p class='normal-text5'><?=$memberList[$x]?></p></td></tr>

this should be

echo "<tr class='d1'><td><p class='normal-text5'>".$memberList[$x]."</p></td></tr>";

echo statements print out a string or variable. in case you have both, you need to use concatenate operator as well. You did not put any for the string (html string) quotes "" you wanted to print out nor you concatenated those strings.

this is the main reason you get nothing/error.

ahmednawazbutt
  • 823
  • 12
  • 34
0

Try this:

  <?php 
    for ($x = 0; $x <= 99; $x++) {
      echo "<tr class='d1'><td><p class='normal-text5'>".$memberList[$x]."
         </p</td>
      </tr>"
    }
  ?>
Kannan K
  • 4,411
  • 1
  • 11
  • 25
  • this will print nothing, because if you want to place the code this way, you need to place opening and closing php tags before and after body of loop – ahmednawazbutt Feb 23 '18 at 05:04
0

You should consider using foreach to iterate over the array, for instance:

foreach ($memberList as $key=>$member) {
  echo "<tr class='d1'><td><p class='normal-text5'>".$member."</p></td></tr>";
}
Anthony L
  • 2,159
  • 13
  • 25
0

please try using this

<?php for ($i = 0; $i <= 99; $i++) { ?>  <!--for loop open-->
   <tr class='d1'>                       <!--HTML content-->
    <td>
        <p class='normal-text5'><?php echo $memberList[$i]?></p> <!--echo php variable/ perform any php operation -->
    </td>
  </tr>
<?php } ?>                              <!--for loop close-->