search
HomeBackend DevelopmentPHP Tutorial「相关文章」结果是越相关排名越前的案例,求一个高效的写法

我的文章系统,想按照多个件条下
,看看有什么可能性可做到

4张表  基础简介
content 文章主表
id
lid = 相关的文集id ,保存格式 (1,2,3)
tags = 相关的TAG 的ID   ,保存格式 (1,2,3)
typeid = 分类ID 只为1个数字 
文集
lid
name

Tags
id
name
分类
typeid
name

typeid = 分类ID, 不别多说
tag = 标签这不用多说,
lid = 一个文集, 但一篇文章可以放到多个文集, 比如: PHP+MYSQL+AJAX无刷新评论
用户可能放到  PHP文集,MYSQL文章,AJAX文集

TAG 是全站共用,重复就不新增,只会选择  
所以不能做成一个自己文章的分类列表,所以会出现了文集功能


因为文章某个部分已经会显示当前文集其他的文章

所以在关相文章的部分,文集中的文件不比TAG文章更应该排在前面

所以准备的关系排名是
1. TAG相关
2. 文集
3. 最新的当前分类文章

为什么要分3次???

因为我就是怕太TAG,同时又没有用到文集功能....
所以最后一步才是当前分类文章



当前文章的
TAG 是3,14
lid = 7,12
typeid = 2

SELECT * FROM `content` WHERE `tags` IN (3,14) ORDER BY `time` DESC LIMIT 10
SELECT * FROM `content` WHERE `lid` IN (7,12) ORDER BY `time` DESC LIMIT 10
SELECT * FROM `content` WHERE `typeid` = '2' ORDER BY `time` DESC LIMIT 10


前台显示十条相关文章
本来是准备,分3次,然后每次拿10条是最安全的

后来想想...这样还有合并数组什么的也麻烦,重点是取太多资料,但前台只需要10条

所以换成一句
SELECT * FROM `video_content` WHERE `tags` IN (3,14) or `lid` IN (3) or `typeid` = 2  LIMIT 10

但这样好像又能以TAG>文集>分类...去排名

请求一下有何方法?

要求好像蛮麻烦的呵呵,因为页面下半部有50%的部分是显示相关文章,所以在营运上很重要


回复讨论(解决方案)

暂时用了这一段代码,感觉有点蠢

有什么更好的方法建议呢?

//相关文章if (!empty($data["tags"]) || !empty($data["lid"])) {	$tags = $data["tags"];	$lid = $data["lid"];	$tag_sql = "SELECT * FROM `content` WHERE `tags` IN ($tags) OR `lid` IN ($lid) ORDER BY `time` DESC LIMIT 8";	$stmt = $pdo->prepare($tag_sql);	$stmt->execute();	$tags_v = $stmt->fetchAll(PDO::FETCH_ASSOC);	$smarty -> assign("r_v",$tags_v);}$tags_num = count($tags_v);if ($tags_num < 8 ) {	$now_v = "";	foreach ($tags_v as $key => $value) {  		$now_v .= $value['vid'].",";	}		$now_v=substr("$now_v",0,-1);	$need = 8-$tags_num;	$typeid = $data["typeid"];	$typeid_sql = "SELECT * FROM `content` WHERE `typeid` = '$typeid' AND  `vid` NOT IN ($now_v) ORDER BY `time` DESC LIMIT $need";	$stmt = $pdo->prepare($typeid_sql);	$stmt->execute();	$typeid_v = $stmt->fetchAll(PDO::FETCH_ASSOC);	$smarty -> assign("r_tv",$typeid_v);}

sql用or、not in本来就不好;
如果数据不需要太及时的话
把你3条sql语句组合好的数据缓存一段时间(半小时、一小时、甚至再长一点也可以)
因为你的数据都是死数据,没有浏览量,评论量等等

sql用or、not in本来就不好;
如果数据不需要太及时的话
把你3条sql语句组合好的数据缓存一段时间(半小时、一小时、甚至再长一点也可以)
因为你的数据都是死数据,没有浏览量,评论量等等

谢谢
这个我也了解

但更想知道的是,如果按照更有关的排得更前

就是先排TAG 再排LID  最后再排同类其他文章

莫非只能用二楼的方法?

你的 意思是不是如果要10条tag,就先取10条tag,不够再取有lib,接着不够再取有type的?

如果是这样的话, 你的sql是不是会有问题!

你的 意思是不是如果要10条tag,就先取10条tag,不够再取有lib,接着不够再取有type的?

如果是这样的话, 你的sql是不是会有问题!


是的

2楼暂时做了个简单的
暂时先不分tag和lid

不够再取type
以及2楼用8条做测试

是否有更好的做法?

用全文检索来做

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
The Continued Use of PHP: Reasons for Its EnduranceThe Continued Use of PHP: Reasons for Its EnduranceApr 19, 2025 am 12:23 AM

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python: Exploring Their Similarities and DifferencesPHP and Python: Exploring Their Similarities and DifferencesApr 19, 2025 am 12:21 AM

PHP and Python are both high-level programming languages ​​that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

PHP and Python: Different Paradigms ExplainedPHP and Python: Different Paradigms ExplainedApr 18, 2025 am 12:26 AM

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP and Python: A Deep Dive into Their HistoryPHP and Python: A Deep Dive into Their HistoryApr 18, 2025 am 12:25 AM

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

Choosing Between PHP and Python: A GuideChoosing Between PHP and Python: A GuideApr 18, 2025 am 12:24 AM

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP and Frameworks: Modernizing the LanguagePHP and Frameworks: Modernizing the LanguageApr 18, 2025 am 12:14 AM

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHP's Impact: Web Development and BeyondPHP's Impact: Web Development and BeyondApr 18, 2025 am 12:10 AM

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

How does PHP type hinting work, including scalar types, return types, union types, and nullable types?How does PHP type hinting work, including scalar types, return types, union types, and nullable types?Apr 17, 2025 am 12:25 AM

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values ​​and handle functions that may return null values.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)