SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Generate $_POST variables dynamically?

    Any ideas on this?

    PHP Code:
    <?php
    require_once('Connections/conn.php');

    // Find Current Portals (these can change)
    mysql_select_db($database_conn$conn);
    $rsColumns mysql_query("SHOW COLUMNS FROM portals LIKE 'portal\_%'");

    // Make Array of current portals
    while($row mysql_fetch_array ($rsColumns)){
        
    $whereColumn .= "`".$row['Field'] . "`, ";
    }
    $whereColumn substr_replace($whereColumn"", -2);

    // Portal status of an individual property
    mysql_select_db($database_conn$conn);

    $propertyID 1133// For testing puposes
    $rsPortalsQuery "SELECT {$whereColumn} FROM portals WHERE portalID LIKE $propertyID";
    $rsPortals mysql_query($rsPortalsQuery$conn);
    $array mysql_fetch_assoc ($rsPortals);


    //
    foreach($array AS $key => $value)
    {
         if (
    $value == "Yes") {
         
    $selectValue "<option value=\"Yes\" selected>Yes</option>
              <option value=\"No\">No</option>"
    ;
         } else {
         
    $selectValue "<option value=\"Yes\">Yes</option>
              <option value=\"No\" selected>No</option>"
    ;     
         }
          
          
    $tableRows .= "<tr> 
            <td>
    $key :</td>
            <td><select name=\"
    $key\">
                 
    $selectValue
              </select></td>
          </tr><br>"
    ;
    }

    // $tableRows is then inserted into a form...
    ?>

    So far so good. The table rows for the form display with the appropriate
    values selected, but how do I get these values into an SQL insert statement.

    Normally I'd retrieve them by using $_POST['portalName1'], $_POST['portalName2'] for <select name="portalName1">, <select name="portalName1">, etc, but in this case the select names are dynamically generated.

    So the question is how do I make a list of $_POST[xxx] variables exchanging the xxx for $key value from the array..?

    hasta later,
    kamm...

  2. #2
    SitePoint Zealot Pie's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ahh, easy.

    On the form make a hidden form that tells you how many different options there are.

    Then do a 'for' with ....
    PHP Code:
    eval('$object' $repeater ' = $_POST["' $repeater '"];'); 
    Zach Inglis
    Reality Art Studios Ltd
    ZachInglis.com Blog |
    Art / Code Chat & Tutorials | Work

  3. #3
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, I don't understand (new to using arrays and never used eval before)...

    Can someone elaborate a bit please.

    Tnx
    kamm...

  4. #4
    SitePoint Zealot Pie's Avatar
    Join Date
    Aug 2003
    Location
    UK
    Posts
    198
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    TipOk well if you've never used a function, goto www.php.net and look up the function.

    Anyway, i'll explain it as best as I can:

    $variable = "text";
    is the same as
    eval('$variable = "text";');

    You just eval-uate code, so:
    eval (" codehere ");

    so if you use variables in it they are parsed as the variable: i.e-

    eval (" echo "Hi $name"; );
    is the same as
    echo "Hi " . $name;

    Basically if you use your $variable in the eval it works like that. Sorry I cant explain well enough. Check php.net
    Zach Inglis
    Reality Art Studios Ltd
    ZachInglis.com Blog |
    Art / Code Chat & Tutorials | Work

  5. #5
    SitePoint Evangelist
    Join Date
    May 2004
    Location
    New Jersey, USA
    Posts
    567
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this (it's your code from above):

    PHP Code:
    <?php 
    require_once('Connections/conn.php'); 

    // Find Current Portals (these can change) 
    mysql_select_db($database_conn$conn); 
    $rsColumns mysql_query("SHOW COLUMNS FROM portals LIKE 'portal\_%'"); 

    // Make Array of current portals 
    while($row mysql_fetch_array ($rsColumns)){ 
        
    $whereColumn .= "`".$row['Field'] . "`, "

    $whereColumn substr_replace($whereColumn"", -2); 

    // Portal status of an individual property 
    mysql_select_db($database_conn$conn); 

    $propertyID 1133// For testing puposes 
    $rsPortalsQuery "SELECT {$whereColumn} FROM portals WHERE portalID LIKE $propertyID"
    $rsPortals mysql_query($rsPortalsQuery$conn); 
    $array mysql_fetch_assoc ($rsPortals);
    Then do this:


    PHP Code:
    foreach($array AS $key => $value

        if (isset(
    $_POST[$key]))
        {
            echo 
    "You set the selection for " $key
                 
    " to be: " $_POST[$key] . "<br />\n";
        }


  6. #6
    SitePoint Zealot kamm's Avatar
    Join Date
    Nov 2003
    Location
    Espaņa
    Posts
    162
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks guys...

    Dunno why I wasn't getting this yesterday - prolly too long in front of the screen...

    Anyway after running Austin's code I was back on track again, so thanks for that...

    hasta later,
    kamm...

  7. #7
    Non-Member
    Join Date
    Jan 2004
    Location
    Planet Earth
    Posts
    1,764
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I wouldn't worry too much about the use of eval(...) either

    I've been working with PHP for about 2 and a half years now and I still don't really understand it

    Never used it before - at all - and I avoid it like the plague, nothing but trouble from what I read on these forums anyway. Meant to be slow as well btw

  8. #8
    SitePoint Guru
    Join Date
    Feb 2004
    Location
    Oregon
    Posts
    686
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I use eval and don't have any problems. just have to know how to code it but yes it does take some gettinguse to.
    success is not by chance, it is by choice.


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
  •