开发者

Trying to figure out how to loop while grouping

Here is the data I have:

ID | Name | Family
 1 | John | Doe
 2 | Jane | Doe
 3 | John | Man
 4 | Jane | Blah

Basically what I am trying to do is group them by Family. I am getting the data inside a while statement and I just tried using array and array_push but that doesn't work at all.

Any ideas or suggestions on how to do this?

I am going to ouput this data for jquery for creating optgroups which would be grouped by Family. Currently here is what is happening:

while ($orders_status = tep_db_fetch_array($orders_status_query)) {
    $status_id = $orders_status['orders_status_id'];
    $status_name = $orders_status['orders_status_name'];
    $status_group = $orders_status['orders_status_group'];
}

That's the basic code. I tried doing this to the above mentioned co开发者_Python百科de:

while ($orders_status = tep_db_fetch_array($orders_status_query)) {
    $status_id = $orders_status['orders_status_id'];
    $status_name = $orders_status['orders_status_name'];
    $status_group = $orders_status['orders_status_group'];

    echo 'if (currentOption.val() == "' . $status_id . '") {
              var optgroup = $("<optgroup/>");
              optgroup.attr("label", "' . $status_group . '");
              currentOption.wrapAll(optgroup);
          }
    ';
 }

What that did is basically created multiple Family of Doe. Instead I would like to somehow create only one Family of Doe and push Name and ID into it


$groups = array();
foreach ($rows as $row) {
  $family = $row["family"];
  if (empty($groups[$family])) $groups[$family] = array();
  $groups[$family] []= $row;
}

// $groups["Doe"] will have the two relevant rows...
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜