大家好! 我有一张桌子!
每个按钮文字及其在标题中的ID 将状态更改为应取决于现在的Staatus。像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;
}
任何人都可以提供建议,一些网站,决定如何制作我需要的东西吗?如果你帮助我,我将非常感谢你
答案 0 :(得分:1)
我希望我能正确理解你的问题。从我看到你有数据包,你需要能够改变他们的状态。
我做了什么:
下面的代码是非常自我解释的,但是为了sumarize:
如果你想测试,只需复制同一个文件中的下三个部分 - 我必须将它们拆分为3,因为该网站不允许我将所有内容粘贴到一个代码区域。
您可以在此处查看其工作原理:http://screencast-o-matic.com/watch/c2hqVcnazP
顺便说一句,我认为你被拒绝是因为你没有提供证据证明你在你的方面尝试了一些东西,所以下次尝试一些东西,如果它不起作用,提供你试过的代码,你和& #39;我会对此发表评论。
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>
希望这会有所帮助 - 它让我忙了一段时间,但我需要改变我目前的项目。