如何在以逗号或其他字符分隔的一行中显示返回的条目

时间:2018-12-29 23:25:08

标签: php mysql

我要在一行中显示所有返回的列,并用“,”或“;”分隔。

我早些时候提交了这个问题,但使它更加混乱。基本上我有一个带有19列A-S的mysql数据库,它也有超过200,000行数据。一些数据是空白单元格,某些在某些列中匹配,但在其他列中不匹配。到目前为止,我所能做的就是根据这19列输入数据,并根据与这些列匹配的表中的所有输入或返回值将输入返回。因此,如果用户将一列留空,它将根据用户使用的值将其返回。因此,用户可以使用1列或全部19个潜在的A-S。

但是,我试图做的是在该页面上的另一个表中,显示在多行中的单个条目上方只有一行,该行返回所有19列以及所有用“,”或“;”分隔的值;

因此,举个简单的例子,假设我有3列A,B,C用户输入,其中3列对应于B并点击提交。当前的回报看起来像这样

A|B|C
4|3|3
2|3|9
1|3|2

我想以某种方式在此页面上显示

A    |B|C 
4,2,1|3|3,9,2

我只是想不通如何获取用户输入并将其返回到用字符分隔的那一行。

<body>

<form action="" method="post">

<input name="Asearch" placeholder="A" type="search" autofocus>
<input name="Bsearch" placeholder="B"type="search">
<input name="Csearch" placeholder="C" type="search">
<input name="Dsearch" placeholder="D" type="search">
<input name="Esearch" placeholder="E" type="search">
<input name="Fsearch" placeholder="F" type="search">
<input name="Gsearch" placeholder="G" type="search">
<input name="Hsearch" placeholder="H" type="search">
<input name="Isearch" placeholder="I" type="search">
<input name="Jsearch" placeholder="J" type="search">
<input name="Ksearch" placeholder="K" type="search">
<input name="Lsearch" placeholder="L" type="search">
<input name="Msearch" placeholder="M" type="search">
<input name="Nsearch" placeholder="N" type="search">
<input name="Osearch" placeholder="O" type="search">
<input name="Psearch" placeholder="P" type="search">
<input name="Qsearch" placeholder="Q" type="search">
<input name="Rsearch" placeholder="R" type="search">
<input name="Ssearch" placeholder="S" type="search">
<input type="submit" name="button">

</form>

<table align="center" border="1px" style="width:auto"; line-height:40px;">
<tr>
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
<th>H</th>
<th>I</th>
<th>J</th>
<th>K</th>
<th>L</th>
<th>M</th>
<th>N</th>
<th>O</th>
<th>P</th>
<th>Q</th>
<th>R</th>
<th>S</th>
</tr>

<?php

$con=mysql_connect('localhost', 'USER', 'PW');
$db=mysql_select_db('DB');


if(isset($_POST['button'])){    //trigger button click

$searchA=$_POST['Asearch'];
$searchB=$_POST['Bsearch'];
$searchC=$_POST['Csearch'];
$searchD=$_POST['Dsearch'];
$searchE=$_POST['Esearch'];
$searchF=$_POST['Fsearch'];
$searchG=$_POST['Gsearch'];
$searchH=$_POST['Hsearch'];
$searchI=$_POST['Isearch'];
$searchJ=$_POST['Jsearch'];
$searchK=$_POST['Ksearch'];
$searchL=$_POST['Lsearch'];
$searchM=$_POST['Msearch'];
$searchN=$_POST['Nsearch'];
$searchO=$_POST['Osearch'];
$searchP=$_POST['Psearch'];
$searchQ=$_POST['Qsearch'];
$searchR=$_POST['Rsearch'];
$searchStem=$_POST[‘Ssearch’];



$query=mysql_query("select * from DB where A like '%{$searchA}%' AND B like '%{$searchB}%' AND C like '%{$searchC}%' AND D like '%{$searchD}%'
AND E like '%{$searchE}%' AND F like '%{$searchF}%' AND G like'{$searchG}%' AND H like '%{$searchH}%'
AND I like '%{$searchI}%' AND J like '%{$searchJ}%' AND K like '%{$searchK}%' AND L like '%{$searchL}%'
AND M like '%{$searchM}%' AND M like '%{$searchM}%' AND N like '%{$searchN}%' AND O like '%{$searchO}%'
AND Q like '%{$searchQ}%' AND R like '%{$searchR}%' AND S like '%{$searchS}%' LIMIT 1000");


if (mysql_num_rows($query) > 0) {


while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['A']."</td><td>".$row['B']." </td><td>".$row['C']."</td><td>".$row['D']."</td><td>".$row['E']."</td><td>".$row['F']."</td><td>".$row['G']."</td>
<td>".$row['H']."</td><td>".$row['I']."</td><td>".$row['J']."</td><td>".$row['K']."</td><td>".$row['L']."</td><td>".$row['M']."</td><td>".$row['N']."</td>
<td>".$row['O']."</td><td>".$row['P']."</td><td>".$row['Q']." </td><td>".$row['R']."</td><td>".$row[‘S’]."</td></tr>";
}}else{echo "No DATA<br><br>";}
}else{                          //while not in use of search  returns all the values
$query=mysql_query("select * from DB LIMIT 10");

while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['A']."</td><td>".$row['B']."</td><td>".$row[‘C’]."</td><td>".$row['D']."</td><td>".$row['E']."</td><td>".$row['F']."</td><td>".$row['G']."</td>
<td>".$row['H']."</td><td>".$row['I']."</td><td>".$row['J']."</td><td>".$row['K']."</td><td>".$row['L']."</td><td>".$row['M']."</td><td>".$row['N']."</td>
<td>".$row['O']."</td><td>".$row['P']."</td><td>".$row['Q']."</td><td>".$row['R']."</td><td>".$row['S']."</td></tr>";
}}mysql_close();?>

已经更改了搜索,现在我没有得到返回的表,但是却出现了无数据错误

$query=mysql_query("select group_concat(distinct A order by A) as A,   group_concat(distinct B order by B) as B, group_concat(distinct C by C) as C, group_concat(distinct D order by D) as D, group_concat(distinct E order by E) as E, group_concat(distinct F order by F) as F, group_concat(distinct G order by G) as G,group_concat(distinct H order by H) as H, group_concat(distinct I order by I) as I, group_concat(distinct J order by J) as J, group_concat(distinct K order by K) as K, group_concat(distinct L order by L) as L, group_concat(distinct M order by M) as M, group_concat(distinct N order by N) as N, group_concat(distinct O order by O) as O, group_concat(distinct P order by P) as P, group_concat(distinct Q order by Q) as Q, group_concat(distinct R order by R) as R, group_concat(distinct S order by S) as S from DB where A like '%{$searchA}%' AND B like '%{$searchB}%' AND C like '%{$searchC}%' AND D like '%{$searchD}%' AND E like '%{$searchE}%' AND F like '%{$searchF}%' AND G like'%{$searchG}%' AND H like '%{$searchH}%' AND I like '%{$searchI}%' AND J like '%{$searchJ}%' AND K like '%{$searchK}%' AND L like '%{$searchL}%' AND M like '%{$searchM}%' AND M like '%{$searchM}%' AND N like '%{$searchN}%' AND O like '%{$searchO}%' AND Q like '%{$searchQ}%' AND R like '%{$searchR}%' AND S like '%{$searchS}%' LIMIT 1000");
if (mysql_num_rows($query) > 0) {


while ($row = mysql_fetch_array($query)) {
echo "<tr><td>".$row['NODE']."</td><td>".$row['KMA']."</td><td>".$row['IRISCARSHE']."</td><td>".$row['CMTS']."</td><td>".$row['DIVISION']."</td><td>".$row['SYS']."</td><td>".$row['PRN']."</td>
<td>".$row['VODBACKOFFICE']."</td><td>".$row['ZIP']."</td><td>".$row['CITY']."</td><td>".$row['STATE']."</td><td>".$row['BILLER']."</td><td>".$row['HUBNAME']."</td><td>".$row['SITEIDBU']."</td>
<td>".$row['ALTHUBID']."</td><td>".$row['USPSCITIES']."</td><td>".$row['COUNTYBYZIP']."</td><td>".$row['HUBID']."</td><td>".$row['SYSTEMORTIMEZONE']."</td>
</tr>";
}}else{echo "No DATA<br><br>";}

1 个答案:

答案 0 :(得分:2)

您可以在SQL查询中使用group_concat来实现此目的:

select group_concat(distinct A order by A) as A,
       group_concat(distinct B order by B) as B
from   DB 
where  A like '%{$searchA}%' AND B like '%{$searchB}%' 

请注意,%的{​​{1}}条件中缺少like

此外,请您帮个忙,并远离G API。几年前已经过时了(文档中有很大的警告标志)。改用mysql_mysqli