-1

好的,这似乎很难解释,但我现在已经到了极限。去过那里,做到了,但我无法让它发挥作用。所以就在这里;

我有一个名为的表passengers,其中包含以下数据:

在此处输入图像描述

我正在尝试以这种表格格式选择以下数据:

在此处输入图像描述

使用此代码:

<?php
require("aacfs.php");
$ac=mysql_query("select distinct fquo_id as 'fquo_id' from passengers where reservno = '0000188'") or die(mysql_error());
echo "<table><tr>";
while($bd=mysql_fetch_array($ac))
{
    $eg=$bd['fquo_id'];
    $w=mysql_query("select * from passengers where fquo_id = '$eg'") or die(mysql_error());
    $no=1;
    while($x=mysql_fetch_array($w))
    {
        $y=$x['pass_name'];

        echo "<tr><td colspan=2>$no. <input type=text name='pass1_".$no."' value='$y'></td><td>$no. <input type=text name='pass2_".$no."' value='$y'></td></tr>";
        $no++;
    }
}

echo"</tr></table>";

?>

但它看起来像这样:

在此处输入图像描述

我知道我做错了什么,但相信我,我已经修改了一段时间,但仍然无法按照我想要的方式进行。这是我能得到的最接近我想要的输出。如何正确垂直打印表格?我知道类似的代码,SELECT * FROM tbl \G但它在 PHP 中不起作用。请帮帮我。谢谢。

4

4 回答 4

0

假设我正确理解了您想要的内容,则以下内容应该可以

<?php
...
    $no=1;
    while($x1 = mysql_fetch_assoc($w))
    {
        $y1 = $x1['pass_name'];
        $y2 = "";
        if($x2 = mysql_fetch_assoc($w))
           $y2 = $x2['pass_name'];            

        echo "<tr><td colspan=2>$no. <input type=text name='pass1_" . $no . "' value='" . $y1 . "'></td><td>$no. <input type=text name='pass2_" . $no . "' value='" . $y2 . "'></td></tr>";
        $no++;
    }
...

?>
于 2013-01-31T08:57:19.503 回答
0

看看这个DEMO能不能帮到你,如果能,你的代码应该改成:

<?php
require("aacfs.php");
$ac=mysql_query("select distinct fquo_id as 'fquo_id' from passengers where reservno = '0000188'") or die(mysql_error());
echo "<table>";
while($bd=mysql_fetch_array($ac))
{
    $eg=$bd['fquo_id'];
    $w=mysql_query("select * from passengers where fquo_id = '$eg'") or die(mysql_error());

    $n = 2;
    $i = 0;
    while($x=mysql_fetch_array($w))
    {
       $i++;
       if($n % 2 == 0){
        echo  "<tr>";
       }
       $y=$x['pass_name'];

       echo "<td colspan=2>$no. <input type=text name='pass1_".$no."' value='$y'></td>";
       if($n % 2 == $n){
        echo  "</tr>";
       }
       $n++;
    }
}

echo"</table>";

?>
于 2013-01-31T09:21:54.273 回答
0
$ac=mysql_query("select distinct fquo_id as 'fquo_id' from passengers where reservno ='0000188'") or die(mysql_error());
while($bd=mysql_fetch_array($ac))
{echo "<table>";
    $eg=$bd['fquo_id'];
    $w=mysql_query("select * from passengers where fquo_id = '$eg'") or die(mysql_error());
    $no=1;
    while($x=mysql_fetch_array($w))
    {
        $y=$x['pass_name'];

  echo "<tr><td>$no. <input type=text name='pass1_".$no."' value='$y'></td></tr>";
        $no++;
    }
</table>
}

试试这个.. :P

于 2013-01-31T09:34:15.030 回答
0
<?php
require("aacfs.php");
$ac         =   mysql_query("SELECT * FROM passengers WHERE reservno = '0000188' ORDER BY fquo_id, pass_id") or die(mysql_error());
$temp_array = array();
while($row  = mysql_fetch_assoc($ac)){
    $temp_array[$row['fquo_id']][] = $row;
}

$td_count   = count($temp_array);
$tr_count   = 1;
$fquo_id    = array();

foreach($temp_array as $key=>$val){
  if($tr_count < count($val)){$tr_count = count($val);}
  $fquo_id[]= $key;
}
$tr_count   = 8; // if you want to display 8 rows;
echo "<table>";
for($i=0; $i<$tr_count; $i++){
    echo "<tr>";
    $no  = $i+1;
    for($j=0; $j<$td_count; $j++){
        echo "<td>";
        if(array_key_exists($i,$temp_array[$fquo_id[$j]])){
            echo $no.". <input type=text name='pass1_".$no."' value='".$temp_array[$fquo_id[$j]][$i]['pass_name']."'>";
        }
        else{
            echo $no.". <input type=text name='pass1_".$no."' value=''>";
        }
        echo "</td>";
    }
    echo "</tr>";
}
echo "</table>";

?>

在此处输入图像描述

于 2013-01-31T09:56:11.640 回答