2016-11-06 102 views
2

我在顯示某些SQL查詢結果時遇到了一些麻煩。 目標:如果在'註冊'表中爲該事件'eid'註冊了一個幫助器(事件ID),我想在正在生成的表中顯示幫助器'名稱'。如果(1) (2)有助手顯示'姓名 - 正在等待批准..',(3)其他只顯示助手的名字。在表中顯示來自PHP中SQL查詢的結果

嘗試運行在phpMyAdmin中使用硬編碼值進行SQL查詢,我得到我想要的結果,所以我知道這不是我的查詢。懷疑它只是將信息打印到表格中,而在某處出錯。該表將顯示數據,直到從地址開始的ZIP,然後是'助手'列的下一列根本不顯示任何內容。因此,它讓我覺得我有一個簡單的拼寫錯誤的地方根據我的if()語句的邏輯也覺得有趣也,當我做這一行:

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

我不能得到表打印出來的。不知道這是否與我的確切問題有關,但似乎可能。在我把這個函數停止工作後,它好像可能是罪魁禍首。該函數的返回應該返回一個ID(int),一個名稱「字符串」,或者只是一個通用值X(字符串)..

任何和所有幫助表示讚賞!

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>"; 
+0

井它看起來像'$ userId'在你的'getHelperIdOrName'函數中是未定義的 –

+0

一些好的[錯誤報告](http://stackoverflow.com/q/845021/4233593)可能會有所幫助。 –

回答

1

謝謝傑夫。問題是,在函數內部,它確實不知道$ userId是什麼,儘管我在我的php文件的頂部有include聲明。我有這條線在頂部加入到我的功能..

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

,然後我也通過了$用戶id的函數作爲參數

這些線是什麼我曾經幫我看看錯誤:

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

我也有一些結束</table>標記內一些邏輯如果使得固定時髦顯示我得到(表中的第二行表中的被外界)