Reputation: 2763
I have a simple function checkReplies()
which checks the reply_id
and if the reply_id
is not equal to 0
, calls himself and checks again. Now I need to create a array ffor the outputs it generate, but I am unable to do it, the array outputs only the last element
function checkReplies( $rnid ){
$r_notes = array();
include_once('include/class.dbc.php');
$dbo=new dbc();
$db=$dbo->dbconnect();
if( $rnid > 0 ):
$qry_rn = "SELECT note_subject,note_body,reply_note_id FROM tbl_notes WHERE note_id = '$rnid' ORDER BY note_date DESC";
$rslt_rn = $dbo->executeQuery( $qry_rn );
$reply = '<p style="border : none;">';
$reply .= $rslt_rn[0]['note_subject'].'<br />'.$rslt_rn[0]['note_body'];
$reply .= '('.$rslt_rn[0]['reply_note_id'].')';
$reply .= '</p>';
echo $reply;
$r_notes[] = $reply;
checkReplies( $rslt_rn[0]['reply_note_id'] );
endif;
return $r_notes;
}
$display = checkReplies( $rnid );
var_dump($display);
How to create the array of the outputs ?
Upvotes: 0
Views: 52
Reputation: 9398
try this
$r_notes = array_merge($r_notes, checkReplies( $rslt_rn[0]['reply_note_id'] ));
instead of
$r_notes[] = $reply;
checkReplies( $rslt_rn[0]['reply_note_id'] );
I edited, sorry for the error.
Upvotes: 1
Reputation: 3890
Just give your array as an optional parameter of your function. And, return the recursive call of your function.
Almost all recursives functions are built the same way : at the beggining, a condition making your recursivity stop or continue : this is called the "terminating case". Then, if this break condition is not true, make what you got to do and return the recursive call of the function.
function checkReplies( $rnid, &$r_notes = array() ){
include_once('include/class.dbc.php');
$dbo=new dbc();
$db=$dbo->dbconnect();
if($rnid == 0)
return $r_notes;
$qry_rn = "SELECT note_subject,note_body,reply_note_id FROM tbl_notes WHERE note_id = '$rnid' ORDER BY note_date DESC";
$rslt_rn = $dbo->executeQuery( $qry_rn );
$reply = '<p style="border : none;">';
$reply .= $rslt_rn[0]['note_subject'].'<br />'.$rslt_rn[0]['note_body'];
$reply .= '('.$rslt_rn[0]['reply_note_id'].')';
$reply .= '</p>';
echo $reply;
$r_notes[] = $reply;
return checkReplies( $rslt_rn[0]['reply_note_id'], $r_notes );
}
$display = checkReplies( $rnid );
var_dump($display);
Upvotes: 1