2013-12-21 41 views
0

我的代码:显示来自不同行的多个值与同一列中的值

<?php 
require_once('auth.php'); 
require_once('connection.php'); 

$value1 = $_POST['value1']; 
$qry=mysql_query("SELECT * FROM table WHERE (`value1` LIKE '%".$value1."%')") or die(mysql_error()); 
if(mysql_num_rows($qry) > 0){ 
while ($result = mysql_fetch_array($qry)){ 
$value1 = $result['value1']; 
$value2 = $result['value2']; 
}}else{die ("can't find ".$value1."");} 
?> 

    <br /> 
    <table width="700" border="0"> 
    <tr> 
     <td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td> 
    </tr> 
    <tr> 
     <td width="100">Some text:</td> 
     <td width="590"><?php echo $value2;?></td> 
    </tr> 

的代码是伟大的工作时,只有一个与值1的相同值的行。如果有2,3行或更多行的value1值相同,那么脚本仅显示查找到的值中我表中最后一行的值。我希望它显示来自所有查询行的条目的单独表格。 我在网上搜索的帮助,但我能找到的是如何检索我的数据库中的值,对我没有帮助

+0

把你的HTML while循环 – bansi

+0

你或许在寻找这样的事情里面:http://pastie.org/8566976? –

+0

我试过了,但是它弄糟了洞页。我知道这是可能的,但在凌晨5点,在我的头撞在墙上的那么多小时之后,我想不出有办法做到这一点。 – user3124519

回答

1

你应该把你的html块移到while循环中。试试这个

<br /> 
    <table width="700" border="0"> 
<?php 
if(mysql_num_rows($qry) > 0){ 
    while ($result = mysql_fetch_array($qry)){ 
    $value1 = $result['value1']; 
    $value2 = $result['value2']; 
?> 

    <tr> 
     <td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td> 
    </tr> 
    <tr> 
     <td width="100">Some text:</td> 
     <td width="590"><?php echo $value2;?></td> 
    </tr> 
<?php 
}}else{die ("can't find ".$value1."");} 
?> 

</table> 

我while循环之前移动<table>代码,把桌子行循环,在循环后表关闭标签内。这样你可以得到所有的行。

如果您不想在没有行的情况下显示表格,则可以将<table ..></table>标签移动到if语句块中。

编辑:

if(mysql_num_rows($qry) > 0){ 
?> 
<br /> 
    <table width="700" border="0"> 
<?php 
    while ($result = mysql_fetch_array($qry)){ 
    $value1 = $result['value1']; 
    $value2 = $result['value2']; 
?> 

    <tr> 
     <td colspan="2" align="center"><strong>Info for <?php echo $value1; ?></strong></td> 
    </tr> 
    <tr> 
     <td width="100">Some text:</td> 
     <td width="590"><?php echo $value2;?></td> 
    </tr> 
<?php 
    } 
    echo '</table>'; 
}else{die ("can't find ".$value1."");} 
?> 
1

您正在将返回行的值分配给$ value1和$ value2。因此,循环的每次迭代都会替换这些变量中最后存储的值。

这将有效地只给你找到的最后一个行值(赋给变量的最后一个值)。

您也需要通过他们将这些值推到一个集合/阵列,并重新循环来构建你的表,或将表代码放在初始循环中。

相关问题