0

I want to print all user names one by one which is array format, so instead of using foreach loop I am using array_walk() and it is printing all user data one by one but as per requirement data should be print in html table so defined one varaible $desc as empty and concatenate html code(table body table heading with users name) and at the end echo $desc but getting error:

expected output:


<html> 
<body> 

<h2>Basic HTML Table</h2> 

<table > 
<tr> 
<th>username</th> 

</tr> 
<tr> 
<td>user1</td> 

</tr> 
<tr> 
<td>user2</td> 

</tr> 
<tr><td>user3</td></tr> 
<tr><td>user4</td></tr> 


 <?php 
$users=$this->db->get('users')->result();
$desc = '';
 $desc .= '<table class="table table-hover" id="catgeory_list">
                      <thead>
                        <tr>
             <th>User Name</th>   
        </tr>
                      </thead>
                      <tbody>';
function myfunction($value)
{
  echo $desc .= '<tr><td>'.$value->name.'</td></tr>';
}
$a = (array) $users;
array_walk($a,"myfunction");
$desc .= '</tbody></table>';
?>

If I use without table it is working here is the code which I have use without table

 <?php 
    $users=$this->db->get('users')->result();
    function myfunction($value,$key)
    {
      echo $value->name;
    }
    $a = (array) $users;
    array_walk($a,"myfunction");
    ?>

Output should print user name in html table assigned in $desc varaible using concatenation

MA Samad
  • 73
  • 6
  • `myfunction` doesn't have access to the [global](https://www.php.net/manual/en/language.variables.scope.php) variable `$desc` and inside the function it is undefined. – msg Aug 11 '19 at 05:24
  • asign `$desc` into the function – Nipun Tharuksha Aug 11 '19 at 05:26
  • let us know what happen then – Nipun Tharuksha Aug 11 '19 at 05:26
  • @Nipun Tharuksha yes as per your suggestion I have used global keyword not getting any error but out put is not in html table and data also getting repeated – MA Samad Aug 11 '19 at 05:29
  • @MASamad could you please show us a snap – Nipun Tharuksha Aug 11 '19 at 05:31
  • @Nipun Tharuksha as per stackoverflow rule score/badge is less than 20 so I can't post image/snap – MA Samad Aug 11 '19 at 05:32
  • 1
    array walk is not the right approach here, you should use a foreach loop –  Aug 11 '19 at 05:32
  • @ Nipun Tharuksha I wnat to print user name in html table one by one can you please correct my code which will work for me – MA Samad Aug 11 '19 at 05:34
  • check now you should be able now – Nipun Tharuksha Aug 11 '19 at 05:34
  • @tim actually in my project so many places using foreach loop and usage of more foreeach loop cause slow performance on server ,this is the reason i used array_walk – MA Samad Aug 11 '19 at 05:35
  • any chnages in code? – MA Samad Aug 11 '19 at 05:36
  • extremly unlikley that foreach performace is going to be an issue, array walk loops also. –  Aug 11 '19 at 05:39
  • Although I've provided an answer, I wasn't aware at the time of the rationale behind using `array_walk`. `foreach` is more performant than `array_walk` but that's beside the point. It's highly unlikely your performance problem is strictly the foreach loop. – Nathan Dawson Aug 11 '19 at 05:41
  • Can you please tell me if I use foreach loop more than 30 times is it good beacuse I am using 30 times foreach loop entire project – MA Samad Aug 11 '19 at 05:45
  • Possible duplicate of ["Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset" using PHP](https://stackoverflow.com/questions/4261133/notice-undefined-variable-notice-undefined-index-and-notice-undefined) – Progman Aug 11 '19 at 08:31
  • @Progman I solved that issue but second issue that is show user data in html table not solved yet – MA Samad Aug 11 '19 at 08:38

1 Answers1

1

Your function has no access to the $desc variable. There is a way to resolve that but even then, it doesn't make sense to do echo $desc .= ....

You're using array_walk but I think this could be a good use case for map instead. You want to map over the array and convert each element to an HTML string. That can then be mixed with implode to return all of the elements as a string.

Example:

$users = $this->db->get('users')->result();

$users_table = array_map(function ($user) {
    return '<tr><td>' . $user->name . '</td></tr>';
}, $users);

echo '<table> ... '; // open table here.
echo implode('', $users_table);
echo '</table>'; // and finally, close it.

Why map? You'll often split up the process of generation and output. This gives you the option to do that. If you wanted to get your original code working there is a simple solution. Forget $desc altogether and stick to echo. Echo the table opening, then walk the array, and finally the table closing.

Nathan Dawson
  • 18,138
  • 3
  • 52
  • 58
  • not printing in html table format, like table heading (username) and below that heading username should come – MA Samad Aug 11 '19 at 05:41
  • The answer doesn't contain your full HTML. The table opening and closing statements are something you must update. I chose not to weigh the answer down with that but you have the code already. Simply replace them with the table HTML you already posted. – Nathan Dawson Aug 11 '19 at 13:20