-2

I want to show the data from this in each row of my html table, how do I do that?

<?php 
   $test = $object2->schoolname;
   $array =  explode(',', $test);
   echo $array[0];
   //print_r($myArray); --> output: Array( [0] => testSchool2  [1] => test)
?>

The only way I can think of is to use something like this:

<td>
            <input type="text" name='schoolname[]' value="<?php 
            $test = $object2->schoolname;

            $array =  explode(',', $test);
            echo $array[0];
       ?>" class="form-control"/>
 </td>

But I have to do it for every row, I want to show that if I have 2 datas inside the array, then 2 of the row must contain those data, I don't want to manually put in the array[0] like this. How can I do that? Can anyone help me? Thanks a lot.

My html table is based on this: https://bootsnipp.com/snippets/featured/dynamic-table-row-creation-and-deletion

6
  • 2
    read more about loops. php.net/manual/en/control-structures.foreach.php
    – hassan
    Commented Jan 8, 2018 at 8:55
  • @hassan I read about it already, I tried following here,stackoverflow.com/questions/9711175/…, but still can't do it since it is a dynamic table
    – Dkna
    Commented Jan 8, 2018 at 8:56
  • The answer at the question you linked clearly states the same: Use a loop. Commented Jan 8, 2018 at 9:05
  • @commonsense yes it is similar, just that I am using a dynamic table, and I want to show the value inside is that possible?
    – Dkna
    Commented Jan 8, 2018 at 9:08
  • What do you mean with "dynamic table"? What makes your table dynamic? Edit your question and show the tables code. Commented Jan 8, 2018 at 9:10

2 Answers 2

1

The answer is loops.

foreach($array as $i => $value) {
    //code for each array item, as $value
}

eg

$test = $object2->schoolname;
$array =  explode(',', $test);

foreach($array as $i => $value) {
    echo "
        <tr>
            <td>
                <input type='text' name='schoolname[".$i."]' value='".$value."' class='form-control'/>
            </td>
        </tr>
    ";
}  

There are a few different types of loops in php; for, foreach, while and do-while

7
  • but I am using a dynamic table as shown in this link, can the loop still even help? bootsnipp.com/snippets/featured/…
    – Dkna
    Commented Jan 8, 2018 at 8:57
  • Why do you use dynamic tables when you fill them up from script? Dynamic rows are useful if you or user fill them up from front end or some kind of panel, but the code suggests the script does that for you. Commented Jan 8, 2018 at 9:00
  • I am doing an update now, so I would like to show the data that had been already entered @MichalSkrzypek
    – Dkna
    Commented Jan 8, 2018 at 9:02
  • I think the loop answer is still valid. It should show the already entered data and still allow to add more rows. There should be no conflict there. Commented Jan 8, 2018 at 9:07
  • The loop does show the value but it is only showing the last part of the array, example, array[1] instead of array[0] @MichalSkrzypek
    – Dkna
    Commented Jan 8, 2018 at 9:09
0

This is a pretty basic foreach loop:

<table>
<?php foreach ($array as $value): ?>
    <tr><td><?= $value ?></td></tr>
<?php endforeach; ?>
</table>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.