开发者

PHP loop which creates X buttons

开发者 https://www.devze.com 2023-04-03 12:08 出处:网络
I\'m creating an banner management tool which uses an SQL database to store its data once added. there is a tab which shows for every banner in this database an update and delete button. These buttons

I'm creating an banner management tool which uses an SQL database to store its data once added. there is a tab which shows for every banner in this database an update and delete button. These buttons are created in a PHP based loop so i will have X amount of update and delete buttons.

Now depending on which button i press i will go to an update vieuw of the the item wherunder the update button was clicked.

The problem i encounter since these buttons all have the same name is that i can't distinguish which button has been pushed. Every item in the DB has en ROWID, however i can't figure out how to get the ID once u push the button...

Here is the code of how these buttons are generated.

do {
    $entry = $dbase->querySingle("SELECT name FROM banners where ROWID='$currentRowNumber'");

    echo "<tr>";
    echo "<t开发者_JS百科d width=\"50\">". $entry ."</td>";
    echo "</tr>";

    echo "<tr>";
    echo "<td><input type=\"submit\" name=\"operation\" value=\"Update\"></td>";
    echo "<td><input type=\"submit\" name=\"operation\" value=\"Delete\"></td>";
    echo "</tr>";

    $currentRowNumber++;

} while ($dbase->querySingle("SELECT name FROM banners where ROWID='$currentRowNumber'") != NULL);

I was wondering if anyone has any idea on how to distinguish these buttons. Maybe a hidden factor that i can read out below? (don't know if such a thing exists)

Regards


You could wrap all operations per row in a form that has an input type="hidden" value="$id" containing the ID of the item, or use an action="/update?id=$id" per form, or, if you want to stick to one large form, you could use name="update[$id]" for the submit button.


First, instead of calling the buttons a generic term like "operation", I'd advise you call them by what they actually do, namely "update" and "delete". This allows you to decouple the button's function from what label is displayed in the button.

<input type="submit" name="update" value="Update banner" />
<input type="submit" name="delete" value="Remove banner" />

You'd check which button has been hit by using array_key_exists or isset to determine which button was pressed.

if (isset ($_POST ['update']))
{
}
else if (isset ($_POST ['delete']))
{
}
else
{
    // No button was pressed
}

As far as identifying which item the button is referring, I'm assuming all your banners have some way of uniquely identifying them (a primary key, normally a numeric value). If you use [square bracket] syntax in the name of the button, you can identify the row for the button pressed.

<input type="submit" name="update[1]" value="Update banner" />
<input type="submit" name="delete[1]" value="Remove banner" />
<input type="submit" name="update[23]" value="Update banner" />
<input type="submit" name="delete[23]" value="Remove banner" />
<input type="submit" name="update[153]" value="Update banner" />
<input type="submit" name="delete[153]" value="Remove banner" />

if (isset ($_POST ['update']))
{
    $rowToUpdate = key ($_POST ['update']);
}
else if (isset ($_POST ['delete']))
{
    $rowToDelete = key ($_POST ['delete']);
}
else
{
    // No button was pressed
}

I've used numbers as identifiers here, but you can use whatever it is you're using to distinguish one banner from another. I doubt you could use punctuation or spaces in your identifiers though, other than underscore, so you'll need to bear that in mind. update[abc] and update[abc_def] should be fine, but I couldn't vouch for update[abc def] or update[abc-def]. All in all, using numerical identifiers will save you a lot of unnecessary headaches.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号