简体   繁体   English

显示具有相同列值的不同行的多个值

[英]Display multiple values from different rows with same column value

My code: 我的代码:

<?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>

The code is working great when there is only one row with the same value of value1. 当只有一行具有相同的value1值时,代码工作正常。 If there are 2,3 or more rows with the same value for value1 the script displays only the values from the last row in my table found with the queried value. 如果有2,3或更多行具有相同的value1值,则脚本仅显示查找值中找到的表中最后一行的值。 I want it to display separate tables with entries from all queried rows. 我希望它显示包含所有查询行的条目的单独表。 I've searched online for help, but all I could find is how to retrieve the values from my database, nothing helpful for me 我在网上寻求帮助,但我能找到的是如何从我的数据库中检索值,对我没有任何帮助

You're assigning values from the returned rows to $value1 and $value2. 您将返回的行中的值分配给$ value1和$ value2。 So each iteration of the loop replaces the last stored values in these variables. 因此,循环的每次迭代都会替换这些变量中的最后存储值。

This will effectively only give you the last row values found (last values assigned to the variables.) 这实际上只会给你找到的最后一行值(分配给变量的最后一个值)。

You either need to push these values into a collection/array and re-loop through them to build your table, or put the table code in the initial loop. 您需要将这些值推送到集合/数组中并重新循环它们以构建表,或将表代码放在初始循环中。

you should move your html block to inside the while loop. 你应该将你的html块移动到while循环内部。 try this 尝试这个

<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>

I moved the <table> code to before the while loop, moved the table rows to inside the loop and the table close tag after the loop. 我将<table>代码移动到while循环之前,将表行移动到循环内部,并在循环之后移动表close标记。 this way you get all rows. 这样你就可以获得所有行。

If you don't want to show the table at all if there is no rows you can move the <table ..> and </table> tags to inside the if statement block. 如果您不想在没有行的情况下显示表,则可以将<table ..></table>标记移动到if语句块内。

Edit: 编辑:

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."");}
?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM