Php 如何筛选属于特定类别的帖子[产品]
我需要在我的网站上过滤不同类别的产品。例如,如果您选择类别“饮料”,它将显示属于该类别的产品 为了更好地解释我自己 我需要按类别过滤我网站的出版物[产品],例如,当选择一个类别时,比如说“饮料”,只显示属于该类别的出版物[产品] 数据库[图像] 类别 发布[产品]Php 如何筛选属于特定类别的帖子[产品],php,mysql,sql,Php,Mysql,Sql,我需要在我的网站上过滤不同类别的产品。例如,如果您选择类别“饮料”,它将显示属于该类别的产品 为了更好地解释我自己 我需要按类别过滤我网站的出版物[产品],例如,当选择一个类别时,比如说“饮料”,只显示属于该类别的出版物[产品] 数据库[图像] 类别 发布[产品] <?php include($_SERVER['DOCUMENT_ROOT'].'/app/controllers/products.php'); include('app/includes/categories.php');
<?php include($_SERVER['DOCUMENT_ROOT'].'/app/controllers/products.php');
include('app/includes/categories.php');
$categorias = selectAll('categories');
$capitulos = selectAll('products');
<div class="productos-list">
<?php foreach ($productos as $key => $producto): ?>
<ul>
<a href="/ver/productos.php?id=<?php echo $producto['id']?>">
<li class="producto-name"><?php echo $producto['name']?></li>
</a>
</ul>
<?php endforeach; ?>
</div>
例如,选择“饮料”类别时,仅显示标有此类别的产品,在本例中为“产品”
这是我目前的代码:
在categories.php[index]中显示产品的代码
<?php include($_SERVER['DOCUMENT_ROOT'].'/app/controllers/products.php');
include('app/includes/categories.php');
$categorias = selectAll('categories');
$capitulos = selectAll('products');
<div class="productos-list">
<?php foreach ($productos as $key => $producto): ?>
<ul>
<a href="/ver/productos.php?id=<?php echo $producto['id']?>">
<li class="producto-name"><?php echo $producto['name']?></li>
</a>
</ul>
<?php endforeach; ?>
</div>
PRODUCTS.PHP
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
$table = 'posts';
$categorias = selectAll('categorias');
$errors = array();
$id = '';
$title = '';
$productos = '';
$categoria_id = '';
$productos = selectAll($table);
if (isset($_POST['add-post'])) {
$_POST['topic_id']=serialize($_POST['topic_id']);
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = "../../images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result){
$_POST['image'] = $image_name;
} else {
array_push($errors, "¡Algo fallo al subir la imagen!");
}
} else {
array_push($errors, "¡Necesitas subir una imagen!");
}
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Post creado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Post eliminado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Post actualizado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
?>
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');
$table = 'categorias';
$errors = array();
$id = '';
$name = '';
$title = '';
$productos = '';
$categoria_id = '';
$categorias = selectAll($table);
if (isset($_POST['add-post'])) {
$errors = validateCategoria($_POST);
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Categoria creada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Categoria eliminada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Categoria actualizada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
?>
<?php
session_start();
require('connect.php');
function dd($value)
{
echo "<pre>", print_r($value, true), "</pre>";
die();
}
function executeQuery($sql, $data)
{
global $conn;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, ...$values);
$stmt->execute();
return $stmt;
}
function selectAll($table, $conditions = [])
{
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$sql = $sql . " ORDER BY id DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
// $sql = "SELECT * FROM $table WHERE username='ElVictox' AND admin=1";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = $conn->prepare($sql);
$values = array_values($conditions);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, $values);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table ";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function create($table, $data)
{
global $conn;
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;
}
function update($table, $id, $data)
{
global $conn;
$sql = "UPDATE $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;
}
function delete($table, $id)
{
global $conn;
$sql = "DELETE FROM $table WHERE id=?";
$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;
}
CATEGORIES.PHP
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
$table = 'posts';
$categorias = selectAll('categorias');
$errors = array();
$id = '';
$title = '';
$productos = '';
$categoria_id = '';
$productos = selectAll($table);
if (isset($_POST['add-post'])) {
$_POST['topic_id']=serialize($_POST['topic_id']);
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = "../../images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result){
$_POST['image'] = $image_name;
} else {
array_push($errors, "¡Algo fallo al subir la imagen!");
}
} else {
array_push($errors, "¡Necesitas subir una imagen!");
}
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Post creado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Post eliminado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Post actualizado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
?>
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');
$table = 'categorias';
$errors = array();
$id = '';
$name = '';
$title = '';
$productos = '';
$categoria_id = '';
$categorias = selectAll($table);
if (isset($_POST['add-post'])) {
$errors = validateCategoria($_POST);
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Categoria creada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Categoria eliminada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Categoria actualizada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
?>
<?php
session_start();
require('connect.php');
function dd($value)
{
echo "<pre>", print_r($value, true), "</pre>";
die();
}
function executeQuery($sql, $data)
{
global $conn;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, ...$values);
$stmt->execute();
return $stmt;
}
function selectAll($table, $conditions = [])
{
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$sql = $sql . " ORDER BY id DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
// $sql = "SELECT * FROM $table WHERE username='ElVictox' AND admin=1";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = $conn->prepare($sql);
$values = array_values($conditions);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, $values);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table ";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function create($table, $data)
{
global $conn;
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;
}
function update($table, $id, $data)
{
global $conn;
$sql = "UPDATE $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;
}
function delete($table, $id)
{
global $conn;
$sql = "DELETE FROM $table WHERE id=?";
$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;
}
DB.PHP
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validatePost.php');
$table = 'posts';
$categorias = selectAll('categorias');
$errors = array();
$id = '';
$title = '';
$productos = '';
$categoria_id = '';
$productos = selectAll($table);
if (isset($_POST['add-post'])) {
$_POST['topic_id']=serialize($_POST['topic_id']);
$errors = validatePost($_POST);
if (!empty($_FILES['image']['name'])) {
$image_name = time() . '_' . $_FILES['image']['name'];
$destination = "../../images/" . $image_name;
$result = move_uploaded_file($_FILES['image']['tmp_name'], $destination);
if ($result){
$_POST['image'] = $image_name;
} else {
array_push($errors, "¡Algo fallo al subir la imagen!");
}
} else {
array_push($errors, "¡Necesitas subir una imagen!");
}
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Post creado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Post eliminado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/posts/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Post actualizado correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/topics/index.php');
exit();
} else {
$id = $_POST['id'];
$title = $_POST['title'];
$topic = $_POST['categoria_id'];
}
}
?>
<?php
include($_SERVER['DOCUMENT_ROOT'].'/app/database/db.php');
include($_SERVER['DOCUMENT_ROOT'].'/app/helpers/validateCategoria.php');
$table = 'categorias';
$errors = array();
$id = '';
$name = '';
$title = '';
$productos = '';
$categoria_id = '';
$categorias = selectAll($table);
if (isset($_POST['add-post'])) {
$errors = validateCategoria($_POST);
if (count($errors) === 0){
unset($_POST['add-post']);
$post_id = create($table, $_POST);
$_SESSION['message'] = '¡Categoria creada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
if (isset($_GET['id'])){
$id = $_GET['id'];
$post = selectOne($table, ['id' => $id]);
}
if (isset($_GET['del_id'])){
$id = $_GET['del_id'];
$count = delete($table, $id);
$_SESSION['message'] = '¡Categoria eliminada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
}
if (isset($_POST['update-post'])){
$errors = validateEdit($_POST);
if (count($errors) === 0){
$id = $_POST['id'];
unset($_POST['update-post'], $_POST['id']);
$post_id = update($table, $id, $_POST);
$_SESSION['message'] = '¡Categoria actualizada correctamente!';
$_SESSION['type'] = 'success';
header('location: ../../admin/categorias/index.php');
exit();
} else {
$id = $_POST['id'];
$name = $_POST['name'];
}
}
?>
<?php
session_start();
require('connect.php');
function dd($value)
{
echo "<pre>", print_r($value, true), "</pre>";
die();
}
function executeQuery($sql, $data)
{
global $conn;
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);
$stmt = $conn->prepare($sql);
$values = array_values($data);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, ...$values);
$stmt->execute();
return $stmt;
}
function selectAll($table, $conditions = [])
{
global $conn;
$sql = "SELECT * FROM $table";
if (empty($conditions)) {
$sql = $sql . " ORDER BY id DESC";
$stmt = $conn->prepare($sql);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
} else {
// $sql = "SELECT * FROM $table WHERE username='ElVictox' AND admin=1";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$stmt = $conn->prepare($sql);
$values = array_values($conditions);
$types = str_repeat('s', count($values));
$stmt->bind_param($types, $values);
$stmt->execute();
$records = $stmt->get_result()->fetch_all(MYSQLI_ASSOC);
return $records;
}
}
function selectOne($table, $conditions)
{
global $conn;
$sql = "SELECT * FROM $table ";
$i = 0;
foreach ($conditions as $key => $value) {
if ($i === 0){
$sql = $sql . " WHERE $key=?";
} else {
$sql = $sql . " AND $key=?";
}
$i++;
}
$sql = $sql . " LIMIT 1";
$stmt = executeQuery($sql, $conditions);
$records = $stmt->get_result()->fetch_assoc();
return $records;
}
function create($table, $data)
{
global $conn;
$sql = "INSERT INTO $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$stmt = executeQuery($sql, $data);
$id = $stmt->insert_id;
return $id;
}
function update($table, $id, $data)
{
global $conn;
$sql = "UPDATE $table SET ";
$i = 0;
foreach ($data as $key => $value) {
if ($i === 0){
$sql = $sql . " $key=?";
} else {
$sql = $sql . ", $key=?";
}
$i++;
}
$sql = $sql . " WHERE id=?";
$data['id'] = $id;
$stmt = executeQuery($sql, $data);
return $stmt->affected_rows;
}
function delete($table, $id)
{
global $conn;
$sql = "DELETE FROM $table WHERE id=?";
$stmt = executeQuery($sql, ['id' => $id]);
return $stmt->affected_rows;
}
您需要使用连接。mysqli示例
file1.php
<?php
include "file1.php";
foreach ($elements as $element){
echo $element["name"];
}
?>
index.php
阅读错误日志。从这里看,第一个代码段似乎没有设置每个变量。您的包含或要求在哪里?@StackSlave |我在显示类别中的产品时没有错误,我想做的是在其类别中具体显示它们。例如,我选择了“饮料”类别,我希望它只显示饮料产品目前,我碰巧所有产品都显示在所有类别中,例如在“饮料”类别中,所有产品都显示,包括不属于它的产品。您选择了所有类别,您希望得到什么?这是您的解决方案@Heimdalssidstevogt |感谢您的解决方案,但很抱歉我有一个问题,我需要在代码中更改什么?我不熟悉这一点,我有很多疑问。这给了我一个错误:致命错误:未捕获错误:调用成员函数bind_param(),这是因为查询中的表值与数据库中的表值不同;)完成了,很有效,请原谅,最后一个问题是,如何让产品在我的index.php中打印?@VictorHugo我刚刚更改了上面的解决方案。只需包含该文件,然后就可以循环包含排序表值的value$元素。记住标记为您的解决方案并向上投票;)@维克多乌普沃特