Barand Posted January 18, 2013 Share Posted January 18, 2013 (edited) Now you have this name (for each checkbox) name='select-subjectes[{$lastCatId}][]' then you should only need value = '{$subj['id']}' Also, pass your category counts for each category so you know how many there should be $myCounter = count($subjects); echo "<input type='hidden' name='catcount[$LastCatId]' value='$myCounter' />"; Edited January 18, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406775 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Yes.. Barand... I have changed code according to your above mention. But still can't get it to work.. This is my validation code.. // ====================================// // ----- Start Validation -------------// if ( isset($_POST['submitted_subjects'])) { if ( isset($_POST['select-subjectes']) && !empty( $_POST['select-subjectes'])) { $counter = 0; foreach ( $_POST['select-subjectes'] AS $category => $subjets) { $counter++; if ( (count($subjets)< 4) && (count($subjets)>=1 )) { //checking that it has 3 or more values. //process echo 'good'; } else { echo "select at leat 1 or upto 3 subjects for {$category} "; } } if(isset($_POST['catcount'])) { if((int)$_POST['catcount'] > $counter){ echo 'you have to select at leat 1 or upto 3 subjects for all the categories'; } } } else { echo 'You have not selected any subject for any category!'; } } // ----- End Start Validation ---------// // ====================================// With this code I can go to 1st and 3rd error messages. But not going to 2nd message. It is this... if(isset($_POST['catcount'])) { if((int)$_POST['catcount'] > $counter){ echo 'you have to select at leat 1 or upto 3 subjects for all the categories'; } } Why I need to get this error message is.. Just assume I have selected 3 categories and select subjects to one category and not selecting any subject in other 2 category. Like this situation I need to get this error message. But still not going to there...Think if I do so its going inside foreach loop and echoing testing word 'good'. This is my Complete form.. // ==============================================================================// // ----- Generaing Select Subject Form with Data from Category page -------------// echo '<form action="" method="post" accept-charset="utf-8" ><br /> <div id="accordion">'; // Check SESSION fromm category page if ( isset ( $_SESSION['category']) && is_array( $_SESSION['category'])) { $categoryIds = implode(',', $_SESSION['category']); $q = "SELECT c.category_id AS ci, c.category_name AS cn, s.subject_id AS si, s.subject_name AS sn FROM category AS c INNER JOIN category_subjects AS cs ON cs.category_id = c.category_id INNER JOIN subjects AS s ON s.subject_id = cs.subject_id WHERE c.category_id IN ($categoryIds)"; $r = mysqli_query( $dbc, $q) ; echo '<div>'; //Flag Varables $numcols= 2; $lastCategory = FALSE; $lastCatId = FALSE; $categoryNo = 0; $myCounter = 0; while (list( $ci, $cn, $si, $sn) = $r->fetch_row()) { if ( $ci != $lastCatId ) { if ($lastCatId == TRUE) { echo '<h3>Category ' . sprintf("%02d ", $categoryNo) . ":<span> " . $lastCategory . "</span><span></span></h3>"; echo "<div class='container'>\n"; $myCounter = count($subjects); //assigning the number of categories $chunksize = ceil(count($subjects)/$numcols); $chunks = array_chunk($subjects, $chunksize); echo "<table>\n"; foreach ($chunks[0] as $k=>$subj) { echo "<tr>"; $cbvalue = "{$subj['id']}"; echo "<td width='50%'><input type='checkbox' value='$cbvalue' name='select-subjectes[{$lastCategory}][]' />{$subj['nm']}</td>"; if (isset($chunks[1][$k])) { $subj = $chunks[1][$k]; $cbvalue = "{$subj['id']}"; echo "<td width='50%'><input type='checkbox' value='$cbvalue' name='select-subjectes[{$lastCategory}][]' />{$subj['nm']}</td>"; } else { echo "<td width='50%'> </td>"; } echo "</tr>\n"; } echo "</table>\n"; echo "</div> <!-- End .container DIV -->\n"; } $subjects = array(); $lastCategory = $cn; $lastCatId = $ci; $categoryNo++; } $subjects[] = array('id' => $si, 'nm' => $sn); } echo '<h3>Category ' . sprintf("%02d ", $categoryNo) . ":<span> " . $lastCategory . "</span><span></span></h3>"; echo "<div class='container'>\n"; $chunksize = ceil(count($subjects)/$numcols); $chunks = array_chunk($subjects, $chunksize); echo "<table>\n"; foreach ($chunks[0] as $k=>$subj) { echo "<tr>"; $cbvalue = "{$subj['id']}"; echo "<td width='50%'><input type='checkbox' value='$cbvalue' name='select-subjectes[{$lastCategory}][]' />{$subj['nm']}</td>"; if (isset($chunks[1][$k])) { $subj = $chunks[1][$k]; $cbvalue = "{$subj['id']}"; echo "<td width='50%'><input type='checkbox' value='$cbvalue' name='select-subjectes[{$lastCategory}][]' />{$subj['nm']}</td>"; } else { echo "<td width='50%'> </td>"; } echo "</tr>\n"; } echo "</table>\n"; echo "</div> <!-- End .container DIV -->\n"; echo '</div>'; } ?> </div> <!-- end #accordion div --> <!-- Closing Form with Submit button and hidden inputs --> <div class="continue"> <p>Please click 'Continue' button to proceed to next step</p> <input type="submit" value="Continue" class="continue-btn" /> <input type="hidden" name="<?php echo "catcount[{$lastCatId}]"; ?>" value="<?php echo $myCounter; ?>" /> <input type="hidden" name="submitted_subjects" value="TRUE" /> </div> </form> <!-- ----- Generaing Select Subject Form END ------------- --> <!-- ===================================================== --> can you imagine what has happened there? Thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406871 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 (edited) echo "select at least 1 or up to 3 subjects for {$category} "; I was under the impression that all subjects had to selected for a category as stated in condition 3 3- all the selected categories' subjects should be checked and passed $_POST['catcount'][$category] has the count of all subjects in the category Edited January 19, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406900 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Barand I have a little bit confuse to understand what has mentioned in your last post... Again I will explain here what are the my conditions... 01. I need to get an error message if select-subjectes array is completely empty. In my validation code that message is " You have not selected any subject for any category!". I can get this error message when I submitting the form without selecting any subjects (Under all category). 02. Then I need to check how many subjects have selected under one category. Here It should be one or less than or equal to 3 subjects. I have tried to check it inside foreach loop with my validation code.. I can get this error message too, when I select greater than 3 subjects to one category. 03. Finally I need to check at least 1 subject have selected in each categories. Just think, it has 3 categories and have its own subjects. I select subjects to only one category and not selecting subjects to other two. So such a situation I need to display an error message mentioning to users to select at least one subject to other two categories. What I can't do with my validation is this one.. Now can you tell me what I need to do with my coding..? Thank you very much. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406906 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 For condition 3, perhaps if (count($_POST['select-subjets']) != count($_POST['catcount']) ) { echo "You have not selected all categories"; } Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406918 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 This is my new validation code.. // ====================================// // ----- Start Validation -------------// if ( isset($_POST['submitted_subjects'])) { if ( isset($_POST['select-subjectes']) && !empty( $_POST['select-subjectes'])) { $counter = 0; foreach ( $_POST['select-subjectes'] AS $category => $subjets) { $counter++; if ( (count($subjets)< 4) && (count($subjets)>=1 )) { //checking that it has 3 or more values. //process echo 'good'; } else { echo "select at leat 1 or upto 3 subjects for {$category} "; } } /* if(isset($_POST['catcount'])) { if((int)$_POST['catcount'] > $counter){ echo 'you have to select at leat 1 or upto 3 subjects for all the categories'; } } */ if (count($_POST['select-subjectes']) != count($_POST['catcount']) ) { echo "You have not selected subjects in all categories"; } } else { echo 'You have not selected any subject for any category!'; } } // ----- End Start Validation ---------// // ====================================// but not going to that error message. When I submitting the form under above my 3rd condition.. its going to foreach and echoing word 'good'.. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406919 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 Change the validation order if ( isset($_POST['submitted_subjects'])) { if ( isset($_POST['select-subjectes']) && !empty( $_POST['select-subjectes'])) { $counter = 0; if (count($_POST['select-subjectes']) != count($_POST['catcount']) ) { echo "You have not selected subjects in all categories"; } else { foreach ( $_POST['select-subjectes'] AS $category => $subjets) { $counter++; if ( (count($subjets)< 4) && (count($subjets)>=1 )) { //checking that it has 3 or more values. //process echo 'good'; } else { echo "select at leat 1 or upto 3 subjects for {$category} "; } } } /* if(isset($_POST['catcount'])) { if((int)$_POST['catcount'] > $counter){ echo 'you have to select at leat 1 or upto 3 subjects for all the categories'; } } */ } else { echo 'You have not selected any subject for any category!'; } } Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406920 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Barand... result is same in my above post.. and there is another problem.. Think.. if I chose subjects for each category between 1 - 3 it should be echo 'good' but then I can get this You have not selected subjects in all categories Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406926 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 Can you post results of echo '<pre>', print_r($_POST, 1), '</pre>'; Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406928 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Array ( [select-subjectes] => Array ( [Grade 11 (O/L)] => Array ( [0] => 10 [1] => 11 [2] => 12 [3] => 14 ) ) [catcount] => Array ( [9] => 26 ) [submitted_subjects] => TRUE ) when I selecting subjects for only one category.. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406930 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 the counts of $_POST['select-subjectes'] and $_POST['catcount'] should be the same (both equal 1) so you shouldn't get that first error message Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406931 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Barand, then how can I check that condition? Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406932 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 put in the same selection that gave you the wrong error message last time Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406933 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Its hard to understand actually what has happened there.. Somehow I need to check that condition with my form. Its important. can you explain your last post little.. Thank you.. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406935 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 Barand... result is same in my above post.. and there is another problem.. Think.. if I chose subjects for each category between 1 - 3 it should be echo 'good' but then I can get this You have not selected subjects in all categories My last post was asking you to put in that selection again - the one that gave the wrong message Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406936 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Array ( [select-subjectes] => Array ( [Grade 5 (Scholarship Exam)] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) [catcount] => Array ( [5] => 5 ) [submitted_subjects] => TRUE ) good I have selected 2 categories and then select 0 subjects for 1st and 2 subjects for 2nd Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406938 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 if you selected 2 categories there should be 2 values in catcount array. There should be a hidden field named "catcount[$LastCatId']" output for each category. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406941 Share on other sites More sharing options...
thara Posted January 19, 2013 Author Share Posted January 19, 2013 Barand... This is result with 3 categories with 0 selected subjects.... Array ( [catcount] => Array ( [5] => 29 ) [submitted_subjects] => TRUE ) You have not selected any subject for any category! This is result with 3 categories and 3 selected subjects to one category.. Array ( [select-subjectes] => Array ( [Grade 5 (Scholarship Exam)] => Array ( [0] => 1 [1] => 2 [2] => 3 ) ) [catcount] => Array ( [5] => 29 ) [submitted_subjects] => TRUE ) good And this is the result with 3 categories and 3 selected subject in each category. This is the correct way I am expecting.. but get this Array ( [select-subjectes] => Array ( [Grade 5 (Scholarship Exam)] => Array ( [0] => 1 [1] => 2 [2] => 3 ) [Grade 6 - 10] => Array ( [0] => 6 [1] => 7 [2] => 8 ) [Grade 11 (O/L)] => Array ( [0] => 19 [1] => 20 [2] => 21 ) ) [catcount] => Array ( [5] => 29 ) [submitted_subjects] => TRUE ) You have not selected subjects in all categories It seems that this form when submitted... If the category has no subjects selected, it'll not be send this is what making the problem for me.. isn't it? Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406946 Share on other sites More sharing options...
Barand Posted January 19, 2013 Share Posted January 19, 2013 In an earlier reply I suggested Also, pass your category counts for each category so you know how many there should be $myCounter = count($subjects); echo "<input type='hidden' name='catcount[$LastCatId]' value='$myCounter' />"; which shows the hidden fields being output after you set $myCounter. This is in the loop for each category (and should also be in the final section for the final category). It looks as though you only output a single hidden catcount field. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1406951 Share on other sites More sharing options...
thara Posted January 20, 2013 Author Share Posted January 20, 2013 Barand thanks for your help.. My problem is now solved. I am greatly appreciated your help. thanks again. Quote Link to comment https://forums.phpfreaks.com/topic/273061-creating-a-list-with-database-values%E2%80%A6/page/2/#findComment-1407075 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.