4

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>";
    };
};
jxmallett
  • 4,087
  • 1
  • 28
  • 35
n00dle
  • 63
  • 5
  • I don't see why not... You can always improve code re usability by being more object oriented – Juan Bonnett Oct 03 '15 at 06:59
  • 2
    I think this kind of question may be a better fit for http://codereview.stackexchange.com. Though you should probably stop using the [deprecated mysql library](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) and make sure your code isn't vulnerable to [SQL injection](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) before you go there, as otherwise you may just drown in suggestions about those... (Don't get me wrong, though, I think it's a good question to ask and it's great that you want to improve your code like this.) – Matt Gibson Oct 03 '15 at 07:57

1 Answers1

3

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

jlocker
  • 1,478
  • 1
  • 12
  • 23
  • thx :) that $html is grt! So now I don't have to use "echo" hundred times! If I want to write this in OOP. How do I do it? – n00dle Oct 05 '15 at 11:43
  • @n00dle your welcome..About OOP, Concatenation will be the same :)..But others like mysqli connect or any there will be changes..Also i would like to recommend you to go with a framework like **codeigniter**..Also you can read more information about OOP,,Check my answer.. – jlocker Oct 05 '15 at 13:54
  • thx jlocker ;) u are grt help! About codeigniter, I never use third party before. But I wouldn't mind give it a try. Is it on this link http://www.codeigniter.com. Just download it right? – n00dle Oct 15 '15 at 05:10
  • @n00dle Your welcome...Yes you can visit [codeigniter official](http://codeigniter.com) and download the latest version which is **3.0.2**..If you need any help...Tell me anytime :) – jlocker Oct 15 '15 at 14:26
  • thx ;) Is there a chat system, so you talk to ppl directly? btw, I download codeingniter but use third party is new to me :/ – n00dle Oct 24 '15 at 04:21