如何使用php回显MySql中某行的某个id?
我有这段代码向我展示了DB中的所有值:
while($row=mysqli_fetch_array($res))
{
// some code goes here
$HTML.='
<li>
<h4 class="">'.$row['product_name'].'</h4>
<div class="grid">
'.$like.'
</div>
</li>';
最后在页面的某个地方:
<ul class="thumbnail-list">
<?php echo $HTML; ?>
</ul>
这显示了我所有的product_names(照片),但我想将它们彼此分开。我的数据库看起来像这样:
我如何能够彼此独立地回应Photos
?
更新:
我的While循环看起来像这样:
while($row=mysqli_fetch_array($res))
{
// get likes and dislikes of a product
$query = mysqli_query($connection,"select sum(`like`) as `like`,sum(`unlike`) as `unlike` from `likes` where pid = ".$row['id']);
$rowCount = mysqli_fetch_array($query);
if($rowCount['like'] == "")
$rowCount['like'] = 0;
if($rowCount['unlike'] == "")
$rowCount['unlike'] = 0;
if($uid == "") // if user not loggedin then show login link on like button click
{
$like = '
<input onclick="location.href = \'login.php\';" type="button" value="'.$rowCount['like'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Login to Like" class="button_like" />
<input onclick="location.href = \'login.php\';" type="button" value="'.$rowCount['unlike'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Login to Unlike" class="button_unlike" />';
}
else
{
$query = mysqli_query($connection,"SELECT * from `likes` WHERE pid='".$row['id']."' and uid='".$uid."'");
if(mysqli_num_rows($query)>0){ //if already liked od disliked a product
$likeORunlike = mysqli_fetch_array($query);
// clear values of variables
$liked = '';
$unliked = '';
$disable_like = '';
$disable_unlike = '';
if($likeORunlike['like'] == 1) // if alredy liked then disable like button
{
$liked = 'disabled="disabled"';
$disable_unlike = "button_disable";
}
elseif($likeORunlike['unlike'] == 1) // if alredy dislike the disable unlike button
{
$unliked = 'disabled="disabled"';
$disable_like = "button_disable";
}
$like = '
<input '.$liked.' type="button" value="'.$rowCount['like'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Like" class="button_like '.$disable_like.'" id="linkeBtn_'.$row['id'].'" />
<input '.$unliked.' type="button" value="'.$rowCount['unlike'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Un-Like" class="button_unlike '.$disable_unlike.'" id="unlinkeBtn_'.$row['id'].'" />
';
}
else{ //not liked and disliked product
$like = '
<input type="button" value="'.$rowCount['like'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Like" class="button_like" id="linkeBtn_'.$row['id'].'" />
<input type="button" value="'.$rowCount['unlike'].'" rel="'.$row['id'].'" data-toggle="tooltip" data-placement="top" title="Un-Like" class="button_unlike" id="unlinkeBtn_'.$row['id'].'" />
';
}
}
$HTML.='
<li>
<h4 class="">'.$row['product_name'].'</h4>
<div class="grid">
'.$like.'
</div>
</li>';
}
答案 0 :(得分:1)
我建议您在变量$HTML
的字符串上使用数组。因为您的字符串只是将所有结果连接在一起,这就是代码应该如何工作。
请参阅PHP ARRAYS。
所以在你的while循环中这样做:
$HTML[]='
<li>
<h4 class="">'.$row['product_name'].'</h4>
<div class="grid">
<p>Photo Name : '.$row['product_name'].' <br>
File Name : '.$row['image'].' </p>
</div>
</li>';
//because I know nothing about how you are retrieving your images, I am only
//display the text value of the code as it would be in the database
当您回显$HTML
时,您需要在数组中指定所需的索引。
<ul class="thumbnail-list">
<?php echo $HTML[0]; ?>
</ul>
<ul class="thumbnail-list">
<?php echo $HTML[1]; ?>
</ul>
或者如果你想遍历每个元素
foreach($HTML as $item){
<ul class="thumbnail-list">
<?php echo $item; ?>
</ul>
}