这是我的第一篇文章,因为我一直在努力寻找自己的方式,但我的大脑即将爆炸,在此之前我会非常感谢你的帮助。我有一个while循环输出所需的数据,但在while循环中我想检查session user == user_id,如果该语句为true,那么我想在循环中添加一个链接,以便用户可以编辑他们的帖子。我已经尝试了很多变化,有太多的列表(一些接近,其他是灾难)这是我目前的不正确,但真的很感激一些建议。起初我将我的if calc嵌入到我的回声中,令我惊讶地认识到列表创建者,但我的循环的其余部分将失败,所以现在我尝试在回声之前将其添加为$ IsUser然后在内部调用该函数回声,但这也不起作用。如果这是基本的东西,或者我的思维过程完全偏离,我道歉。
<?php
// CONNECT TO THE DATABASE
include_once("../php_includes/db_conx.php");
//Run a select query
$result = mysqli_query($db_conx,"SELECT listing.user_id, listing.id, users.username, listing.date_created, listing.status, listing.category, listing.country, listing.date_deadline, listing.date_start, listing.title, listing.description
FROM listing
INNER JOIN users
ON listing.user_id=users.id
ORDER BY date_created DESC");
//Is User listing creator
//$isUser = "no";
if($string1==$string2){
$isUser = '<a href="listing_add.php?id='. $row['id'] .'">Edit Listing</a>';
}
//Run listing loop
while($row = mysqli_fetch_array($result))
$string1 = $row['user_id'];
$string2 = $_SESSION['userid'];
{
echo '
<div id=listings>
<div id=img><br>No image to display</div>
<div id=row>
<div id=title><a href="listing_disp.php?id='. $row['id'] .'">'. $row['title'] .'</a></div>
</div>
<div id=row>
<div id=description>'. $row['description'] .'</div>
<div id=created>Added: '. date('jS M Y',strtotime($row['date_created'])) .'</div>
<div id=deadline>Deadline: '. date('jS M Y',strtotime($row['date_deadline'])) .'</div>
<div id=category>Category: <a href="index.php?'. $row['category'].'">'. $row['category'] .'</a></div>
<div id=user>User: <a href="index.php?'. $row['user_id'].'">'. $row['username'].'</a> </div>
</div>
<div id=row>
<div id=edit>'.$isUser.'</div>
<div id=country>'. $row['country'] .'</div>
</div>
</div>
';
}
mysqli_close($con);
?>
答案 0 :(得分:0)
从页面顶部删除条件并尝试按照
while($row = mysqli_fetch_array($result)){
$string1 = $row['user_id'];
if($string1 == $_SESSION['userid'])
{
$isUser = '<a href="listing_add.php?id='. $row['id'] .'">Edit Listing</a>';
}else{
$isUser = '';
}
echo '
<div id=listings>
<div id=img><br>No image to display</div>
<div id=row>
<div id=title><a href="listing_disp.php?id='. $row['id'] .'">'. $row['title'] .'</a></div>
</div>
<div id=row>
<div id=description>'. $row['description'] .'</div>
<div id=created>Added: '. date('jS M Y',strtotime($row['date_created'])) .'</div>
<div id=deadline>Deadline: '. date('jS M Y',strtotime($row['date_deadline'])) .'</div>
<div id=category>Category: <a href="index.php?'. $row['category'].'">'. $row['category'] .'</a></div>
<div id=user>User: <a href="index.php?'. $row['user_id'].'">'. $row['username'].'</a> </div>
</div>
<div id=row>
<div id=edit>'.$isUser.'</div>
<div id=country>'. $row['country'] .'</div>
</div>
</div>
';
}
mysqli_close($con);
答案 1 :(得分:0)
你需要把它放在里面,同时在你的代码中,{
在while之后放错了。
由于$string2
保持不变,您可以在循环外定义它,但不能对$string1
执行相同操作,因为它需要来自$row
的当前数据
$string2 = $_SESSION['userid'];
while($row = mysqli_fetch_array($result)) {
$string1 = $row['user_id'];
if ($string1==$string2) { /* do what you have to do */ }
// output
}
答案 2 :(得分:0)
您需要将if
语句放在while
循环中。另请注意,我将您的ID更改为类您不想回显DOM中的重复ID 。
我还将您的变量从$isUser
重命名为$editLink
,确保将其清除以用于后续循环迭代。
while ($row = mysqli_fetch_array($result)) {
if ($row['user_id'] == $_SESSION['userid'] {
$editLink = '<a href="listing_add.php?id=' . $row['id'] . '">Edit Listing</a>';
} else {
$editLink = '';
}
echo '
<div class="listings">
<div class=lsitings-img"><br />No image to display</div>
<div class="listings-row">
<div class="listings-title"><a href="listing_disp.php?id='. $row['id'] .'">'. $row['title'] .'</a></div>
</div>
<div class="listings-row">
<div class="istings-row-description">'. $row['description'] .'</div>
<div class="istings-row-created">Added: '. date('jS M Y',strtotime($row['date_created'])) .'</div>
<div class="istings-row-deadline">Deadline: '. date('jS M Y',strtotime($row['date_deadline'])) .'</div>
<div class="istings-row-category">Category: <a href="index.php?'. $row['category'].'">'. $row['category'] .'</a></div>
<div class="istings-row-user">User: <a href="index.php?'. $row['user_id'].'">'. $row['username'].'</a> </div>
</div>
<div class="listings-row">
<div class="listings-edit">' . $editLink . '</div>
<div class="listings-country">' . $row['country'] . '</div>
</div>
</div>
';
}