gpt4 book ai didi

php - 如何根据用户输入从多个表的单个列中获取所有行?

转载 作者:行者123 更新时间:2023-11-29 17:13:49 24 4
gpt4 key购买 nike

我正在构建一个流程选择应用程序,我什至不确定我的方法是否正确(我是新人,抱歉!)。我有一个带有 5 个输入参数的表单,使用单选按钮(选择 1-10)。

Form

我为每个参数设置了 5 个表,用于存储进程名称和设置为 1 - 10 的字段以对应于用户输入...其中一个如下所示:

enter image description here

我希望将用户输入的每个参数(1-10)存储为 session 变量,以输出到数组以进行进一步计算。

我有以下代码,用于设置表单中的 session 变量,并仅返回与仅来自一个参数的用户输入相匹配的记录:

    <?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) 匹配的一列,以创建一个新数组,以便我可以在计算中使用这些值对该流程类型进行评分/排名。

感谢任何帮助。谢谢!

最佳答案

我不完全确定这是否是您所需要的:

// 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,
];

说实话,我个人不会在每次获取新数据时都将其存储在 session 中。只需执行一次查询,将所有数据放入一个数组中,然后计算这些数据。

例如:

// 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 中。但请让我提醒您,一旦 session 被销毁,输入就会丢失。如果这不能回答您的问题,请告诉我,我将帮助您找到解决方案!

关于php - 如何根据用户输入从多个表的单个列中获取所有行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51748400/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com