# Generate $\_POST variables dynamically?

**URL:** <https://www.phphelp.com/t/generate-post-variables-dynamically/8979>\
**Category:** General PHP Help\
**Created:** [May 22, 2004, 3:46pm UTC](https://www.phphelp.com/t/generate-post-variables-dynamically/8979 "2004-05-22T15:46:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kammo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kammo](https://www.phphelp.com/u/kammo)\
**Post date:** [May 22, 2004, 3:46pm UTC](https://www.phphelp.com/t/generate-post-variables-dynamically/8979/1 "2004-05-22T15:46:52Z")

</div>

The question is under the code. Anyone know how to do this? ![:-?]()

[php]\<?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 = “Yes  
No”;  
} else {  
$selectValue = “Yes  
No”;   
}

```
  $tableRows .= "<tr> 
    <td>$key :</td>
    <td><select name="$key">
         $selectValue
      </select></td>
  </tr><br>";

```

}  
echo $tableRows;  
?\> [/php]

//  
// 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 , , 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…?  
//

---

<div class="post-metadata">

**Author:** ![aixccapt99](https://www.phphelp.com/letter_avatar_proxy/v4/letter/a/e0b2c6/32.png) [@aixccapt99](https://www.phphelp.com/u/aixccapt99)\
**Post date:** [May 22, 2004, 7:12pm UTC](https://www.phphelp.com/t/generate-post-variables-dynamically/8979/2 "2004-05-22T19:12:52Z")

</div>

You can use a foreach loop to build a list of the keys to the array.

$keys = array();  
foreach($\_POST as $key =\> $value) {  
$keys[] = $\_POST[$key];  
}

I didn’t read your code closely, so I don’t know if that really solves your problem. But here’s hoping…

---

<div class="post-metadata">

**Author:** ![kammo](https://www.phphelp.com/letter_avatar_proxy/v4/letter/k/7c8e57/32.png) [@kammo](https://www.phphelp.com/u/kammo)\
**Post date:** [May 23, 2004, 12:52pm UTC](https://www.phphelp.com/t/generate-post-variables-dynamically/8979/3 "2004-05-23T12:52:03Z")

</div>

Dunno why I couldn’t sort this yesterday - prolly too long without a break… ![:o]()

Anyway I ended up with this:

`foreach($array AS $key => $value) 
{ 
    if (isset($_POST[$key])) 
    { 
        $values .= "'" . $_POST[$key] . "', "; 
    } 
}`

Seems to do the trick.

Thanks for your help aixccapt99

hasta later,  
kamm…
