SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    problem storing variables in array

    A user has the option to check off any combination of 6 check boxes on the form. After he hits submit, the following code should execute.

    PHP Code:
        while (list ($key,$val) = @each ($box)) {
            echo 
    "$val,";
            
    $array[i] = $val ;
            echo 
    $array[i] ;
            
    $i $i;
            
    $a $i ;    
        }

    //output array contents
    for ($i 1$i <= $a$i++) {

        echo 
    $array[i] ;


    "While ()" function is working; it outputs variables. but array (last portion of the code) is only outputting last variable checked. How can I go by storing user entered variables into array?

    Thank you.

  2. #2
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I am not too sure I can help. I am pretty new at this BUT,
    I think you would be better off with a foreach loop instead.

    I am achieving what I want to assemble the checkbox list in a form with the following:
    (assuming that $box is an array with the check box values)


    PHP Code:
    $box=array('optionname'=>'This is a check option',
                    
    'option2name'=>'this is the second option');

    foreach (
    $box as $key=>$val) {
        echo 
    '<label><input type="checkbox" name="' $key '" value="' $key '"  /> ' $val '</label>';
        echo 
    "\n";


    Then to validate, I use the same $box array to check that the value submitted is a key in the array:

    PHP Code:
    if (!array_key_exists($_POST['value'], $box) || $_POST['value'] != '')) {
    $errormsg 'There was an error with your submission';

    of course you should escape those POST values first.
    Last edited by agentforte; Jun 6, 2007 at 08:39. Reason: fixed code a bit

  3. #3
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I fixed the code. It is outputting desired result in Localhost, but when I test it online on shared server, it says:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in ..../test1.php on line XX (the line with "$querzipytrack1 = mysql_fetch_array($queryziptrack2)")
    PHP Code:
    if(register_globals) {
    echo 
    "Register globals is ON";}
    else{echo 
    "Register globals is OFF";}

    include 
    'init.php';
    $a ;
        while (list (
    $key,$val) = @each ($_POST['box'])) {
            
    $a $a ;
        }

    for (
    $j 0$j <= $a-1$j++) {

        for (
    $i 0$i <= $a-1$i++) {
        
    $box1 $box["$i"] ;
        
    $box2 $box["$j"] ;
        
    $queryziptrack2 mysql_query("SELECT {$box1} FROM paint WHERE color='$box2'");
        
    $querzipytrack1 mysql_fetch_array($queryziptrack2);
            if (
    $querzipytrack1["{$box1}"] == 1) {
                
    $masking "Extra masking is required" ;
            } else {
                
    $masking1 "Extra masking is not required" ;
            }
        }

    Register Globals is ON.

  4. #4
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Why are there curly braces around $box1 in your sql query for "$queryziptrack2"?
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  5. #5
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by byron3@earthlink View Post
    Why are there curly braces around $box1 in your sql query for "$queryziptrack2"?
    Should anything else be there?

    As I mentioned, it works flawless on localhost, but issues that warning on server when uploaded.

  6. #6
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, try removing the curly braces first.

  7. #7
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $queryziptrack2 mysql_query("SELECT $box1 FROM paint WHERE color='$box2'"); 
    did not work. I don't think there is problem with {}.

    Is there anything else like Register_Globals that might be different in localhost vs shared server?

  8. #8
    Always learning kigoobe's Avatar
    Join Date
    May 2004
    Location
    Paris
    Posts
    1,565
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    $queryziptrack2 mysql_query("SELECT ".$box1." FROM paint WHERE color='".$box2."'"); 
    And this? Else, try this -

    PHP Code:
    $queryziptrack2 mysql_query("SELECT ".$box1." FROM paint WHERE color=".$box2); 

  9. #9
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by kigoobe View Post
    PHP Code:
    $queryziptrack2 mysql_query("SELECT ".$box1." FROM paint WHERE color='".$box2."'"); 
    And this? Else, try this -

    PHP Code:
    $queryziptrack2 mysql_query("SELECT ".$box1." FROM paint WHERE color=".$box2); 
    1st one worked on localhost, but produced the same warning on shared server.

    2nd one did not work at all.

  10. #10
    SitePoint Addict agentforte's Avatar
    Join Date
    May 2007
    Location
    Toronto, ON, Canada
    Posts
    213
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb fix

    remember, when you use single quotes '$something' the string is taken literally as $somthing
    if you use double quotes "$something" the value is taken as a variable.

    so the statement WHERE color='$box2' whould look for the string $box2 in the color column. it should be color=$box2.

    I found that my server requires a different type of quote for sql queries: `
    (usually above the tab key on the left of your keyboard)

    the new sql would be:

    PHP Code:
    $queryziptrack2 mysql_query("SELECT `$box1` FROM `paint` WHERE color=`$box2`"); 


    since you used double quotes, it should be fine. Another way you could probably do it is this:

    PHP Code:
    $queryziptrack2 mysql_query('SELECT `' $box1 '` FROM `paint` WHERE color=`' $box2 '`'); 
    I hope this helps.

  11. #11
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    My bad. register_globals was OFF, but this script said that it was ON:
    PHP Code:
    if(register_globals) {
    echo 
    "Register globals is ON <br>";}
    else{echo 
    "Register globals is OFF <br>";} 
    How can I make this work when register_globals = OFF


    PHP Code:
    while (list ($key,$val) = @each ($_POST['box'])) {
        $
    $key $val;
        echo 
    "$val$key";


  12. #12
    SitePoint Evangelist
    Join Date
    Aug 2005
    Posts
    453
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($_POST AS $key => $value) {
        ${
    $key} = $value;

    Read the online documentation:http://us2.php.net/register_globals
    Computers and Fire ...
    In the hands of the inexperienced or uneducated,
    the results can be disastrous.
    While the professional can tame, master even conquer.

  13. #13
    SitePoint Enthusiast
    Join Date
    Oct 2006
    Posts
    78
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    PHP Code:
    foreach($_POST AS $key => $value) {
        ${
    $key} = $value;

    did not work. Output: "Extra masking is not required."

    whole code:

    PHP Code:
    include 'xxx.php';
    $a ;

    //    while (list ($key,$val) = @each ($_POST['box'])) {
    //    $$key = $val;
    //        $a = $a + 1 ;
    //    }

    foreach($_POST AS $key => $value) {
        ${
    $key} = $value;
        
    $a $a ;


    for (
    $j 0$j <= $a-1$j++) {

        for (
    $i 0$i <= $a-1$i++) {
        
    $box1 $box["$i"] ;
        
    $box2 $box["$j"] ;
        
    $queryziptrack2 mysql_query("SELECT {$box1} FROM paint WHERE color='$box2'");
        
    $querzipytrack1 mysql_fetch_array($queryziptrack2);
        
        if (
    mysql_num_rows($queryziptrack2) == 0) {
        echo 
    "No rows found, nothing to print so am exiting";
        exit;
        }

        if (
    $querzipytrack1["{$box1}"] == 1) {
            
    $masking "<font color=red>Extra masking is required.</font>" ;
        } else {
            
    $masking1 "<font color=red>Extra masking is not required.</font>" ;
        }

        }
    }

    echo 
    "<form method=post action=''>";
    echo 
    "<table border='0' cellspacing='0' style='border-collapse: collapse' width='200' >

    <tr bgcolor='#ffffff'>
    <td width='25%'><input type=checkbox name=box[] value='black' /></td>
    <td width='25%'>&nbsp;Black</td>
    .....................................
    <tr><td colspan =6 align=center><input type=submit value='Tell Me'></form></td></tr>
    </table>"

    below worked when register_globals = on

    PHP Code:
    foreach($_POST['box'] AS $key => $value) {
        ${
    $key} = $value;
        
    $a $a ;



Bookmarks

Posting Permissions

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