3

Currently I am trying to create a daily class routine using PHP. Firstly the site will have seven days name as check box and a text box where users could enter number of class period.

When user will submit the form the code will create another form inside another table (looks like this). And finally user could enter class name, teacher name and the code will store it in database.

But my problem is creating the table dynamically. I can not finding out how to solve this problem.

Any help would be appreciated.

Below you can see what I have tried so far:

<h1>Form two</h1>

<form action="routine_create_process.php" method="POST">
<h3>How many class period do you want to add?</h3>

<input type="text" name="period"/>

<h3>Avalible class day</h3>

<label><input type="checkbox" name="f[]" value="sat" /> Saturday </label>
<br />
<label><input type="checkbox" name="f[]" value="sun"  /> Sunday </label>
<br />
<label><input type="checkbox" name="f[]"  value="mon" /> Monday </label>
<br />
<label><input type="checkbox" name="f[]"  value="tues" /> Tuesday </label>
<br />
<label><input type="checkbox" name="f[]"  value="thurs" /> Wednesday </label>
<br />
<label><input type="checkbox" name="f[]"  value="thus" /> Thursday </label>
<br />
<label><input type="checkbox" name="f[]"  value="fri" /> Friday </label>
<br />
<input type="submit" value="SUBMIT"/>
</form>

My PHP code:

<?php
$period=$_POST['period'];
$arr2=$_POST['f'];
?>

<table border='2'>
<?php 
$count=count($arr2)-1;
for($i=0;$i<=$count;$i++){
  echo "<tr><td>";
  if($i==0){
    echo "<table>";
    echo "<div class='wrap_p'>";
    for($r=1;$r<=$period;$r++){
      echo "<td>";
      echo "<div class='add_css'>";
      echo $r;
      echo "</div></td>";
    }
    echo "</div>";
    echo "</table>";
  }
  echo "</tr><tr><td>"; 
  echo $arr2[$i];
  echo "</td>";
  echo "<td> <input type='text' size='20' /></tr></td>";
}
?>
</table>
2
  • The variable $period is not defined. Can you please update your code. Commented Feb 16, 2014 at 10:43
  • Sorry, I have updated the code Commented Feb 16, 2014 at 10:49

2 Answers 2

2

This should solve your problem.

<?php

$arr2=$_POST['f'];
$period = $_POST['period'];

?>

<table border='2'>
<?php 
$count=count($arr2)-1;
?>
<tr><td>&nbsp;</td>
<?php
                for($r=1;$r<=$period;$r++){
                    echo "<td>";
                    echo "<div class='add_css'>";
                    echo $r ;
                    echo "</div></td>";

                }
    for($i=0;$i<=$count;$i++){

echo "<tr><td>"; 

echo $arr2[$i];

        for($r=1;$r<=$period;$r++)
        {
            echo "<td>";
            echo "<div class='add_css'>";
            echo "<input type='text' size='20' />" ;
            echo "</div></td>";
        }

echo "</td>";


}

?>
</table>
Sign up to request clarification or add additional context in comments.

3 Comments

it works , but I also need to display the class period number at the top of the table .
the number as whole? Just print the $period it will show the number of period.
I can not understand where should I print the variable which prints period number. I have tried this. echo "<td>"; echo "<div class='add_css'>"; echo $r; echo "<input type='text' size='20' />" ; echo "</div></td>"; and it returns like this s8.postimg.org/ckzufr5xx/stack_php2.jpg but I dont want that
2

I think this is close to what you need.

<html>
<body>
<?php
$period = 8;
$arr2=array('sat','sun','mon','tue','wed','thu','fri');
?>
<table border='2'>
<tr>
<th>Days</th>
<?php 
for($i=1;$i <= $period; $i++){
    ?><th><?php echo $i; ?></th><?php
}
?>
</tr>
<?php 
foreach($arr2 as $day){
    ?>
    <tr>
        <th><?php echo $day; ?></th>
        <?php
        for($i=1;$i <= $period; $i++){
            ?>
            <td>
                <input type="text" placeholder="subject">
                <input type="text" placeholder="subject code">
                <input type="text" placeholder="teachers name">
            </td>
            <?php
        } 
        ?>
        </tr>
    <?php
}
?>
</table>
</body>
</html>

1 Comment

No problem. you get the idea right? remember to replace $arr2 with your POST input

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.