/    Sign up×
Community /Pin to ProfileBookmark

Array Chunks from Query Result

Is there a way to chunk arrays according to a db field?

Example:

  • Pull from the database several records.

  • Each record has a “class_id” field.

  • Sort each record by their “class_id”.

  • Create an array for each “class_id”.
  • So all the records with “class_id” of 1 would be in an array, and all with 2 in an array and so on…

    Is this possible?

    Thanks ?

    Will

    to post a comment
    PHP

    2 Comments(s)

    Copy linkTweet thisAlerts:
    @tgrk35authorMay 02.2007 — Ok, here's what I have so far:

    [code=php]$classification = array();
    $first = true;
    $employees = mysql_query("SELECT id,f_name,l_name,job_class,job_id FROM " . $table_prefix . "_staff ORDER BY job_class LIMIT 5");

    echo '<ul style="clear: both;">';
    while ($employee = mysql_fetch_array($employees)){
    if (!in_array($employee['job_class'],$classification)){
    if (!$first){
    echo '</ul></li></ul></li>';
    }
    echo '<li>';
    echo $employee['job_class'];
    echo '</li>';

    $classification[] = $employee['job_class'];
    }
    echo '<li><strong>' . $employee['f_name'] . '</li>';
    $first = false;
    }

    echo '</ul></li></ul></li></ul>';[/code]


    Here's the page:

    http://scott.summel.net/test.php

    Unfortunately, it's not echoing out the 2nd job_class, just the first one.

    Where did I go wrong?
    Copy linkTweet thisAlerts:
    @tgrk35authorMay 02.2007 — Ok...heh...so I got it to print the second job_class...just took all my life ;p.

    I think this is solved then ;p.

    Thanks ?.
    ×

    Success!

    Help @tgrk35 spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.17,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @nearjob,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,
    )...