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 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库
  • ¥15 关于qualoth编辑及缝合服装领子的问题解决方案探寻
  • ¥15 请问怎么才能复现这样的图呀