-1

这是我的第一篇文章,因为我一直在努力寻找自己的方式,但我的大脑即将爆炸,在此之前我非常感谢您的帮助。我有一个输出所需数据的while循环,但在那个while循环中我想检查会话用户== user_id,如果该语句为真,那么我想在循环中添加一个链接,以便用户可以编辑他们的帖子。我已经尝试了很多变化,列出的太多了(有些接近,有些是灾难)这是我目前所拥有的,这是不正确的,但非常感谢一些建议。起初,我将 if calc 嵌入到我的 echo 中,令我惊讶的是它识别出了列表创建者,但我的循环的其余部分将失败,所以现在我 我试图在回声之前将其添加为 $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: &nbsp;<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);
?>
4

3 回答 3

0

您需要将您的if语句放在while循环中。还注意到我将您的 id 更改为 classes,因为您不想在 DOM 中回显重复的 id

我还将您的变量从$isUserto重命名,$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: &nbsp;<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>
  ';
}
于 2013-10-22T09:30:32.693 回答
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: &nbsp;<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);
于 2013-10-22T09:24:13.653 回答
0

你需要把它放在while里面,在你的代码中{,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
}
于 2013-10-22T09:25:36.370 回答