add rowspan if previous value equals current value php while

I'm trying to add a rowspan to table cell if the previous date is equal to the current row's date. I have it now so that it doesn't display the date, but I still need it to add a rowspan and that is what I'm stuck on.


$getOpps = mysql_query("SELECT id, group_id, group_name, event_date, max_size, day, meeting_time, avaliable_seats FROM occ_schedule WHERE location_id = '".$location."' ORDER BY event_date") or ("Critical Error! Cannot pull volunteer opportunities"); 

if(mysql_num_rows($getOpps) > 0)
    {
    $prevDate = '';

    while($oppData = mysql_fetch_assoc($getOpps))
        {
        //Check registrations
        $checkReg = mysql_query("SELECT COUNT(id) as totalRegistered FROM occ_volunteers WHERE group_id = '".$oppData['group_id']."'") or die("Critical Error! Cannot check registrations.");

        if(mysql_num_rows($checkReg) > 0)
            {
            while($regData = mysql_fetch_assoc($checkReg))
                {
                if($regData['totalRegistered'] > 0)
                    {
                    $totalRegistered = $regData['totalRegistered'];
                    }

                else
                    {
                    $totalRegistered = '0';
                    }
                }
            }

        $altrow = 'class="alt1"';

        $currentDate = $oppData['event_date'];

        $seatsAvailable = ($oppData['avaliable_seats'] - $totalRegistered);

        if($seatsAvailable > 0)
            {
            $link = '<a href="registerNow.php?id='.$oppData['group_id'].'">Volunteer Now</a>';
            }

        else
            {
            $link = 'All spots have been filled.';
            }

        echo '<tr>';


        if($currentDate == $prevDate)
            {
            echo '<td>&nbsp;</td>';
            }

        else
            {
            echo '<td ' . $altrow . '>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
            }

        echo '
                <td ' . $altrow . '>'.$oppData['meeting_time'] .'</td>
                <td ' . $altrow . '>'.$totalRegistered . ' of ' . $oppData['avaliable_seats'] .' ' . $link . '</td>
             </tr>';

        $prevDate = $currentDate;
        }
    }

Open in new window

LVL 1
t3chguyAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
I'll take a look at it.  While I do that, please read this article.  MySQL is dead and to avoid having your script become dead, you will need to make a change.
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/PHP_Databases/A_11177-PHP-MySQL-Deprecated-as-of-PHP-5-5-0.html
0
GaryCommented:
Do you mean here?

       if($currentDate == $prevDate)
            {
            echo '<td>&nbsp;</td>';
            }


Just change to and add the colspans amount
 echo '<td colspan="">&nbsp;</td>';
0
t3chguyAuthor Commented:
echo '<td ' . $altrow . '>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';

Open in new window


^That cell there needs to have the rowspan added to it and this one...

<td colspan="">&nbsp;</td>'; 

Open in new window


should no longer exist because the date has been carried down.
0
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

GaryCommented:
Replace that whole if else block with

$currentDate == $prevDate ? $cols=' colspan="2"' : $cols='';

echo '<td ' . $altrow . $cols . '>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';

Open in new window

0
t3chguyAuthor Commented:
I need the rowspan to be set though. and if I use your code and change the colspan to rowspan it messes up the table view even more.
0
GaryCommented:
My mistake, just change it to rowspan.

$currentDate == $prevDate ? $rowspan=' rowspan="2"' : $rowspan='';

echo '<td ' . $altrow . $rowspan. '>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';

Open in new window

0
t3chguyAuthor Commented:
This is the result of that code.

Screenshot
0
t3chguyAuthor Commented:
This is the ultimate goal of the page.

result
0
GaryCommented:
$currentDate == $prevDate ? $rowspan=' rowspan="2"' : $rowspan='';

if($currentDate != $prevDate){

echo '<td ' . $altrow . $rowspan. '>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
}

Open in new window


Yeah I realised where it was going wrong.
0
t3chguyAuthor Commented:
Closer
We are closer now.
0
GaryCommented:
I'm hacking it in reverse, so everything is rowspan=2 unless it is the same date - I think my logic is right...
$getOpps = mysql_query("SELECT id, group_id, group_name, event_date, max_size, day, meeting_time, avaliable_seats FROM occ_schedule WHERE location_id = '".$location."' ORDER BY event_date") or ("Critical Error! Cannot pull volunteer opportunities"); 

if(mysql_num_rows($getOpps) > 0)
    {
    $prevDate = '';

    while($oppData = mysql_fetch_assoc($getOpps))
        {
        //Check registrations
        $checkReg = mysql_query("SELECT COUNT(id) as totalRegistered FROM occ_volunteers WHERE group_id = '".$oppData['group_id']."'") or die("Critical Error! Cannot check registrations.");

        if(mysql_num_rows($checkReg) > 0)
            {
            while($regData = mysql_fetch_assoc($checkReg))
                {
                if($regData['totalRegistered'] > 0)
                    {
                    $totalRegistered = $regData['totalRegistered'];
                    }

                else
                    {
                    $totalRegistered = '0';
                    }
                }
            }

        $altrow = 'class="alt1"';

        $currentDate = $oppData['event_date'];

        $seatsAvailable = ($oppData['avaliable_seats'] - $totalRegistered);

        if($seatsAvailable > 0)
            {
            $link = '<a href="registerNow.php?id='.$oppData['group_id'].'">Volunteer Now</a>';
            }

        else
            {
            $link = 'All spots have been filled.';
            }

        echo '<tr>';

	$currentDate != $prevDate ? $rowspan=' rowspan="2"' : $rowspan='';

	if($currentDate != $prevDate){
		echo '<td ' . $altrow . 'rowspan="2">' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
	}

        echo '
                <td ' . $altrow . $rowspan . '>'.$oppData['meeting_time'] .'</td>
                <td ' . $altrow . $rowspan . '>'.$totalRegistered . ' of ' . $oppData['avaliable_seats'] .' ' . $link . '</td>
             </tr>';

        $prevDate = $currentDate;
        }
    }

Open in new window

0
t3chguyAuthor Commented:
Here is what that produces now:

Result
0
GaryCommented:
Can you post the HTML, that screen shot looks like its not terminating the <TR>
0
t3chguyAuthor Commented:
<table width = "750px" cellspacing = "0" cellpadding = "0">
			<tr>	
				<td class="tblHeader">Date</td>
				<td class="tblHeader">Time</td>
				<td class="tblHeader">Need</td>
			</tr><tr><td class="alt1"rowspan="2">Monday, Nov 18</td>
                <td class="alt1" rowspan="2">11am-1pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC1">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC2">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Tuesday, Nov 19</td>
                <td class="alt1" rowspan="2">11am-1pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC3">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC4">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Wednesday, Nov 20</td>
                <td class="alt1" rowspan="2">11am-1pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC5">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC6">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Thursday, Nov 21</td>
                <td class="alt1" rowspan="2">11am-1pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC7">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 3 <a href="registerNow.php?id=SCC8">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Friday, Nov 22</td>
                <td class="alt1" rowspan="2">4pm-6pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC10">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC9">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Saturday, Nov 23</td>
                <td class="alt1" rowspan="2">10am-1pm</td>
                <td class="alt1" rowspan="2">0 of 2 <a href="registerNow.php?id=SCC11">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">1pm-3pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC12">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Sunday, Nov 24</td>
                <td class="alt1" rowspan="2">1pm-4pm</td>
                <td class="alt1" rowspan="2">0 of 5 <a href="registerNow.php?id=SCC13">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="2">Monday, Nov 25</td>
                <td class="alt1" rowspan="2">11am-1pm</td>
                <td class="alt1" rowspan="2">0 of 3 <a href="registerNow.php?id=SCC14">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 4 <a href="registerNow.php?id=SCC15">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">5:30PM</td>
                <td class="alt1">0 of 3 <a href="registerNow.php?id=SCC16">Volunteer Now</a></td>
             </tr></table>

Open in new window

0
GaryCommented:
Gonna have to store the rows in a var til you have made one pass, think I've got the logic correct here

$getOpps = mysql_query("SELECT id, group_id, group_name, event_date, max_size, day, meeting_time, avaliable_seats FROM occ_schedule WHERE location_id = '".$location."' ORDER BY event_date") or ("Critical Error! Cannot pull volunteer opportunities"); 

if(mysql_num_rows($getOpps) > 0)
    {
    $prevDate = '';
    $currow="";
    while($oppData = mysql_fetch_assoc($getOpps))
        {
        //Check registrations
        $checkReg = mysql_query("SELECT COUNT(id) as totalRegistered FROM occ_volunteers WHERE group_id = '".$oppData['group_id']."'") or die("Critical Error! Cannot check registrations.");

        if(mysql_num_rows($checkReg) > 0)
            {
            while($regData = mysql_fetch_assoc($checkReg))
                {
                if($regData['totalRegistered'] > 0)
                    {
                    $totalRegistered = $regData['totalRegistered'];
                    }

                else
                    {
                    $totalRegistered = '0';
                    }
                }
            }

        $altrow = 'class="alt1"';

        $currentDate = $oppData['event_date'];

        $seatsAvailable = ($oppData['avaliable_seats'] - $totalRegistered);

        if($seatsAvailable > 0)
            {
            $link = '<a href="registerNow.php?id='.$oppData['group_id'].'">Volunteer Now</a>';
            }

        else
            {
            $link = 'All spots have been filled.';
            }


	if($currentDate == $prevDate ){
		$currow=str_replace('spantemp',' rowspan="2"',$currow);
	}
	else {
		echo str_replace('spantemp','',$currow);
		$currow = "";
	}

        $currow .=  '<tr>';


	if($currentDate != $prevDate){
		$currow .=  '<td ' . $altrow . 'spantemp>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
	}

        $currow .=  '
                <td ' . $altrow . '>'.$oppData['meeting_time'] .'</td>
                <td ' . $altrow . '>'.$totalRegistered . ' of ' . $oppData['avaliable_seats'] .' ' . $link . '</td>
             </tr>';

        $prevDate = $currentDate;
        }
    }
	echo str_replace('spantemp','',$currow);

Open in new window

0
t3chguyAuthor Commented:
Close! The only issue now is the last record (the third) for the 25th moved to the left column where it should be apart of the rest.
0
t3chguyAuthor Commented:
Close!
0
GaryCommented:
Can you have more than 2 rows? That's what it looks like from the last part
0
t3chguyAuthor Commented:
Yes...there is just the one case where I have 3 rows.
0
GaryCommented:
Try this, I'm not quite sure its right but I will be leaving soon

$getOpps = mysql_query("SELECT id, group_id, group_name, event_date, max_size, day, meeting_time, avaliable_seats FROM occ_schedule WHERE location_id = '".$location."' ORDER BY event_date") or ("Critical Error! Cannot pull volunteer opportunities"); 

if(mysql_num_rows($getOpps) > 0)
    {
    $prevDate = '';
    $currow="";
    $rowcounter=0;
    while($oppData = mysql_fetch_assoc($getOpps))
        {
        //Check registrations
        $checkReg = mysql_query("SELECT COUNT(id) as totalRegistered FROM occ_volunteers WHERE group_id = '".$oppData['group_id']."'") or die("Critical Error! Cannot check registrations.");

        if(mysql_num_rows($checkReg) > 0)
            {
            while($regData = mysql_fetch_assoc($checkReg))
                {
                if($regData['totalRegistered'] > 0)
                    {
                    $totalRegistered = $regData['totalRegistered'];
                    }

                else
                    {
                    $totalRegistered = '0';
                    }
                }
            }

        $altrow = 'class="alt1"';

        $currentDate = $oppData['event_date'];

        $seatsAvailable = ($oppData['avaliable_seats'] - $totalRegistered);

        if($seatsAvailable > 0)
            {
            $link = '<a href="registerNow.php?id='.$oppData['group_id'].'">Volunteer Now</a>';
            }

        else
            {
            $link = 'All spots have been filled.';
            }


	if($currentDate == $prevDate ){
		$rowcounter++;
	}
	elseif($rowcounter!=0) {
		echo str_replace('spantemp','rowspan="'.$rowcounter.'"',$currow);
		$currow = "";
		$rowcounter=0;
	}
	else{
		echo str_replace('spantemp','',$currow);
		$currow = "";
		$rowcounter=0;
	}

        $currow .=  '<tr>';


	if($currentDate != $prevDate){
		$currow .=  '<td ' . $altrow . 'spantemp>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
	}

        $currow .=  '
                <td ' . $altrow . '>'.$oppData['meeting_time'] .'</td>
                <td ' . $altrow . '>'.$totalRegistered . ' of ' . $oppData['avaliable_seats'] .' ' . $link . '</td>
             </tr>';

        $prevDate = $currentDate;
        }
    }
	echo str_replace('spantemp','',$currow);

Open in new window

0
t3chguyAuthor Commented:
Yeah everything became misaligned with that last update.
0
GaryCommented:
Can you post the HTML, makes it easier to understand where it is going wrong rather than trying to wrack my brain.
0
t3chguyAuthor Commented:
<table width = "750px" cellspacing = "0" cellpadding = "0">
			<tr>	
				<td class="tblHeader">Date</td>
				<td class="tblHeader">Time</td>
				<td class="tblHeader">Need</td>
			</tr><tr><td class="alt1"rowspan="1">Monday, Nov 18</td>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC1">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC2">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="1">Tuesday, Nov 19</td>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC3">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC4">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="1">Wednesday, Nov 20</td>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC5">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC6">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="1">Thursday, Nov 21</td>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC7">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 3 <a href="registerNow.php?id=SCC8">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="1">Friday, Nov 22</td>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC10">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC9">Volunteer Now</a></td>
             </tr><tr><td class="alt1"rowspan="1">Saturday, Nov 23</td>
                <td class="alt1">10am-1pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC11">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">1pm-3pm</td>
                <td class="alt1">0 of 2 <a href="registerNow.php?id=SCC12">Volunteer Now</a></td>
             </tr><tr><td class="alt1">Sunday, Nov 24</td>
                <td class="alt1">1pm-4pm</td>
                <td class="alt1">0 of 5 <a href="registerNow.php?id=SCC13">Volunteer Now</a></td>
             </tr><tr><td class="alt1">Monday, Nov 25</td>
                <td class="alt1">11am-1pm</td>
                <td class="alt1">0 of 3 <a href="registerNow.php?id=SCC14">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">4pm-6pm</td>
                <td class="alt1">0 of 4 <a href="registerNow.php?id=SCC15">Volunteer Now</a></td>
             </tr><tr>
                <td class="alt1">5:30PM</td>
                <td class="alt1">0 of 3 <a href="registerNow.php?id=SCC16">Volunteer Now</a></td>
             </tr>
</table>

Open in new window

0
GaryCommented:
Think this it.

$getOpps = mysql_query("SELECT id, group_id, group_name, event_date, max_size, day, meeting_time, avaliable_seats FROM occ_schedule WHERE location_id = '".$location."' ORDER BY event_date") or ("Critical Error! Cannot pull volunteer opportunities"); 

if(mysql_num_rows($getOpps) > 0)
    {
    $prevDate = '';
    $currow="";
    $rowcounter=1;
    while($oppData = mysql_fetch_assoc($getOpps))
        {
        //Check registrations
        $checkReg = mysql_query("SELECT COUNT(id) as totalRegistered FROM occ_volunteers WHERE group_id = '".$oppData['group_id']."'") or die("Critical Error! Cannot check registrations.");

        if(mysql_num_rows($checkReg) > 0)
            {
            while($regData = mysql_fetch_assoc($checkReg))
                {
                if($regData['totalRegistered'] > 0)
                    {
                    $totalRegistered = $regData['totalRegistered'];
                    }

                else
                    {
                    $totalRegistered = '0';
                    }
                }
            }

        $altrow = 'class="alt1"';

        $currentDate = $oppData['event_date'];

        $seatsAvailable = ($oppData['avaliable_seats'] - $totalRegistered);

        if($seatsAvailable > 0)
            {
            $link = '<a href="registerNow.php?id='.$oppData['group_id'].'">Volunteer Now</a>';
            }

        else
            {
            $link = 'All spots have been filled.';
            }


	if($currentDate == $prevDate ){
		$rowcounter++;
	}
	elseif($rowcounter>1) {
		echo str_replace('spantemp',' rowspan="'.$rowcounter.'"',$currow);
		$currow = "";
		$rowcounter=1;
	}
	else{
		echo str_replace('spantemp','',$currow);
		$currow = "";
		$rowcounter=1;
	}

        $currow .=  '<tr>';


	if($currentDate != $prevDate){
		$currow .=  '<td ' . $altrow . 'spantemp>' . $oppData['day'] .', ' . date("M d", strtotime($oppData['event_date'])) . '</td>';
	}

        $currow .=  '
                <td ' . $altrow . '>'.$oppData['meeting_time'] .'</td>
                <td ' . $altrow . '>'.$totalRegistered . ' of ' . $oppData['avaliable_seats'] .' ' . $link . '</td>
             </tr>';

        $prevDate = $currentDate;
        }
    }
	if($rowcounter>1) {
	echo str_replace('spantemp',' rowspan="'.$rowcounter.'"',$currow);
}
else{
	echo str_replace('spantemp','',$currow);
}

Open in new window


(forgot my counter was starting at 0 - if this isn't right I will have to leave it til tomorrow)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
t3chguyAuthor Commented:
Excellent solution!  Thank you for all the work you put into it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.