Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy returning a checkbox array - help with code

    hi there

    i have a mysql db table with a field named 'ans'.
    i also have a form with several checkboxes, which when checked implode into a comma delimited string. all this works fine.

    my problem occurs when i wish to return the string back to checkboxes for editing. here's what i have so far, but it doesn't work.

    it works if i use this line instead, but it needs to pull in from the db
    PHP Code:
    $saved_ans = array('a''b'); 
    what am i doing wrong?? please help!


    PHP Code:
    <?php 
    $ans 
    = array('a''b''c''d');
    $saved_ans explode(',',$ans);

    for (
    $i 0$i count($ans); $i++){ 
    $checked in_array($ans[$i],$saved_ans)? " checked" ""
    echo 
    "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n"

    ?>

    cheers, jas

  • #2
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Netscape

    Welcome here!

    Quote Originally Posted by jasboy
    ...it works if i use this line instead, but it needs to pull in from the db
    PHP Code:
    $saved_ans = array('a''b'); 
    what am i doing wrong?? please help!


    PHP Code:
    <?php 
    $ans 
    = array('a''b''c''d');
    $saved_ans explode(',',$ans);

    for (
    $i 0$i count($ans); $i++){ 
    $checked in_array($ans[$i],$saved_ans)? " checked" ""
    echo 
    "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n"

    ?>
    You don't realy show the most crucial code --> your db-result processing.
    This $saved_ans = explode(',',$ans); can never work since you're exploding the array from the line above.

    to do this with your db-value, you'll normally just have something like
    PHP Code:
    $sql "SELECT your_saved_options FROM yourtable WHERE yourid=$whatever";
    $result mysql_query($sql) or die ('Queryproblem');
    if (
    mysql_num_rows($result) === 1){
         
    $ans = array('a''b''c''d');
         
    $saved_ans explode(','mysql_result($result,0));

         for (
    $i 0$i count($ans); $i++){ 
            
    $checked in_array($ans[$i],$saved_ans)? " checked" ""
            echo 
    "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n"
         } 

    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #3
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sorry about missing stuff. did you mean this?

    PHP Code:
    $colname_Recordset1 "1";
    if (isset(
    $HTTP_GET_VARS['id'])) {
      
    $colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
    }
    mysql_select_db($database_conn$conn);
    $query_Recordset1 sprintf("SELECT * FROM q01 WHERE id = %s"$colname_Recordset1);
    $Recordset1 mysql_query($query_Recordset1$conn) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1); 
    all help very much appreciated!
    jas

  • #4
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    also, here is the complete form:
    PHP Code:
    <form method="post" name="form1" action="<?php echo $editFormAction?>">
      <table align="center">
        <tr valign="baseline">
          <td width="27" align="right" nowrap>Answers:</td>
          <td width="270">
    <?php 
    $ans 
    = array('a''b''c''d');
    $saved_ans explode(',',$ans);

    for (
    $i 0$i count($ans); $i++){ 
    $checked in_array($ans[$i],$saved_ans)? " checked" ""
    echo 
    "$ans[$i]: <input type=\"checkbox\" name=\"ans[$i]\" value=\"$ans[$i]\"$checked><br>\n"

    ?>
          </td>
        </tr>
        <tr valign="baseline">
          <td nowrap align="right">&nbsp;</td>
          <td><input type="submit" value="Update Record">
          </td>
        </tr>
      </table>
      <input type="hidden" name="MM_update" value="form1">
      <input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>">
    </form>
    cheers raf.

  • #5
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Quote Originally Posted by jasboy
    Sorry about missing stuff. did you mean this?

    PHP Code:
    $colname_Recordset1 "1";
    if (isset(
    $HTTP_GET_VARS['id'])) {
      
    $colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
    }
    mysql_select_db($database_conn$conn);
    $query_Recordset1 sprintf("SELECT * FROM q01 WHERE id = %s"$colname_Recordset1);
    $Recordset1 mysql_query($query_Recordset1$conn) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1); 
    we need to know the columnname where you store the selected options. in your code, you'd then need to use
    PHP Code:
    $saved_ans explode(',', ($row_Recordset1['your_column_name'])); 
    where your_column_name then needs to be replaced by the actual columnname.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #6
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    if attempted raf's example but to no avail. i'm a novice with php.
    could you simplify it for me??

    cheers

  • #7
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the column name is "ans". sorry to be a pain!!

  • #8
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still not working...

    i keep trying. trial and error. lots of errors though!!
    this is what i have so far...
    PHP Code:
    <?php 
    $sql
    *=*"SELECT*ans*FROM*tester*WHERE ans='{$row_Recordset1['ans']}'";
    $result*=*mysql_query($sql)*or*die*('Queryproblem'); 
    if*(
    mysql_num_rows($result)*===*1){ 
    *****
    $ans*=*array('a',*'b',*'c',*'d'); 
    *****
    $saved_ans*=*explode(',',*mysql_result($result,0)); 

    *****for*(
    $i*=*0;*$i*<*count($ans);*$i++){* 
    ********
    $checked*=*in_array($ans[$i],$saved_ans)?*" checked"*:*"";* 
    ********echo*
    "$ans[$i]: <input*type=\"checkbox\"*name=\"ans[$i]\"*value=\"$ans[$i]\"$checked><br>\n";* 
    *****}* 
    }
    ?>

  • #9
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    can anyone else help me on this problem? it seems so close, but it;s just quite not there yet.

    cheers guys

  • #10
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    what's the value of {$row_Recordset1['ans']} in
    $sql="SELECT ans FROM tester WHERE ans='{$row_Recordset1['ans']}'";
    ?

    add and
    echo $sql;

    right after that $sql="SELECT etc" line and then look at the posted query to see if a value is set for it.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #11
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    i'm very lost on all this now.
    would you be able to print out the complete script?

    my database table is called "tester"
    the table is called "q01"
    the "id" is url parameter driven. ie. editPage.php?id=5
    the field which i pull the comma separated string from is called "ans"
    the recordset is called "Recordset1"

    for ease, lets say that the values in the field "ans" are a,b,c,d

    also, for ease, lets say there are 6 checkboxes in the form with values of the following: a,b,c,d,e,f

    i hope this makes sense. many thanks for your time spent on this!!

  • #12
    raf
    raf is offline
    Master Coder
    Join Date
    Jul 2002
    Posts
    6,589
    Thanks
    0
    Thanked 0 Times in 0 Posts
    PHP Code:
    $sql "SELECT ans FROM q01 WHERE id="$_GET['id'];
    $result mysql_query($sql) or die ('Queryproblem');
    if (
    mysql_num_rows($result) === 1){
         
    $ans = array('a''b''c''d');
         
    $saved_ans explode(','mysql_result($result,0));

         for (
    $i 0$i count($ans); $i++){ 
            
    $checked in_array($ans[$i],$saved_ans)? ' checked="checked"' ''
            echo 
    $ans[$i], ': <input type="checkbox" name="'ans[$i] ,'" value="'$ans[$i], '" '$checked'><br />'
         } 

    from your explanation, i can't realy make up what the table is called ... tester or q01 ?

    if you get error, then post the first errormessage that you get.
    Posting guidelines I use to see if I will spend time to answer your question : http://www.catb.org/~esr/faqs/smart-questions.html

  • #13
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    sorry, my mistake,

    the database is called "tester"
    the table is called "q01"
    the field that has configurations of 'a,b,c,d,e' is called "ans"

    thanks for all your help!

  • #14
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    this is the entire page. i get this error:
    Parse error: parse error, unexpected T_CONSTANT_ENCAPSED_STRING in /Library/Apache2/htdocs/chBoxT/edit.php on line 53

    PHP Code:
    <?php require_once('Connections/conn.php'); ?>
    <?php
    function GetSQLValueString($theValue$theType$theDefinedValue ""$theNotDefinedValue ""
    {
      
    $theValue = (!get_magic_quotes_gpc()) ? addslashes($theValue) : $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 $HTTP_SERVER_VARS['PHP_SELF'];
    if (isset(
    $HTTP_SERVER_VARS['QUERY_STRING'])) {
      
    $editFormAction .= "?" $HTTP_SERVER_VARS['QUERY_STRING'];
    }

    if ((isset(
    $HTTP_POST_VARS["MM_update"])) && ($HTTP_POST_VARS["MM_update"] == "form1")) {
      
    $updateSQL sprintf("UPDATE q01 SET ans=%s WHERE id=%s",
                           
    GetSQLValueString(implode(',',$_POST['ans']), "text"),
                           
    GetSQLValueString($HTTP_POST_VARS['id'], "int"));

      
    mysql_select_db($database_conn$conn);
      
    $Result1 mysql_query($updateSQL$conn) or die(mysql_error());

      
    $updateGoTo "s.php";
      if (isset(
    $HTTP_SERVER_VARS['QUERY_STRING'])) {
        
    $updateGoTo .= (strpos($updateGoTo'?')) ? "&" "?";
        
    $updateGoTo .= $HTTP_SERVER_VARS['QUERY_STRING'];
      }
      
    header(sprintf("Location: %s"$updateGoTo));
    }

    $colname_Recordset1 "1";
    if (isset(
    $HTTP_GET_VARS['id'])) {
      
    $colname_Recordset1 = (get_magic_quotes_gpc()) ? $HTTP_GET_VARS['id'] : addslashes($HTTP_GET_VARS['id']);
    }
    mysql_select_db($database_conn$conn);
    $query_Recordset1 sprintf("SELECT * FROM q01 WHERE id = %s"$colname_Recordset1);
    $Recordset1 mysql_query($query_Recordset1$conn) or die(mysql_error());
    $row_Recordset1 mysql_fetch_assoc($Recordset1);
    $totalRows_Recordset1 mysql_num_rows($Recordset1);
    ?>
    <html>
    <head>
    <title>Untitled Document</title>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    </head>

    <body>
    <form method="post" name="form1" action="<?php echo $editFormAction?>">
      <table align="center">
        <tr valign="baseline">
          <td width="27" align="right" nowrap>Ans:</td>
          <?php
    $sql
    *=*"SELECT ans FROM q01 WHERE id=".*$_GET['id']; 
    $result*=*mysql_query($sql)*or*die*('Queryproblem'); 
    if*(
    mysql_num_rows($result)*===*1){ 
    *****
    $ans*=*array('a',*'b',*'c',*'d'); 
    *****
    $saved_ans*=*explode(',',*mysql_result($result,0)); 

    *****for*(
    $i*=*0;*$i*<*count($ans);*$i++){* 
    ********
    $checked*=*in_array($ans[$i],$saved_ans)?*' checked="checked"'*:*'';* 
    ********echo*
    $ans[$i],*': <input type="checkbox" name="',*ans[$i]*,'" value="',*$ans[$i],*'" ',*$checked,*'><br />';* 
    *****}* 

    ?>
        </tr>
        <tr valign="baseline">
          <td nowrap align="right">&nbsp;</td>
          <td><input type="submit" value="Update Record">
          </td>
        </tr>
      </table>
      <input type="hidden" name="MM_update" value="form1">
      <input type="hidden" name="id" value="<?php echo $row_Recordset1['id']; ?>">
    </form>
    </body>
    </html>
    <?php
    mysql_free_result
    ($Recordset1);
    ?>
    any thought??

  • #15
    New Coder
    Join Date
    Sep 2005
    Posts
    11
    Thanks
    0
    Thanked 0 Times in 0 Posts
    still not been able to figure out why this isn't working??

    i have to go back to my original code as this is the closest i have come to reaching a solution:
    PHP Code:
    <?php
    $ans*=*array('a',*'b',*'c',*'d'); 
    $saved_ans*=*explode(',',$ans); 

    for*(
    $i*=*0;*$i*<*count($ans);*$i++){* 
    $checked*=*in_array($ans[$i],$saved_ans)?*" checked"*:*"";* 
    echo*
    "$ans[$i]: <input*type=\"checkbox\"*name=\"ans[$i]\"*value=\"$ans[$i]\"$checked><br>\n";* 
    }* 
    ?>
    as i have stated, the database is called 'tester'
    the table is called 'q01'
    the field i wish to edit is called 'ans'

    i don't have a problem populating the field with a comma separated string from a series of checkboxes, but i do have a problem pulling it back to edit via checkboxes.

    any thoughts please, most welcome??


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •