Hello all!

I'm working on a CMS for an online magazine. I'm currently struggling with finding a way to organize all the written articles into a given issue, ordering their page numbers while making sure that the page numbers the user enters do not conflict with each other. Take a look at the following:

The idea is that once articles are added to an issue, they initially have 0 as their page number: "Not listed." The administrator then uses the drop down menus to select which page each article should go on, and once they are ready, they select the 'Save pages' button at the bottom and the info from each drop down menu is saved in the 'page' field of each article's mysql row.

My question is how to pull this off (saving each of the drop down boxes), but first making sure that no two entries are the same.

Thanks in advance!
-Ange52

    considering the values of the dropdowns should be stored in an array, you could use [man]array_count_values/man to see if there is more than 1 of each allowed value, then go from there.

      First off, thanks for the quick reply!

      That sounds like a great start, and if the page numbers are in an array, then I can index through the array running the same mysql update command to save the given page number in the DB. I'm getting tripped up because I don't know how I can access the array of the entries in the drop down boxes. I'm sure that sounds stupid, so hopefully it'll be a simple fix to get this thing rolling...

      ...or perhaps it isn't that easy? What were you expecting when you said that the dropdowns --should-- be in an array? How can I tap into that array?

        welll u know how it goes

        <option value="bla">Option 1</option>

        well

        <?php
        $dropdown = $_POST['drop_down_name_here'];
        echo($dropdown);
        ?>
        

        now if i selected option 1 that script would echo bla, or the value of that option... ... hope you understand 😃

          I'm not sure I get it (sorry for being a PHP newbie...)

          I think the issue is the way that I am laying out this content to begin with-- because I'm not posting these vars anywhere at this point. The current code I have that is created that table I have the link to is...

          echo "Issue for <b>$f_publish_date</b><br><br>";
          echo "<table border=1 cellpadding=7>";
          $article_query = "SELECT * FROM articles WHERE issue_id = '$issue_id'";
          $article_result = $handle->query( $article_query );
          if( $article_result ) {
            while( $row = $handle->fetchArray( $article_result )) {
              echo "<tr><td><b>$row[article_title]</b></td>";
              echo "<td><select name=page_num_$row[article_id]>";
              for( $i=0; $i<26; $i++ ) {
                if( $i == 0 ) {
          	echo "<option value='$i'>Not listed</option>";
                } else if( $row[page_num] == $i ) {
          	echo "<option value='$i' selected>$i</option>";
                } else {
                  echo "<option value='$i'>$i</option>";
                }
              }
              echo "</select></td>";
              echo "<td><a href='editArticle.php?action=edit&id=$row[article_id]'>Edit</td>";
              echo "<td>Remove from this issue</td></tr>";
            }
            echo "<tr><td> </td>
            <td><input type=button value='Save pages'></td>
            <td> </td></tr></table>";
          }

          Is the issue in how I laid it out in there? And if not, how can I take that 'Save pages' button and attach to it the function that is going to check the array for duplicates, and then enter each array value into its correct spot in the mysql database?

          Again, I apologize for my newbie-ness, but I do greatly appreciate all the help I am getting on this issue... I've been sitting with this one problem for weeks now...

          Thanks again,
          -Ange52

            i hope this can help...
            onlick() event of the save button, submit the form to this script, or make it a submit form element

            	//suppose $array() is the array variable containing the values of your combo boxes
            
            $array = array(1,2,3,4,1,2,1); // test data
            
            function checkDuplicate($array) {
            	$flag = false;
            	if (is_array($array)) {
            		$result = array_count_values($array);
            		foreach($result AS $count) {
            			if ((int)$count > 2) {
            				// there is at least 1 duplicate entry
            				$flag = true;
                                                break;
            			}
            		}
            	}
            	return $flag;
            }
            
            $return = checkDuplicate($array);
            if ($return) {
            	// array has duplicates. issue warning and return to issue page
            }
            else {
            	// no duplicates, save in database
            	// to do this, your combo element and value must be associated with the corresponding DB record
            }
            

              That looks perfect, and you walked me right through it (just like I know I need), however I'm not sure about that first step of adding the dropdown values to an array so that I can run it in a function.

              How can I take those dropdowns into an array at that first step?

                one way is to create an array of combo boxes instead of naming them individually for easy manipulation
                try changing

                echo "<td><select name=page_num_$row[article_id]>"; 

                to

                echo "<td><select name=page_num>";
                

                then you can grab those values by

                if (is_array($_POST['page_num'])) {
                  	  $array = array();
                	  foreach ($_POST['page_num'] AS $value) {
                	  	$array[] = $value;
                	  }
                  }
                  $return = checkDuplicate($array); 
                

                  I can't figure out why this isn't working! I'm still struggling just to pick up the dropdown box values as an array, and send that to another page so that they can be processed and added to the database! Please review my code, because I don't know what fundamental mistake I must be making here! (PS- A few of those echo lines in there are to help me see what I am getting right... which isn't much, at this point...)

                  function checkDuplicate($array) { 
                    $flag = false; 
                    if (is_array($array)) { 
                      $result = array_count_values($array); 
                      foreach($result AS $count) { 
                        if ((int)$count > 2) { 
                          // there is at least 1 duplicate entry 
                          $flag = true; 
                          break; 
                        } 
                      } 
                    } 
                    return $flag; 
                  } 
                  
                  if( $_POST['action'] == 'save' ) {
                    // Save pages
                    $type = gettype($_POST['page_num']);
                    echo "page_num var type : $type<br>";
                    echo "page_num = $_POST[page_num]<br>";
                    if (is_array($_POST['page_num'])) { 
                      $array = array(); 
                      foreach ($_POST['page_num'] AS $value) { 
                        $array[] = $value;
                        echo "$value - ";
                      } 
                    } else {
                      echo "post-page_num is not an array, cannot function <br>";
                    }	
                    $return = checkDuplicate($array); 
                    if ($return) { 
                      // array has duplicates. issue warning and return to issue page
                      echo "<center><strong>Sorry- you have duplicated pages...</strong></center><br><br>";
                    } else {
                      echo "<center><strong>All clear!!</strong></center><br><br>";
                      // no duplicates, save in database 
                      // to do this, your combo element and value must be associated with the corresponding DB record 
                    }
                  }
                  
                  // Display page info
                  $issue_id = $_REQUEST[issue_id];
                  $query = "SELECT * FROM issues WHERE issue_id = '$issue_id'";
                  $result = $handle->query( $query );
                  $issue_info = $handle->fetchAssoc( $result );
                  $f_publish_date = strftime("%B %Y", $issue_info[publish_date]);
                  
                  echo "Issue for <b>$f_publish_date</b><br><br>";
                  echo "<table border=1 cellpadding=7><form name='completeIssue' method='POST' action='$PHP_SELF'>";
                  $article_query = "SELECT * FROM articles WHERE issue_id = '$issue_id'";
                  $article_result = $handle->query( $article_query );
                  if( $article_result ) {
                    while( $row = $handle->fetchArray( $article_result )) {
                      echo "<tr><td><b>$row[article_title]</b></td>";
                      echo "<td><select name=page_num>";
                      for( $i=0; $i<26; $i++ ) {
                        if( $i == 0 ) {
                          echo "<option value='$i'>Not listed</option>";
                        } else if( $row[page_num] == $i ) {
                          echo "<option value='$i' selected>$i</option>";
                        } else {
                          echo "<option value='$i'>$i</option>";
                        }
                      }
                      echo "</select></td>";
                      echo "<td><a href='editArticle.php?action=edit&id=$row[article_id]'>Edit</td>";
                      echo "<td>Remove from this issue</td></tr>";
                    }
                    echo "<tr><td> </td>
                    <input name='action' type='hidden' value='save'>
                    <td><input type=submit value='Save pages'></td>
                    <td> </td></tr></form></table>";
                  }

                  That is the entire page (without opening and closing stuff). Help??? What am I doing wrong?? 🙁

                    sorry bout that but i guess you have to put an index to your html element to be able to reference it as an array

                        echo "<td><select name=page_num[$index]>";
                    

                    index can be the article_id

                      12 days later

                      Sorry for the long delay-- I hope that someone out there might still be with me.

                      I have put many hours into fiddling with this thing, and now I receive the page number variables when I click 'Save Pages', however the checkDuplicate function isn't picking up on whether or not there are duplicates entered (quick note-- once this part of the code is working, do you think it would be possible to modify it to allow duplicates only if the repeated number is 0?).

                      Take a gander at my code here (note that the troubled area seems to be the checkDuplicate function at the beginning). The following is the entire page:

                      <?php
                      function checkDuplicate( $array ) { 
                        $flag = false; 
                        if ( is_array( $array )) { 
                          $result = array_count_values( $array );
                      	echo " . . . result : ".print_r( $result ); 
                          foreach( $result AS $count ) { 
                            if (( int )$count > 2) { 
                              // there is at least 1 duplicate entry 
                              $flag = true; 
                              break; 
                            }
                          }
                        }
                        return $flag; 
                      } 
                      
                      if( $_POST['action'] == 'save' ) {
                        // Save pages
                        $article_total = $_POST['article_total'];
                        $array = array();
                        for ($j=1; $j<$article_total; $j++) {
                          $array[$j] = $_POST["page_num_$j"];
                      	echo " __ $j : ".$_POST["page_num_$j"]." __<br> ";
                        }
                        //print_r( $array );
                        $return = checkDuplicate( $array ); 
                        if ( $return ) { 
                          echo "<center><strong>Sorry- you have duplicated pages...</strong></center><br><br>";
                        } else {
                          echo "<center><strong>All clear!!</strong></center><br><br>";
                        }
                      }
                      
                      // Display page info
                      $issue_id = $_REQUEST[issue_id];
                      $query = "SELECT * FROM issues WHERE issue_id = '$issue_id'";
                      $result = $handle->query( $query );
                      $issue_info = $handle->fetchAssoc( $result );
                      $f_publish_date = strftime("%B %Y", $issue_info[publish_date]);
                      
                      echo "Issue for <b>$f_publish_date</b><br><br>";
                      echo "<table border=1 cellpadding=7><form name='completeIssue' method='POST' action='$PHP_SELF'>";
                      $article_query = "SELECT * FROM articles WHERE issue_id = '$issue_id'";
                      $article_result = $handle->query( $article_query );
                      $article_index = 1;
                      if( $article_result ) {
                        while( $row = $handle->fetchArray( $article_result )) {
                          echo "<tr><td><b>$row[article_title]</b></td>";
                          echo "<td><select name=page_num_$article_index>";
                      	$article_index++;
                          for( $i=0; $i<26; $i++ ) {
                            if( $i == 0 ) {
                              echo "<option value='$i'>Not listed</option>";
                            } else if( $row[page_num] == $i ) {
                              echo "<option value='$i' selected>$i</option>";
                            } else {
                              echo "<option value='$i'>$i</option>";
                            }
                          }
                          echo "</select></td>";
                          echo "<td><a href='editArticle.php?action=edit&id=$row[article_id]'>Edit</td>";
                          echo "<td>Remove from this issue</td></tr>";
                        }
                        echo "<tr><td> </td>
                        <input name='action' type='hidden' value='save'>
                        <input name='article_total' type='hidden' value='$article_index'>
                        <td><input type=submit value='Save pages'></td>
                        <td> </td></tr></form></table>";
                      }
                      ?>
                      

                      I know it is very newbie-ish of me to include the code from the whole page, but I just can't bring myself to spend any more hours on something that feels like it should be so simple. I greatly appreciate any more help I can get on this.

                      Thank you,
                      Ange52

                        Write a Reply...