在表中显示PHP查询的结果

时间:2022-03-30 11:33:23

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..

我在显示一些SQL查询结果时遇到了一些麻烦。目标:如果在'注册'表中为该事件'eid'(事件ID)注册了帮助器,我想在正在生成的表中显示Helper'name'。如果(1)没有帮助者然后显示'等待帮助',(2)有一个助手然后显示'名称 - 等待批准..'和(3)否则只显示帮助者的名字..

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:

尝试在phpMyAdmin中运行带有硬编码值的SQL查询,我得到了我想要的结果,所以我知道这不是我的查询。怀疑它只是打印出表格中的信息而在某处出错了。该表将显示数据,直到来自地址的ZIP,然后下一列是“助手”列,根本不显示任何内容。所以它让我觉得我有一个简单的拼写错误基于我的if()语句逻辑BUT也觉得有趣的是当我做行时:

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)..

我根本无法打印出桌子。不确定这是否与我的确切问题有关,但似乎可能是。我把这个功能放在停止工作的东西后,似乎它可能是罪魁祸首。函数的返回应该返回一个ID(int),一个名称“string”,或者只返回一个通用值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>";

1 个解决方案

#1


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..

谢谢杰夫。问题是在函数内部它确实不知道$ userId是什么,即使我在我的php文件的顶部有include语句。我不得不在顶部的函数中添加这一行。

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

然后我还将$ userId作为参数传递给函数

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)

我也在一些if逻辑中有一些结束 标签,以便修复我得到的时髦显示(第二行表在表外)

#1


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..

谢谢杰夫。问题是在函数内部它确实不知道$ userId是什么,即使我在我的php文件的顶部有include语句。我不得不在顶部的函数中添加这一行。

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

然后我还将$ userId作为参数传递给函数

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)

我也在一些if逻辑中有一些结束 标签,以便修复我得到的时髦显示(第二行表在表外)