Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8681081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:24:45+00:00 2026-06-12T21:24:45+00:00

i am having problems with a function that is supposed to return all the

  • 0

i am having problems with a function that is supposed to return all the childern of a certian object and encode the whole thing into JSON. Currently it is working but there are a bunch of nulls in the JSON encode and i have tried array_filter() and a foreach loop to scrub them out, both didn’t work.

findChildren($conn, $topic);

$data = array();
function findChildren($conn, $topic) {
   $rst = $conn->query("SELECT topicID, topicTitle, topicParentID, topicDeleted FROM tbl_topics WHERE topicParentID = $topic");
   while ($row = $rst->fetch_assoc()) {
      if ($row['topicDeleted'] == 0) {
         //$data[] = htmlentities($row, UTF-8);
         if($row != '') {
            $data[] = $row;   
         }

         findChildren($conn, $row['topicID']);
      }              
   }
   echo json_encode( $data );
}

any help would be awesome. Thanks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T21:24:47+00:00Added an answer on June 12, 2026 at 9:24 pm
    <?php
    
    $data = findChildren($conn, $topic);
    echo json_encode($data);
    
    function findChildren($conn, $topic) {
        $data = array();
        $rst  = $conn->query(
            "
                SELECT topicID, topicTitle, topicParentID, topicDeleted
                FROM tbl_topics
                WHERE topicParentID = ${topic} and topicDeleted = 0
            "
        );
    
        while ($row = $rst->fetch_assoc()) {
            $data[] = array_filter($row);
    
            if(!empty($row['topicID'])) {
                $data = array_merge(
                    $data,
                    findChildren($conn, $row['topicID'])
                );
            }
        }
    
        return $data;
    }
    
    • Changed the function to return an array, rather then echo directly, or use variable as if it was global
    • Moved the ‘topicDeleted’ checking to the query
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having a problem trying to create a Javascript function that checks all the
I'm having problems below. In my howmany function, it is supposed to read in
I'm having a problem with using this in js. I have a function that
Im having problems with a method in a class file: public static function getPageLeft($pid)
I'm having problems understanding how preventDefault function works. I created a simple jsfiddle, but
I'm having problems with a nested function and can't figure out why because i
I'm having problems adding a new table row by clicking the link_to_add function link.
We're having a significant number of problems creating a bulk upload function for our
I am doing work for school, I'm supposed to write a function that accepts
I am having a problem with a simple script that is supposed to update

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.