故障循环-在Mysqli_Fetch_Array

时间:2018-09-08 10:08:36

标签: php html mysql mysqli-fetch-array

我正在设计此页面,该页面查询两个不同的表T1 =任务T2 =项目

它从表中提取项目详细信息和任务(分步说明)。

然后页面进入Fetch_Array和While语句以为每个项目打印项目详细信息标题。在每个“项目详细信息”标题的下面,它进入另一个循环以显示任务。

页面正确显示项目标题。 然而,当它进入任务的第二个循环时;

(1)打印查询中每隔一行(偶数行)的任务。 (2)将所有内容打包成一个自动换行,而不是逐行。     和 (3)它仅显示第一组指令,而不会继续显示第二组指令。

下面是我为页面编写的代码。

<html lang="en">
<head>
<title>Deferred and Completed Projects</title>
<meta charset="utf-8">
<style>
table {margin: auto;
      border: 3px solid #000066;
      width: 1000px;
      max-width: 1000px;
      border-spacing: 0;
      border-collapse: collapse;}
      
  td, th {border: 1px solid #000066;
           padding: 0.5em;       
           font-size: .75em;
          font-family: Arial, sans-serif;}
  caption {font-family: Verdana, sans-serif;
          font-weight: bold;
          font-size: .80em;
          padding-bottom: 0.5em;}
  td, th { padding: 0.7em;
          border-style: none;
          font-size: .75em;
          font-family: Arial,sans-serif; }
  tr:nth-of-type(even) {background-color: #eaeaea;}
  tr:first-of-type {background-color: #006; color: #eaeaea;}
</style>
</head>
<body>
 <table border="1" #6a8fba>  
 <caption>Completed and Deferred Projects</caption>
<?php  
// Get a connection for the database
require_once('../mysqli_connect.php');

// Create a query for the database
$query = "SELECT Projects.*
FROM Projects
WHERE (((Projects.Status)='Completed' Or (Projects.Status)='Deferred'))
ORDER BY Projects.Status DESC , Projects.Priority, Projects.EndDate DESC";
// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);



// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo '<table align="center"
cellspacing="5" cellpadding="8">

<tr><th align="left" width="12%">Project Name</th>
<th align="left" width="14%">Owner</th>
<th align="left" width="17%">Category</th>
<th align="left" width="12%">Priority</th>
<th align="left" width="10%">Status</th>
<th align="center" width="35%">Begin Date</th>
<th align="left" width="14%">End Date</th>
<th align="left" width="17%">Budget In Days</th>
<th align="left" width="12%">Budget</th>
<th align="left" width="12%">Notes</th>
<th align="left" width="12%">Attachments</th></tr>';
// mysqli_fetch_array will return a row of data from the query
// until no further data is available
echo '<tr><td align="center">' . 
$row['ProjectName'] . '</td>';
$_SESSION [projnamvalu]=$row['ProjectName'];
echo '<td align="left">' . 
$row['Owner'] . '</td><td align="left">' .
$row['Category'] . '</td><td align="left">' . 
$row['Priority'] . '</td><td align="left">' .
$row['Status'] . '</td><td align="left">' . 
$row['Begin Date'] . '</td><td align="left">' .
$row['EndDate'] . '</td><td align="left">' .
$row['BudgetinDays'] . '</td><td align="left">' . 
$row['Budget'] . '</td><td align="left">' . 
$row['Notes'] . '</td><td align="left">' . 
$row['Attachments'] . '</td></tr><br>';
//echo '<tr><td align="center">' . $row = include('taskssubform3.php');'</td></tr><br>';
//$sql = "SELECT Project, Title, AssociatedDocuments FROM Tasks JOIN Projects WHERE Projects.ID = Tasks.Project";
$sql = "select * from (SELECT * from Tasks order by Title asc)as sub left join Projects on Projects.ID=sub.Project";
//$result = $dbc->query($sql);
$result023 = @mysqli_query($dbc, $sql);

// get text values of project names from the database
// while($row023 = @mysqli_fetch_array($result023)){
echo " <table border='1' #6a8fba>  
 <caption>Tasks</caption>
    <tr>
    <th>Task Title</th>
    <th>Associated Document</th>
    </tr></table>";       
    if ($result023->num_rows > 0) {
     // output data of each row

// ********THE PROBLEM STARTS HERE*************

$i = 0; 
while ($rows[$i] = mysqli_fetch_array ($result023)){
 echo '<td>'. $row6[2] . $row6[8] .'</td></table>'; 

    if(($row7 = mysqli_fetch_array ($result023)) != false)
    {
        echo '
            <td>'. $row7[2] . $rows1[8] .'</td>'; 
    }
    else
   {
        echo '&nbsp;';
    }
    echo '
        </td>
        </tr>';

}
$i++;
}
}
}

?>
</table>
</body>
</html>

============================================= ======================   这是我对代码进行的新修改。

此修改可解决格式问题和行的显示(现在是所有行,而不仅仅是偶数行),并以正确的顺序显示ftask表。但是,它仍然仅显示一组过程,而没有经历并显示所有指令集。

<html lang="en">
<head>
<title>Get Request Info</title>
<meta charset="utf-8">
<style>
table {margin: auto;
      border: 3px solid #000066;
      width: 1000px;
      max-width: 1000px;
      border-spacing: 0;
      border-collapse: collapse;}
      
  td, th {border: 1px solid #000066;
           padding: 0.5em;       
           font-size: .75em;
          font-family: Arial, sans-serif;}
  caption {font-family: Verdana, sans-serif;
          font-weight: bold;
          font-size: .80em;
          padding-bottom: 0.5em;}
  td, th { padding: 0.7em;
          border-style: none;
          font-size: .75em;
          font-family: Arial,sans-serif; }
  tr:nth-of-type(even) {background-color: #eaeaea;}
  tr:first-of-type {background-color: #006; color: #eaeaea;}
</style>
</head>
<body>
 <table border="1" #6a8fba>  
 <caption>Completed and Deferred Projects</caption>
<?php  
// Get a connection for the database
require_once('../mysqli_connect.php');

// Create a query for the database
$query = "SELECT Projects.*
FROM Projects
WHERE (((Projects.Status)='Completed' Or (Projects.Status)='Deferred'))
ORDER BY Projects.Status DESC , Projects.Priority, Projects.EndDate DESC";
// Get a response from the database by sending the connection
// and the query
$response = @mysqli_query($dbc, $query);



// If the query executed properly proceed
if($response){
while($row = mysqli_fetch_array($response)){
echo '<table align="center"
cellspacing="5" cellpadding="8">

<tr><th align="left" width="12%">Project Name</th>
<th align="left" width="14%">Owner</th>
<th align="left" width="17%">Category</th>
<th align="left" width="12%">Priority</th>
<th align="left" width="10%">Status</th>
<th align="center" width="35%">Begin Date</th>
<th align="left" width="14%">End Date</th>
<th align="left" width="17%">Budget In Days</th>
<th align="left" width="12%">Budget</th>
<th align="left" width="12%">Notes</th>
<th align="left" width="12%">Attachments</th></tr>';
// mysqli_fetch_array will return a row of data from the query
// until no further data is available
echo '<tr><td align="center">' . 
$row['ProjectName'] . '</td>';
echo '<td align="left">' . 
$row['Owner'] . '</td><td align="left">' .
$row['Category'] . '</td><td align="left">' . 
$row['Priority'] . '</td><td align="left">' .
$row['Status'] . '</td><td align="left">' . 
$row['Begin Date'] . '</td><td align="left">' .
$row['EndDate'] . '</td><td align="left">' .
$row['BudgetinDays'] . '</td><td align="left">' . 
$row['Budget'] . '</td><td align="left">' . 
$row['Notes'] . '</td><td align="left">' . 
$row['Attachments'] . '</td></tr><br>';

$sql = "select * from (SELECT * from Tasks order by Title asc)as sub left join Projects on Projects.ID=sub.Project";

 $result023 = @mysqli_query($dbc, $sql);

// get text values of project names from the database
while($row023 = @mysqli_fetch_array($result023)){
echo " <table border='1' #6a8fba>  
 <caption>Tasks</caption>
    <tr>
    <th>Task Title</th>
    <th>Associated Document</th>
    </tr>";       
   if ($result023) {
//


// ********* I MODIFIED THIS PORTION  **************************
// ********* I remove the old subroutine for the second loop of  
// ********* tasks,and replaced it with a counter subroutine. 
// *****************


	$count = mysqli_num_rows($result023);
	if($count == "0"){
	echo' <h2>No result found!</h2>';
	}else{
	while($row = mysqli_fetch_array($result023)){
	$s = $row[2]; 
	echo '<tr><td>'. $s .'</td></tr>'; 
	}
	}
	}

echo '<br></table>';
}
}
}

?>
</table>
</body>
</html>

// *********这里是新结果的快照*************** RESULT PRODUCED SCREENSHOT

0 个答案:

没有答案