我的代码遇到的问题是,我只在name
表中使用相同search_db
字段的多个结果显示1个按钮。 name
字段具有不同的ID。我需要收集所有相同的fullnames
。这是一个非常复杂的解释主题,这是我解释它的最佳方式。
这是我的源代码:
<?php
include('connect.php');
?>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<title>Example</title>
</head>
<body>
<form action="test.php" method="POST">
<input type="text" name="value" placeholder="Hello" />
<input type="submit" name="submit" value="Search" />
</form><br />
<?php
if(isset($_POST['submit']))
{
$value = htmlspecialchars($_POST['value']);
$stmt = $pdo->prepare("SELECT * FROM search_db WHERE fullname = :fullname");
$stmt -> execute(array(':fullname' => $value));
$result = $stmt -> fetchAll();
foreach($result as $row)
{
$id = $row['id'];
$db_name = $row['name'];
$name = $row['fullname'];
$username = $row['username'];
$password = $row['password'];
$dob = $row['date'];
$email = $row['email'];
$test = '<p >'.$name. ' ' . $password . '</p>';
}
echo '<button data-toggle="collapse" data-target="#test">'. count($result) .' Results Found</button><br />';
echo '<div id="test" class="collapse">'. $test . '</div>';
}
?>
</body>
</html>
答案 0 :(得分:0)
我认为你需要连接php变量形式的值,你可以在单个按钮中显示所有值
检查$test
变量
if(isset($_POST['submit']))
{
$test='';
$value = htmlspecialchars($_POST['value']);
$stmt = $pdo->prepare("SELECT * FROM search_db WHERE fullname = :fullname");
$stmt -> execute(array(':fullname' => $value));
$result = $stmt -> fetchAll();
foreach($result as $row)
{
$id = $row['id'];
$db_name = $row['name'];
$name = $row['fullname'];
$username = $row['username'];
$password = $row['password'];
$dob = $row['date'];
$email = $row['email'];
$test .= '<p >'.$name. ' ' . $password . '</p><br>';
}
答案 1 :(得分:0)
有一些有用的PHP数组函数可以帮助计算值并删除重复项。
http://php.net/manual/en/function.array-count-values.php
http://php.net/manual/en/function.array-unique.php
<?php
if(isset($_POST['submit']))
{
$value = htmlspecialchars($_POST['value']);
$stmt = $pdo->prepare("SELECT * FROM search_db WHERE fullname = :fullname");
$stmt -> execute(array(':fullname' => $value));
$result = $stmt -> fetchAll();
$namesArr = array(); //create empty array
foreach($result as $row)
{
$id = $row['id'];
$db_name = $row['name'];
$name = $row['fullname'];
$username = $row['username'];
$password = $row['password'];
$dob = $row['date'];
$email = $row['email'];
$test = '<p >'.$name. ' ' . $password . '</p>';
array_push($namesArr, $name); //push each name into new array
}
$uniqueNamesArr = array_unique($namesArr); //http://php.net/manual/en/function.array-unique.php
$nameCounts = array_count_values($namesArr); //http://php.net/manual/en/function.array-count-values.php
foreach( $uniqueNamesArr as $uniqueName ) {
echo '<div id="' . $uniqueName . '" class="collapse">'. $uniqueName . '</div>';
echo '<button data-toggle="collapse" data-target="#' . $uniqueName . '">'. $nameCounts[$uniqueName] .' Results Found</button><br />';
}
}
?>