www.webdeveloper.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 22

Thread: Combine variable to make new variable

  1. #1
    Join Date
    Apr 2009
    Posts
    107

    Combine variable to make new variable

    I have the variable $id and I want to make it $id1, $id2, $id3...
    PHP Code:
    $adder 1;

    $id.$adder++; 
    That makes the variable just 1 and not $id1.

    How can I code it so that it will make $id1?

  2. #2
    Join Date
    Feb 2009
    Posts
    102
    The way to do what you're asking is with eval()
    PHP Code:
    <?php
    for($adder 1$adder 10$adder++)
       eval(
    '$id' $adder ' = "whatever";');
    ?>
    However, that's probably not the best way. A better way would be to use an array:

    PHP Code:
    <?php
    $id 
    = array();
    $id[1] = "one";
    $id[2] = "two";
    for(
    $adder 3$adder 10$adder++)
       
    $id[$adder] = "Something";
    ?>
    This way you can use things like foreach loops (to loop through all the id variables even if you don't know how many variables there are) and array functions that will do a lot of cool stuff.
    -Steve


    "Build a man a fire and he'll be warm for a day. Set a man on fire and he'll be warm for the rest of his life."

  3. #3
    Join Date
    Apr 2009
    Posts
    107
    Not totally making sense to me. Let me give you more code to explain better.
    PHP Code:
    $adder 1;

    while(
    $adder <= $count)
        
        
    $id1=$_POST['id1'];
        
    $desc1=$_POST['desc1'];
        
    $value1=$_POST['value1'];
        
    $num1=$_POST['num1'];
        
        
    updateSpec($id1,$desc1,$value1,$num1
    I want to be able to repeat
    PHP Code:
        $id1=$_POST['id1'];
        
    $desc1=$_POST['desc1'];
        
    $value1=$_POST['value1'];
        
    $num1=$_POST['num1']; 
    without copying and pasting the code and changing the number 1 to 2, 2 to 3, and so on.

  4. #4
    Join Date
    Jan 2009
    Posts
    3,346
    Something like this?
    PHP Code:
    $adder 1;

    while(
    $adder <= $count){
        
        
    $id[$adder]=$_POST['id1'];
        
    $desc[$adder]=$_POST['desc1'];
        
    $value[$adder]=$_POST['value1'];
        
    $num[$adder]=$_POST['num1'];
        
        
    updateSpec($id[$adder],$desc[$adder],$value[$adder],$num[$adder]) ;
        
        
    $adder++;


  5. #5
    Join Date
    Nov 2008
    Posts
    2,477
    Like Stevish says you should really use an array for this. For example, if you create your forms like so:

    HTML Code:
    <input type="text" name="id[]" value="this is id 1" />
    <input type="text" name="id[]" value="this is id 1" />
    <input type="text" name="id[]" value="this is id 1" />
    Then you will receive post data as an array:

    PHP Code:
    echo $_POST['id'][0]; // this is id 1
    echo $_POST['id'][1]; // this is id 2 
    I don't see why you would need to create a whole new set of variables when the data is already there in the post array. What purpose does copying all the data serve?

  6. #6
    Join Date
    Apr 2009
    Posts
    107
    yes Could it also work like this?
    PHP Code:
    $adder 1

    while(
    $adder <= $count){ 
         
        
    $id[$adder]=$_POST['id[$adder]']; 
        
    $desc[$adder]=$_POST['desc[$adder]']; 
        
    $value[$adder]=$_POST['value[$adder]']; 
        
    $num[$adder]=$_POST['num[$adder]']; 
         
        
    updateSpec($id[$adder],$desc[$adder],$value[$adder],$num[$adder]) ; 
         
        
    $adder++; 

    I need both numbers to change.

  7. #7
    Join Date
    Apr 2009
    Posts
    107
    here is the form page code:
    PHP Code:
    <tbody>
    <?php
    $num_adder
    1;
    $desc_adder1;
    $value_adder 1;
    $id_adder 1;
                                     while(
    $spec=mysql_fetch_array($sql)) { ?>
            <tr>
                <td width="10%" valign="top"><input type="text" size="2" value="<?php echo $spec['num']; ?>" name="num<?php echo $num_adder++; ?>"></td>
                <td width="30%" valign="top"><input type="text" size="30" value="<?php echo $spec['descriptor']; ?>" name="desc<?php echo $desc_adder++; ?>"></td>
                <td width="30%" valign="top"><textarea name="value<?php echo $value_adder++; ?>"><?php echo $spec['value']; ?></textarea></td>
                <input type="hidden" value="<?php echo $spec['id']; ?>" name="id<?php echo $id_adder++;?>">
                <td width="30%"></td>
            </tr>
    <?php ?>
                            </tbody>

  8. #8
    Join Date
    Apr 2009
    Posts
    107
    would something like this work?
    PHP Code:
    $count 25;
    for(
    $adder 1$adder $count$adder++) 
       
    $id[$adder] = $_POST['id[$adder]']; 
    would it then do this 25 times?
    Last edited by themonkey40; 02-10-2010 at 10:52 AM.

  9. #9
    Join Date
    Apr 2009
    Posts
    107
    would something like this work?
    PHP Code:
    $count 25
    for($adder 1$adder $count$adder++) 
       
    $id[$adder] = $_POST['id[$adder]']; 
    updateSpec($id[$adder],$desc[$adder],$value[$adder],$num[$adder]); 
    or to Mindzai point, count I just do
    PHP Code:
    $count 25
    for($adder 1$adder $count$adder++) 
    $_POST['id[$adder]']; 
    $_POST['desc[$adder]']; 
    $_POST['value[$adder]']; 

    updateSpec($_POST['id[$adder]'],$_POST['desc[$adder]'],$_POST['value[$adder]']); 
    Last edited by themonkey40; 02-10-2010 at 10:54 AM.

  10. #10
    Join Date
    Apr 2009
    Posts
    107
    when I use the following code:
    PHP Code:
    $count 25;
    for(
    $i 1$i $count$i++) {
        echo 
    "the var is" $id[$i] . "<br>";
    }
    exit(); 
    i get the following output:
    HTML Code:
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    the var is
    number and var are missing. not sure why?

  11. #11
    Join Date
    Jan 2009
    Posts
    3,346
    Something more like?
    PHP Code:
    $i 0;
    foreach(
    $_POST['id'] as $id) {
        echo 
    "the var is" $_POST['id'][$i] . "<br />";
        
    $i++;


  12. #12
    Join Date
    Apr 2009
    Posts
    107
    criterion9 I tried your code, but got the following error message:

    Warning: Invalid argument supplied for foreach() in /edit_spec_process.php on line 5

    Any ideas why?

  13. #13
    Join Date
    Jan 2009
    Posts
    3,346
    This will only run the code when the parameters are correct. Have you submitted the form correctly (i.e. form elements' name attribute = "id[]")?
    PHP Code:
    if(isset($_POST['id']) && is_array($_POST['id'])){
        
    $i 0;
        foreach(
    $_POST['id'] as $id) {
            echo 
    "the var is" $_POST['id'][$i] . "<br />";
            
    $i++;
        } 


  14. #14
    Join Date
    Apr 2009
    Posts
    107
    What should my form look like?

  15. #15
    Join Date
    Jan 2009
    Posts
    3,346
    Quote Originally Posted by Mindzai View Post
    Like Stevish says you should really use an array for this. For example, if you create your forms like so:

    HTML Code:
    <input type="text" name="id[]" value="this is id 1" />
    <input type="text" name="id[]" value="this is id 1" />
    <input type="text" name="id[]" value="this is id 1" />
    Then you will receive post data as an array:

    PHP Code:
    echo $_POST['id'][0]; // this is id 1
    echo $_POST['id'][1]; // this is id 2 
    I don't see why you would need to create a whole new set of variables when the data is already there in the post array. What purpose does copying all the data serve?
    See above post.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

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
  •  
HTML5 Development Center



Recent Articles