显示相同列的结果但在php中具有不同的值

时间:2014-07-25 05:59:53

标签: php mysql

我有以下php代码,根据地点查询我的数据库表。 表结构如下:

 s/n | inventory no |   asset no  |    place
=====+==============+============+=============
  1  |      125A    |    5245     |   London
  2  |     1254B    |    7545     |   London
  3  |      128A    |    5645     |  New York
  4  |      254B    |    1545     |    Tokyo
  5  |      6545    |    1456     |    Tokyo

代码:

$location=$_POST['loc'];  
foreach ($location as $chk1)
{ 
   echo "<br>";
   $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);


if($result==null)
 {
   echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
 }
 $row = mysql_fetch_array($result);
 if($row==null)
 {
   echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
  continue;
 }

   mysql_data_seek($result, 0);
  ?><table border='1' id="desktop" caption=<?php $location ?> >
 <tr>
 <th>S/N</th>
 <th>Inventory No</th>
 <th>Asset No</th>
 <th>Place</th>
 </tr>
  <?php
 while($row = mysql_fetch_array($result)) {?>
 <tr class="alt">
 <?php
    echo "<td>" . $row['S/N'] . "</td>";
    echo "<td>" . $row['Inventory no'] . "</td>";
    echo "<td>" . $row['Asset No'] . "</td>";
    echo "<td>" . $row['Place'] . "</td>";
    echo "</tr>";

  }

 echo "</table>";
}

修改: CSS代码:

  #desktop {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}

#desktop th {
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #FFCC33;
color: #ffffff;
}

#desktop td, #desktop th {
font-size: 1em;
border: 1px solid #FFCC33;
padding: 3px 7px 2px 7px;
}


#desktop tr.alt td {
color: #000000;
background-color: #FFFFCC;
}

此处,$location正在存储以html格式选择的位置。

我希望将不同位置的查询返回结果显示为单个表。目前,它显示不同位置的结果,因为不同的表具有自己的表头。我希望将返回的查询附加到一个变量中。

2 个答案:

答案 0 :(得分:1)

只需将你的表放在foreach循环之前。因为它在循环内部,所以每次循环都会重复

<table border='1' id="desktop" caption=<?php $location ?> >
   <tr>
    <th>S/N</th>
    <th>Inventory No</th>
    <th>Asset No</th>
    <th>Place</th>
  </tr>
foreach ($location as $chk1)
  { 
    echo "<br>";
    $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);
 ...
 ...

  } 
}
echo "</table>";

答案 1 :(得分:0)

尝试使用此代码,我已将位置数组默认更改为london,

您已更改字段名称的首字母不应为大写字母。希望这有帮助:)

  $location=array('London','Tokyo'); 
 $str.='<table border="1" id="desktop">
 <tr>
 <th>S/N</th>
 <th>Inventory No</th>
 <th>Asset No</th>
 <th>Place</th>
 </tr>'; 
foreach ($location as $chk1)
{ 
   echo "<br>";
   $sql="SELECT * FROM desktop WHERE Place='$chk1';";
    $result=mysql_query($sql);


if($result==null)
 {
   echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
 }
 $row = mysql_fetch_array($result);
 if($row==null)
 {
   echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
  continue;
 }

   mysql_data_seek($result, 0);



 while($row = mysql_fetch_array($result)) {
 $str.='<tr class="alt">

        <td>' . $row['sr'] . '</td>
        <td>' . $row['inventory_no'] . '</td>
    <td>'. $row['asset_no'] . '</td>
    <td>' . $row['place'] . '</td>
    </tr>';

  }


}
$str.='</table>';
echo $str;
?>