#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    259
    Rep Power
    0

    Printing variable value out of 2 variables


    I have an issue with a bigger project, but instead of explainning all the main problem, I've made a smaller project as an example, you can see it here : Variable printing

    I know I can use Array, but on my bigger project, I can't.

    So, what I want to do is simply take a variable ($var1) with another variable ($var2) and 'combine' them to get the value of that new variable.

    Ex.: $var1 = checkbox and $var2 = 1,2,3,4,5, etc.

    So I can print this : $combined_var, which will be in fact $var1$var2 (checkbox1, checkbox2, checkbox3 and so on).

    Go check the link and check some of the checkboxes, you'll know what I mean.

    If you have questions, feel free to ask.

    Oh, here's the current code;

    PHP Code:
    <?php

        
    if ($submit == 'Submit')
        {
            print(
    "<b>Manual printout</b><br><br>");
            print(
    "checkbox_1 = $checkbox_1<br>");
            print(
    "checkbox_2 = $checkbox_2<br>");
            print(
    "checkbox_3 = $checkbox_3<br>");
            print(
    "checkbox_7 = $checkbox_7<br>");
            print(
    "checkbox_9 = $checkbox_9<br><br>");
            
            print(
    "<b>LOOP printout (while $xcount < 10 etc.)</b><br><br>");
            
            
    $xcount 1;
            
            while (
    $xcount 10)
            {
                print(
    "checkbox_$xcount = $checkbox_$xcount<br>");
                
                
    $xcount++;
            }
            
            print(
    "<br>It's not printing actual 'values' contained in the form, I want it to print like the 'manual printout' mode, but I want it to be automatic, like in this while loop.<br><br><hr size='1' noshade>");
        }

        print(
    "
        Those checkboxes are populated by a MySQL DB, their names are 'checkbox' with the ID for the MySQL field at the end, so : checkbox_
    $dbid, there's some missing, it's normal.<br><br>
        <form name='edit' action='
    $PHP_SELF' method='post'>
            <input type='checkbox' name='checkbox_1'> ID:1<br>
            <input type='checkbox' name='checkbox_2'> ID:2<br>
            <input type='checkbox' name='checkbox_3'> ID:2<br>
            <input type='checkbox' name='checkbox_7'> ID:7<br>
            <input type='checkbox' name='checkbox_9'> ID:9<br><br>
            <input type='submit' name='submit' value='Submit'>
        </form>
        "
    );
        
    ?>
  2. #2
  3. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Location
    The Pleiades
    Posts
    299
    Rep Power
    8
    Try adding a 'value={$db_value}' within your checkboxes.

    I don't 100% understand your issue

    Regards,

    NM.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    259
    Rep Power
    0
    Originally Posted by Nanomech
    Try adding a 'value={$db_value}' within your checkboxes.

    I don't 100% understand your issue

    Regards,

    NM.
    Actually, that is what I'm doing right now. The checkboxes in the form are 'printed' out from a MySQL while loop, so there's 5 entries in my DB, id1, id2, id3, id7 and id9.

    So, in the form I write in the source code name='checkbox_$dbid', the $dbid comes from my SQL, so the input field name becomes 'checkbox_1', 'checkbox_2', 'checkbox_3', 'checkbox_7', 'checkbox_9'.

    Now, when I submit the form, I do a manual printout, I manually write, for each checkbox, print("checkbox_1 = $checkbox_1<br>");, so it actually prints 'on' or nothing for the $checkbox_1 value (Do some test with the form, check some checkboxes and submit the form, you'll see some of them will write 'on' and others won't.

    Now, what I want to achieve is that I don't want to manually write each entry in the results output, I want to do a simple 'while' loop (while $xcount < 10) that will dynamically print the results, just like in 'LOOP printout' with this code;

    PHP Code:
     $xcount 1
             
            while (
    $xcount 10
            { 
                print(
    "checkbox_$xcount = $checkbox_$xcount<br>"); 
                 
                
    $xcount++; 
            } 
    BUT, I want the variable to actually print 'on' or nothing instead of only the number like in the results. I want '$checkbox_$xcount' to be a variable that is $checkbox_1 so it can output 'on' or nothing, just like my Manual printout, just like I would have wrote $checkbox_1.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2004
    Posts
    259
    Rep Power
    0
    Ok, I just actually made it!!

    Go over at 2 variables to make only one !

    And here's my code;

    PHP Code:
    <?php

        
    if ($submit == 'Submit')
        {
            print(
    "<b>Manual printout</b><br><br>");
            print(
    "checkbox_1 = $checkbox_1<br>");
            print(
    "checkbox_2 = $checkbox_2<br>");
            print(
    "checkbox_3 = $checkbox_3<br>");
            print(
    "checkbox_7 = $checkbox_7<br>");
            print(
    "checkbox_9 = $checkbox_9<br><br>");
            
            print(
    "<b>LOOP printout (while $xcount < 10 etc.)</b><br><br>");
            
            
    $xcount 1;
            
            while (
    $xcount 10)
            {
                
    $one "checkbox_";
                
    $two "$xcount";
                
    $value = ${$one.$two};
                print(
    "checkbox_$xcount = $value<br>");
                
                
    $xcount++;
            }
            
            print(
    "<br>It's not printing actual 'values' contained in the form, I want it to print like the 'manual printout' mode, but I want it to be automatic, like in this while loop.<br><br><hr size='1' noshade>");
        }

        print(
    "
        Those checkboxes are populated by a MySQL DB, their names are 'checkbox' with the ID for the MySQL field at the end, so : checkbox_
    $dbid, there's some missing, it's normal.<br><br>
        <form name='edit' action='
    $PHP_SELF' method='post'>
            <input type='checkbox' name='checkbox_1'> ID:1<br>
            <input type='checkbox' name='checkbox_2'> ID:2<br>
            <input type='checkbox' name='checkbox_3'> ID:3<br>
            <input type='checkbox' name='checkbox_7'> ID:7<br>
            <input type='checkbox' name='checkbox_9'> ID:9<br><br>
            <input type='submit' name='submit' value='Submit'>
        </form>
        "
    );
        
    ?>

IMN logo majestic logo threadwatch logo seochat tools logo