Jarla Jarla - 5 months ago 11
PHP Question

How can I use a foreach loop inside my function?

I have a function:

function animals($a,$b,$c,$d){
$pdo = Database::connect();
$sql = 'SELECT * FROM animals ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo "<td>".$a."</td>";
echo "<td>".$b."</td>";
echo "<td>".$c."</td>";
echo "<td>".$d."</td>";
}
}


My problem is, I need to use the function multiple times with different amounts of parameters. This means in my foreach loop, sometimes there are 4 items...

animals($row['a'],$row['b'],$row['c'],$row['d'],);


... sometimes there are 6 items...

animals($row['e'],$row['f'],$row['g'],$row['h'],$row['i'],$row['j']);


... or sometimes 3...

animals($row['k'],$row['l'],$row['m']);


...and so on. So I am not sure how to tell my function, that the amount of my foreach items vary




To be more specific, if I do not use a function my page would look like this:

$pdo = Database::connect();
$sql = 'SELECT * FROM animals ORDER BY id DESC';

<div>

<!-- Nav tabs -->
<ul class="nav nav-tabs" role="tablist">
<li role="presentation" class="active"><a href="#home" aria-controls="home" role="tab" data-toggle="tab">Home</a></li>
<li role="presentation"><a href="#profile" aria-controls="profile" role="tab" data-toggle="tab">Profile</a></li>
<li role="presentation"><a href="#messages" aria-controls="messages" role="tab" data-toggle="tab">Messages</a></li>
<li role="presentation"><a href="#settings" aria-controls="settings" role="tab" data-toggle="tab">Settings</a></li>
</ul>

<!-- Tab panes -->
<div class="tab-content">
<div role="tabpanel" class="tab-pane active" id="home">
<?php
foreach ($pdo->query($sql) as $row) {
echo "<td>".$a."</td>";
echo "<td>".$b."</td>";
echo "<td>".$c."</td>";
echo "<td>".$d."</td>";
}

?>
</div>
<div role="tabpanel" class="tab-pane" id="profile">
<?php
foreach ($pdo->query($sql) as $row) {
echo "<td>".$row['a']."</td>";
echo "<td>".$row['b']."</td>";
echo "<td>".$row['c']."</td>";
echo "<td>".$row['d']."</td>";
}

?>
</div>
<div role="tabpanel" class="tab-pane" id="messages">
<?php
foreach ($pdo->query($sql) as $row) {
echo "<td>".$row['e']."</td>";
echo "<td>".$row['f']."</td>";
echo "<td>".$row['g']."</td>";
echo "<td>".$row['h']."</td>";
}

?>
</div>
<div role="tabpanel" class="tab-pane" id="settings">
<?php
foreach ($pdo->query($sql) as $row) {
echo "<td>".$row['i']."</td>";
echo "<td>".$row['j']."</td>";
}

?>
</div>
</div>

</div>

Answer

You can get array of function arguments with func_get_args().

function animals(){
  $args = func_get_args();

  $pdo = Database::connect(); 
  $sql = 'SELECT  *  FROM animals ORDER BY id DESC';

  foreach ($pdo->query($sql) as $row) {
    foreach ($args as $arg_value) {
      echo "<td>".$arg_value."</td>"; 
    }
  }
}

// Call function
animals(1, 2, 3);
// or 
animals(1, 2, 3, 4);

EDIT

I guess that you just want to print certains fields from $row?

function animals(){
  $args = func_get_args();

  foreach ($args as $arg_value) {
    echo "<td>".$arg_value."</td>"; 
  }
}

$pdo = Database::connect(); 
$sql = 'SELECT * FROM `animals` ORDER BY id DESC';

foreach ($pdo->query($sql) as $row) {
  animals($row['a'], $row['b'], $row['c']);
  // or
  animals($row['i'], $row['j']);
}

And shorter:

function animals(){
  $args = func_get_args();

  $pdo = Database::connect(); 
  $sql = 'SELECT `'.implode('`, `', $args).'` FROM `animals` ORDER BY id DESC';

  foreach ($pdo->query($sql) as $row) {
    foreach ($row as $value) {
      echo "<td>".$value."</td>"; 
    }
  }
}

// Then you can just do
animals('a', 'b', 'c');

// or
animals('i', 'j');
Comments