2017-04-05 58 views
0

所以我不熟悉如何使用MySql和PHP進行編程,所以我不確定如何解析從PHP腳本到HTML工具提示的數據。將MySql數據從PHP打印到HTML工具提示

這是我的PHP:

<?php 
$servername = "server"; 
$username = "username"; 
$password = "password"; 
$dbname = "dbname"; 

// Create connection 
$conn = new mysqli($servername, $username, $password, $dbname); 
// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$sql = "select courseCode, courseName from building b, room r, occupy o, timeslot t, meet m, section s, course c, timespan ts, day d, class cl where b.name = 'BH' and r.roomNum = '128' and b.buildingID = r.buildingID and o.roomID = r.roomID and t.timeID = o.timeID and t.timeID = m.timeID and m.sectionID = s.sectionID and c.courseID = s.courseID and t.spanID = ts.spanID and ts.start = '10:05' and ts.end = '11:25' and t.dayID = d.dayID and d.dayOfWeek = 'M' and cl.roomID = r.roomID and cl.sectionID = s.sectionID"; 
$result = $conn->query($sql); 

if ($result->num_rows > 0) { 
// output data of each row 
while($row = $result->fetch_assoc()) { 
    echo "Course Code: " . $row["courseCode"]. " - Name: " . $row["courseName"]."<br>"; 
} 
} else { 
echo "0 results"; 
} 
$conn->close(); 
?> 

最後,我想選擇的數據放入我已經把一個列表項的工具提示,它說標題=「」。有什麼建議麼?

HTML:

<div class="list-group"> 
     <a class="list-group-item list-group-item-action list-group-item-success listmargin"><abbr title="" rel="tooltip">HH 201</abbr></a> 
     <a class="list-group-item list-group-item-action list-group-item-success listmargin"><abbr title="From DB </em>." rel="tooltip">HH 203</abbr></a> 

回答

0

就像你已經做了。

<?php 
$output = ''; 
while ($row = $result->fetch_assoc()): 
    // escape data 
    $courseCode = htmlspecialchars($row['courseCode']); 
    $courseName = htmlspecialchars($row['courseName']); 

    $output .= sprintf('<a class="list-group-item list-group-item-action list-group-item-success listmargin"><abbr title="%s" rel="tooltip">%s</abbr></a>', $courseName, $courseCode); 
endwhile; 
?> 
<div class="list-group"> 
    <?= $output ?> 
</div> 
+0

那麼使用這個PHP代碼與其他PHP代碼分開使用?目前,我正在從單獨的PHP文件調用頭部的PHP代碼,並嘗試將它應用於列表組中。 – Christian

+0

我更新了我的答案。希望我正確理解你。 – Mary