php code reuse. Is there a better way to do it? -
i have upgrade code. in old code had 2 functions: display_maker_success()
, display_maker_fail()
realised can combine 2 functions 1 display_maker_stat()
putting more arguments function. much!
is better way this? want more code reuse.
function display_maker_success($link, $userid){ $status="closed"; $result="completed"; $sql = "select start, name wuuk tasker_id ='$userid' , status ='$status' , result ='$result' order 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 wuuk tasker_id ='$userid' , status ='$status' , result ='$result' order 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 wuuk tasker_id ='$userid' , status ='$status' , result ='$result' order 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>"; }; };
try below,
also there few errors in code , have corrected them.
function display_maker_stat($link, $userid, $reuslt = 'fail', $limit) { $status = "closed"; $html = ''; $sql = "select start, name wuuk tasker_id ='$userid' , status ='$status' , result ='$result' order 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 oop
Comments
Post a Comment