PARSING JSONjs PROBLEM

I am back again with another problem. This is my MIT block:

I am fetching the candidates per position. But I always getting "not found" when retrieving the candidate. Why is that? This is my php code:

<?php

// Check if eventId and voterId are set and are valid integers
if (isset($_GET['eventId']) && isset($_GET['voterId'])) {
    $eventId = $_GET['eventId'];
$voterId = $_GET['voterId'];

// Check if the user has already voted
$alreadyVoted = $conn->prepare("SELECT * FROM votersLogs WHERE user_id=? AND event_id=?");
$alreadyVoted->bind_param("ii", $voterId, $eventId);
$alreadyVoted->execute();
$logsRes = $alreadyVoted->get_result();

if ($logsRes->num_rows > 0) {
    // User has already voted
    $output['error'] = 'You have already voted in this event.';
    echo json_encode($output);
    exit;
} else {
    $getEvent = $conn->prepare("SELECT * FROM votingEvents WHERE id=?");
$getEvent->bind_param("i", $eventId);
$getEvent->execute();
$eventRes = $getEvent->get_result();

if ($eventRes->num_rows > 0) {
    $event = $eventRes->fetch_assoc();
    $output['eventName'] = $event['eventName'];
    $output['eventType'] = $event['eventType'];

    // Function to retrieve and group candidates by position
    function getCandidatesByPosition($conn, $eventId, $voterId = null, $userCollege = null) {
        // Construct the query based on event type and whether college filtering is needed
        $query = "SELECT candidates.id, candidates.position, candidates.partylist, users.firstName, users.lastName
                  FROM candidates
                  JOIN users ON candidates.user_id = users.id
                  WHERE candidates.event_id = ?";

        if ($userCollege) {
            $query .= " AND users.college = ?";
        }

        $getCandidates = $conn->prepare($query);
        if ($userCollege) {
            $getCandidates->bind_param("is", $eventId, $userCollege);
        } else {
            $getCandidates->bind_param("i", $eventId);
        }

        $getCandidates->execute();
        $candidatesRes = $getCandidates->get_result();

        $candidatesByPosition = array();

        if ($candidatesRes->num_rows > 0) {
            while ($candidates = $candidatesRes->fetch_assoc()) {
                $position = $candidates['position'];
                $candidateInfo = array(
                    'id' => $candidates['id'],
                    'name' => $candidates['firstName'] . ' ' . $candidates['lastName'],
                    'partylist' => $candidates['partylist']
                );

                if (!isset($candidatesByPosition[$position])) {
                    $candidatesByPosition[$position] = array(
                        'position' => $position,
                        'candidatesInfo' => array()
                    );
                }

                $candidatesByPosition[$position]['candidatesInfo'][] = $candidateInfo;
            }
        }

        return $candidatesByPosition;
    }

    // Handle event types
    if ($event['eventType'] === 'Campus Election') {
        $candidatesByPosition = getCandidatesByPosition($conn, $eventId);
    } elseif ($event['eventType'] === 'College Election') {
        // Get the user's college
        $getUserCol = $conn->prepare("SELECT college FROM users WHERE id=?");
        $getUserCol->bind_param("i", $voterId);
        $getUserCol->execute();
        $userCollegeRes = $getUserCol->get_result();

        if ($userCollegeRes->num_rows > 0) {
            $userCollege = $userCollegeRes->fetch_assoc()['college'];
            $candidatesByPosition = getCandidatesByPosition($conn, $eventId, $voterId, $userCollege);
        }
    }

    // Add grouped candidates to output
    $output['candidates'] = array_values($candidatesByPosition);
}
}

// Fetch event details

}



// Return the output as JSON
header('Content-Type: application/json');
echo json_encode($output);
?>

I can fetch the positions. The problem is the candidate info.

It would help if you provide an .aia and the PHP in a more readable form.

untitled.txt (3.8 KB)

This is the php file. I manually checked the JSON for this and it is working.

So you are saying that this

is generating the "not found" ?

If so, we need to see the responseContent

What I think is strange, is that the .GotText block should have something in the url local variable, but you are overwriting this immediately. What are you trying to do?