duansao20000508 2017-04-05 15:43
浏览 35
已采纳

将MySql数据从PHP打印到HTML工具提示

So I am new to programming with MySql and PHP so I am not sure how to parse the data from a PHP script to an HTML tooltip.

Here is my 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();
?>

Ultimately I would like to place the selected data into a tooltip I have placed on a list item where it says title="". Any suggestions?

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>
  • 写回答

1条回答 默认 最新

  • doulu2011 2017-04-05 15:49
    关注

    Just like you already did.

    <?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>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值