[英]Php loop need help?
This is sql table 这是sql表
id id_user id_userfor description date
1 231 119 a to b 2010-02-05 17:42:47
2 231 231 myself 2010-02-05 17:36:28
3 231 119 from x to b 2010-02-05 17:36:29
I could not find the way to select output result which looks something like this: 我找不到选择输出结果的方法,如下所示:
user 119
a to b 2010-02-05 17:42:47
from x to b 2010-02-05 17:36:29
user 231
myself 2010-02-05 17:36:28
You can use 您可以使用
select id_userfor, description, date from table order by id_userfor;
then you can print all the date
column data for each distinct id_userfor
然后,您可以打印每个不同的id_userfor
所有date
列数据
$old = '';
while($row = mysql_fetch_array($result)) {
if($old != $row['id_userfor']) {
$old = $row['id_userfor'];
echo "$row['id_userfor']<br />";
}
echo $row['description'] . " " . $row['date'] . "<br />";
}
You can query with the id_userfor
field sorted Then loop all record and everytime you encounter a new value of id_userfor
print it. 您可以使用id_userfor
字段进行查询排序然后循环所有记录,每次遇到id_userfor
的新值时id_userfor
打印它。
Something like this: 像这样的东西:
$SQL = 'select * from ... order by `id_userfor`';
$Result = ...;
$PrevUserFor = '';
while($Row = ...) {
const $UserFor = $Row['id_userfor'];
if ($PrevUserFor != $UserFor) {
// Here is where you print the user ID
echo "user $UserFor<br>";
$PrevUserFor != $UserFor;
}
$Description = $Row['description'];
$Date = $Row['date'];
echo(' $Description $Date<br>');
}
Hope this helps. 希望这可以帮助。
The codaddict query a little more readable: codaddict查询更具可读性:
SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`
And then, inside the loop, you'll use something like this: 然后,在循环内部,你将使用这样的东西:
$sql = "SELECT `id_userfor`, `description`, `date` FROM `table` ORDER BY `id_userfor`";
$query = mysql_query($sql);
$user = null; // Empty user
while($data = mysql_fetch_assoc($query)) {
if ($data['id_userfor'] != $user) {
// Display the user only when it changes
echo '<H1>'. $data['id_userfor'] .'</H1>';
$user = $data['id_userfor'];
}
// Display the rest of the fields here :)
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.