2017-04-06 80 views
-1

代碼!如何垂直顯示PDO行結果?

<?php 

    $stmt = $DB_con->prepare('SELECT userID, FullName, BirthDate, DeathDate FROM name'); 
    $stmt->execute(); 

    if($stmt->rowCount() > 0) 
    { 
     while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
     { 
      extract($row);  

echo "<td align='center'><div class='listxtNB' style='width:30px;'>" .$row['userID']. "</div></td>"; 
echo "<td align='center'><div class='listxtNB' style='width:135px;'>" .$row['FullName']. "</div></td>"; 
echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['BirthDate']. "</div></td>"; 
echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['DeathDate']. "</div></td>"; 
echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='editform.php?edit_id=".$row['userID']."'>EDIT</a></div></td>"; 
echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='?delete_id=".$row['userID']."' onclick='return confirm('sure to delete ?')>DELETE</a></div></td>"; 
echo "</tr></table>"; 
?> 

looks like now

Needs to look like this

+0

哪裏 HTML行的開始呢? – masm64

+2

你需要開始一個新行,而不是結束表 –

+0

看到我的回答,你正在循環遍歷行時在while循環期間構建錯誤表 –

回答

0

你有你的代碼稍微調整爲以下:

<?php 
$stmt = $DB_con->prepare('SELECT userID, FullName, BirthDate, DeathDate FROM name'); 
$stmt->execute(); 
if($stmt->rowCount() > 0) { 
    echo "<table>"; 
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) { 
     extract($row); 
     echo "<tr>"; 
     echo "<td align='center'><div class='listxtNB' style='width:30px;'>" .$row['userID']. "</div></td>"; 
     echo "<td align='center'><div class='listxtNB' style='width:135px;'>" .$row['FullName']. "</div></td>"; 
     echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['BirthDate']. "</div></td>"; 
     echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['DeathDate']. "</div></td>"; 
     echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='editform.php?edit_id=".$row['userID']."'>EDIT</a></div></td>"; 
     echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='?delete_id=".$row['userID']."' onclick='return confirm('sure to delete ?')>DELETE</a></div></td>"; 
     echo "</tr>"; 
     } 
    echo "</table>"; 
    } 
?> 
+1

並刪除不必要的和非常討厭的'提取($ row)' – RiggsFolly

+0

@RiggsFolly,那和這個問題沒有關係。沒有想法,他可能需要它在進一步的代碼? –

0

這應該工作我猜:

<?php 

     $stmt = $DB_con->prepare('SELECT userID, FullName, BirthDate, DeathDate FROM name'); 
     $stmt->execute(); 

     if($stmt->rowCount() > 0) 
     { 
      while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
      { 
       extract($row); 

       echo "<tr>"; 
       echo "<td align='center'><div class='listxtNB' style='width:30px;'>" .$row['userID']. "</div></td>"; 
       echo "<td align='center'><div class='listxtNB' style='width:135px;'>" .$row['FullName']. "</div></td>"; 
       echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['BirthDate']. "</div></td>"; 
       echo "<td align='center'><div class='listxtNB' style='width:95px;'>" .$row['DeathDate']. "</div></td>"; 
       echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='editform.php?edit_id=".$row['userID']."'>EDIT</a></div></td>"; 
       echo "<td align='center'><div class='listxtNB' style='width:45px;'><a href='?delete_id=".$row['userID']."' onclick='return confirm('sure to delete ?')>DELETE</a></div></td>"; 
       echo "</tr>"; 
      } 
    ?> 
+0

不要忘記做一個表開始和結束標籤rowCount> 0 if語句爲有效的html,看到我的答案(瀏覽器解析器將糾正這個我猜,但仍然...) –

+1

謝謝所有得到3所有3 ,不忘記桌子的開始標籤,只是忘了張貼在這裏...但是再次感謝.. – abmoid

+1

同樣,在這裏宣佈桌子標籤並不是必要的。你可以在適當的地方做到這一點。如果它是適當的,那麼肯定,追加並在前面加上必要的

標籤。 – masm64

0

table標籤它的封閉的循環,你不需要很多回聲

<?php 

$stmt = $DB_con->prepare('SELECT userID, FullName, BirthDate, DeathDate FROM name'); 
$stmt->execute(); 

if($stmt->rowCount() > 0) 
{ 
    echo "<table>"; 
    while($row=$stmt->fetch(PDO::FETCH_ASSOC)) 
    { 
     extract($row);  
    echo "<tr><td align='center'><div class='listxtNB' style='width:30px;'>" .$row['userID']. "</div></td> 
    <td align='center'><div class='listxtNB' style='width:135px;'>" .$row['FullName']. "</div></td> 
    <td align='center'><div class='listxtNB' style='width:95px;'>" .$row['BirthDate']. "</div></td> 
    <td align='center'><div class='listxtNB' style='width:95px;'>" .$row['DeathDate']. "</div></td> 
    <td align='center'><div class='listxtNB' style='width:45px;'><a href='editform.php?edit_id=".$row['userID']."'>EDIT</a></div></td> 
    <td align='center'><div class='listxtNB' style='width:45px;'><a href='?delete_id=".$row['userID']."' onclick='return confirm('sure to delete ?')>DELETE</a></div></td> 
    </tr>"; 
    } 
    echo "</table>"; 
    } 
?> 
+1

這將導致無效的HTML。看我的標籤。你忘了同時打開表格標籤和tr標籤(瀏覽器解析器會糾正這個我猜,但仍然...) –

+0

@TVAvanHesteren是的,謝謝你的回覆(y) – ThaSayou

+0

不一定。有機會在其他地方創建

。 – masm64