samoht Posted February 8, 2011 Share Posted February 8, 2011 hello I am trying to print a query out into a table that is grouped and styled by date. If the same date appears I want to style the row similar. Here is what I have: <table class="signup-sched"> <tr><th>signup date</th><th>Name</th><th>Notes</th></tr> <?php $myrows = $wpdb->get_results("SELECT `datefield-256` as signupdate, `your-name` as name, `your-email` as email, `your-message` as notes FROM wp_cc3_signup WHERE DATE_FORMAT(`datefield-256`, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') ORDER BY `datefield-256`"); $sd = ''; if(count($myrows)){ foreach($myrows as $m){ if ($m->signupdate != $sd){ if ($sd != ''){ echo '<tr class="b"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; } echo '<tr class="n"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; $sd = $m->signupdate; } } } ?> </table> What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/ Share on other sites More sharing options...
Maq Posted February 8, 2011 Share Posted February 8, 2011 Is there an error? What is happening, or not happening? Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171494 Share on other sites More sharing options...
samoht Posted February 8, 2011 Author Share Posted February 8, 2011 sorry - no error, but the output is coming out in correct I get: <table class="signup-sched"> <tr><th>signup date</th><th>Name</th><th>Notes</th></tr> <tr class="n"><td>2011-02-02</td><td>Jendy</td><td></td></tr> <tr class="b"><td>2011-02-09</td><td>Sonya Adcock</td><td></td></tr> <tr class="n"><td>2011-02-09</td><td>Sonya Adcock</td><td></td></tr> <tr class="b"><td>2011-02-16</td><td>Sonya Adcock</td><td></td></tr> <tr class="n"><td>2011-02-16</td><td>Sonya Adcock</td><td></td></tr> <tr class="b"><td>2011-02-23</td><td>Sonya Adcock</td><td></td></tr> <tr class="n"><td>2011-02-23</td><td>Sonya Adcock</td><td></td></tr></table> which should look like: <table class="signup-sched"> <tr><th>signup date</th><th>Name</th><th>Notes</th></tr> <tr class="n"><td>2011-02-02</td><td>Jendy</td><td></td></tr> <tr class="b"><td>2011-02-09</td><td>Sonya Adcock</td><td></td></tr> <tr class="b"><td>2011-02-09</td><td>Heather Harer</td><td></td></tr> <tr class="n"><td>2011-02-16</td><td>Sonya Adcock</td><td></td></tr> <tr class="b"><td>2011-02-23</td><td>Sonya Adcock</td><td></td></tr></table> for some reason my loop is including more than is in the table - and not including some of the names? Also - the loop is simply alternating "n" and then "b" rather than grouping by date Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171505 Share on other sites More sharing options...
samoht Posted February 8, 2011 Author Share Posted February 8, 2011 Is there a better way to right the conditions on the loop so that it groups by similar date? Maybe something like: $sd = ''; if(count($myrows)){ foreach($myrows as $m){ if ($m->signupdate != $sd){ if ($sd != ''){ echo '<tr class="not"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; } echo '<tr class="not"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; $sd = $m->signupdate; }else if ($m->signupdate === $sd){ echo '<tr class="does"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171574 Share on other sites More sharing options...
samoht Posted February 8, 2011 Author Share Posted February 8, 2011 This is as close as I can get: <?php $myrows = $wpdb->get_results("SELECT `datefield-256` as signupdate, `your-name` as name, `your-email` as email, `your-message` as notes FROM wp_cc3_signup WHERE DATE_FORMAT(`datefield-256`, '%Y%m') = DATE_FORMAT(CURDATE(), '%Y%m') ORDER BY `datefield-256`"); $sd = ''; if(count($myrows)){ foreach($myrows as $m){ if ($m->signupdate != $sd){ if($sd != '') echo '<tr class="g"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; else echo '<tr class="c"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; $sd = $m->signupdate; }else if ($m->signupdate === $sd){ echo '<tr class="f"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes.'</td></tr>'; } } } ?> This outputs: <table class="signup-sched"><tr><th>signup date</th><th>Name</th><th>Notes</th></tr> <tr class="c"><td>2011-02-02</td><td>Jendy</td><td></td></tr> <tr class="g"><td>2011-02-09</td><td>Sonya Adcock</td><td></td></tr> <tr class="f"><td>2011-02-09</td><td>Heather Harer</td><td></td></tr> <tr class="g"><td>2011-02-16</td><td>Sonya Adcock</td><td></td></tr> <tr class="g"><td>2011-02-23</td><td>Sonya Adcock</td><td></td></tr></table> notice the "class" of the tr's and the date I'm trying to get the class to change when the date changes. I only need two different classes but I gave each echo a different class for debugging Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171595 Share on other sites More sharing options...
Psycho Posted February 8, 2011 Share Posted February 8, 2011 if(count($myrows)) { $lastDate = false; foreach($myrows as $m) { //Date has changed, change class if($lastDate != $m->signupdate) { $class = ($class=='class1') ? 'class2' : 'class1'; $lastDate = $m->signupdate; } echo '<tr class="{$class}"><td>'.$m->signupdate.'</td><td>'.$m->name.'</td><td>'.$m->notes."</td></tr>\n"; } } Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171598 Share on other sites More sharing options...
samoht Posted February 9, 2011 Author Share Posted February 9, 2011 Thanks mjdamato! I had to change it to ... echo '<tr class="'.$class.'"><td>'. ... for some reason but this is what I was trying to accomplish. Quote Link to comment https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/#findComment-1171608 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.