I have a while loop that will loop for 2 times (Monday and Tuesday). Within the while loop I have a condition to check whether the first looped is "Monday", if is Monday it will check for the value and checked the corresponding checkbox.

The code work perfectly for Monday. But checkboxes of Tuesday is not echo at all.

<div class="form-group">
                                            <table>
                                                <tr>
                                                    <td style="width: 10%">&nbsp;</td>
                                                    <?php
                                                        //Get the list of time
                                                        $sqlTime="SELECT timeDesc FROM time";
                                                        $resultTime=mysql_query($sqlTime);
                                                        while($rowTime=mysql_fetch_array($resultTime))
                                                        {
                                                    ?>
                                                            <td style="text-align: center"><?php echo $rowTime['timeDesc'];?></td>
                                                    <?php
                                                        }
                                                    ?>
                                                </tr>
                                                <?php
                                                    //Get the list of day
                                                    $sqlDay="SELECT dayDesc FROM day";
                                                    $resultDay=mysql_query($sqlDay);
                                                    while($rowDay=mysql_fetch_array($resultDay))
                                                        {
                                                    ?>
                                                            <tr>
                                                            <td><?php echo $rowDay['dayDesc'];?></td>
                                                            <?php
                                                                //Get the list of time
                                                                $sqlTime="SELECT * FROM time";
                                                                $resultTime=mysql_query($sqlTime);
                                                                while($rowTime=mysql_fetch_array($resultTime))
                                                                {
                                                                    if($rowDay['dayDesc'] == 'Monday')
                                                                    {
                                                                        //Get tutor preferences for Monday
                                                                        $sqlGetMonday = mysql_query("SELECT tutorPreferencesMon FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetMonday  = mysql_fetch_array($sqlGetMonday);
                                                                        $tutorGetMonday = $resultGetMonday['tutorPreferencesMon'];
                                                                        $showMonday = $tutorGetMonday;
                                                                        $mondayArray = explode(',', $showMonday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedMonday = "";
                                                                            foreach($mondayArray as $monday_Array)
                                                                            {
                                                                               if($monday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedMonday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedMonday."></td>";
                                                                        }
                                                                    }//End if Monday

                                                                    elseif($rowDay['dayDesc'] == 'Tuesday')
                                                                    {
                                                                        //Get tutor preferences for Tuesday
                                                                        $sqlGetTuesday = mysql_query("SELECT tutorPreferencesTues FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetTuesday  = mysql_fetch_array($sqlGetTuesday);
                                                                        $tutorGetTuesday = $resultGetTuesday['tutorPreferencesTues'];
                                                                        $showTuesday = $tutorGetTuesday;
                                                                        $tuesdayArray = explode(',', $showTuesday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedTuesday = "";
                                                                            foreach($tuesdayArray as $tuesday_Array)
                                                                            {
                                                                               if($tuesday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedTuesday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedTuesday."></td>";
                                                                        }
                                                                    }//End if Tuesday

                                                                    elseif($rowDay['dayDesc'] == 'Wednesday')
                                                                    {
                                                                        //Get tutor preferences for Wednesday
                                                                        $sqlGetWednesday = mysql_query("SELECT tutorPreferencesWed FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetWednesday  = mysql_fetch_array($sqlGetWednesday);
                                                                        $tutorGetWednesday = $resultGetWednesday['tutorPreferencesWed'];
                                                                        $showWednesday = $tutorGetWednesday;
                                                                        $wednesdayArray = explode(',', $showWednesday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedWednesday = "";
                                                                            foreach($wednesdayArray as $wednesday_Array)
                                                                            {
                                                                               if($wednesday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedWednesday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedWednesday."></td>";
                                                                        }
                                                                    }//End if Wednesday

                                                                    elseif($rowDay['dayDesc'] == 'Thursday')
                                                                    {
                                                                        //Get tutor preferences for Thursday
                                                                        $sqlGetThursday = mysql_query("SELECT tutorPreferencesThurs FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetThursday  = mysql_fetch_array($sqlGetThursday);
                                                                        $tutorGetThursday = $resultGetThursday['tutorPreferencesThurs'];
                                                                        $showThursday = $tutorGetThursday;
                                                                        $thursdayArray = explode(',', $showThursday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedThursday = "";
                                                                            foreach($thursdayArray as $thursday_Array)
                                                                            {
                                                                               if($thursday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedThursday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedThursday."></td>";
                                                                        }
                                                                    }//End if Thursday

                                                                    elseif($rowDay['dayDesc'] == 'Friday')
                                                                    {
                                                                        //Get tutor preferences for Friday
                                                                        $sqlGetFriday = mysql_query("SELECT tutorPreferencesFri FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetFriday  = mysql_fetch_array($sqlGetFriday);
                                                                        $tutorGetFriday = $resultGetFriday['tutorPreferencesFri'];
                                                                        $showFriday = $tutorGetFriday;
                                                                        $fridayArray = explode(',', $showFriday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedFriday = "";
                                                                            foreach($fridayArray as $friday_Array)
                                                                            {
                                                                               if($friday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedFriday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedFriday."></td>";
                                                                        }
                                                                    }//End if Friday

                                                                    elseif($rowDay['dayDesc'] == 'Saturday')
                                                                    {
                                                                        //Get tutor preferences for Saturday
                                                                        $sqlGetSaturday = mysql_query("SELECT tutorPreferencesSat FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetSaturday  = mysql_fetch_array($sqlGetSaturday);
                                                                        $tutorGetSaturday = $resultGetSaturday['tutorPreferencesSat'];
                                                                        $showSaturday = $tutorGetSaturday;
                                                                        $saturdayArray = explode(',', $showSaturday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedSaturday = "";
                                                                            foreach($saturdayArray as $saturday_Array)
                                                                            {
                                                                               if($saturday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedSaturday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedSaturday."></td>";
                                                                        }
                                                                    }//End if Saturday

                                                                    elseif($rowDay['dayDesc'] == 'Sunday')
                                                                    {
                                                                        //Get tutor preferences for Sunday
                                                                        $sqlGetSunday = mysql_query("SELECT tutorPreferencesSun FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                        $resultGetSunday  = mysql_fetch_array($sqlGetSunday);
                                                                        $tutorGetSunday = $resultGetSunday['tutorPreferencesSun'];
                                                                        $showSunday = $tutorGetSunday;
                                                                        $sundayArray = explode(',', $showSunday);

                                                                        while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                        {       
                                                                            $checkedSunday = "";
                                                                            foreach($sundayArray as $sunday_Array)
                                                                            {
                                                                               if($sunday_Array == $rowTimeList['timeID'])
                                                                               {
                                                                                 $checkedSunday = "checked";
                                                                               }           
                                                                            }
                                                                            echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedSunday."></td>";
                                                                        }
                                                                    }//End if Sunday
                                                                }//End while
                                                            ?>
                                                            </tr>
                                                    <?php
                                                        }
                                                    ?>
                                            </table>
                                        </div>
share|improve this question
    
1  
your code is very complex. try to write what output you want and also snapshot of database. it will cleat the concept of your question and it will solve your problem – Aftab Ahmad Oct 26 '15 at 17:33

Your while loop changes variable $rowTime, while you're checking for the variable $rowDay. You may want to double-check that.

EDIT:

Use this code:

                                    <div class="form-group">
                                        <table>
                                            <tr>
                                                <td style="width: 10%">&nbsp;</td>
                                                <?php
                                                    //Get the list of time
                                                    $sqlTime="SELECT timeDesc FROM time";
                                                    $resultTime=mysql_query($sqlTime);
                                                    while($rowTime=mysql_fetch_array($resultTime))
                                                    {
                                                ?>
                                                        <td style="text-align: center"><?php echo $rowTime['timeDesc'];?></td>
                                                <?php
                                                    }
                                                ?>
                                            </tr>
                                            <?php
                                                //Get the list of day
                                                $sqlDay="SELECT dayDesc FROM day";
                                                $resultDay=mysql_query($sqlDay);
                                                while($rowDay=mysql_fetch_array($resultDay))
                                                    {
                                                ?>
                                                        <tr>
                                                        <td><?php echo $rowDay['dayDesc'];?></td>
                                                        <?php
                                                            //Get the list of time
                                                            $sqlTime="SELECT * FROM time";
                                                            $resultTime=mysql_query($sqlTime);
                                                            $preferences = array('Monday'    => 'tutorPreferencesMon',
                                                                                 'Tuesday'   => 'tutorPreferencesTues',
                                                                                 'Wednesday' => 'tutorPreferencesWed',
                                                                                 'Thursday'  => 'tutorPreferencesThurs',
                                                                                 'Friday'    => 'tutorPreferencesFri',
                                                                                 'Saturday'  => 'tutorPreferencesSat',
                                                                                 'Sunday'    => 'tutorPreferencesSun');
                                                            while($rowTime=mysql_fetch_array($resultTime))
                                                            {
                                                                $preferences = $preferences[$rowDay['dayDesc']];
                                                                $sqlGetMonday = mysql_query("SELECT ".$preferences." FROM tutorpreferences WHERE tutorID='$showTutorID'");
                                                                $resultGetMonday  = mysql_fetch_array($sqlGetMonday);
                                                                $tutorGetMonday = $resultGetMonday[$preferences];
                                                                $showMonday = $tutorGetMonday;
                                                                $mondayArray = explode(',', $showMonday);

                                                                while($rowTimeList=mysql_fetch_array($resultTimeList))
                                                                {       
                                                                    $checkedMonday = "";
                                                                    foreach($mondayArray as $monday_Array)
                                                                    {
                                                                       if($monday_Array == $rowTimeList['timeID'])
                                                                       {
                                                                         $checkedMonday = "checked";
                                                                       }           
                                                                    }
                                                                    echo "<td style='text-align: center'><input type='checkbox' name='".$rowDay['dayDesc']."[]' value='".$rowTimeList['timeID']."' ".$checkedMonday."></td>";
                                                                } 
                                                            }//End while
                                                        ?>
                                                        </tr>
                                                <?php
                                                    }
                                                ?>
                                        </table>
                                    </div>
share|improve this answer
    
Thanks for your response. But I am not quite sure about it. – beny lim Oct 27 '15 at 1:38
    
Sorry, about what? You just need to re-set $rowDay in the while according to $rowTime – Luis Ávila Oct 27 '15 at 1:45
    
Now I am structuring these content in a table format. $rowDay will consists of Monday, Tuesday... Sunday. $rowTime will consists of 9am, 10am .... 9pm. I am trying to ouput <td>Monday</td> all the way to Sunday. Then when it output Monday it will check through from 9am to 9pm. It will do for monday till sunday – beny lim Oct 27 '15 at 1:52
    
You're not understand it. Please put the full code here and I'll fix it for you – Luis Ávila Oct 27 '15 at 1:54
    
Now I get why we weren't talking about the same - we really weren't. It's a different case now. Let me know if the code works for the other days but tuesday? – Luis Ávila Oct 27 '15 at 2:00

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.