MYSQL Query statment to replace PHP loops -



MYSQL Query statment to replace PHP loops -

suppose have next tables :

projects( project_id , project_name ) documents( document_id,document_name , project_id)

which kind of top-down hierarchically , utilize select statement , loops generate nested array ex.:

$result_array = array(); $projects = mysql_query(' select * projets') ; $i = 0 ; while( $row_proj = mysql_fetch_assoc($projects) ) { $result_array[$i] = $row_proj ; $documents = mysql_query('select * documents project_id='.$row['project_id']); $doc_res = array(); while( $row_doc = mysql_fetch_assoc($documents) { $doc_res[] = $row_doc; } $result_array[$i]['documents'] = $row_doc ; $i++; } .. , on

the result nesteed array in view can print :

foreach( $projects $project ) { echo $project['project_name']; foreach( $project['documents'] $document ) { echo .. } }

how can generate using simple flat loop using sql statment or other thing , way seem not best 1 , utilize bring together not generate things nested array .

if understand thing want accomplish do:

select * documents inner bring together projects on projects.id = documents.project_id

then iterate through of documents grouping them in arrays project_id php. have 1 query again. possible mysql loop rather ugly thing , above illustration won't give advantage.

php mysql multidimensional-array

Comments

Popular posts from this blog

iphone - Dismissing a UIAlertView -

intellij idea - Update external libraries with intelij and java -

javascript - send data from a new window to previous window in php -