Query the database and display the content in arrays by conditions. how can this problem be solved?

Source: Internet
Author: User
Query the database and display the content in arrays by conditions. how can this problem be solved? Because of the website business, the order content contains N different names, mobile phone gender, and so on. when uploading a form, it is uploaded in an array. now you need to display a total order, do I have to query the output and display of each order by order number? I am a newbie, so I would like to ask you here!

The database structure is as follows:

Id name shouji sex zhuangtai orderid
1 Zhang San 138XXX male 1 1
2 Li Si 186XXX female 1 1
3 Wang Wu 172XXX male 1 1
4 Xiaobao 189XXX male 1 2
5 Shao Zhuang 186XXX male 1 2
6 sweet 133XXX female 2 3
7 Dabao xxx male 3 4
......................

As you can see, all the data is stored in the database according to this structure. each order number only has the same orderid field, and others are different!

Now, you need to output these orders on a page and display them as follows. what should I do? Could you write a method and example? Thank you!


Reply to discussion (solution)

That's not easy.
Put the data read from the database into the array for processing

while($arr = mysql_fetch_row($result)){   $row[$arr['orderid']][] = $arr;}

That's not easy.
Put the data read from the database into the array for processing

PHP code? 1234 while ($ arr = mysql_fetch_row ($ result) {$ row [$ arr ['orderid'] [] = $ arr ;}

Hello, thank you very much for your answer. can you be more specific? I really don't understand PHP or databases at all.

You can write it like this.

$ R = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex ') as sex from 'order' group by orderid'); echo"
 
 
  
  
"; Echo"
  
  "; While ($ row = mysql_fetch_assoc ($ r) {$ shouji_arr = explode (',', $ row ['shouji ']); $ sex_arr = explode (', ', $ row ['sex']); echo" 
   "; Foreach (explode (',', $ row ['name']) as $ k => $ v) {echo" 
   
     "; Echo" 
    "; Echo" 
   ";}} Echo" 
  
Name Mobile phone Gender
Order Number: ". $ row [orderid]." status: ". $ row ['zhuangtai']."
$ V $ Shouji_arr [$ k] $ Sex_arr [$ k]
";

You can write it like this.
PHP code? 1234567891011121314 $ r = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex ') as sex from 'order' group ......


The explode () function separates strings into arrays.

You can write it like this.
PHP code? 1234567891011121314 $ r = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex ') as sex from 'order' group ......


Group_concat (), which returns a string with a non-NULL value from a group of connections.

Definition and usage
The explode () function separates strings into arrays.

Syntax
Explode (separator, string, limit)

Parameter description

Separator is required. Specifies where to split the string.

String is required. The string to be split.

Limit is optional. Specifies the maximum number of returned array elements.


It seems that explode separates the columns in the string ~! Right?

You can write it like this.
PHP code? 1234567891011121314 $ r = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex ') as sex from 'order' group ......

Thank you very much, boss. what should I do if I add a time field in this table and arrange it in chronological order?

You can write it like this.
PHP code? 1234567891011121314 $ r = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex ') as sex from 'order' group ......

Hello boss. Next, if the time of each ORDER (that is, the line with the same orderid) is the same, I can directly add ORDER BY time in the query statement to solve the problem, but what if the time of different rows in an order is different and arranged in the minimum time?

Reference the reply from jordan102 on the third floor: you can write it like this.
PHP code? 1234567891011121314 $ r = mysql_query ('select orderid, zhuangtai, group_concat ('name') as name, group_concat ('shouji ') as shouji, group_concat ('sex') as se ......

I used order by min (time) to query. I don't know if there is any problem? If there is a big data table, will it be more efficient?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.