[英]How to display data from a MySql table in indiviual cards using php?
我一直想在我的网站上执行此操作,但是我找不到正确的方法来执行此操作。 任何资源都会很棒。 总的来说,我试图显示单个引导卡中的每个数据值。 此外,是否有可能使卡的数量等于MySql表中值的数量? 我不了解的是如何将PHP集成到HTML中,以使页面显示可变数量的引导卡。 我正在尝试使用PHP(在使用中是我的新手)中做到这一点。 谢谢您的帮助。 感谢所有答复。
//the following gets values from the two tables, I am trying to get the values from the "data" table
if (isset($_SESSION['username'])) {
$userLoggedIn = $_SESSION['username'];
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'" );
$user = mysqli_fetch_array($user_details_query);
$user_id = $user['id'];
$data_detail_query = mysqli_query($con, "SELECT * FROM data WHERE user_id ='$user_id'");
$data = mysqli_fetch_array($data_detail_query);
$num_rows = mysqli_num_rows($data_detail_query);
}
//The following shows one bootstrap card that I was able to create using one row.
<div class="card" style="width: 18rem;float:left;margin-top:20px;margin-left:20px;border-color:white;border-width:5px;">
<div class="card-block">
<img style="height:250px;width:100%;" src="<?php echo $data['icon']; ?>" alt="Card image cap">
<div>
<h1 class="card-title" ><?php echo $data['name']?><span style="color:#5aff28;float:right" ><?php echo $data['level']; ?></span></h1>
<h5 class="card-subtitle mb-2 text-muted"><?php echo $data['category']; ?></h5>
<p class="card-text"><?php echo $data['description']; ?></p>
<a href="<?php echo $data['link']; ?>" class="card-link">Link</a>
<a href="<?php echo $data['developer_link']; ?>" class="card-link">Developer Profile</a>
<form method="post">
<div style="width:200;float:left;margin-top:5px" class="form-group">
<input type="text" class="form-control" id="code" name="code" placeholder="Enter Code">
</div>
</form method="post">
<a href="" name="code_btn" class="btn btn-success">Enter</a>
</div>
我正在尝试获取名为“ data”的表中具有用户名$ userLoggedIn的每一行,并在其自己的引导卡中显示每个结果。
mysqli_fetch_array
只会为您提供NEXT结果,而不是全部。 您只需要从数据库中获取结果,直到没有更多结果为止,例如:
while (($data = mysqli_fetch_array($data_detail_query))) {
/// ... do the output as above, e.g.:
?><a href="<?php echo $data['link']; ?>" class="card-link">Link</a><?php
}
顺便说一句,请确保您在此处没有产生SQL注入孔:
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username='$userLoggedIn'" );
^^^^^^^^^^^^^^^^
使用Prepared Statements或执行正确的字符串引用:
$userLoggedIn = $con->real_escape_string($userLoggedIn);
$user_details_query = mysqli_query($con, "SELECT * FROM users WHERE username={$userLoggedIn}" );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.