0

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 ?

2 Answers 2

1

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);
Sign up to request clarification or add additional context in comments.

3 Comments

:( its the same output coming
The array should probably be passed as a reference, only objects are automatically passed as references.
Edited, i forgot to give array as reference and to return the recursive call.
1

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.

3 Comments

Warning : Warning: array_merge() [function.array-merge]: Argument #1 is not an array in xxxx.php
I used $r_notes[] = array_merge($r_notes, checkReplies( $rslt_rn[0]['reply_note_id'] )); But the O/P is like : array 0 => array 0 => array 0 => array ...
edited: $r_notes = array_merge($r_notes, checkReplies( $rslt_rn[0]['reply_note_id'] )); should work without the need to add a parameter to the function

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.