大家好! 我有一张 table !
标题中的每个按钮文本及其 ID 将状态更改为 应取决于当前的状态。像 1 行或者如果 Staatus - “mitteakttivne” 应该有 Aktiivne 和 Kustutatud 按钮等等。此外,如果我单击其中一个按钮,Staatus 应更改为单击的按钮状态,并且按钮应按第一部分问题中的方式更改。
表代码:
<table>
<tr>
<th>Paketi nimi</th>
<th>Hind</th>
<th>Kirjeldus</th>
<th>Staatus</th>
<th>Change staatus to</th>
</tr>
<?php
$paketi_list = get_paketi_list();
?>
<?php foreach ($paketi_list as $pakett): ?>
<tr>
<td><?= $pakett['nimi']?></td>
<td><?= $pakett['hetke_hind']?></td>
<td><?= $pakett['kirjeldus']?></td>
<td><?= $pakett['seisundi_liik']?></td>
<td><button>Mitteaktiivne</button><button>Kustutatud</button></td>
<td><a href="changePakett.php?pakett_id=<?=$pakett['pakett_id']?> ">change</a></td>
</tr>
<?php endforeach; ?>
get_pakei_list()
函数:
function get_paketi_list(){
db_connect();
$query = "SELECT pakett.nimi, pakett.kirjeldus, pakett.hetke_hind, pakett.pakett_id, pakett.paketi_seisundi_liik_id AS seisundi_id, paketi_seisundi_liik.nimetus AS seisundi_liik FROM pakett INNER JOIN paketi_seisundi_liik ON pakett.paketi_seisundi_liik_id = paketi_seisundi_liik.paketi_seisundi_liik_id";
$result = mysql_query($query);
$res_array = array();
$count = 0;
while ($row = mysql_fetch_assoc($result)) {
$res_array[$count] = $row;
$count++;
}
return $res_array;
}
任何人都可以提供建议、一些网站、决定如何制作我需要的东西吗?如果您帮助我,我将非常感激
最佳答案
我希望我正确理解了你的问题。据我所知,您有数据包,并且您需要能够更改它们的状态。
我做了什么:
- 我使用了您的示例,但仅保留了必要的信息:当前状态、可能要修改的状态
- 我删除了每行上的更改链接,并在最后移动了一次
- 我引入了数据包 ID 的概念,它基本上是数组 $paketi_list 的键
下面的代码非常不言自明,但总结一下:
- 我假设您有 3 种状态
- 我使用 jQuery 来切换状态,每当有人单击按钮时 + 我会更新表单中的隐藏字段,以便稍后保存更改
- 表单处理在同一页面完成
- 我使用带有 WAMP 的虚拟数组对此进行了测试,效果很好
如果您想测试,只需将接下来的 3 个部分复制到同一文件中 - 我必须将它们分成 3 个部分,因为该网站不允许我将所有内容粘贴到一个代码区域中。
您可以在此处查看其工作原理:http://screencast-o-matic.com/watch/c2hqVcnazP
顺便说一句,我认为你被否决是因为你没有提供证据证明你尝试过某些东西,所以下次尝试一些东西,如果它不起作用,请提供你尝试过的代码,然后你就会获得对此的评论。
PHP - 变量初始化+表单处理
<?php
//modify next line with all your statuses
$arrayAllStatuses = array ('Status 1', 'Status 2', 'Status 3');
/* used for example purposes - to replace with your own code */
$paketi_list = array (
array('seisundi_liik' => 'Status 2'),
array('seisundi_liik' => 'Status 3'),
array('seisundi_liik' => 'Status 1')
);
/* ========== */
//this next part is the code responsible for treating the form submission
//you can move this elsewhere (as long as you update the action attribute of the form tag)
if (isset($_POST['change_status'])) {
$numberOfLines = $_POST['numberOfLines'];
foreach ($paketi_list as $currentId => $pakett) {
if (isset($_POST['current_status_row_' . $currentId]) && $_POST['current_status_row_' . $currentId] != '') {
echo 'Packet with ID = ' . $currentId . ' has a new status = ' . $_POST['current_status_row_' . $currentId];
//I imagine you want to save this to a database - sanitize it first using mysqli_real_escape_string
echo "<br />";
}
}
}
?>
HTML 部分 - 包含修改状态的表格和表单
<table border="1">
<tr>
<th>Status</th>
<th>Change status to</th>
</tr>
<?php foreach ($paketi_list as $currentId => $pakett): ?>
<tr>
<td id="status_row_<?php echo $currentId; ?>"><?php echo $pakett['seisundi_liik']; ?></td>
<?php
$remainingUnusedStatuses = array_diff($arrayAllStatuses, array($pakett['seisundi_liik']));
//next line rebases array keys - so you don't end up with an array that has a key of 0 & 2 or 1 & 2
$remainingUnusedStatuses = array_values($remainingUnusedStatuses);
?>
<td>
<button id="tochange-first_row_<?php echo $currentId; ?>"><?php echo $remainingUnusedStatuses[0]; ?></button>
<button id="tochange-second_row_<?php echo $currentId; ?>"><?php echo $remainingUnusedStatuses[1]; ?></button>
</td>
</tr>
<?php endforeach; ?>
</table>
<form action="" method="post">
<input type="hidden" name="numberOfLines" value="<?php echo sizeof($paketi_list); ?>">
<?php foreach ($paketi_list as $currentId => $pakett): ?>
<input type="hidden" id="current_status_row_<?php echo $currentId; ?>" name="current_status_row_<?php echo $currentId; ?>" value="">
<?php endforeach; ?>
<input type="submit" value="Save changes" name="change_status">
</form>
JS - 你需要 jQuery 和一些函数
<script src="//code.jquery.com/jquery-1.11.0.min.js"></script>
<script type="text/javascript">
$(function() {
$("button[id^='tochange']").click(function() {
var currentRow = getCurrentRow($(this));
var currentButtonId = $(this).attr('id');
//get current status
var currentStatus = $("td#status_row_" + currentRow).html();
var futureStatus = $("button#" + currentButtonId).html();
//switch the two statuses (visually)
$("button#" + currentButtonId).html(currentStatus);
$("td#status_row_" + currentRow).html(futureStatus);
//save the change in the hidden form field
$("input#current_status_row_" + currentRow).val(futureStatus);
//$("#btnAddProfile").html('Save');
});
});
function getCurrentRow(currentObject)
{
//do not use any numerical values in the id, except at the end, otherwise this won't work anymore
var currentRow = ($(currentObject).attr('id').match(/\d+/));
return currentRow;
}
</script>
希望这会有所帮助 - 它让我忙碌了一段时间,但我需要对当前的项目进行更改。
关于php - mysql,php更改按钮的值和名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23715874/