请在下面查看我的代码。通过该课程,我可以显示如下结果:
$connectTest = new testResults();
$test = $connectTest->grabResults(test, id, id);
echo $test['id'];
echo $test['name'];
echo $test['address'];
Run Code Online (Sandbox Code Playgroud)
在我的数据库中,“测试”表中有几个字段。我使用 index.php?id=1 进入我的页面。有了这个,我只显示一行的结果,因为它抓取所有结果 WHERE id = 1。
我需要的是下面的类来显示多个结果。它只显示一行。但是如果我有 id = 1 的多行,我想显示这些结果,但我无法让它工作。我尝试了很多东西,但我总是只得到一个结果。
班级:
class testResults
{
public function grabResults($table, $field, $id)
{
$result = $this->db->mysqli->query("SELECT * FROM $table WHERE $field = $id");
$resultData[] = array();
if(!$result)
{
return false;
}
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
foreach ($rows as $resultData)
{
return $resultData;
}
}
}
Run Code Online (Sandbox Code Playgroud)
编辑:
Array ( [id] => 25 [name] => test [status] => 1 )
Array ( [id] => 25 [name] => test [status] => 3 )
Array ( [id] => 25 [name] => test [status] => 5 )
Array ( [id] => 25 [name] => test [status] => 4 )
Array ( [id] => 26 [name] => test [status] => 1 )
Array ( [id] => 26 [name] => test [status] => 3 )
Array ( [id] => 27 [name] => test [status] => 1 )
Array ( [id] => 27 [name] => test [status] => 3 )
Array ( [id] => 27 [name] => test [status] => 5 )
Array ( [id] => 27 [name] => test [status] => 4 )
Array ( [id] => 27 [name] => test [status] => 2 )
Array ( [id] => 27 [name] => test [status] => 4 )
Array ( [id] => 27 [name] => test [status] => 1 )
Run Code Online (Sandbox Code Playgroud)
我得到了上述结果,有什么方法可以轻松地在回声中显示这些结果?对于每个 id 有不同的结果,因此每个查询的结果都会有所不同。所以我想在表格中显示结果,例如:
echo '<table>
<tr>
<td>$id</td>
<td>$name</td>
<td>$status</td>
</tr>
</table>';
Run Code Online (Sandbox Code Playgroud)
因此,所有结果都将像在 while 循环中一样显示。
您可以从函数返回数组,然后在脚本中循环
while($row = $result->fetch_assoc())
{
$rows[] = $row;
}
return $rows;
Run Code Online (Sandbox Code Playgroud)
您可以在脚本中循环
$test = $connectTest->grabResults(test, id, id);
foreach($test as $value)
{
print_r($value);
}
Run Code Online (Sandbox Code Playgroud)
OP编辑后
如果您需要将它们分开打印,您可以使用以下键访问具有变量名称和范围的所有元素
$test = $connectTest->grabResults(test, id, id);
echo '<table>';
foreach($test as $value)
{
echo '<tr>
<td>'.$value['id'].'</td>
<td>'.$value['name'].'</td>
<td>'.$value['status'].'</td>
</tr>';
}
echo '</table>';
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
27308 次 |
最近记录: |