PHP并输出一对多结果 [英] PHP and outputting one-to-many results

查看:145
本文介绍了PHP并输出一对多结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

到目前为止,我只处理了php中的一对一关系,但我遇到了一个涉及一对多关系的问题。我一直坐在这里几天没有运气,所以我绝望有人在我失去之前介入并告诉我一个解决方案。



在我的数据库中有一系列的urls,它们被SELECT查询以及来自不同表的各种其他字段接收。每个网址至少有一个类别与其相关联,但可以有多个类别。所以在我的结果中,我可能会看到一些看起来有点像这样:

  link_id = 3 url ='http:// www .somesite1.com'category ='uncategorised'
link_id = 4 url​​ ='http://www.somesite2.com'category ='travel'
link_id = 4 url​​ ='http:// www .somesite2.com'category ='fun'
link_id = 4 url​​ ='http://www.somesite2.com'category ='misc'
link_id = 3 url ='http:// www .somesite3.com'category ='uncategorised'

我有这个工作,当我循环并打印它们,使用一个while循环和mysql fetch数组,结果看起来完全像上面。这是伟大的,除了我需要的是它读的东西像:

  link_id = 4 url​​ ='http: .somesite2.com'category ='travel fun misc'

url以某种方式结合,因为他们被打印出来。我的第一个尝试让我尝试嵌套while循环,但它没有工作,我不知道这是否可行。除此之外,我想知道我是否可能需要一个多维数组(完全猜测,我从来没有使用过一个)。



我排序这些结果通过链接id如上,所以我知道如果链接id在当前循环迭代,匹配一个在上次迭代 - 然后我有一个东西,有多个类别..我想我真的很接近,但我只是

解决方案



在mysql中还有GROUP_CONCAT函数。



如下:

  SELECT url,GROUP_CONCAT(category)AS categories FROM yourtable GROUP BY url 


I've only dealt with one-to-one relationships in php so far, but I'm stuck on a problem which involves a one-to-many relationship. I've been sitting on this for a few days with no luck, so I'm desperate for someone to step in and show me a solution before I lose my mind.

In my database have a series of urls, which are received by a SELECT query along with various other fields, from different tables. Every url has at least one category associated with it, but can have multiple categories. So in my results I might see something that looks a bit like this:

link_id = 3   url= 'http://www.somesite1.com'   category = 'uncategorised'
link_id = 4   url= 'http://www.somesite2.com'   category = 'travel'
link_id = 4   url= 'http://www.somesite2.com'   category = 'fun'
link_id = 4   url= 'http://www.somesite2.com'   category = 'misc'
link_id = 3   url= 'http://www.somesite3.com'   category = 'uncategorised'

I have got this to work, kind of. When I loop through and print them off, using a while loop and mysql fetch array, the result looks exactly like it does above. Thats great, except what I need is for it to read something like:

link_id = 4   url = 'http://www.somesite2.com'   category = 'travel fun misc'

So that basically all of the categories for each url get combined somehow, as they are printed out. My first attempt led me to try a nested while loop, but it didn't work and i'm not sure if this is feasible. Apart from that I'm wondering if I might need a multidimensional array (complete guess, i've never had to use one before).

I'm ordering these results by link id as above, so I know if the link id in the current loop iteration, matches the one in the last iteration - then I have something which has more than one category.. I think I'm really close, but I just can't figure it out.

Any ideas?

解决方案

There is also the "GROUP_CONCAT" function in mysql. That should do exactly what you want to achieve.

Something like :

SELECT url, GROUP_CONCAT(category) AS categories FROM yourtable GROUP BY url 

这篇关于PHP并输出一对多结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆