PHP 查询循环

发布于 2024-12-03 02:01:22 字数 1931 浏览 1 评论 0原文

在创建表时,我无法找到循环值的正确方法。我有一个表,其中包含日期、ID 和其他数据的列表。我需要循环遍历看起来像这样的表:

rest_id    date          galls
133        2011-06-01    75 
139        2011-06-12    60 
133        2011-06-13    85 
139        2011-06-22    25 
133        2011-06-30    80

最后得到:

  id     week 1    week 2   week 3   week 4   week 5
  133    75        85       wk3      4        80
  139    wk1       60       wk3      25       wk5

我已经尝试了很多变体,但无法正确循环。我已经玩这种类型的循环几个星期了,但无法得到它。

我有一个获取每月星期几的例程:

$weekNum = intval((intval($row_rests['day'])+6)/7);

do {  ?>
  <td><?php if($weekNum == 1 AND !in_array('1',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '1'); continue;} else echo '1'; array_push($weeklist, '1'); ?></td>
  <td><?php if($weekNum == 2 AND !in_array('2',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '2'); continue;} else echo '2'; array_push($weeklist, '2'); ?></td>
  <td><?php if($weekNum == 3 AND !in_array('3',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '3'); continue;} else echo '3'; array_push($weeklist, '3'); ?></td>
  <td><?php if($weekNum == 4 AND !in_array('4',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '4'); continue;} else echo '4'; array_push($weeklist, '4'); ?></td>
  <td><?php if($weekNum == 5 AND !in_array('5',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '5'); continue;} else echo '5'; array_push($weeklist, '5'); ?></td>
<?php
} while ($row_rests = mysql_fetch_assoc($rests));
$weeklist = array();
?>

我一直在尝试在循环时填充一个数组。如果周数已在数组中,则跳过该条目。问题是,有时会有 5 个条目,有时是 0 个或介于两者之间的任何条目。

我需要在每个单元格中获取一些文本,但我还没有找到正确的循环组合(我认为我应该总是循环 5 次)和数组写入。我已经尝试了所有的 continue 组合;并打破;并在不同的地方重置数组。

上面的代码位于一个循环中,该循环遍历rest_ids,并且工作正常。只是这个子循环难倒了我。

有想法吗?

I am having trouble finding the right way to loop over values when creating a table. I have a table with a list of dates, ids and other data. I need to loop over the table that looks like:

rest_id    date          galls
133        2011-06-01    75 
139        2011-06-12    60 
133        2011-06-13    85 
139        2011-06-22    25 
133        2011-06-30    80

And end up with:

  id     week 1    week 2   week 3   week 4   week 5
  133    75        85       wk3      4        80
  139    wk1       60       wk3      25       wk5

I have tried many variations but can't get the loop right. I have been playing with this type of loop for weeks and can't get it.

I have a routine that gets the week of the month:

$weekNum = intval((intval($row_rests['day'])+6)/7);

do {  ?>
  <td><?php if($weekNum == 1 AND !in_array('1',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '1'); continue;} else echo '1'; array_push($weeklist, '1'); ?></td>
  <td><?php if($weekNum == 2 AND !in_array('2',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '2'); continue;} else echo '2'; array_push($weeklist, '2'); ?></td>
  <td><?php if($weekNum == 3 AND !in_array('3',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '3'); continue;} else echo '3'; array_push($weeklist, '3'); ?></td>
  <td><?php if($weekNum == 4 AND !in_array('4',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '4'); continue;} else echo '4'; array_push($weeklist, '4'); ?></td>
  <td><?php if($weekNum == 5 AND !in_array('5',$weeklist)) {echo $row_rests['galls']; array_push($weeklist, '5'); continue;} else echo '5'; array_push($weeklist, '5'); ?></td>
<?php
} while ($row_rests = mysql_fetch_assoc($rests));
$weeklist = array();
?>

I have been trying to populate an array as I loop. If the week number is already in the array, than skip that entry. Trouble is, sometimes there will be 5 entries, sometimes 0 or anywhere in between.

I need to get SOME text in each cell but I haven't found the right combo of loops (I should always loop 5 times I think) and array writing. I have tried all combos of continue; and break; and resetting the array in different places.

The above code is within a loop that goes through the rest_ids, and that works fine. It's just this sub-loop that is stumping me.

Ideas?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

水晶透心 2024-12-10 02:01:22

你意识到,你不需要使用 PHP/OOP 循环来完成这个任务。

查看你的表和你想要获得的结果,你需要做的就是使用 SQL 中的 GROUPBY 子句对查询结果进行分组。在该查询中,您可以重命名属性/列名称并为其添加条件。

U realise, u dont need to use PHP/OOP loops to accomplish that..

Looking at your table and the result that you want to get, all you need to do is to group your query result using the GROUPBY clause in SQL. In that very query, you can rename the attribute/column name and put conditions on it.

墨洒年华 2024-12-10 02:01:22
function getWeekOfMonth($date) {
    return intval((intval(substr($date,8))+6)/7); //Took just your function
}


$rows = array();
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $found = false;
    foreach ($rows as &$savedRow) {
        if ($savedRow['id'] == $row['id']) {
            $found = true;
            $savedRow['wk'.getWeekOfMonth($row['date'])] = $row['galls'];
            break;
        }
    }
    if (!$found) {
        $tempRow = array(
            'id' => $row['id'],
            'wk1' => 'wk1',
            'wk2' => 'wk2',
            'wk3' => 'wk3',
            'wk4' => 'wk4',
            'wk5' => 'wk5'
        );
        $tempRow['wk'.getWeekOfMonth($row['date'])] = $row['galls'];

        $rows[] = $tempRow;
    }
}

正如您所看到的,它首先查看 id 的行是否已经存在。如果不是,它会使用 5 个标准值初始化该行,然后将当前行设置为其值。如果它已经存在,它只是将正确的 wk 设置为正确的值。

function getWeekOfMonth($date) {
    return intval((intval(substr($date,8))+6)/7); //Took just your function
}


$rows = array();
$sql = "SELECT * FROM table";
$result = mysql_query($sql);
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $found = false;
    foreach ($rows as &$savedRow) {
        if ($savedRow['id'] == $row['id']) {
            $found = true;
            $savedRow['wk'.getWeekOfMonth($row['date'])] = $row['galls'];
            break;
        }
    }
    if (!$found) {
        $tempRow = array(
            'id' => $row['id'],
            'wk1' => 'wk1',
            'wk2' => 'wk2',
            'wk3' => 'wk3',
            'wk4' => 'wk4',
            'wk5' => 'wk5'
        );
        $tempRow['wk'.getWeekOfMonth($row['date'])] = $row['galls'];

        $rows[] = $tempRow;
    }
}

As you see, it first looks, if the row for the id already exists. If not, it initializes the row with its 5 standard values and then sets the current row to its value. If it already exists, it just sets the right wk to the correct value.

演多会厌 2024-12-10 02:01:22

感谢您的回复!这是我在这里发表的第一篇文章,所以我在格式化方面遇到了一些困难。
我发布后很快就解决了......当然。

<?php $i = 0;
        $y = 0;
     while ($i < 5): 
          $weekNum = intval((intval($row_rests['day'])+6)/7);
    ?>
      <td align="center"><php if(intval($weekNum) == intval($i+1)) {echo $row_rests['galls'];} else echo "-"; ?></td>
      <?php  
         $i++;
         if ($weekNum == $i)
           $y++;
           if($totalRows_rests = 0){
               $y = 0;
               }
               else
               { $y = $y;}
           mysql_data_seek ($rests, $y);
           $row_rests = mysql_fetch_assoc($rests);
    endwhile; ?>

感谢您的帮助!

Thanks for the replies! This was my first post here so I struggled with the formatting a bit.
I solved it soon after I posted...of course.

<?php $i = 0;
        $y = 0;
     while ($i < 5): 
          $weekNum = intval((intval($row_rests['day'])+6)/7);
    ?>
      <td align="center"><php if(intval($weekNum) == intval($i+1)) {echo $row_rests['galls'];} else echo "-"; ?></td>
      <?php  
         $i++;
         if ($weekNum == $i)
           $y++;
           if($totalRows_rests = 0){
               $y = 0;
               }
               else
               { $y = $y;}
           mysql_data_seek ($rests, $y);
           $row_rests = mysql_fetch_assoc($rests);
    endwhile; ?>

Thanks for the help!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文