PHP getting variables outside while loop

最后都变了- 提交于 2019-12-13 03:36:32

问题


earlier i posted a question about getting a single constant value from a loop and use it outside of the loop (reference : Question). now i'm facing another problem with another while loop. it goes like this: i have a query:

$sql = "
        SELECT alarms.id, alarms.date, clients.name, clients.number, alarms.controller, 
        alarmstype.type, alarms.alarmstatus, alarms.starttime, alarms.endtime
        FROM alarms
        INNER JOIN clients ON alarms.clientid = clients.id 
        INNER JOIN alarmstype ON alarms.typeid = alarmstype.id
        WHERE alarms.alarmstatus = 'ON' ORDER BY alarms.date ASC";
$result = mysql_query($sql);

and for each row i have assinged a button in the while loop:

      while ($row = mysql_fetch_array($result)) {
      print "<tr>
      <td>" . $row['date'] . "</td>
      <td>" . $row['number'] . " | " . $row['name'] . "</td>
<td>" . $row['controller'] . "</td>
    <td>" . $row['type'] . "</td>
    <td style='color: red'>
        <select name = 'statusupdate' style='font-size:10px;'>
          <option> " . $row['alarmstatus'] . " </option>
            <option> OFF </option>
        </select>
      </td>
            <td>" . $row['starttime'] . "</td>
            <td>" . $row['endtime'] . "</td>
            <td><input type='submit' name='delete' value='delete' /></td>

so far so good. The rows are displayed fine and the button's there. (all of the while loop is in a form tag so inputs are cool. any way. i want to make something like this:

 }
    if (isset($_POST['delete'])) {
      delete_alarm_from_database($alarmid)
    }

and the function goes like this:

function delete_alarm_from_database($id) {
  //find alarm by id and delete:
  $sql = "SELECT * FROM alarms WHERE id = " . $id;
  $result = mysql_result($sql);
  if (!$result) {
    print "couldn't find record.";
  }
  //delete record from database:
  else {
    $sql = "DELETE FROM alarms WHERE id = " . $id;
    $result = mysql_result($sql);
  }
}

my problem goes like this: each row has an id but setting it as a variable will always overwrite until he puts in the variable the last id the loop reached. (meaning every delete button of each row will simply delete the last id each page load)

the question is like this. how can i make each delete button recognize it's row id?

thanks in advance - Cheers.


回答1:


I suggest changing your process up a bit. Change the delete buttons to check boxes and allow the user to delete more than one at a time.

Change

<td><input type='submit' name='delete' value='delete' /></td>

to

<td><input type='checkbox' name='ids_to_delete[]' value='<?php echo $row['id']; ?>' /></td>

Then add a new Delete button at the end of the table

<input type='submit' name='delete' value='Delete selected rows' />

Change your PHP to handle an array of values now

if (isset($_POST['ids_to_delete']) && count($_POST['ids_to_delete']) > 0) {
    foreach($_POST['ids_to_delete'] as $alarmid) {
        delete_alarm_from_database($alarmid)
    }
}

or even better - change your delete_alarm_from_database function to handle an array and delete them all at once.




回答2:


Add an id for each button. And call it with that id, probably the best shout being using jQuery. This way you will know the exact No. of the button :)

Take a look at this - it is kind of the same problem - PHP - making a switch statement using id from form input




回答3:


You need to use separate form for each record.

 While ($row=mysql_fetch_array($result)){

  <form method="post" .....>
   .....
   <select>
         <option value="'.$value.'">title</option>
   </select>
   .....

  </form>
 }


来源:https://stackoverflow.com/questions/10759218/php-getting-variables-outside-while-loop

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!