Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

I can't figure out while the table rows and columns are not display as intended. I have tried everything I could but no solution yet.

**$days_of_week_rows['week_day']**
$period_rows['period']   $class_rows['class']  $time_rows['time']
$period_rows['period']   $class_rows['class']  $time_rows['time']
$period_rows['period']   $class_rows['class']  $time_rows['time']
$period_rows['period']   $class_rows['class']  $time_rows['time']

But instead I see something like this:

**$days_of_week_rows['week_day'**
$period_rows['period']
$period_rows['period']
$period_rows['period']
$period_rows['period'] 
$class_rows['class']  
$class_rows['class']
$class_rows['class']
$class_rows['class']     
$time_rows['time']
$time_rows['time']
$time_rows['time']
$time_rows['time']

Here is the PHP Code

?>
<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="1px">
<?php
$days_of_week_count = 1;
            while($days_of_week_count < 6)
            {
                $select_days_of_week = "SELECT week_dayID, week_day FROM week_day
                                        WHERE week_dayID = '".$days_of_week_count."'";
                $result_days_of_week = mysql_query($select_days_of_week) or die('Couldn\'t select from the week_day table' . mysql_error());
                while($days_of_week_rows = mysql_fetch_array($result_days_of_week))
                {
                    $week_dayID = $days_of_week_rows['week_dayID'];
                    echo "<tr><td colspan='3'>" . $days_of_week_rows['week_day'] . "</td></tr>";
                       //Select information from period
                        $period_select = "SELECT period FROM table_period
                                           INNER JOIN table_relate
                                             ON table_period.periodID = table_relate.periodID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $period_result = mysql_query($period_select) or die('Couldn\'t select from table_period ' . mysql_error());

                        while($period_rows = mysql_fetch_array($period_result))
                        {    
                            echo "<tr><td>" .$period_rows['period']. "</td></tr>";

                        }

                        //Select information from class
                        $class_select = "SELECT class FROM table_class
                                           INNER JOIN table_relate
                                             ON table_class.classID = table_relate.classID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $class_result = mysql_query($class_select) or die('Couldn\'t select from table_class ' . mysql_error());
                        while($class_rows = mysql_fetch_array($class_result))
                        {
                            echo "<tr><td>" .$class_rows['class']. "</td></tr>";
                        }

                        //Select information from time
                        $time_select = "SELECT time FROM table_time
                                           INNER JOIN table_relate
                                             ON table_time.timeID = table_relate.timeID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $time_result = mysql_query($time_select) or die('Couldn\'t select from table_time ' . mysql_error());
                        while($time_rows = mysql_fetch_array($time_result))
                        {
                            echo "<tr><td>" .$time_rows['time']. "</td></tr>";
                        }

                }

                $days_of_week_count = $days_of_week_count  + 1;
            }
        echo "</table>";

Please help me point out where I'm doing it wrong

share|improve this question
4  
I'm sorry but this is offtopic. See the FAQ: "To the best of your knowledge, does the code work?" It doesn't work, so it should be on StackOverflow. –  Quentin Pradet Mar 5 '12 at 14:05
 
SQL queries in loops are death to performance. Avoid them at all costs! –  GordonM Mar 6 '12 at 10:26
add comment

closed as off topic by Quentin Pradet, Paul, Michael K Apr 2 '12 at 18:11

Questions on Code Review Stack Exchange are expected to relate to code review request within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

2 Answers

In every of your while loops, you generate a new row with only one cell with one value, I think you are missing the fact that HTML tables are row based and not column based. So in your current construct you will never get the desired results.

share|improve this answer
add comment

Try this one:

<table width="100%" height="100%" cellspacing="0" cellpadding="0" border="1px">
<?php
$days_of_week_count = 1;
            while($days_of_week_count < 6)
            {
                $select_days_of_week = "SELECT week_dayID, week_day FROM week_day
                                        WHERE week_dayID = '".$days_of_week_count."'";
                $result_days_of_week = mysql_query($select_days_of_week) or die('Couldn\'t select from the week_day table' . mysql_error());
                while($days_of_week_rows = mysql_fetch_array($result_days_of_week))
                {
                    $week_dayID = $days_of_week_rows['week_dayID'];
                    echo "<tr><td colspan='3'>" . $days_of_week_rows['week_day'] . "</td>";
                       //Select information from period
                        $period_select = "SELECT period FROM table_period
                                           INNER JOIN table_relate
                                             ON table_period.periodID = table_relate.periodID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $period_result = mysql_query($period_select) or die('Couldn\'t select from table_period ' . mysql_error());

                        while($period_rows = mysql_fetch_array($period_result))
                        {    
                            echo "<td>" .$period_rows['period']. "</td>";

                        }

                        //Select information from class
                        $class_select = "SELECT class FROM table_class
                                           INNER JOIN table_relate
                                             ON table_class.classID = table_relate.classID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $class_result = mysql_query($class_select) or die('Couldn\'t select from table_class ' . mysql_error());
                        while($class_rows = mysql_fetch_array($class_result))
                        {
                            echo "<td>" .$class_rows['class']. "</td>";
                        }

                        //Select information from time
                        $time_select = "SELECT time FROM table_time
                                           INNER JOIN table_relate
                                             ON table_time.timeID = table_relate.timeID
                                          WHERE teacherID = '".$teacherID."' AND week_dayID = '".$week_dayID."'";
                        $time_result = mysql_query($time_select) or die('Couldn\'t select from table_time ' . mysql_error());
                        while($time_rows = mysql_fetch_array($time_result))
                        {
                            echo "<td>" .$time_rows['time']. "</td>";
                        }

                }
                echo '</tr>';
                $days_of_week_count = $days_of_week_count  + 1;
            }
        echo "</table>";
share|improve this answer
add comment

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