Returning A String From Two Functions

Returning A String From 2 Functions

The purpose of the code below is to show you how you can take rows within a while loop and call a function. Then, you call another function within a function.

In the second function, you will return a string that gets stacked into a larger array.

Finally, the array is parsed outside of the loop with two foreach loops.

Sounds confusing? Code like this can be quite commonplace when you work with other people's scripts. Therefore, knowing the logic can make life simpler for a an average or expert php programmer.

Note: You can get the same results by returning a single function as described on this page.


function second_func($var2,$date2){
global $db; 

$my_values2 = $var2.",".$date2;
return $my_values2; //return a string

function get_data($var1,$date){
global $db; 

$my_values = second_func($var1,$date);
return $my_values; //return a string
$command = "SELECT DISTINCT id, email FROM table_sort WHERE id IN (1,2,3,4,5,6,7) ORDER BY id ASC";
$result = mysqli_query($db, $command);
while($row = mysql_fetch_assoc($result)) {
$var1 = $row['id'];
$command2= "SELECT min(date) as date FROM table_sort WHERE id ='$var1' ORDER BY id ASC";
$result2 = mysqli_query($db, $command2);
while($row2 = mysqli_fetch_assoc($result2)){
$date = $row2['date'];
echo $var1."-".$date."<br/>";
$get_data = get_data($var1,$date);
$my_array[] = $get_data;


foreach($my_array as $key => $value) {
echo "<br/>".$key."-".$value;

foreach($my_array as $key => $value) {
$all = explode(",",$value);
$id = $all[0];
$date = $all[1];
echo "<br/>".$id."-".$date;