2013-04-01 2 views
2

루프를 돌릴 때마다 while 루프 안에 라디오 버튼이 있고 라디오의 이름이 1 씩 증가합니다.
코드가 점진적으로 증가하지 않기 때문에 코드가 작동하지 않습니다. 어떤 제안이라도 좋을 것입니다.

$query = mysql_query("SELECT * FROM table WHERE id = '1' ORDER BY time ASC"); 
echo '<table> <th> A </th> <th> time </th> <th> B </th>'; 

while($row = mysql_fetch_assoc($query)) { 

    $i= 1; 
    echo '<tr><td>'; 
    echo '<input type="radio" name="';echo $i++; echo'" /> '; echo $row['a']; 
    echo '</td>'; 
    echo '<td>'; 
    echo $row['time']; 
    echo '</td>'; 
    echo '<td>'; 
    echo '<input type="radio" name="';echo $i++; echo '" />'; echo $row['b']; 
    echo '</td> </tr> '; 
} 


echo '</tr></table>'; 
+9

while 루프에서'$ i'를 1로 설정했습니다. – andrewsi

+0

감사합니다. @ andrewsi 감사합니다. – Ricky

답변

7

매번 카운터를 재설정합니다.

$i = 1; 

while($row = mysql_fetch_assoc($query)) { 
    // Your code 

    $i++; 
} 

.. 및 echo $i;echo $i++;를 교체합니다.

1

은 예를 들어 다음과 같이 루프

외부 $i= 1; 이동 :

... 
$i= 1; 
while($row = mysql_fetch_assoc($query)) { 
    ... 
    echo '<input type="radio" name="';echo $i; echo'" /> '; echo $row['a']; 
    ... 
    ... 
    $i++; 
} 
1

로직이 유효하지 않습니다

$i = 0; 
while($row = mysql_fetch_assoc($query)) { 
$i++; 
echo '<tr><td>'; 
echo '<input type="radio" name="';echo $i; echo'" /> '; echo $row['a']; 
echo '</td>'; 
echo '<td>'; 
echo $row['time']; 
echo '</td>'; 
echo '<td>'; 
echo '<input type="radio" name="';echo $i; echo '" />'; echo $row['b']; 
echo '</td> </tr> '; 
} 

당신이 $i++ 입력 할 때마다 PHP 그것을 증가합니다.

0

ㅎㅎ ..;))

$query = mysql_query("SELECT * FROM table WHERE id = 1 ORDER BY time ASC"); 

echo "<table>\n"; 
echo " <tr>\n"; 
echo " <th> A </th>\n"; 
echo " <th> time </th>\n"; 
echo " <th> B </th>\n"; 
echo " </tr>\n"; 

$i = 0; 

while($row = mysql_fetch_assoc($query)) { 
    echo " <tr>\n" 
    echo ' <td><input type="radio" name="' . $i . '" />' . $row['a'] . "</td>\n"; 
    echo ' <td>' . $row['time'] . "</td>\n"; 
    echo ' <td><input type="radio" name="' . $i . '" />' . $row['b'] . "</td>\n"; 
    echo " </tr>\n"; 
    $i++; 
} 

echo "</table>\n"; 

나는 '이 맞다 경우 SQL 쿼리를 포함하여 코드를 확인, 아직 오류가 있다면이 코드를 해결하기 위해 휴식을하거나 형식 wasn를 경우하지 않았다 맞아!