brooksie Posted October 13, 2008 Share Posted October 13, 2008 Hi, hopefully somebody can help me, prob pretty simple but its hurting my head. basically i have a SQL database with 5 colums and one being the date. my query calls the date as month. i want to create a loop so it will print the month then create an html table with all the data from that month, end the table then go and do the same thing but with the next month. Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/ Share on other sites More sharing options...
CMK08 Posted October 13, 2008 Share Posted October 13, 2008 Off the top of my head, it sounds like you will have to create an array or arrays to get that to happen. I am not sure about the syntax, but I did something similar once when I needed to create a list of cities in a given country. I created the content array first - example: $us = array ('1'=>'Los Angeles', '2'=>'New York'...) $brasil=array('1'=>'Sao Paulo', '2'=>'Rio de Janeiro'...) and finally $country = array($us, $brasil) Then you will have to create a foreach loop inside another foreach loop in order to display things as you described... foreach (country array){ title of the table goes here foreach (city array){ table syntax goes here } } This should get you started. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-664296 Share on other sites More sharing options...
Maq Posted October 13, 2008 Share Posted October 13, 2008 You need to create something like this pseudo: query for dates... while dates... start table... query for other info WHERE date = $query['date_from_other_table']... rows, cols, info inside them... close while... close table... close while... Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-664327 Share on other sites More sharing options...
brooksie Posted October 13, 2008 Author Share Posted October 13, 2008 ok i think that makes sense il give it a go and let you know how i get on. thanks for your help Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-664451 Share on other sites More sharing options...
Maq Posted October 13, 2008 Share Posted October 13, 2008 Sure. If you post code it will be much easier to help. Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-664516 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 is there any way i can call the previous part of the array? so basically i have while ($row = mysql_fetch_array($result)) { if($row['month'] == previous_month_in_array ) { ...[prints data]... } else { ...[new table]... } } // or maybe i should just have one big table as im confusing myself ??? Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665396 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Are you basically trying to display all the months, then under each month have the data for it? Like: January --------- blah blah blah blah blah blah February --------- blah blah blah blah blah blah March --------- blah blah blah blah blah blah Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665411 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 yes mate, sorry i didnt make it clear. all this is coming from a sql database Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665415 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 MySQL? Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665417 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 yep oops took the my off as i thought it was just sql :$ Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665421 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 while ($row = mysql_fetch_array($result)) { ?> </pre> <table> </table> <b Does this help? Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665430 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 along them lines yes. but there is quite a few with the same dates. i did try a for loop so it runs throught all the dates using mysql select where month = $i but obviously it wouldnt take into account the year what i want is <h1>Jan</h1> <table> <th>header</th> <tr><td>data</td></tr> <tr><td>data</td></tr> <tr><td>data</td></tr> </table> <h1>Feb</h1> <table> <th>header</th> <tr><td>data</td></tr> <tr><td>data</td></tr> <tr><td>data</td></tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665454 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 Is your date in date_time format? Cause that would be easy to compare the month with the year. Also, if you want that format, put it in then... </pre> <table> </ Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665482 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 really appriciate your time on this maq, the month comes back 1 - 12 but ive set a function using the mktime() function to echo January February etc.. Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665494 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 So call the function on the query return. Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665506 Share on other sites More sharing options...
brooksie Posted October 14, 2008 Author Share Posted October 14, 2008 but that doesnt solve the problem i want a loop that lists all the data for say january in one table then jumps to next month and does same thing Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665557 Share on other sites More sharing options...
Maq Posted October 14, 2008 Share Posted October 14, 2008 i want a loop that lists all the data for say january in one table then jumps to next month and does same thing That's the point of the while loop. So however many months you have in the database is how many tables are going to be generated. Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-665638 Share on other sites More sharing options...
brooksie Posted October 15, 2008 Author Share Posted October 15, 2008 i got it working, but not the proper way.. <?php $prev = false; while($row = mysql_fetch_array($result)) //while loop to fetch array { if($prev != $row['month']) { echo "</table><h2>"; month($row['month']);//<-- function for format as JANUARY FEBRUARY ETC.... echo "</h2>\n"; echo "<table>\n<th>Date</th><th>Account</th><th>Deposit</th><th>Withdrawn</th>"; $prev = $row['month']; } echo "\n<tr>"; echo "<td>".$row['date']."</td>"; echo "<td>".$row['account']."</td>"; echo "<td>£".$row['deposit']."</td>"; echo "<td>£".$row['withdraw']."</td>"; echo "<td class =\"hidden\">Delete</td>"; echo "</tr>\n"; } ?> as you can see i have to end the table within the if statement, so i have an extra end table in code not sure how to break out the bottom code to end table appriciate any suggestions Quote Link to comment https://forums.phpfreaks.com/topic/128259-beginer-help-creating-a-loop-for-multiple-html-tables-for-each-month/#findComment-666399 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.