Jump to content

creating a list with database values…


thara

Recommended Posts

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 by Barand
Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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 by Barand
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'..

Link to comment
Share on other sites

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!';
    }	     
}

Link to comment
Share on other sites

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
Link to comment
Share on other sites

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..

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.