SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate $_POST Values

    Hi

    I would like to generate list of $_POST variables using records from database:
    example:

    PHP Code:
    $amount1=$_POST['amount1'];
    $amount2=$_POST['amount2'];
    $amount4=$_POST['amount4'];
    $amount7=$_POST['amount7'];
    ... and 
    so on .... 
    where number next to amount word is simply id value in the database. $_POST values i get from the form erlier.

    I tried following way and some other ways too but doesnt seems to work (and im not even sure if possible).

    PHP Code:
    $result2 mysql_query("SELECT * FROM list") or die(mysql_error());
              while(
    $row2=mysql_fetch_array($result2))
            {

            
    $amo "amount";    
            
    $amount $row2['id'];
            
    $amo1 $amo.$amount;
            
    $amo1 $_POST['$amo1'];
            echo 
    "($amo1)";

                } 
    Hope is clear what i want to do!
    Thank you very much in advance.

  2. #2
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by nitapita View Post
    Hi

    I would like to generate list of $_POST variables using records from database:
    example:

    PHP Code:
    $amount1=$_POST['amount1'];
    $amount2=$_POST['amount2'];
    $amount4=$_POST['amount4'];
    $amount7=$_POST['amount7'];
    ... and 
    so on .... 
    where number next to amount word is simply id value in the database. $_POST values i get from the form erlier.
    sounds like you need variable variables.

  3. #3
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Directly do:
    PHP Code:
    //-------------
    $amo1 $_POST['amount' $row2['id']];
    echo 
    $amo1;
    //------------- 
    Your problem here is about using quotes; single or double!
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  4. #4
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Great, thank you, its a progress..

    PHP Code:
    $amo1 $_POST['amount' $row2['id']]; 
    $_POST is genereted and is getting and displaying values from the form correctly.

    But there is one more think:

    From what i can understand whats happening now is

    PHP Code:
    $amo1 $_POST['amount' $row2['id']]; //-- 1 - id value so $amo1 = amount1 
    $amo1 $_POST['amount' $row2['id']]; //-- 2 -than  $amo1 = amount2
    $amo1 $_POST['amount' $row2['id']]; //-- 5 -than  $amo1 = amount5 
    Let say there is 50 id's, what i want to achive is to generate variables according to id value:
    Example:


    PHP Code:
    $amo1 $_POST['amount' $row2['id']]; //-- 1 - id value so $amo1 = amount1 
    $amo2 $_POST['amount' $row2['id']]; //-- 2- than  $amo2 = amount2
    $amo5 $_POST['amount' $row2['id']]; //-- 5 - than  $amo5 = amount5 
    How to generete variables $amo1, $amo2, $amo5 and ... more .... can that be done?

    Thank you

  5. #5
    Non-Member Max Height's Avatar
    Join Date
    Dec 2011
    Posts
    303
    Mentioned
    6 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by nitapita View Post
    How to generete variables $amo1, $amo2, $amo5 and ... more .... can that be done?
    The link I posted shows examples of what you want.

    Or another option is to use an array instead of $amo1, $amo2, $amo5......

  6. #6
    rajug.replace('Raju Gautam'); bronze trophy Raju Gautam's Avatar
    Join Date
    Oct 2006
    Location
    Kathmandu, Nepal
    Posts
    4,013
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Use array instead of generating such more variables:
    PHP Code:
    $result2 mysql_query("SELECT * FROM list") or die(mysql_error()); 
    $data = array();
    while(
    $row2 mysql_fetch_array($result2)) { 
        
    $data['amount' $row2['id']] = $_POST['amount' $row2['id']];
    }
    print_r($data); 
    Mistakes are proof that you are trying.....
    ------------------------------------------------------------------------
    PSD to HTML - SlicingArt.com | Personal Blog | ZCE - PHP 5

  7. #7
    Twitter: @AnthonySterling silver trophy AnthonySterling's Avatar
    Join Date
    Apr 2008
    Location
    North-East, UK.
    Posts
    6,111
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Another vote for an array, the less global variables the better.
    @AnthonySterling: I'm a PHP developer, a consultant for oopnorth.com and the organiser of @phpne, a PHP User Group covering the North-East of England.

  8. #8
    SitePoint Enthusiast
    Join Date
    May 2007
    Posts
    44
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hi

    Thanks a lot for your help, Got it right ! Used Arrays.
    Special thanks to Raju!


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
  •