Generate $_POST variables dynamically?

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

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…

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…

Sponsor our Newsletter | Privacy Policy | Terms of Service