I am attaching a database table id number to buttons dynamically rendered, like so:
echo '</div><!-- close formRowDiv -->';
echo '<div class="buttonDiv">';
echo '<input type="submit" id="buttonUpdate_' . $row[ 'id' ] . '" name="buttonUpdate_' . $row[ 'id' ] . '" class="button" value="Update Trouble Log Entry">';
echo '</div><!-- closes updateDiv -->';
So that when clicked I end up with $_POST[ 'buttonUpdate_1' ] for example as id #1 in the DB table referenced. Now I want to extract the 1 from the $_POST var name.
I am assuming I need to use preg_replace but can't think how to use the var name literally in pattern to extract the id # attached. I have been googling and can't seem to find exactly what I need either. So how can I parse a var name as literal string in preg_replace to extract and assign part of it to a new var?
Although not a preg-replace solution like you asked, if I read your question right, you want to be able to do something to the row in the table with the appended id number.
If so, you could first, query the table for the highest value id and use that as a counter in a for loop. Then using a for loop you could cycle thru all rows in the table until the attached ID in your $_POST[ 'buttonDisplay_1' ] variable is found/matched.
Once matched, you can then grab the ID number of the returned query row and do whatever you want to the row with the grabbed ID number. Maybe something like:
$sql_query_to_find_high_id = "SELECT * FROM table ORDER BY id DESC LIMIT 1";
$result=mysqli_query(dbcon(), $sqlQHighId);
while ($row=mysqli_fetch_array($result)){high_id_number=$row['id'];}
To find the highest assigned id in the table. Then to loop thru using that high id number as your counter you could:
for($i=0; $i<$high_id_number; $i++){ // cycle thru all rows in table to find a match for ID appended to $_POST[ 'buttonDisplay_1' ]
if(isset($_POST["buttonUpdate_$i"])){
$id=$i;
$sql_update_statement="UPDATE table SET column=value WHERE id='$id'";
}
Name the input fields in array-style. I don't know the right name for it. Anyways, it looks like this:
echo '<input type="text" name="myinput[' . $row[ 'id' ] . ']">';
if you post the data you will get an array with the data of the input fields. so if you write
foreach ($_POST['myinput'] as $id => $input) {
echo $id;
echo $input;
}
it will return the data of the input fields line by line.