drmy1050 2015-10-03 06:50
浏览 68
已采纳

php代码重用。 有没有更好的方法呢?

I have upgrade my code. In the old code I had 2 functions: display_maker_success() and display_maker_fail() but I realised I can combine those two functions into one display_maker_stat() by putting more arguments into the function. I like it very much!

Is better way to do this? I want more code reuse.

function display_maker_success($link, $userid){
    $status="closed";
    $result="completed";

    $sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 6;";

    $result = mysql_query($sql, $link);
    $isempty=mysql_num_rows($result);
    If ($isempty ==0) {
        echo "No Record";
    } else {
        echo "<table border=1>";
        echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo "<tr><td>$row[0]</td><td>$row[1]</td><td>Completed</td></tr>";
        };
        echo "</table>";
    };
};

function display_maker_fail ($link, $userid) {
    $status="closed";
    $result="fail";

    $sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
    $result = mysql_query($sql, $link);
    $isempty=mysql_num_rows($result);
    If($isempty ==0){
        echo "No Record";
    } else {
        echo "<table border=1>";
        echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo "<tr><td>$row[0]</td><td>$row[1]</td><td>fail</td></tr>";
        };
        echo "</table>";
    };
};

function display_maker_stat ($link, $userid, $reuslt, $limit) {
    $status="closed";
    $result="fail";

    $sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
    $result = mysql_query($sql, $link);
    $isempty=mysql_num_rows($result);
    If($isempty ==0){
        echo "No Record";
    } else {
        echo "<table border=1>";
        echo "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
        while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
            echo "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
        };
        echo "</table>";
    };
};
  • 写回答

1条回答 默认 最新

  • dongzhou5344 2015-10-03 07:08
    关注

    Try the below,

    Also there were few errors in your code and i have corrected them.

    function display_maker_stat($link, $userid, $reuslt = 'fail', $limit)
    {
        $status = "closed";
        $html = '';
        $sql = "select start, name from wuuk where tasker_id ='$userid' and status ='$status' and result ='$result' order by id desc LIMIT 1;";
        $query = mysql_query($sql, $link);
        if (mysql_num_rows($query) != 0) {
            $html .= "<table border=1>";
            $html .= "<tr><th>Date & Time</th><th>Name</th><th>Status</th></tr>";
            while ($row = mysql_fetch_array($query, MYSQL_NUM)) {
                $html.= "<tr><td>$row[0]</td><td>$row[1]</td><td>$result</td></tr>";
            }
            $html.= "</table>";
            echo $html;
        }
        else {
            echo "No Record";
        }
    }
    

    Read about OOP

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。