Jump to content

Recommended Posts

Hey everyone,

 

I have a database for projects and each entry has one or more project type category that it falls into. The values are separated with a semi-colon to make the distinction. I've used the following code to output the data onto the page:

 

$sql = "SELECT project_type FROM projects ORDER BY project_type";
$result = mysql_query($sql);


while ($rs = mysql_fetch_array($result)) {
$database_value = $rs['project_type'];
$array = explode(';', $database_value);

foreach($array as $key => $value){
	echo " ".$value."<br />";  
}


}

 

This outputs the following:

 

Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Export Development
Agriculture
Impact Assessments
Impact Studies
Agriculture
M & E
Agriculture
Private Sector Development
Corporate and Public Governance
Corporate and Public Governance
Corporate and Public Governance

 

In PHP, how can I group these values together, similar to SQL's GROUP BY method? I've tried grouping it in the query but some of the data goes lost because it must be exploded first.

 

Can anybody help?

 

 

Thanks in advance!

 

Karen

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/
Share on other sites

As much as I hate sounding like a jerk for saying so: if your database was designed properly you wouldn't have run into this problem.  You should have made this into a cross-reference table so you can do your many-to-many relationships without resorting to storing a delimited string (which as you've discovered is difficult to maintain and display).

 

To solve your immediate problem: put all these items into a temporary array then call array_count_values

 

-Dan

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/#findComment-1266868
Share on other sites

Thanks for the reply. @dan - this is not my database. I inherited it from someone else so I need to make it work in its current state. There is no time or budget to rebuild.

 

@muddy_funster - correct. I would like each one to only display once. Any ideas?

 

 

Regards,

Karen

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/#findComment-1266883
Share on other sites

Thanks, I tried your code but I'm still getting this:

 

Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Agriculture
Export Development
Agriculture
Impact Assessments
Impact Studies
Agriculture
M & E
Agriculture
Private Sector Development
Corporate and Public Governance
Corporate and Public Governance
Corporate and Public Governance

 

Am I doing something wrong?

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/#findComment-1266906
Share on other sites

ManiacDan already gave you a solution. Although I would think array_unique() would be a better fit for this scenario

 

Not tested:

$sql = "SELECT project_type FROM projects ORDER BY project_type";
$result = mysql_query($sql);

//Dump results into array
$project_types = array();
while ($rs = mysql_fetch_array($result))
{
    $project_types = array_merge($project_types, explode(';', $rs['project_type']));
}

//Filter out duplicates
$project_types = array_unique($project_types);

//Display results
foreach($project_types as $type)
{
    echo " {$type}<br />\n";  
}

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/#findComment-1266913
Share on other sites

If you want only unique items, array_unique() is the solution.

 

If you want the items grouped by count, array_count_values() is the solution.

 

Since you said you wanted it similar to GROUP BY (which implies a count) rather than UNIQUE (which implies uniqueness), I gave you the GROUP BY solution.

 

-Dan

Link to comment
https://forums.phpfreaks.com/topic/246705-grouping-arrays/#findComment-1266926
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.