0

I'm probably making a very basic error but I'm quite new to this.

I have a table where I need to edit what is displayed in each box using variables but I'm having trouble with getting the outputs into the table. Experimentation helped me work out the first box but I can't get the second one working because I think the function is written incorrectly. I need a conditional loop that displays all even numbers between 10 and 20 (the code below doesn't have anything to do with even numbers at the moment I'm just trying to get it to work)

<?php

$random = rand() . srand(3034);

function loop() {
    for ($i = 10; $i <= 20; $i++) {
      $loop = $i;
      return $loop;
    }
}

echo "<table border='1'>
      <tr>
       <td>Box 1 - ".$random."</td>
       <td>Box 2 - ".$loop."</td>
      </tr>

     </table> ";
?>

Any help is much appreciated.

2
  • 1
    The whole thing is not useful. $loop gets overwritten in every iteration of $i, but as there is a return the loop does not work at all, it quits straight after the first time it runs. So the return of the function (which you arent calling btw!) will always be 10. You would instead echo the <tr>'s within the loop and not return inside the loop. It seems you might want to look into PHP basics first. Commented Jan 17, 2018 at 12:45
  • Read about variable scope, your $loop is only available inside your function loop() Commented Jan 17, 2018 at 12:47

3 Answers 3

2

You need to loop on the tags itself, because the return condition in the loop breaks it to only 1 iteration.

So you should do it like this:

echo "<tr>
    <td>Box 1 - ".$random."</td>";
for ($i = 10; $i <= 20; $i++) {
    echo "<td>Box 2 - ".$i."</td>";
}
echo"</tr>";
Sign up to request clarification or add additional context in comments.

2 Comments

I see, so it would not be possible to define the function outside the table and then have it executed within the table?
@codenoob nope there is no idea to make it like that :)
0

Additionally to my comment here is some more code:

<?php
$random = rand() . srand(3034);

function loop($randomNumber) {
    for ($loop = 10; $loop <= 20; $loop++) {
        echo
            '<tr>' .
            '<td>Box 1 - ' . $randomNumber . '</td>' . 
            '<td>Box 2 - ' . $loop . '</td>' . 
            '</tr>';
    }
}

echo
    '<table border="1">' . 
    loop($random) . 
    '</table>';

Comments

0

Try this:

<?php 

    function loop(){
         $return = '';
         for($i = 10; $i <=20; $i++){
              $random = rand() . srand(3034);
              if($i%2==0){
                 $return.='<tr>
                           <td>Box 1 - '.$random.'</td>
                           <td>Box 2 - '.$i.'</td>
                          </tr>';
              }
         }
        return $return;
    }

    echo '<table>'.loop().'</table>';

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.