开发者

Calling a function() in a printf()

I have a while() function that loops a row of data with开发者_如何学JAVAin my table.

Now What I need to to have a dropdown within 1 particular column of every row.

This dropdown is executed by utilizing a loop as well.

So here is the function that creates the dropdown:

function createDropdown($total)  
{  
    $i = 1;  
    echo "<select>";  
    while($i <= $total)  
    {  
        echo "<option value = '$i' id =* '$i'>$i</option>";  
        $i++;  
    }  
    echo "</select>";  
}

To print the table:

while($row1 = mysql_fetch_array($result1))  
{  
    print("<tr>");
    printf("<td>%s %s</td>", $row1["fname"], $row1["lname"]`);  
    printf("<td>".createDropdown($count)."</td>");  
    printf("<td colspan = '2'>%s</td>",$count);  
    printf("<td><span id = 'attendance'>111</span></td>");  
    print("</tr>");  
}

So now I have many dropdowns of which gives me what I want.

The problem is, I need to post all of these in the next page. Which means the ID of each dropdown has to be somehow be put into an array.

Anyway I can do this? Thanks in advance.

To make things clearer, the <select> can have any ID, but the problem is, every dropdown would have the same ID. When using $_POST, I will only attain the value from the last dropdown, not all of them. I have to array the dropdowns itself, which I have had problems with for some time.


How about this (sorry, I'm also a bit unclear on what you're asking):

function createDropdown($num, $total)
{
    $i = 1;
    $result = "<select id='sel$num' name='sel$num'>";
    while($i <= $total)
        {
        $result .= "<option value = '$i' id = '$i'>$i</option>";
        $i++;
        }
    $result .= "</select>";
    return $result;
}


$num = 0;
while($row1 = mysql_fetch_array($result1))
{
    print("<tr>");
    printf("<td>%s %s</td>", $row1["fname"], $row1["lname"]);
    printf("<td>".createDropdown($num++, $count)."</td>");
    printf("<td colspan = '2'>%s</td>",$count);
    printf("`111");
    print("");
}

I've changed your echos in your function and made it return the string instead (I think this is better style), and the select boxes will be given the name (sel0, sel1, sel2, etc...)


You'd have to pass the $id in as a parameter to the createDropDown() function:

function createDropdown($total, $id = 'somedefaultvalue') {
   echo "<select id=\"$id\">";
   for (...) {
      ...
   }
   echo "</select>"
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜