开发者

PHP while loop - exclude most recent row? [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 11 years ago.

I have a 开发者_StackOverflowPHP while loop that loads user comments - I need to exclude the most recent row in the database.

$sql = "SELECT c.trackid, c.comment, c.time, c.userid, u.id, u.username FROM 
comments c
LEFT JOIN users u
ON u.id = c.userid
WHERE trackid='$trackid'
ORDER BY c.time DESC";



$i=1;
while ($row = mysql_fetch_assoc($result)) {
  echo "<div class=\"commentDivs\">" . $row['time'] ."<br><br> " . $row['username'] . "<div class=\"userPostedComments\">" . $row['comment'] . "</div></div>";
}

How do I exclude the most recent row from the loop?


you can do it this way you will have to run 2 queries first to find the id of the latest comment ....

$sql = mysql_query("SELECT id FROM tbl_name ORDER BY id DESC");
$row = mysql_fetch_array($sql);
$latest_id = $row['id'];

This will give you the latest id and then you can run the next query like this

$sql1 = mysql_query("SELECT * FROM tbl_name WHERE id != '$latest_id' ORDER BY id DESC");
 while($row1 = mysql_fetch_array($sql1))
 {    }

and let us know what you trying to do , might be that will make your code more clean


If your query returns results sorted by most recent descending, you can do this on the query's side by specifying OFFSET 1.

Alternately, you can use mysql_data_seek to move the row pointer however you need.


SELECT c.trackid, c.comment, c.time, c.userid, u.id, u.username FROM 
comments c
LEFT JOIN users u
ON u.id = c.userid
WHERE trackid='$trackid'
AND c.time != ( SELECT max(c.time) FROM comments WHERE u.id = c.userid )
ORDER BY c.time DESC


please provide the database structure and the sql query so we can help , you can use ORDER BY id or store the time as DATETIME and ORDER BY time in the sql query


You could use something as

$rows = array();
while ($row = mysql_fetch_array($result))
{
    $rows[] = $row;
}

then you can easily use $rows as an array, and can step in a for-loop until count($rows)-1

like

for($i = 0; $i < count($rows) - 1 ; $i++)
{
    print '<div class="commentDivs">' . $rows[$i]['time'] .'<br><br> ' . $rows[$i]['username'] . '<div class="userPostedComments">' . $rows[$i]['comment'] . '</div></div>';
}

EDIT: or you can jump over the specific line, if you hav not ordert them by the time...

or if you have it ordert DESC by the time yust modify my loop-header to

for($i = 1; $i < count($rows) ; $i++)


Here is a simple solution, using your current query:

for($i=1; $i < mysql_num_rows($result); $i++) {
    mysql_data_seek($result, $i);
    $row = mysql_fetch_assoc($result);
    echo "<div class=\"commentDivs\">" . $row['time'] ."<br><br> " . $row['username'] . "<div class=\"userPostedComments\">" . $row['comment'] . "</div></div>";
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜