假设我想在页面刷新时重新排序表单中的按钮,这样当页面刷新时,按钮会重新排列其位置。 那么主要的问题是,我将在按钮中显示的文本来自数据库数据。 以下是我现在尝试的代码 -
<?php
session_start();
include_once 'dbconnect.php';
$res=mysqli_query($con, "SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_assoc($res);
?>
<?php
$array = array(
'<?php echo $userRow['test1']; ?>' => array( 'class' => 'foo', 'name' => 'update' ),
'<?php echo $userRow['test2']; ?>' => array( 'class' => 'john', 'name' => 'update' ),
'<?php echo $userRow['test3']; ?>' => array( 'class' => 'x', 'name' => 'update' ),
'<?php echo $userRow['test4']; ?>' => array( 'class' => 'a', 'name' => 'update' ),
'<?php echo $userRow['test5']; ?>' => array( 'class' => 'b', 'name' => 'update' ),
'<?php echo $userRow['test6']; ?>' => array( 'class' => 'c', 'name' => 'update' ),
'<?php echo $userRow['test7']; ?>' => array( 'class' => 'd', 'name' => 'update' ),
'<?php echo $userRow['test8']; ?>' => array( 'class' => 'e', 'name' => 'update' ),
'<?php echo $userRow['test9']; ?>' => array( 'class' => 'f', 'name' => 'update' ),
'<?php echo $userRow['test10']; ?>' => array( 'class' => 'g', 'name' => 'update' ),
'<?php echo $userRow['test11']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test12']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test13']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test14']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test15']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test16']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test17']; ?>' => array( 'class' => 'h', 'name' => 'idchange' ),
'<?php echo $userRow['test18']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test19']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
'<?php echo $userRow['test20']; ?>' => array( 'class' => 'h', 'name' => 'update' ),
);
uksort( $array, function() { return rand() > rand(); } ); //This will mix them up.
foreach( $array as $item => $atts ){
<form action="check1.php" method="post">
echo '<div class="'. $atts['class'] .'"><button name="'. $atts['name'] .'" type="submit" >'. $item .'</button></div>';
</form>
}
?>
所以,它显示错误.. 所以谁能帮助我解决这个问题?
答案 0 :(得分:0)
如果您在数据库中没有错误
,这应该有效<?
session_start();
include_once 'dbconnect.php';
$res=mysqli_query($con, "SELECT * FROM users WHERE user_id=".$_SESSION['user']);
$userRow=mysqli_fetch_assoc($res);
$array = array(
$userRow['test1'] => array( 'class' => 'foo', 'name' => 'update' ),
$userRow['test2']=> array( 'class' => 'john', 'name' => 'update' ),
$userRow['test3'] => array( 'class' => 'x', 'name' => 'update' ),
$userRow['test4'] => array( 'class' => 'a', 'name' => 'update' ),
$userRow['test5'] => array( 'class' => 'b', 'name' => 'update' ),
$userRow['test6'] => array( 'class' => 'c', 'name' => 'update' ),
$userRow['test7'] => array( 'class' => 'd', 'name' => 'update' ),
$userRow['test8'] => array( 'class' => 'e', 'name' => 'update' ),
$userRow['test9'] => array( 'class' => 'f', 'name' => 'update' ),
$userRow['test10'] => array( 'class' => 'g', 'name' => 'update' ),
$userRow['test11'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test12'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test13'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test14'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test15'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test16'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test17'] => array( 'class' => 'h', 'name' => 'idchange' ),
$userRow['test18'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test19'] => array( 'class' => 'h', 'name' => 'update' ),
$userRow['test20'] => array( 'class' => 'h', 'name' => 'update' ),
);
uksort( $array, function() { return rand() > rand(); } );
foreach( $array as $item => $atts ){
echo '<form action="check1.php" method="post">';
echo '<div class="'. $atts['class'] .'"><button name="'. $atts['name'] .'" type="submit" >'. $item .'</button></div>';
echo '</form>';
}
?>