尝试根据输入生成表格列和行。使用较小的数字,它似乎正常工作,例如输入2行和3列。但是当输入稍微大一些的数字(例如5行)时,输出的行数有时会无限循环。
InitializeComponent();
SqlConnection conn = new SqlConnection("Data Source=(local);Initial Catalog=Project;Integrated Security=True");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter
("SELECT [StandardCode] FROM [dbo].[StandardCodesAndDetails]", conn);
adapter.Fill(ds);
this.lstBoxStandardCodes.DataSource = ds.Tables[0];
this.lstBoxStandardCodes.DisplayMember = "StandardCode";
conn.Close();
答案 0 :(得分:2)
首先,你不需要While循环,它没有意义,可能会导致问题。
其次,您对行和列使用$i
,您不能对两者使用相同的变量。使用$i
表示一个,$j
表示另一个。这应该有效:
if(isset($_POST['submit'])) {
$rows = (int)$_POST['row_num'];
$cols = (int)$_POST['col_num'];
$n = 1;
echo '<table id="">';
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n++ . '"></td>';
}
echo '</tr>';
}
echo '</table>';
}
答案 1 :(得分:1)
范围变量$i
问题在这里:
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($i = 0; $i < $cols; $i++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}
我认为条件改变$ i为$ j:
for($i = 0; $i < $rows; $i++) {
echo '<tr>';
for($j = 0; $j < $cols; $j++) {
echo '<td><input type="text" name="field_' . $n . '"></td>';
$n++;
}
}