Php 如何一次从mysql输出两列值并循环tru其余部分
在使用mysql的php中,如何只输出两列值一次,然后循环其他列 在这里: 期望输出:Php 如何一次从mysql输出两列值并循环tru其余部分,php,Php,在使用mysql的php中,如何只输出两列值一次,然后循环其他列 在这里: 期望输出: Category->SubCategory //Outputted only once Title, //Title Body and Views depend on whatever's there in the db. Could be 10 or more Body, Views Title, Body, Views Title, Body, Views 一种解决方案可能是一个bool,
Category->SubCategory //Outputted only once
Title, //Title Body and Views depend on whatever's there in the db. Could be 10 or more
Body,
Views
Title,
Body,
Views
Title,
Body,
Views
一种解决方案可能是一个bool,它可以记住类别是否已经输出:
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$category_output_done = false;
while($row = mysql_fetch_array($exe_fetch_data))
{
if (!$category_output_done) {
echo $row['category']; //Only once
echo $row['subcategory']; //Only once
$category_output_done = true;
}
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
另一种解决方案可以是每次迭代时递增的计数器。添加一个简单的计数器-
$counter = 0;
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
while($row = mysql_fetch_array($exe_fetch_data))
if ($counter == 0){
echo $row['category']; //Only once
echo $row['subcategory']; //Only once
$counter = 1;
}
这段代码应该可以做到这一点
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$row_once = mysql_fetch_array($exe_fetch_data)
echo $row_once['category']; //Only once
echo $row_once['subcategory']; //Only once
while($row = mysql_fetch_array($exe_fetch_data)) {
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
您还可以尝试使用一个标志,其值最初为true,在第一次迭代后将变为false。
但是我认为上面的代码会比使用标志更快,因为它不会在每次迭代后比较/检查标志值,并且有一个巨大的数据库,它会节省执行时间。因为您可能会有很多结果,所以在while循环中保留一个标志,无论您是否显示了已经消耗CPU的类别。由于您总是使用第一个结果集显示类别,因此我不会在while循环中保留标志,而是这样做:
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$iNrOfResults = mysql_num_rows($exe_fetch_data);
//See if the result set has any rows at all, display the categories once
if(iNrOfResults > 0)
{
$row = mysql_fetch_array($exe_fetch_data)
echo $row['category']; //Only once
echo $row['subcategory']; //Only once
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
//See if there are any more results, do not display the categories anymore
if(iNrOfResults > 1)
{
while($row = mysql_fetch_array($exe_fetch_data))
{
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
}
在第一次显示后,将标志添加到false out the loop,然后添加到true
$exe_fetch_data = mysql_query("select category, subcategory, title, body, views from table");
$iNrOfResults = mysql_num_rows($exe_fetch_data);
//See if the result set has any rows at all, display the categories once
if(iNrOfResults > 0)
{
$row = mysql_fetch_array($exe_fetch_data)
echo $row['category']; //Only once
echo $row['subcategory']; //Only once
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
//See if there are any more results, do not display the categories anymore
if(iNrOfResults > 1)
{
while($row = mysql_fetch_array($exe_fetch_data))
{
echo $row['title'];
echo $row['body'];
echo $row['views'];
}
}