0

I hope this hasn't been asked somewhere else because I looked but here goes. I wrote this function to create tables for me. It works but the problem I'm coming across is it runs the query everytime I call on this function. Is there a way to call this function and pass the query to the function?

function createawardtables($awardname, $awardshortname, $maxawards, $id){    
    $query="SELECT * FROM awards WHERE id = $id";
    $result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
    while($row = mysql_fetch_array($result)){
        $order = array("","1st","2nd","3rd","4th","5th","6th","7th","8th",'9th',"10th");
        echo "<table><th colspan=4><font color=maroon  size='4pt'><u><b>Orders of the $awardfullname</b></u></font></th>";   
        for($i=1; $i<$maxawards+1; $i++) {
            ${$awardshortname.$i} = dateconvert(($row["$awardshortname$i"]), 2);
            ${$awardshortname.$i.'by'} = $row["$awardshortname{$i}by"];
            ${$awardshortname.$i.'memo'} = $row["$awardshortname{$i}memo"];
            echo "<tr>
                <td>$order[$i] Order given on </td>
                <td><input type=text name='$awardshortname$i' title='mm/dd/yyyy' value= '${$awardshortname.$i}' size=8/></td>
                <td>by <input type=text name='$awardshortname{$i}by' title='Name of who gave the award.' value='${$awardshortname.$i.'by'}'size=15/></td>
                <td>for <input type=text name='$awardshortname{$i}memo' title='What the award was give for.' value='${$awardshortname.$i.'memo'}'size=15/></td>
                </tr>";

            if($i==10 or $awardshortname=="initiate"){
            echo "</table><br />";

            };
        };
    };
    mysql_free_result($result); 
};
Warmour
  • 53
  • 5
  • 4
    You are using [an **obsolete** database API](http://stackoverflow.com/q/12859942/19068) and should use a [modern replacement](http://php.net/manual/en/mysqlinfo.api.choosing.php). You are also (depending on where `$id` came from) **vulnerable to [SQL injection attacks](http://bobby-tables.com/)** that a modern API would make it easier to [defend](http://stackoverflow.com/questions/60174/best-way-to-prevent-sql-injection-in-php) yourself from. – Quentin Jan 14 '13 at 10:47

2 Answers2

2

Sure, just add a new parameter (like $awardname) to your function and use that when you call 'mysql_query()`.

Also be advised that using mysql_* functions is strongly discouraged:

This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQL extension should be used.

http://www.php.net/mysql_query

Jan Hančič
  • 53,269
  • 16
  • 95
  • 99
0

Here you go

function createawardtables($result, $awardname, $awardshortname, $maxawards, $id)
{    
    while($row = mysql_fetch_array($result)){
        $order = array("","1st","2nd","3rd","4th","5th","6th","7th","8th",'9th',"10th");
        echo "<table><th colspan=4><font color=maroon  size='4pt'><u><b>Orders of the $awardfullname</b></u></font></th>";   
        for($i=1; $i<$maxawards+1; $i++) {
            ${$awardshortname.$i} = dateconvert(($row["$awardshortname$i"]), 2);
            ${$awardshortname.$i.'by'} = $row["$awardshortname{$i}by"];
            ${$awardshortname.$i.'memo'} = $row["$awardshortname{$i}memo"];
            echo "<tr>
                <td>$order[$i] Order given on </td>
                <td><input type=text name='$awardshortname$i' title='mm/dd/yyyy' value= '${$awardshortname.$i}' size=8/></td>
                <td>by <input type=text name='$awardshortname{$i}by' title='Name of who gave the award.' value='${$awardshortname.$i.'by'}'size=15/></td>
                <td>for <input type=text name='$awardshortname{$i}memo' title='What the award was give for.' value='${$awardshortname.$i.'memo'}'size=15/></td>
                </tr>";

            if($i==10 or $awardshortname=="initiate"){
            echo "</table><br />";

            };
        };
    };
    mysql_free_result($result); 
};

and function calling

$query="SELECT * FROM awards WHERE id = $id";
$result = mysql_query($query) or die("There was a problem with the SQL query: " . mysql_error());
createawardtables($result, $awardname, $awardshortname, $maxawards, $id);
Nick
  • 602
  • 9
  • 22