dtcyv3985 2014-12-05 03:22
浏览 112

如何在php上循环变量

i have problem on my comment list in my forum website php please help i trying to looping all the user comment, but it just appear 1 or fail looping that show all same comment in many number.

<?php
include 'connection.php'; 
        echo '<table border="1" bgcolor="#FBFBEF" width="100%" height="20"><tr>';
        echo '<td width="80%" height="20"><strong>Thread :';
        $query="select title from topic where topic_id=".$_REQUEST['topic'];
        $result=mysql_query($query);
        $row=mysql_fetch_array($result);
        echo $row['title'];
    if($row){
      do{   
    echo'</strong></td><td  align="center" valign="top"><strong><?php ';
      $query2="select * from comment where topic_id=".$_REQUEST['topic'];
      $result2=mysql_query($query2);
      $row2=mysql_fetch_array($result2);
      echo $row2['post_date'];
      echo '</strong></td></tr></table>';
    echo '<table border="1" bgcolor="#FBFBEF" width="100%" height="20"><tr><td align="center" valign="top" height="20"><strong> Post By :<strong></td><td width="85%" ><strong>';
        echo $row['title'];
        echo'</strong></td></tr>';
    echo '<tr><td align="center" valign="top"><strong>';
            $query3="select * from user_login where email='".$row2['post_by_user']."'";
            $result3=mysql_query($query3);
            $row3=mysql_fetch_array($result3);
            echo '<br>'.$row3['first_name'].' '.$row3['last_name'];

        echo '</strong></td><td align="left" valign="top" height=200>';
      //displaying list comment
    echo '<p>'.$row2['description'].'</p><hr>';
     }while($row=mysql_fetch_array($result2));
      }
      ?>
        </td>
    </tr>

why is the looping failed ?

  • 写回答

3条回答 默认 最新

  • doupingtang9627 2014-12-05 03:44
    关注

    I have re-arranged your loop. The first thing wrong was while($row=mysql_fetch_array($result2)) because you are using $row2 in that loop and changing $row won't help.

    <?php
    include 'connection.php';
    echo '<table border="1" bgcolor="#FBFBEF" width="100%" height="20"><tr>';
    echo '<td width="80%" height="20"><strong>Thread :';
    $query = "select title from topic where topic_id=" . $_REQUEST['topic'];
    $result = mysql_query($query);
    $row = mysql_fetch_array($result);
    echo $row['title'];
    if ($row) {
        echo '</strong></td><td  align="center" valign="top"><strong><?php ';
        $query2 = "select * from comment where topic_id=" . $_REQUEST['topic'];
        $result2 = mysql_query($query2);
        while ($row2 = mysql_fetch_array($result2)) {
            echo $row2['post_date'];
            echo '</strong></td></tr></table>';
            echo '<table border="1" bgcolor="#FBFBEF" width="100%" height="20"><tr><td align="center" valign="top" height="20"><strong> Post By :<strong></td><td width="85%" ><strong>';
            echo $row['title'];
            echo '</strong></td></tr>';
            echo '<tr><td align="center" valign="top"><strong>';
            $query3 = "select * from user_login where email='" . $row2['post_by_user'] . "'";
            $result3 = mysql_query($query3);
            $row3 = mysql_fetch_array($result3);
            echo '<br>' . $row3['first_name'] . ' ' . $row3['last_name'];
    
            echo '</strong></td><td align="left" valign="top" height=200>';
            //displaying list comment
            echo '<p>' . $row2['description'] . '</p><hr>';
        }
    }
    ?>
    </td>
    </tr>
    

    Very Important: You are highly prone to SQL-Injection. Please at-least sanitize your input before feeding to SQL. Also you are using depreciated mysql_* functions. Instead, use the MySQLi or PDO_MySQL extension.

    Side note: errors are inversely proportional to readability of your code. and maintainability is proportional to square of readability.

    Edit: If you want echo $row2['post_date'] only once for the loop you can use the loop like this. This will print post_date of the first row only.

    if ($row) {
        echo '</strong></td><td  align="center" valign="top"><strong><?php ';
        $query2 = "select * from comment where topic_id=" . $_REQUEST['topic'];
        $result2 = mysql_query($query2);
        $row2 = mysql_fetch_array($result2);
        if ($row2) {
            echo $row2['post_date'];
            do {
                echo '</strong></td></tr></table>';
                echo '<table border="1" bgcolor="#FBFBEF" width="100%" height="20"><tr><td align="center" valign="top" height="20"><strong> Post By :<strong></td><td width="85%" ><strong>';
                echo $row['title'];
                echo '</strong></td></tr>';
                echo '<tr><td align="center" valign="top"><strong>';
                $query3 = "select * from user_login where email='" . $row2['post_by_user'] . "'";
                $result3 = mysql_query($query3);
                $row3 = mysql_fetch_array($result3);
                echo '<br>' . $row3['first_name'] . ' ' . $row3['last_name'];
    
                echo '</strong></td><td align="left" valign="top" height=200>';
                //displaying list comment
                echo '<p>' . $row2['description'] . '</p><hr>';
            } while($row2 = mysql_fetch_array($result2));
        }
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题