Hello Everyone..
Im a programer in php with little experience.
I have a form with 3 pages. 1st page display categories which come from category table. then user can select 1 or upto 3 category there. Then 2nd page display that selected categories and need to display relevant subject to that categories. In second page I use radio button for selected categories to keep a one category as a main category. each categories' subjects display with check boxes. In there too, user can select 1 or 3 more subject in to a particular category. In third form page, that my problem was encounter, there I need to print out selected category and their subjects in a table.
first two pages are no problem for me and third form I have a problem in how to get categories and their subjects and they print as apiece in the page. like this
category1 | subject1, subject2, subject3
category2 | subject1, subject2, subject3, subject and so forth.
$q = 'SELECT * FROM category ORDER BY category_id';
$r = mysqli_query( $dbc, $q);
$c = 0;
$i = 0;
echo '<form action="subjects.php" method="post">';
echo '<table class="form_table" ><tr>';
while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC )){
// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}
echo '<td width="50%"><input type="checkbox" name="category[]" value="' . $row['category_id'] . '" /> ' . $row['category_name'] . '</td>';
$c++;
} // while..
// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){
// str_repeat() will be handy when you want more than 2 columns
echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}
echo "</tr></table>";
?>
In second page 'subjects.php' I use
if ( isset($_POST['submitted1']) && isset($_POST['category']) && sizeof( $_POST['category']) == 3 ) {
$_SESSION['category'] = $_POST['category'];
print_r ( $_SESSION['category']);
$q = 'SELECT * FROM category ORDER BY category_id';
$r = mysqli_query( $dbc, $q);
while($_SESSION = mysqli_fetch_array( $r, MYSQLI_ASSOC )){
foreach( $_POST['category'] as $value ) {
if ( $value == $_SESSION['category_id']) {
$q = "SELECT category_subject.category_id, category_subject.subject_id, subjects
FROM category_subject
INNER JOIN category ON category_subject.category_id = category.category_id
INNER JOIN subject ON category_subject.subject_id = subject.subject_id
WHERE category_subject.category_id = {$_SESSION['category_id']}";
$r1 = mysqli_query( $dbc, $q);
$c = $i = 0;
echo '<table class="form_table" ><tr>';
while($_SESSION = mysqli_fetch_array( $r1, MYSQLI_ASSOC )){
// if remainder is zero after 2 iterations (for 2 columns) and when $c > 0, end row and start a new row:
if( ($c % 2) == 0 && $c != 0){
echo "</tr><tr>";
}
echo '<td width="50%"><input type="checkbox" name="subject[]" value="' . $_SESSION['subject_id'] . '" /> ' . $_SESSION['subjects'] . '</td>';
$c++;
} // while..
// in case you need to fill a last empty cell:
if ( ( $i % 2 ) != 0 ){
// str_repeat() will be handy when you want more than 2 columns
echo str_repeat( "<td> </td>", ( 2 - ( $i % 2 ) ) );
}
echo "</tr></table>";
}
}
}
}
Those two pages working properly.
In third page that my problem page I tried something like this , but its not working.
value come from subject.php page.
if ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) && (isset( $_SESSION['subjects'])) && sizeof( $_SESSION['category']) == 1) {
//$_SESSION['category'] = $_POST['category'];
print_r ( $_SESSION['category']);
echo 'good';
} elseif ( ( isset($_POST['submitted2'])) && ( isset($_SESSION['category'])) &&
(isset( $_POST['main_category'] ))) {
print_r ( $_SESSION );
echo 'very good';
}
So can any body tell me where is my mistake and help me fix this problem.
any help greatly appreciated.
thank you.