2

I am having some trouble with displaying some SQL query results. Goal: I want to display the Helper 'name' in the table that is being generated if there is a helper signed up in the 'signup' table for that event 'eid' (event id).. If (1)there is no helper then display 'waiting for help', (2) there is a helper then display 'name -- awaiting approval..' and (3) else just display the name of helper..

Tried running the SQL query in phpMyAdmin with hard coded values and I get the results that I want so I know it is not my query. Have a suspicion that it is just the print out of the info into the table that is wrong somewhere. The table will display the data up until the ZIP from the address and then the next column which is the 'Helper' column does not display anything at all. So it makes me think I have a simple typo somewhere based on my if() statement logic BUT also find it interesting also that when I do the line:

echo "testing method -> ".getHelperIdOrName(2, 80)."<br>";

I cant get the table to print out at all. Not sure if this is related to my exact issue but it seems it could be. After I put this function in stuff stopped working so it seems like it could be culprit. The return of the function should either return an ID (int), a name "string", or just a generic value X (string)..

Any and all help is appreciated!

function getHelperIdOrName($x, $eid){
    //Get the helper name first
    $helperName = "";
    $helperId = 0;
    $sql = "SELECT id, first FROM users WHERE id IN (SELECT helper FROM signup WHERE gner = '".$userId."' AND eid = '".$eid."')";
    $result = mysqli_query($db,$sql);
    $row = $result->fetch_assoc();
    if ($x == 2){
        $helperName = $row["first"];
        return $helperName;
    }
    else if ($x == 1){
        $helperId = $row["id"];
        return $helperId;
    }
    else {
        return "X";
    }
}

echo "testing method -> ".getHelperIdOrName(2, 80)."<br>";

//look for calendar and/or business approved events (approved=1) to display on page
$sql = "SELECT s.gner, s.helper, s.eid, s.approved, e.name, e.date, e.summary, e.street, e.city, e.state, e.zip
FROM signup s 
INNER JOIN events e ON e.id = s.eid
INNER JOIN users u ON u.id = s.gner
WHERE s.gner = '".$userId."'";

$result = mysqli_query($db,$sql);
echo "<h3 class=\"text-center\">Events I'm Going To</h3>";
echo "<table class=\"table table-hover\"><tr><th>Event Name</th><th>Date</th><th>Summary</th><th>Location</th><th>Helper</th><th>Remove</th></tr>";
if ($result->num_rows > 0) {
         // output data of each row
         while($row = $result->fetch_assoc()) {
             echo "<tr><td>".$row["name"]."</td><td>".$row["date"]."</td><td>".$row["summary"]."</td><td>".$row["street"].", "
             .$row["city"].", ".$row["state"]." ".$row["zip"]."</td>";
             $tmp_eid = $row["eid"];
             if (getHelperIdOrName(2, $temp_eid) == "X"){
                 echo "<td>Waiting for help..</td>";
             }
             else if ($row["approved"] == 0){
                 echo "<td>".getHelperIdOrName(2, $temp_eid)." -- Awaiting Approval (see below)</td>";
             }
             else {
                 echo "<td>".getHelperIdOrName(2, $temp_eid)."</td>"; 
             }
             echo "<td><form method=\"post\" action=\"remove.php\">
             <button type=\"submit\" name=\"remove\" value=\"".$row["eid"]."\">Not Going</button></form></td></table>";
         }
}
else echo "</table><br><p class=\"text-center\">You are not signed up for any events.  Click <a href=\"index.php\">here</a> to sign up for events near you!</p>";
Retro
  • 113
  • 13

1 Answers1

1

Thanks for that Jeff. The issue was that inside of the function it indeed did not know what $userId was even though I had the include statement at the top of my php file. I had to add this line into my function at the top..

global $db; //is part of my db my connection info in my config.php file 

and then I also passed the $userId to the function as a parameter

these lines are what I used to help me see the errors:

ini_set('display_startup_errors', 1);
ini_set('display_errors', 1);
error_reporting(-1);

i also had some ending < /table > tags inside some if logic so that fixed the funky displays I was getting (2nd row of table being outside of the table)

Retro
  • 113
  • 13