为while循环创建一个函数

时间:2014-03-12 15:24:37

标签: php function loops while-loop

您好,我试图将此代码转换为函数,我正在努力使其正确。我正在使用include来调用函数页面,它只是使它成为一个看似问题的函数。

<?php

        $sql= "SELECT `username`, `agent_name`, `building_number`, `building_name`, `street`, `town`, `post_code`, `email`, `description`, `profile`, `phone` 
               FROM `users` 
               ORDER BY `username` ASC";

        $result = mysql_query($sql);




    while ($user = mysql_fetch_array($result)) {
    echo '<div class="results">';
    echo '<table>';
    echo '<tr>';
    echo '<td><h2>';?><a href ="<?php echo $user['username'];?>"> <?php echo $user['agent_name'];'</a></h2></td>';
    echo '</tr>';
    echo '</table>';
    echo '</div>';
    }
    ?>

我的尝试是:

<?php
function test ($sql, $result){

    $sql= "SELECT `username`, `agent_name`, `building_number`, `building_name`, `street`, `town`, `post_code`, `email`, `description`, `profile`, `phone` 
           FROM `users` 
           ORDER BY `username` ASC";

    $result = mysql_query($sql);
}



while (test($user)) {
     $user= mysql_fetch_array($result);
echo '<div class="results">';
echo '<table>';
echo '<tr>';
echo '<td><h2>';?><a href ="<?php echo $user['username'];?>"> <?php echo $user['agent_name'];'</a></h2></td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
?>

4 个答案:

答案 0 :(得分:0)

我不会在你的功能中尝试这一切。你有功能只做一个'功能'。在这种情况下,它可能会传递一些参数并返回结果(从数据库中获取)。然后我会让函数将数据作为数组返回。然后,您可以为数组执行'foreach'语句,然后将其设置为表格。

答案 1 :(得分:0)

那应该做的工作,但是这是非常糟糕的做法:

<?php
function test (){

    $sql= "SELECT `username`, `agent_name`, `building_number`, `building_name`, `street`, `town`, `post_code`, `email`, `description`, `profile`, `phone` 
           FROM `users` 
           ORDER BY `username` ASC";

    return mysql_query($sql);

}



while ($user= mysql_fetch_array(test())) {
echo '<div class="results">';
echo '<table>';
echo '<tr>';
echo '<td><h2>';?><a href ="<?php echo $user['username'];?>"> <?php echo $user['agent_name'];'</a></h2></td>';
echo '</tr>';
echo '</table>';
echo '</div>';
}
?>

答案 2 :(得分:0)

为什么你要调用函数......你可以尝试这样......

    <?
     $start=$_GET["start"];
     if($start=="")
     $start=0;
     $max=50; //you can change this to number of records to display//
     $objTotalRS=mysql_query("SELECT * from users");
     $sqlcat="SELECT * FROM users Order by username asc limit $start,$max";
     $cat_data = mysql_query($sqlcat);
     ?>

在HTML中

     <div class="results">
     <table>
     <tr>
     <td> User Name </td>
     <td> Agent Name </td>
     </tr>
     <? while ($data = mysql_fetch_array($cat_data)) { ?>
     <tr>
     <td><?=stripslashes($data["username"])?></td>
     <td><?=stripslashes($data["agent_name"])?></td>
     </tr>
     <?}?>
     </table>
     </div>

答案 3 :(得分:0)

<?php

function getUsers() {
    //assumed mysql_connect()
    $sql= "SELECT `username`, `agent_name`, `building_number`, `building_name`, `street`, `town`, `post_code`, `email`, `description`, `profile`, `phone` FROM `users` ORDER BY `username` ASC";
    $query = mysql_query($sql);
    while (($row = @mysql_fetch_assoc($query)) != false) {
?>
<div class="results">
    <table>
        <tr>
            <td>
                <h2><a href="<?php echo $row['username'];?>"><?php echo $row['agent_name'];' ?></a></h2>
            </td>
        </tr>
    </table>
</div>
<?
    }
}
//call it!
getUsers();

?>