Parsing var name as a string literal to assign part of a string to a new var

I bind a database table id

to dynamically rendered buttons like this:

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 when clicked, me ends up $_POST[ 'buttonUpdate_1' ]

like id # 1 in the referenced DB table. Now I want to extract 1 from the variable name $ _POST var.

I guess I need to use preg_replace

, but can't think of how to use the var name literally in the template to extract the attached # ID. I've been googling and can't seem to find what I need. So how can I parse the var name as a literal string in preg_replace

to extract and assign part of it to the new var?

+3


source to share


2 answers


Even though you are not asking a pre-replaceable solution as you asked, if I read your question correctly, you want to do something on the row in the table with the id number appended.

If so, you can first query the table for the highest id and use it as a counter in the for loop. Then, using a for loop, you can iterate over all the rows in the table until the attached id is found / matched in your $ _POST ['buttonDisplay_1'] variable.

Once negotiated, you can get the id of the returned query string and do whatever you want on the captured id string. 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 identifier in the table. Then, to loop through using that high id as a 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'";
}

      

+4


source


Name the input fields in array style. I don't know the correct name. In any case, it looks like this:

echo '<input type="text" name="myinput[' . $row[ 'id' ] . ']">';

      

if you post data you will get an array with input fields data. so if you write



foreach ($_POST['myinput'] as $id => $input) {
    echo $id;
    echo $input;
}

      

it will return the input fields data in turn.

+1


source







All Articles