I have a database that has two fields: current, and previous.
Here is the code:
<?
$username="*****";
$password="*****";
$database="******";
mysql_connect(localhost,$username,$password) or die("Unable to connect to server");
mysql_select_db($database) or die( "Unable to select database");
$query='SELECT * FROM tasks WHERE current=1 OR previous=1';
$result=mysql_query($query);
$num=mysql_num_rows($result);
mysql_close();
?>
It then output it and manipulate it like this:
<div class="tasklist">
<div class="currentProject">
Current Project:
</div>
<?
$i=0;
while ($i < $num) {
$title=mysql_result($result,$i,"Title");
$开发者_如何学运维link=mysql_result($result,$i,"Weblink");
$description=mysql_result($result,$i,"Description");
$id=mysql_result($result,$i,"ID");
$howto=mysql_result($result,$i,"howto");
$blogpost=mysql_result($result,$i,"blogpost");
$done=mysql_result($result,$i,"done");
$current=mysql_result($result,$i,"current");
$previous=mysql_result($result,$i,"previous");
if ( $current == 1 ) {
$current = "current";
} else {
$current = "";
}
if ( $previous == 1 ) {
$previous = "previous";
} else {
$previous = "";
}
?>
<div class="<? echo $done ?> task <? echo $id ?>" id="<? echo $current.$previous ?>">
<div class="titleWrapper">
<div class="title">
<a class="article" href="<? echo $link ?>"><? echo $title ?></a>
</div>
</div>
</div><BR>
<?
$i++;
}
echo "</div>";
?>
The problem is that since previous comes before current in the database, it outputs previous and then current.
Question:
How do I make sure to output current
first, and then previous
?
Instead of outputting both directly, store them until you actually have both, and then output both in the correct order.
Just append this to your query order by previous
.
It must work as long as current task registry has previous=0
and previous task registry has previous=1
.
精彩评论