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 8 of 8
  1. #1
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts

    Problem with loop and $i

    Hi,

    I have 68 elements in my form and when I process it
    I would like to use a loop so that I don't have to write
    the same code out 68 times.

    The problem is that it is not liking the $i

    This is my code:


    PHP Code:
    IF(isset($_POST['saver']) && $_POST['saver'] == "sav85pg"){ 
            
        for (
    $i=1$i<=68$i++) {
            if(isset(
    $_POST['ch$i'])) {
                
    $N_ch.$i $_POST['ch$i'];
                
    $D_ch.$i safe_sql($N_ch.$i);
                } 
    // end if
            
    // end for
            
        
    for ($i=1$i<=68$i++) {
            echo 
    "D_ch$i: $D_ch.$i<br>";
            } 
    // end for
        
        
    // end IF 

    If I wasn't using the loop I would be writing:


    PHP Code:
    IF(isset($_POST['saver']) && $_POST['saver'] == "sav85pg"){ 

    if(isset(
    $_POST['ch2'])) {
        
    $N_ch1 $_POST['ch1'];
        
    $D_ch1 safe_sql($N_ch1);
        } 
    // end if

    if(isset($_POST['ch1'])) {
        
    $N_ch2 $_POST['ch2'];
        
    $D_ch2 safe_sql($N_ch2);
        } 
    // end if

    if(isset($_POST['ch3'])) {
        
    $N_ch3 $_POST['ch3'];
        
    $D_ch3 safe_sql($N_ch3);
        } 
    // end if

    etcetc.

    // end IF 

    The error I get is:

    Notice: Undefined variable: D_ch in /home/com567b/public_html/saver-pro.php on line 116 D_ch1: .1
    Notice: Undefined variable: D_ch in /home/com567b/public_html/saver-pro.php on line 116 D_ch2: .2
    Notice: Undefined variable: D_ch in /home/com567b/public_html/saver-pro.php on line 116 D_ch3: .3


    Would appreciate any help in correcting my code.

    Thanks.


    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #2
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    You need to use arrays, starting with your form field name ( name='somename[]' ) all the way to the end of using the values in your form processing code, so that you can use array functions to processing the data.

    What type of form fields are these? text, textarea, checkboxes, radiobuttons?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #3
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    My rows in my form are:

    Code:
    <input type = "checkbox" id = "x_mus" name='ch1' <?php echo $chk_1 ?> value = '<?php echo $ch1 ?>' ><label for = "x_mus">Music</label>
    
    <input type = "checkbox" id = "x_mov" name='ch2' <?php echo $chk_2 ?> value = '<?php echo $ch2 ?>' ><label for = "x_mov">Movies</label>
    
    <input type = "checkbox" id = "x_gam" name='ch3' <?php echo $chk_3 ?> value = '<?php echo $ch3 ?>' ><label for = "x_gam">Games</label>
    
    ... up to ...
    
    <input type = "checkbox" id = "x_trv" name='ch68' <?php echo $chk_68 ?> value = '<?php echo $ch68 ?>' ><label for = "x_trv">Travel Accessories</label>
    I have already written these out so I was not looking to use an array
    for the form.

    If I just fix the code that I showed in the OP, I think that will be fine.

    Thanks



    .
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #4
    Master Coder felgall's Avatar
    Join Date
    Sep 2005
    Location
    Sydney, Australia
    Posts
    6,462
    Thanks
    0
    Thanked 633 Times in 623 Posts
    Replace $D_ch.$i with ${'D_ch'.$i}
    Stephen
    Learn Modern JavaScript - http://javascriptexample.net/
    Helping others to solve their computer problem at http://www.felgall.com/

    Don't forget to start your JavaScript code with "use strict"; which makes it easier to find errors in your code.

  • #5
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,090
    Thanks
    51
    Thanked 506 Times in 493 Posts
    Quote Originally Posted by jeddi View Post
    PHP Code:
            if(isset($_POST['ch$i'])) {
                
    $N_ch.$i $_POST['ch$i']; 
    Those two lines of code use the wrong quote marks. You are using single quotes so what you see is what you get and this php won't access the item you think it will from the $_POST array because ch$i doesn't exist (instead it's ch1, ch2, ch3 etc).

    You need to change the single quotes to something like this:
    $_POST['ch' .$i]

    OR
    $_POST["ch$i"]//Possibly won't work

    OR
    $_POST["ch{$i}"]
    My helpful sig is on vacation trying to loose some weight. It got a bit fat and caused a few problems but it will be back at some point!

  • #6
    Senior Coder
    Join Date
    May 2006
    Posts
    1,673
    Thanks
    28
    Thanked 4 Times in 4 Posts
    Thanks very much for your help felgall and tangoforce.

    Very much appreciated.

    I still have one problem - and that is:

    In the form I have this:

    Code:
    Code:
    <input type = "checkbox" id = "x_mus" name='ch1' <?php echo $chk_1 ?> value = '<?php echo $ch1 ?>' ><label for = "x_mus">Music</label>

    I am returning the value from the form .. but ...
    How does the value change in the form ?

    Does it change automatically when the checkbox is checked ???

    Thanks.


    .
    Last edited by jeddi; 05-11-2013 at 05:27 AM.
    If you want to attract and keep more clients, then offer great customer support.

    Support-Focus.com. automates the process and gives you a trust seal to place on your website.
    I recommend that you at least take the 30 day free trial.

  • #7
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    Only checked checkboxes are submitted. If you provide a value = ' ... ' attribute, that's the value that is submitted. If you don't provide a value = ' ... ' attribute, the value 'on' is submitted.

    Most people just test if a checkbox is set or not and the actual value doesn't matter. Are you actually using the submitted value or are you just testing if the checkbox is set?
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.

  • #8
    Senior Coder CFMaBiSmAd's Avatar
    Join Date
    Oct 2006
    Location
    Denver, Colorado USA
    Posts
    2,958
    Thanks
    2
    Thanked 304 Times in 296 Posts
    I'm going to try and sell you on using arrays again. I know in addition to the 68 checkbox fields, you have 68 sets of logic to produce the $chk_1, $chk_2, ... checked='checked' values and with your question about the value being submitted, you either need 68 more sets of logic to set the values to something or you need to edit the 68 checkbox fields to remove the value= ' ... ' attribute.

    In the time it will take you to just edit those 68 checkbox fields one time, you can replace all that form logic with the following -
    Code:
    <?php
    
    // define the fields (or even better, I suspect you have them defined in a database table already and can query for them)
    $data = array(1=>'Music','Movies','Games', ... the rest of your list here...);
    
    // some sample form processing code
    echo '<pre>',print_r($_POST,true),'</pre>'; // look at the posted data
    // here is how you can process the submitted 'ch' array all at once
    if(isset($_POST['ch']) && is_array($_POST['ch'])){
    	// at least one checkbox was checked
    	$ch_safe = array_map('sql_safe',$_POST['ch']); // escape all the 'ch' data (assuming the submitted values will be used in a query)
    }
    
    
    // produce the fields in the form
    foreach($data as $i => $legend){
    	$chk = isset($_POST['ch'][$i]) ? "checked='checked'" : '';
    	echo "<input type = 'checkbox' id = 'x_$i' name='ch[$i]' $chk><label for = 'x_$i'>$legend</label>\n";
    }
    You can wrap a <form></form> around the checkbox fields to test the above code by itself to see what it does. By storing the fields in an array, $data in the above code, or getting them from a database query, the only thing you need to do to add/remove/change the form is to change the list of fields. The form field is only defined once, in case you need to change any of the markup in it. You only have one place, not 68 of them to change.
    Last edited by CFMaBiSmAd; 05-11-2013 at 05:01 PM.
    If you are learning PHP, developing PHP code, or debugging PHP code, do yourself a favor and check your web server log for errors and/or turn on full PHP error reporting in php.ini or in a .htaccess file to get PHP to help you.


  •  

    Tags for this Thread

    Posting Permissions

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