如何根据第三列从两列中选择值 [英] how to select value from two columns based on third column
本文介绍了如何根据第三列从两列中选择值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个以书籍命名的数据库表。它由以下内容组成
-------------------- -------------------------------------------
title |作者| category
| |
---------------------------------------------- ----------------
xyz狂人小说
适应无脑小说
adaxx愚蠢小说
关注白痴期刊
你的男人acadamic
------------------------- -------------------------------------
用户试图按作者或标题搜索价值并基于类别
例如:如果用户通过选择小说来搜索适应需要显示适应和adaxx <下面的
是使用mysql数据库的代码
< 表格 action = search.php 方法 = 发布 >
<? php
$ select_query = 从书籍中选择不同的类别跨度>;
$ select_query_run = mysql_query($ select_query);
$ select_query_array = mysql_fetch_array($ select_query_run);
$ cat = $ select_query_array [' category 跨度>];
echo < select name ='名'>中跨度>;
while ($ select_query_array = mysql_fetch_array($ select_query_run))
{ // 动态下拉列表
echo < option value =' .htmlspecialchars($ select_query_array [ category])。 '>。 htmlspecialchars($ select_query_array [ category])。 < / option>;
}
echo < /选择> 中跨度>;
?>
< 输入 类型 = text name = searchvalue size = 5 id = searchfield title = searchfield onFocus = clearText(this) / >
< < span class =code-leadattribute> input type = 提交 名称 = 提交 value = alt = 搜索 id = searchbutton title = 搜索 / >
< < span class =code-leadattribute> / form >
<? php
$ button = $ _POST [' 提交'跨度>];
$ search = $ _POST [' searchvalue 跨度>];
if(strlen($ search)< = 1)
{
$ name = $ _POST [' name'];
// echo $ a;
echo 搜索词太短;
}
else {
echo 您搜索了 $ search < hr size ='1'>< / br>;
$ search_exploded = explode( ,$ search);
$ x = ;
$ construct = ;
foreach($ search_exploded as $ search_each)
{
$ x ++;
if($ x == 1)
$ construct 。= < span class =code-string> title LIKE'%$ search_each%';
else
$ construct 。= AND LIKE'%$ search_each%';
}
$ construct = SELECT * FROM books WHERE $ construct and category ='$ name'; // 我试过这个但没有工作
$ run = mysql_query($ construct);
$ foundnum = mysql_num_rows($ run);
if ($ foundnum == 0)
echo 抱歉, $ search 没有匹配的结果。< / br>< / br> 1。 跨度>;
else
{
echo $ foundnum results found!< p>;
}
?>
解决方案
select_query = 从书籍中选择不同的类别;
select_query_run = mysql_query(
select_query);
I have a database table named with books. it consists of something like below
---------------------------------------------------------------
title | author | category
| |
--------------------------------------------------------------
xyz madman fiction
adapt brainless fiction
adaxx stupid fiction
follow idiot journal
your man acadamic
--------------------------------------------------------------
user is trying to search the value by author or title and based on category
ex: if user search for adapt by selecting fiction need display both adapt and adaxx
below is the code
using mysql database
<form action="search.php" method="post">
<?php
$select_query= "Select distinct category from books";
$select_query_run = mysql_query($select_query);
$select_query_array= mysql_fetch_array($select_query_run) ;
$cat = $select_query_array['category'];
echo "<select name='name'>";
while ($select_query_array= mysql_fetch_array($select_query_run) )
{// dynamic drop list
echo "<option value='".htmlspecialchars($select_query_array["category"])."' >".htmlspecialchars($select_query_array["category"])."</option>";
}
echo "</select>";
?>
<input type="text" name="searchvalue" size="5" id="searchfield" title="searchfield" onFocus="clearText(this)"/>
<input type="submit" name="submit" value="" alt="Search" id="searchbutton" title="Search" />
</form>
<?php
$button = $_POST['submit'];
$search = $_POST['searchvalue'];
if(strlen($search)<=1)
{
$name = $_POST['name'];
//echo $a;
echo "Search term too short";
}
else{
echo "You searched for $search <hr size='1'></br>";
$search_exploded = explode (" ", $search);
$x = "";
$construct = "";
foreach($search_exploded as $search_each)
{
$x++;
if($x==1)
$construct .="title LIKE '%$search_each%'";
else
$construct .="AND title LIKE '%$search_each%'";
}
$construct ="SELECT * FROM books WHERE $construct and category = '$name'";// I tried this but not working
$run = mysql_query($construct);
$foundnum = mysql_num_rows($run);
if ($foundnum==0)
echo "Sorry, there are no matching result for $search.</br></br>1.";
else
{
echo "$foundnum results found !<p>";
}
?>
解决方案
select_query= "Select distinct category from books";
select_query_run = mysql_query(
select_query);
这篇关于如何根据第三列从两列中选择值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文