Thinkphp Association query

Source: Internet
Author: User
Thinkphp Association query

There are two tables: Hospital r_hospital and department r_department. After you select a hospital, the department displays only the Department of the hospital, but not the department of other hospitals.


Reply to discussion (solution)

Post your code

Post your code
Class RoomAction extends Action {
Public function room (){
$ Room = M ('Department ');
$ H_id = I ('Get. h_id ');
$ H_name = I ('Get. h_name ');
Print_r ($ h_id );
Print_r ($ h_name );
$ Data = $ _ POST ['text'];
Print_r ($ data );
If ($ data! = ''){
// Execute the SQL statement (execute like query, search)
$ Map ['name'] = array ('like', "% $ data % ");
$ List = $ room-> where ($ map)-> select ();
$ This-> list = $ list;
$ This-> display ();
} Else {
$ Map ['hospital _ id'] = $ h_id;
$ List = $ room-> where ($ map)-> select ();
$ This-> assign ("list", $ list );
$ This-> display ();
}
}

Join queries (multi-table queries) in THINKPHP can use the table () method or join method. see example 1. table () $ list = $ user-> table ('User _ status stats, user_profile profile ')-> where ('stats. id = profile. typeid')-> field ('stats. id as id, stats. display as display, profile. title as title, profile. content as content ')-> order ('stats. id desc ')-> select (); 2. join () $ user = new Model ('user '); $ list = $ user-> join ('right JOIN user_profile ON user_stats.id = user_profile.typeid '); 3. native query $ Model = new Model (); $ SQL = 'SELECT. id,. title, B. content from think_test1 as a, think_test2 as B where. id = B. ID '. $ map. 'order by. ID '. $ sort. 'limit '. $ p-> firstRow. ','. $ p-> listRows; $ voList = $ Model-> query ($ SQL );

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.