How do I use foreach within a while loop

Helen Lee

I have a script that displays all the values in a db table. I'd like to be able to update the price value of each row individually on clicking the update button but as it's in a while loop it's only updating the final value successfully. I realise this probably requires foreach but I can't figure where to code it within the loop to send the data from each row to the processing script on the next page. Can anyone help? Thank you!

Display script

<h2>Boiler Prices</h2>
<?php
$sql = "SELECT * FROM emb_prices_boilers ORDER BY id ASC";
$result = $connection->query($sql);

if ($result->num_rows > 0) {

  /* Start Form and Table ---------------------------------------------------------*/

  echo "<form action='../admin/actions/set-boiler-price.php' method='post'>";
  echo "<table id='boiler-prices'>
  <tr>
    <th>ID</th>
    <th>Type</th>
    <th>Boiler</th>
    <th>Ref</th>
    <th>Price</th>
  </tr>";

  /* Start Loop and Variables --------------------------------------------*/

  while($row = $result->fetch_assoc()) {

  $id = $row["id"];
  $type = $row["type"];
  $manufacturer = $row["manufacturer"];
  $model = $row["model"];
  $boilerref = $row["boilerref"];
  $price = $row["price"];

  // output data of each row
  echo "
  <tr>
    <td>" .$id. "</td>
    <td>" .$type. "</td>
    <td>" .$manufacturer. " " .$model. "</td>
    <td>" .$boilerref. "</td>
    <td>&pound;<input type='text' name='price' value='" .$price. "' /><input type='hidden' name='boilerref' value='" .$boilerref. "' /><input type='submit' value='update' /></td>
  </tr>";      

  }
/* End Loop -------------------------------------------------------*/

echo "</table>";
echo "</form>";
/* End Table and Form -------------------------------------------------------*/

}

else {

echo "0 results";

}

?>

Processing script

<?php

$boilerref = $_POST['boilerref'];
$price = $_POST['price'];

if ($setboilerprice = mysqli_prepare($connection, "UPDATE  emb_prices_boilers SET price = ? WHERE boilerref = ?")
) {

/* bind parameters for markers */
mysqli_stmt_bind_param($setboilerprice, "ds", $price, $boilerref );

/* execute query */
mysqli_stmt_execute($setboilerprice);

/* close statement */
mysqli_stmt_close($setboilerprice);

}
else {
    echo "error: Failed to write to db"; 
}

?>
u_mulder

What you need is to create a separate form for each record. Remove <form>/</form> tags from the beginning and the end of <table> and use this code in your last td:

echo "
    ...
    <td>
        <form action='../admin/actions/set-boiler-price.php' method='post'>
            &pound;<input type='text' name='price' value='" .$price. "' />
            <input type='hidden' name='boilerref' value='" .$boilerref. "' />
            <input type='submit' value='update' />
        </form>
    </td>
</tr>";

With such mark up each form will send it's own data to your set-boiler-price.php and updating will work as you expect.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

How do I create and name checkboxes within a foreach loop PHP

How do I print foreach loop content within a controller?

How do I call an array within an IIFE using a forEach loop?

How can I use <bind> within a loop while using MyBatis?

How do I return a value within a while loop

How do I create a while loop within a PHP string?

How do I get multiple conditions within one while loop?

How do I update an api call within a while loop?

How Do I Exit While Loop From Within a Switch Statement

How do I use cor.test within foreach in R?

How do I use getline in do-while loop?

How do I properly use a function in a do/while loop?

How do I use Do-while Loop and Switch statement?

How do I use Do While to stop a loop?

How to reset do while loop within while loop?

How do I get it to loop while getting it to save what happened within the loop?

How can I use a FOREACH loop inside of a WHILE loop to create several columns

How do I configure while loop to use picamera?

How do I use a while loop with an onTouch function? (Android)

How do i use user input for a string array that is in a while loop?

Correct use of foreach loop within foreach loop

How do I retrieve data from within a handlebars loop and use it within backbone model?

In Python, how do I restart a for loop within a while loop? The furthest out loop is a while loop that runs while r < y(length of integer list)

How do i convert for loop to while loop?

How do I convert a for loop into a while loop?

How do I end a while loop with a for loop in it?

how do i use For loop to calculate and print the sums from within the loop? Python

How do I leave a while loop which is within an if statement and carry on with the if statement?

How do I use a computed function to specify the CSS binding within a foreach observable array