我怎样才能破坏每一行并在桌子上展示?

时间:2015-06-03 04:05:07

标签: javascript php html mysql

我想在td标记中显示每行中有多少个键和值,但在我的第二个td标记中显示第一行和第二行..然后是第三行...

如何在第一个td代码中显示第一行,在第二个td代码中显示第二行?到目前为止,这是我的代码:

 <table>
   <thead>
     <tr>
       <th>Rows</th>        
     </tr>
  </thead>
<tbody>     
<?php  
$query  = "SELECT from_monday, from_tuesday, from_wednesday,from_thursday, from_friday, to_monday, to_tuesday,to_wednesday, to_thursday, to_friday from tabletest ORDER BY survey_id DESC LIMIT 4";           

$result = mysql_query($query, $connection);

if($result === FALSE) {     die(mysql_error()); }

while($details = mysql_fetch_array($result))    
{ 
    $newArray = array_count_values($details) ; 
    foreach ($newArray as $key => $value) 
    {       
         $newValue = $value/2;
         $key      = trim(addslashes($key));        
         $newValue = preg_replace('/\D/', '', $newValue);
         $myurl[]  = "['".$key."', ".$newValue."]"; 

    }

     echo "<tr><td>"; 
     echo implode(",", $myurl);  
     echo "</td></tr>";
}

?>        
       </tbody>
 </table>

1 个答案:

答案 0 :(得分:1)

您正在将数据存储在数组中,这意味着它将在每次循环迭代时包含先前的值,您需要在每次迭代时重置数组

$myurl = array();

尝试以下代码

<?php  
$query  = "SELECT from_monday, from_tuesday, from_wednesday,from_thursday, from_friday, to_monday, to_tuesday,to_wednesday, to_thursday, to_friday from tabletest ORDER BY survey_id DESC LIMIT 4";           

$result = mysql_query($query, $connection);

if($result === FALSE) {     die(mysql_error()); }

while($details = mysql_fetch_array($result))    
{ 
    $newArray = array_count_values($details) ; 
    $myurl = array();
    foreach ($newArray as $key => $value) 
    {       
         $newValue = $value/2;
         $key      = trim(addslashes($key));        
         $newValue = preg_replace('/\D/', '', $newValue);
         $myurl[]  = "['".$key."', ".$newValue."]"; 

    }

     echo "<tr><td>"; 
     echo implode(",", $myurl);  
     echo "</td></tr>";
}

?>