6 Replies Latest reply: Jul 26, 2012 2:56 PM by Willymannen Branched from an earlier discussion. RSS

    Organizing checkbox group in rows and columns

    Willymannen Community Member

      OK! I have a checkbox group populated in this way, I will chose from maybe 100 drills for golf players and put the drill_ID it together in a lookup table with players_id.

      Maybe up to 20 drills per player.

       

      Now can I organice the checkbox group in columns or rows so I dont get a 100 long list straight down?

        • 1. Re: Organizing checkbox group in rows and columns
          David_Powers ACP

          Please don't add a new question to a 3-year-old thread. I have branched this into a separate thread of its own.

          • 2. Re: Organizing checkbox group in rows and columns
            David_Powers ACP

            If you're using PHP or ASP, Tom Muck has a free horizontal looper extension that you can obtain from here: http://www.tom-muck.com/extensions/help/horizontallooper/.

            • 3. Re: Organizing checkbox group in rows and columns
              Willymannen Community Member

              I bougt the horizontal vertical looper PHP from Tom Muck and got i to work here

               

              Now I can't figure out how to insert a playerID (hidden field id) and a drillID (the loop) in a lookup table "exercise_spelare_ref" where I have 3 columns:

               

              id drillID kommentar

              21 32 NULL

              21 34 NULL

              21 47 NULL

              35 31 NULL

              35 33 NULL

               

              I think I want it this way instead of comma separated, not sure why though

               

              id and drillID are composite index kommentar is for later use (problems :-) a possibility to comment on every drill on a player level (id)

               

              I understand that I should make a foreeach loop and end the checkbox name with [] to make an array maybe even the hidden field id, not sure.

               

              With the standard Dreamweaver insert record I can only insert the last choosen checkbox along with the id.

               

              Code here:

               

              <?php
              if(!isset($tfm_rows) || intval($tfm_rows) == 0){$tfm_rows = -1;}
              if(!isset($tfm_columns) || intval($tfm_columns) == 0){$tfm_columns = 4;}
              if(!isset($tfm_vertical)){$tfm_vertical = "true";}
              ?>
              <?php  require_once('../dirName/fileName.php'); ?>
              <?php
              if (!function_exists("GetSQLValueString")) {
              function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
              {
                if (PHP_VERSION < 6) {
                  $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
                }

                $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);

                switch ($theType) {
                  case "text":
                    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                    break;   
                  case "long":
                  case "int":
                    $theValue = ($theValue != "") ? intval($theValue) : "NULL";
                    break;
                  case "double":
                    $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
                    break;
                  case "date":
                    $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
                    break;
                  case "defined":
                    $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
                    break;
                }
                return $theValue;
              }
              }

              $editFormAction = $_SERVER['PHP_SELF'];
              if (isset($_SERVER['QUERY_STRING'])) {
                $editFormAction .= "?" . htmlentities($_SERVER['QUERY_STRING']);
              }

              if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {
                $insertSQL = sprintf("INSERT INTO exercise_spelare_ref (id, drillID) VALUES (%s, %s)",
                                     GetSQLValueString($_POST['spelareID'], "text"),
                                     GetSQLValueString($_POST['drillSelect'], "text"));

                mysql_select_db($database_connect, $connect);
                $Result1 = mysql_query($insertSQL, $connect) or die(mysql_error());
              }

              // Horizontal looper block 1 -- set up array, row and column values
              $HLoop_Recordset1 = array();
              $HLoop_Recordset1_rows = $tfm_rows;
              $HLoop_Recordset1_columns = $tfm_columns;
              $HLoop_Recordset1_vertical = $tfm_vertical;

              if($HLoop_Recordset1_vertical == "true") {
              $HLoop_Recordset1_loopTo = $HLoop_Recordset1_rows;
              }else{
              $HLoop_Recordset1_loopTo = $HLoop_Recordset1_columns;
              }
              $HLoop_Recordset1_i = 0;
              $HLoop_Recordset1_ii = 0;
              $HLoop_Recordset1_actualrows = 1;

              mysql_select_db($database_connect, $connect);
              $query_Recordset1 = "SELECT * FROM exercise";
              $Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
              $row_Recordset1 = mysql_fetch_assoc($Recordset1);
              $totalRows_Recordset1 = mysql_num_rows($Recordset1);

              $colname_rsSpelare = "-1";
              if (isset($_GET['id'])) {
                $colname_rsSpelare = $_GET['id'];
              }
              mysql_select_db($database_connect, $connect);
              $query_rsSpelare = sprintf("SELECT id, user_login, first_name, last_name FROM analys_kund WHERE id = %s", GetSQLValueString($colname_rsSpelare, "int"));
              $rsSpelare = mysql_query($query_rsSpelare, $connect) or die(mysql_error());
              $row_rsSpelare = mysql_fetch_assoc($rsSpelare);
              $totalRows_rsSpelare = mysql_num_rows($rsSpelare);

              mysql_select_db($database_connect, $connect);
              $query_rsRef = "SELECT * FROM exercise_spelare_ref";
              $rsRef = mysql_query($query_rsRef, $connect) or die(mysql_error());
              $row_rsRef = mysql_fetch_assoc($rsRef);
              $totalRows_rsRef = mysql_num_rows($rsRef);
              $query_Recordset1 = "SELECT drillID, categori, rubrik FROM exercise";
              $Recordset1 = mysql_query($query_Recordset1, $connect) or die(mysql_error());
              $row_Recordset1 = mysql_fetch_assoc($Recordset1);
              $totalRows_Recordset1 = mysql_num_rows($Recordset1);

              if ($HLoop_Recordset1_rows < 1){
              $HLoop_Recordset1_rows = ceil($totalRows_Recordset1 / $HLoop_Recordset1_columns);
              if($HLoop_Recordset1_loopTo < 1) {
                $HLoop_Recordset1_loopTo = $HLoop_Recordset1_rows;
              }
              }
              ?>
              <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
              <html xmlns="http://www.w3.org/1999/xhtml">
              <head>
              <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
              <title>Välj Övningar</title>
              </head>

              <body>
              <strong>Spelare: <?php echo $row_rsSpelare['first_name']; ?>  <?php echo $row_rsSpelare['last_name']; ?></strong><br />
              <br />
              <form id="form1" name="form1" method="POST" action="<?php echo $editFormAction; ?>">
                <?php
              // Horizontal Looper start code for Recordset1

              do {
              ob_start();
              ?>
                  <input type="checkbox" name="drillSelect" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />
                  <?php echo $row_Recordset1['rubrik']; ?>
                  </label>
                  <?php
              // HLooper manage the arrays for Recordset1
              $HLoop_Recordset1_temp = ob_get_contents(); //dump buffer to variable
              ob_end_clean(); //clear buffer
              $HLoop_Recordset1[$HLoop_Recordset1_i][$HLoop_Recordset1_ii] = $HLoop_Recordset1_temp;
              $HLoop_Recordset1_ii++;
              if($HLoop_Recordset1_ii >= $HLoop_Recordset1_loopTo){
              $HLoop_Recordset1_i++;
              $HLoop_Recordset1_ii = 0;
              $HLoop_Recordset1_actualrows++;
              };
              } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1));
              if($HLoop_Recordset1_actualrows < $HLoop_Recordset1_rows && $HLoop_Recordset1_vertical == "false") $HLoop_Recordset1_rows = $HLoop_Recordset1_actualrows;
              if($HLoop_Recordset1_actualrows < $HLoop_Recordset1_columns && $HLoop_Recordset1_vertical == "true") $HLoop_Recordset1_columns = $HLoop_Recordset1_actualrows;

              ?>
                <table class="hloop">
                  <?php for($i = 0; $i < $HLoop_Recordset1_rows; $i++) { ?>
                  <tr>
                    <?php for($ii = 0; $ii < $HLoop_Recordset1_columns; $ii++) { ?>
                    <td><?php
                          if($HLoop_Recordset1_vertical == "true") {
                            echo(isset($HLoop_Recordset1[$ii][$i]) ? $HLoop_Recordset1[$ii][$i] : " ");
                           }else{
                            echo(isset($HLoop_Recordset1[$i][$ii]) ? $HLoop_Recordset1[$i][$ii] : " ");
                          } ?></td>
                    <?php } ?>
                  </tr>
                  <?php } // End Horizontal/Vertical Looper code for Recordset1 ?>
                </table>
                <input name="spelareID" type="hidden" value="<?php echo $row_rsSpelare['id']; ?>" />
                <input type="hidden" name="MM_insert" value="form1" />
                <input type="submit" name="submit" id="submit" value="Välj övningar" />
              </form>
              <p>
              </p>

               

               

              </body>
              </html>
              <?php
              mysql_free_result($Recordset1);

              mysql_free_result($rsSpelare);

              mysql_free_result($rsRef);
              ?>

               

              Thank's in advance

              Willy

              • 4. Re: Organizing checkbox group in rows and columns
                David_Powers ACP

                Have you tried contacting Tom Muck to see if he has a solution?

                • 5. Re: Organizing checkbox group in rows and columns
                  Willymannen Community Member

                  I have been contacting Tom Muck but unfortunately he hasn't respond.

                   

                  This seems to work though:

                   

                  <?php 

                  $id = $_POST['spelarID'];  

                      $drills = $_POST['drillSelect'];  

                   

                  $inserts = array();  

                  foreach ($drills as $drills) 

                      $inserts[] = "('$id','$drills','')";  

                   

                  $query = "INSERT INTO exercise_spelare_ref VALUES ". implode(", ", $inserts);  

                   

                  //echo "query = $query"; // for debugging purposes, remove this once it is working  

                  mysql_query($query) or die(mysql_error());  

                  ?>

                   

                  id and drillID are now inserted in Myssql table "exercise_spelare_ref" with new rows for every checked value.

                   

                  Then ofcourse a new question appears. When I want to make a update page where the inserted records shows as Checked I cant manage to pull that out of the table.

                  In a non dynamic checkbox I would do something like:

                   

                  <input <?php if (!(strcmp($row_rsRef['drillID'],1))) {echo "checked=\"checked\"";} ?> type="checkbox" name="CheckboxGroup1" value="1" id="CheckboxGroup1_0" />

                          chkbox1</label>

                  • 6. Re: Organizing checkbox group in rows and columns
                    Willymannen Community Member

                    I abandoned the Tom Muck looper and made a sandard Dreamweaver "Repeat Region" and "Recordset Pagin".

                    With the new code below I can (for test reason) echo correct id and drillID corresponding to every checkbox.

                     

                    <?php do { ?>
                          <tr>
                            <td width="96">
                             
                              <?php
                    $id = $row_rsRef['id'];
                    $drills = $row_Recordset1['drillID'];

                    //echo $id;
                    //echo $drills;
                    $arr = array($id,$drills);
                    $selected = implode(", ", $arr);
                    echo $selected;
                    ?>
                             
                            </td>
                            <td width="784">
                           
                            <input <?php if ($row_rsRef['drillID']==$drills) {echo "checked=\"checked\"";} ?>  type="checkbox" name="drillSelect[]" value="<?php echo $row_Recordset1['drillID']; ?>" id="drillSelect" />
                              <?php echo $row_Recordset1['rubrik']; ?>
                              </label>
                           
                            </td>
                          </tr>
                          <?php } while ($row_Recordset1 = mysql_fetch_assoc($Recordset1)); ?>

                     

                    You can see the output here.

                     

                    Then I think I should be able to show the checkbox as "checked" in some way (obviously not the ways I have tried) by using one or another of the variables $id, $drills or $selected if the row exists in the lookup table  "exercise_spelare_ref" where the values are inserted, when I want to do an update.

                     

                    id    drillID kommentar
                    ===============

                    1559  1       NULL

                    1559  4       NULL

                    1559  9       NULL

                    1560  1       NULL

                    1560  9       NULL