Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

133
Views
parsing var name as string literal to assign part of string to new var

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?

over 4 years ago · Santiago Trujillo
2 answers
Answer question

0

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'";
}
over 4 years ago · Santiago Trujillo Report

0

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.

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!