thara Posted April 23, 2012 Share Posted April 23, 2012 hello.. In this script. if part is working properly. but elseif part not working i expect. it display only 1 category and its subjects. Anybody can tell me what the mistake that I have did... Here is my script } elseif ( isset($_POST['category']) && ( sizeof( $_POST['category']) == 2 || sizeof( $_POST['category']) == 3 )) { $q = 'SELECT * FROM category ORDER BY category_id'; $r = mysqli_query( $dbc, $q); while($row = mysqli_fetch_array( $r, MYSQLI_ASSOC )) { foreach( $category as $value ) { if ( $value == $row['category_id']) { echo '<input type="radio" name="category[]" value="' . $row['category_id'] . '" class="radio" />'; echo '<label for="" class="radio"> ' . $row['category_name'] . ' <strong>: Mark this category as my "Main Category"</strong></label><br />'; $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 = {$row['category_id']}"; $r = mysqli_query( $dbc, $q); $c = $i = 0; echo '<table class="form_table" ><tr>'; while($info = 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="' . $info['subject_id'] . '" /> ' . $info['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>"; } } } } Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/ Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 You're redefining $r inside your loop. This is why you need to use descriptive variable names. Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339841 Share on other sites More sharing options...
thara Posted April 23, 2012 Author Share Posted April 23, 2012 Its ok now. thanks for your assistance guys!!! Here I use two php page with two forms. my first form is 'sign_up.php' and second is select_subject.php'. sign_up.php page has more categories and users can select up to 3 more categories there. So. after selecting and user click the continue bottom, page want to go to second form page its select_subject.php page. If a user not select a category of who selected over 3 category I need to display a error message. like this "Please select atleast 1, not more than 3 categories." I use this HTML my first page <form method="post" action="select_subject.php"> <my select boxes> </form> Then I process it in my second page and if user have made a mistake in first page I need to redirect to the first page again with relevant error message. So I use this code in my second page. } else { // No valid ID, kill the script. $_SESSION['errors'] = "Please select atleast 1, not more than 3 categories."; $url = 'http://localhost/lanka_institute/tutorsignup/tutor_registration.php'; // Define the URL: ob_end_clean(); // Delete the buffer. header("Location: $url"); exit(); // Quit the script. } and my first page I use this if (isset($_SESSION['errors'])) { echo '<p> ' . $_SESSION['errors'] . '</p>'; } unset($_SESSION['errors']); but it is not printing my error message in the first page that Im expecting. But it printing 'Array' instead of my message. can you help me, what is the mistake that I have done??? thanks in advance. Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339856 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 Are you calling session_start() at the top of every page? Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339864 Share on other sites More sharing options...
thara Posted April 23, 2012 Author Share Posted April 23, 2012 yes dear... I have started it in my header.html. it is a include file. Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339865 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 ...are HTML files being parsed as PHP on your system? Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339876 Share on other sites More sharing options...
thara Posted April 23, 2012 Author Share Posted April 23, 2012 ya.. this is my header.html file.. <?php # header.html // This page begins the HTML header for the site. // Start output buffering: ob_start(); // Initialize a session: session_start(); ?><!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> <title><?php echo $page_title;?></title> <link type="text/css" rel="stylesheet" href="../styles/main.css" /> </head> <body> <div id="header_warp"> Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339878 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 Put session_start before ob_start Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339881 Share on other sites More sharing options...
thara Posted April 23, 2012 Author Share Posted April 23, 2012 problem is still same... It display 'Array' instead of my error message.. Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339883 Share on other sites More sharing options...
ManiacDan Posted April 23, 2012 Share Posted April 23, 2012 Oh geeze, I thought the errors weren't showing at all. your errors are an array. echo implode("<br />", $_SESSION['errors']); Quote Link to comment https://forums.phpfreaks.com/topic/261478-loop-not-working-properly/#findComment-1339888 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.