我正在构建一个流程选择应用程序,我甚至不确定我的方法是否正确(我是新手,对不起!)。我有一个带有5个使用单选按钮的输入参数的表单(选择1-10)。
我为每个参数设置了5个表,用于存储进程的名称和设置为1-10以与用户输入相对应的字段...其中一个看起来像这样:
我希望将用户输入(1-10)的每个参数存储为会话变量,以输出到数组以进行进一步的计算。
我有以下代码,用于从表单中设置会话变量,并仅返回与仅来自一个参数的用户输入相匹配的那些记录:
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "selectionapp";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['Submit'])) {
$_SESSION['temperature'] = $_POST['temperature'];
$_SESSION['partSize'] = $_POST['partSize'];
$_SESSION['volume'] = $_POST['volume'];
$_SESSION['stiffness'] = $_POST['stiffness'];
$_SESSION['weight'] = $_POST['weight'];
}
echo $_SESSION['temperature'];
echo $_SESSION['partSize'];
echo $_SESSION['volume'];
echo $_SESSION['stiffness'];
echo $_SESSION['weight'];
$result = mysqli_query($conn, "SELECT * FROM processes WHERE temperature LIKE '%{$_SESSION['temperature']}%'");
$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
while ($row = mysqli_fetch_assoc($result)) {
echo '<tr>';
echo '<td>' . $row['score'] .'</td>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['temperature'] . "</td>";
echo "<td>" . $row['partSize'] . "</td>";
echo "<td>" . $row['volume'] . "</td>";
echo "<td>" . $row['stiffness'] . "</td>";
echo "<td>" . $row['weight'] . "</td>";
echo "<td>" . $row['tool'] . "</td>";
echo "<td>" . $row['paint'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo '<tr/>';
}
echo '</table>';
?>
<?php
// close connection
mysqli_close($conn);
?>
除了,我认为我真正需要的是从5个表中的每一个中获取与用户输入(1-10)相匹配的一列,以创建一个新数组,以便我可以在计算中使用这些值对该流程类型评分/排名。
感谢任何帮助。谢谢!
答案 0 :(得分:0)
不能完全确定这是否是您所需要的:
// You can start to create an array:
$array = [];
// Next is to set the $array with data
$array = [
'temperature' => (int) $_POST['temperature'],
'partSize' => (int) $_POST['partSize'],
];
// In case you want to echo a piece of the array simply do this:
echo $array['temperature'];
// If you want to 'add' a number to the current one you can do this:
$array['partSize'] = ($array['partSize'] + (int) $_POST['partSize']);
// Do a check if $_POST data actually exist and/or is valid:
$array = [
'temperature' => (isset($_POST['temperature'])) ? (int) $_POST['temperature'] : 0,
'partSize' => (isset($_POST['partSize'])) ? (int) $_POST['partSize'] : 0,
];
说实话,每次您获取新数据时,我个人都不会不将其存储在会话中。只需执行一次查询,然后将所有数据放入一个数组,然后进行计算即可。
例如:
// Set $temperature
$temperature = [];
// Query to get all temperature's and put into an array:
$temperature = [1, 6, 3, 8, 2, 9, 10, 3, 7, 9];
// Count how many items you've got
$count_temperature = count($temperature); // = 10
// Now calculate and get an average
$sum_temperature = 0;
foreach ($temperature as $temp)
{
$sum_temperature = ($sum_temperature + $temp);
}
echo $sum_temperature; // 58
$avg_temperature = ($sum_temperature / $count_temperature);
echo $avg_temperature; // 5.8
我希望您能从中得到启发,这可以回答您的问题。您不必强制使用数据库连接,它可以存储在$_SESSION
中。但是,请允许我提醒您,一旦会话被破坏,输入将丢失。如果此不回答您的问题,请告诉我,我们将帮助您找到解决方案!
答案 1 :(得分:0)
我做到了!它既不漂亮也不简洁也不优雅...但是它可以工作。谢谢@RonnieOosting!虽然我没有走阵列路线,但您帮助我指出了正确的方向。
<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "selectionapp";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
if (isset($_POST['Submit'])) {
$_SESSION['temperature'] = $_POST['temperature'];
$_SESSION['partSize'] = $_POST['partSize'];
$_SESSION['volume'] = $_POST['volume'];
$_SESSION['stiffness'] = $_POST['stiffness'];
$_SESSION['weight'] = $_POST['weight'];
}
echo $_SESSION['temperature'];
echo $_SESSION['partSize'];
echo $_SESSION['volume'];
echo $_SESSION['stiffness'];
echo $_SESSION['weight'];
if ($result_temp = mysqli_query($conn, "SELECT * FROM temperature WHERE '%{$_POST['temperature']}%' LIKE '%{$_SESSION['temperature']}%'"));
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Temperature Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_temp)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['temperature']] * 10 . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_size = mysqli_query($conn, "SELECT * FROM partSize WHERE '%{$_POST['partSize']}%' LIKE '%{$_SESSION['partSize']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Part Size Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_size)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['partSize']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_vol = mysqli_query($conn, "SELECT * FROM volume WHERE '%{$_POST['volume']}%' LIKE '%{$_SESSION['volume']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Volume Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_vol)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['volume']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_stiff = mysqli_query($conn, "SELECT * FROM stiffness WHERE '%{$_POST['stiffness']}%' LIKE '%{$_SESSION['stiffness']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Stiffness Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_stiff)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['stiffness']] . "</td>";
echo '<tr/>';
}
echo '</table>';
$result_wght = mysqli_query($conn, "SELECT * FROM weight WHERE '%{$_POST['weight']}%' LIKE '%{$_SESSION['weight']}%'");
//$row = mysqli_fetch_assoc($result);
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'Process Type' . "</td>";
echo "<td>" . 'Weight Score' . "</td>";
echo '<tr/>';
while ($row = mysqli_fetch_assoc($result_wght)) {
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['weight']] . "</td>";
echo '<tr/>';
}
echo '</table>';
echo "<table border='1'>";
echo '<tr>';
echo "<td>" . 'LFI Results' . "</td>";
echo "<td>" . 'Score' . "</td>";
echo '<tr/>';
echo '<tr>';
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row[$_SESSION['weight']] . "</td>";
echo '<tr/>';
echo '</table>';
?>
<?php
// close connection
mysqli_close($conn);
?>
这将输出:
工艺类型温度得分 LFI 5 DCPD 5 SMC 10
工艺类型零件尺寸分数 LFI 0.0 DCPD 0.0 SMC 1.0
处理类型体积分数 LFI 0.5 DCPD 0.5 SMC 1.0
工艺类型刚度得分 LFI 1.0 DCPD 1.0 SMC 1.0
工艺类型重量得分 LFI 1.0 DCPD 1.0 SMC 1.0