prepare($nsql); $nstmt->bind_param("s", $main_user_name); $nstmt->execute(); $nres = $nstmt->get_result(); while ($r = $nres->fetch_assoc()) { $niches[] = $r['niche']; } $nstmt->close(); // 2. Fetch posts with reviewstatus = 'successfull' $sql = " SELECT p.*, pn.nichename FROM posts p LEFT JOIN postniche pn ON pn.postid = p.postid WHERE p.reviewstatus = 'successfull' ORDER BY p.id DESC "; $stmt = $conn->prepare($sql); $stmt->execute(); $result = $stmt->get_result(); $posts = []; while ($row = $result->fetch_assoc()) { $postNiches = array_filter(array_map('trim', explode(',', $row['nichename'] ?? ''))); $matched = array_intersect($niches, $postNiches); if (!empty($matched)) { $media = json_decode($row['mediaurl'] ?? '[]', true); $originals = json_decode($row['originalfilename'] ?? '[]', true); $posts[] = [ 'id' => $row['id'], 'accountId' => (int)$row['defaultaccountid'], 'username' => $row['defaultusername'], 'content' => $row['postcontent'], 'media' => is_array($media) ? $media : [], 'audiofilename' => $row['audiofilename'], 'originals' => is_array($originals) ? $originals : [], 'hashtags' => array_filter(array_map('trim', explode(',', $row['hashtag']))), 'niche' => $row['niche'], 'ip' => $row['ipaddress'], 'userAgent' => $row['useragent'], 'postId' => $row['postid'], 'likes' => (int)$row['totallikes'], 'comments' => (int)$row['totalcomment'], 'shares' => (int)$row['totalshares'], 'viewType' => $row['distributedviewstype'], 'potentialImpressions' => (int)$row['assignedpotentialimpressions'], 'impressions' => (int)$row['totalimpressions'], 'nsfw' => $row['nsfw'] ]; } } $stmt->close(); echo json_encode([ 'status' => 'success', 'posts' => $posts ]);