- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我需要按不同类别过滤我网站上的产品。例如,如果您选择“DRINKS”类别,它将向我显示属于该类别的产品。
为了更好地解释自己。
我需要按类别过滤我网站的出版物 [产品],例如,在选择一个类别时说“饮料”,只显示属于该类别的出版物 [产品]。
数据库 [图像]
分类
帖子 [产品]
例如,选择“饮料”类别时,仅显示标有此类别的产品,在本例中为“产品”
这是我目前的代码:
代码显示类别中的产品.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>
产品.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
<?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'];
}
}
?>
DB.PHP
<?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 的例子
文件1.php
<?php
$db = new mysqli(ip,username,password,db);
$query = $db->prepare("SELECT * FROM products INNER JOIN categorias ON product.categoria_id = categorias.id WHERE categorias.name = ?");
$query->bind_param("s",$categoria);
$query->execute();
$coll = $query->get_result();
$query->store_result();
$elements = [];
while ($row = $coll->fetch_assoc()){
$elements[] = $row; // products where categorias.name = $categoria
}
?>
索引.php
<?php
include "file1.php";
foreach ($elements as $element){
echo $element["name"];
}
?>
关于php - 如何过滤属于特定类别的帖子 [产品],我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64761500/
关闭。这个问题是opinion-based .它目前不接受答案。 想改善这个问题吗?更新问题,以便可以通过 editing this post 用事实和引文回答问题. 9 个月前关闭。 Improve
我使用 partykit打包并遇到以下错误消息: Error in matrix(0, nrow = mi, ncol = nl) : invalid 'nrow' value (too large
我一直在尝试寻找一个量表或分类指标,为 VADER 情绪分析分配一些情感程度,而不仅仅是积极、消极或中性。如果有人可以分享他们的观点或资源来帮助按以下方式对 VADER 复合分数进行分类,我将非常感激
伙计们,我想自动循环..但我不知道是我放错了 while 还是循环错了? 我的数据库 标签:kt_barang kd_kategori | nama_kategori 1
我正在创建一个列出本地企业并按类别、子类别和关键字对它们进行分组的应用程序。以下是企业排序规则: 一个企业可以属于多个类别和子类别 一个企业可以有多个关键字 并非每个类别都有子类别,但有子类别的只有两
我有一系列单词 - 我的刺激 - 它们显示在屏幕上。然而,每个词都有另一个“条件”,即它们是类别 A、类别 B 或类别 C。这可能很简单,但我找不到答案并坚持下去。我的最终目标是在每次运行脚本时将类别
我正在使用 Laravel 5.5 和 MySql。如果不向 Services 表中添加 subCategoryID 列,我无法弄清楚如何将类别和子类别与服务相关联。 目前这是我的表结构 服务类别 i
我有两个部分/类别结构的链接表。 the section table structure id sec_title 1 section 1 2 section 2 the category str
我有一个类层次结构如下 @interface PTLDatasource : NSObject ... @interface PTLFetchedDatasource : PTLDatasource
我有一个 DataFrame df 一列,category 使用以下代码创建: import pandas as pd import random as rand from string import
我经常在多个类中设置获取请求以从核心数据(加上一些其他结果)中检索“allRecipes”或“lastModifiedDate”。 为此使用专门的类别 NSManagedObjectContext+R
可以在 Objective C 中创建类别之间的依赖关系吗?也在类别和它们的基类之间? 我知道在运行时应该没有区别,它们可能只是在编译时合并在一起。例如,假设我将 B 类分解为: B(base cla
这个问题在这里已经有了答案: 关闭 10 年前。
example img of a category selection by user 嘿,我正在尝试设置一个选择,用户必须选择一个类别和第二个类别,但我不知道如何获取他单击的信息。用户单击类别后,它
尝试将投资组合库添加到我正在制作的自定义 wp 主题中。我已经筋疲力尽地试图寻找甚至可以修改一些的解决方案和插件。我认为我在寻找解决方案时遇到的一个问题是我不完全确定哪些搜索词可以帮助我找到与我想要实
当我查看 Cocoa Touch API 时,我可以在同一个头文件中找到一些与类别一起声明的类,例如 @interface NSArray : NSObject @property (readonl
我的 log4j.properties 中有以下内容 log4j.rootLogger = debug, stdout, fileLog log4j.appender.stdout = org.apa
如果我在类中添加类别方法,比如NSXMLNode: @interface NSXMLNode (mycat) - (void)myFunc; @end NSXMLNode 的子类,例如 NSXMLEl
先说场景,wordpress的分类结构是这样的 Level 1: Top Level 2: -Nextme_1 Level 3: --Nextme_2 --Nextme_3 Leve
我有一个解析网络,现在我想浏览标签,或显示图表。我怎样才能得到图表?或者在树中导航。显示第一步然后其他等。并了解这棵树是如何 build 的。 import urllib from lxml impo
我是一名优秀的程序员,十分优秀!