Jump to content

Recommended Posts

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?

Link to comment
https://forums.phpfreaks.com/topic/227080-group-foreach-output-by-date/
Share on other sites

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

 

 

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>';
	}
}
}

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

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";
    }
}

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.