문제

I am attempting to write some PHP that places values in a 2D array and create a table to present the data. My problem comes when I try to create a button at the end of each row, I am trying to give it a unique name based off the $row['ID'] from the SQL query(the first dimension of the array). I just do not know how to pull this data in the loop context.

$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);

if ($num_rows > 0){
while($row = mysql_fetch_assoc($result))
    {
    $list[$row['ID']]['ProductionNo']=$row['ProductionNo'];
    $list[$row['ID']]['UserID']=$row['UserID'];
    $list[$row['ID']]['StartTime']=$row['StartTime'];
    $list[$row['ID']]['EndTime']=$row['EndTime'];

    }

$openproduction = '<table><tbody><td>';
foreach ($list as &$value) 
    {
    $openproduction .= '<tr>';
        foreach ($value as &$valueitem)
        {
        $openproduction .= '<td> '.$valueitem.'</td>';
            }
    $openproduction .= "<td><input type='button' name='$key' class='button' ></td></tr></tr>";
    }
$openproduction .= '</tbody></td></table>';
unset($valueitem);
unset($value); 
도움이 되었습니까?

해결책

In this line:

foreach ($list as &$value) 

Use:

foreach ($list as $row_id => &$value) 

Then you'll have a valid $row_id inside that loop that you can reference.

다른 팁

Some of the things you were doing were simply weird and in some cases incorrect, try something like this instead (you're making it way more complicated than it actually is):

$openProduction = "<table><tbody>";
$openProduction .= "<td>productionId</td>";
$openProduction .= "<td>userID</td>";
$openProduction .= "<td>startTime</td>";
$openProduction .= "<td>endTime</td>";

while($row = mysql_fetch_assoc($result))
{
    $id = $row['ID'];
    $productionId = $row['ProductionNo']; 
    $userID = $row['UserID'];
    $startTime = $row['StartTime'];
    $endTime = $row['EndTime'];

    $openProduction .= "<tr id='row_$id'>";
         $openProduction .= "<td>$productionId</td>";
         $openProduction .= "<td>$userID</td>";
         $openProduction .= "<td>$startTime</td>";
         $openProduction .= "<td>$endTime</td>";
         $openProduction .= "<td><input type='button' id='button_$id' class='button' /></td>";
    $openProduction .= "</tr>";
}

$openProduction = ."</tbody></table>";

Well, If you want to do it in your own way then see the below code:

$result = mysql_query($query) or die(mysql_error());
$num_rows = mysql_num_rows($result);

if ($num_rows > 0){
while($row = mysql_fetch_assoc($result))
    {
    $row_id=0;// This variable will denote row number. 
    $list[$row_id]['ProductionNo']=$row['ProductionNo'];
    $list[$row_id]['UserID']=$row['UserID'];
    $list[$row_id]['StartTime']=$row['StartTime'];
    $list[$row_id]['EndTime']=$row['EndTime'];
    $row_id++;
    }

$openproduction = '<table><tbody><td>';
foreach ($list as &$value) 
    {
    $openproduction .= '<tr>';
        foreach ($value as &$valueitem)
        {
        $openproduction .= '<td> '.$valueitem.'</td>';
            }
    $openproduction .= "<td><input type='button' name='$key' class='button' ></td></tr></tr>";
    }
$openproduction .= '</tbody></td></table>';
unset($valueitem);
unset($value); 
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top