douzhannao5357 2013-04-05 15:23
浏览 21
已采纳

PHP循环和表格

I'm having trouble getting this into one table. Right now I am using two tables and having them side by side but I don't think that is the best solution. I have the table all set up I just can't figure out how to get this into one.

while ($row = mysql_fetch_assoc($query)) {

$result .= "<tr> <td> {$row['a']} the {$row['b']} </td>      </tr>";
 }

  echo $thegames;

This produces something like:

       james the giant
       rick the monster
       chris the goblin

What I want is to be able to add more tds from a separate while loop...

  while($secondrow = mysql_fetch_assoc($secondquery)) { 
   $first = "<td> {$secondrow['alias']} </td>";
   $second = "<td> {$secondrow['number']} </td>";
  }

So in the end I would like it would look like this:

   james the giant $secondrow['alias']  $secondrow['number'] 
   rick the monster $secondrow['alias']  $secondrow['number']

etc...

Hope this makes sense. What I'm doing now is creating two separate tables and trying to line them up but don't like that method. Any help would be great.

  • 写回答

2条回答 默认 最新

  • dongmei8460 2013-04-05 15:28
    关注

    Judging by your code, you don't actually need to do two queries. If you are expecting 4 columns for each row, there's no need to do multiple while loops either. Rather, a change in the structure of your query to use a JOIN; that will enable you to get the full result set in one shot.

    As it has been mentioned, please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated.

    I'll show you how to do a JOIN with PDO, making some assumptions about your table names and primary keys based on the code you've shown so far:

    $pdo = new PDO("mysql:host=localhost;dbname=database", '-username-', '-password-');
    $sql = '
        SELECT 
            `players`.`id`, 
            `players`.`a`,
            `players`.`b`, 
            `player_info`.`alias`,
            `player_info`.`number`   
        FROM 
            `players` 
        LEFT JOIN
            `player_info` ON  `players`.id = `player_info`.`player_id`
    ';
    $players = $pdo->query($sql);
    
    $row_data= array();
    while ($row = $players->fetch()) {
        $row_data[] = '
            <td>'.$row->a.' the '.$row->b.'</td>
            <td>'.$row->alias.'</td>
            <td>'.$row->number.'</td>   
        ';
    }
    
    echo '<table><tr>'.implode('</tr><tr>', $row_data).'</tr></table'>;
    

    If the join proves to be impractical (not typical), then you should build your data array and generate the HTML as two seperate steps:

    // gather the data
    $data = array();
    $first = $pdo->query($sql);
    while($arow = $first->fetch()) {
        $one_data['a'] = $arow->a;
        $one_data['b'] = $arow->b;
    
        $second = $pdo->query($second_sql);
        while($brow = $second->fetch()) {
            $one_data['alias'] = $brow->alias;
            $one_data['number'] = $brow->number;
        }
        $data[] = $one_data;
    }
    
    // now generate HTML
    print '<table>';
    foreach ($data as $row) {
        print '
            <tr>
                <td>'.$row['a'].' the '.$row['b'].'</td>
                <td>'.$row['alias'].'</td>
                <td>'.$row['number'].'</td> 
            </tr>
        ';
    }
    print '</table>';
    

    And as a side note... columns named a and b are just a bad. idea. Those columns represent data, so give them labels that reflect what that data is. Dealing with mystery columns TODAY might be fine... you remember what they mean. 6 months from now? A year from now? Use meaningful names for all columns and variables -- why not?

    Documentation

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 centos7.9 IPv6端口telnet和端口监控问题
  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单