2017-07-21 4 views
2

ここでは、foreachを使用してテーブルカートからデータを取得しました。今、私は以下のようなとして(表中などの見出しと値で)表示したい:CodeIgniterビューでforeachの結果にCSSを追加する方法は?

enter image description here

コントローラ

$query = $this->db->query('SELECT id,username,useremail FROM tbl_cart'); 

    $resultdata['results'] = $query->result_array(); 

$this->load->view('one/home_comman_page/head'); 
$this->load->view('one/usercart', $resultdata); 
$this->load->view('one/home_comman_page/footer'); 
$this->load->view('one/home_comman_page/script'); 
}else{ 

     redirect('users/login'); 
    } 
}  

ビュー

<?php 

foreach($results as $result) 
{ 

echo '<span>'.$result['id'].'</span>';echo'</br>'; 
echo '<span>'.$result['useremail'].'</span>';echo'</br>'; 
echo '<span>'.$result['username'].'</span>';echo'</br>'; 


} 

私は表示することができますどのようにdbテーブルのようにforeach結果データ?

+0

あなたは**インライン要素です ''タグを使用しています**。それは、彼らがページ上でお互いに隣に座ることを意味します。 '​​'タグを使用してテーブル構造にスワップすることができます: –

答えて

1

HTMLテーブルタグを使用する必要があります。

<table> 
    <?php foreach($results as $result) 
    { 
    echo '<tr style="border:1px solid grey;">'; 
    echo '<td>.$result['id'].</td>'; 
    echo '<td>.$result['useremail'].</td>'; 
    echo '<td>.$result['username'].</td></tr>'; 

    } ?> 

</table> 
+1

' tr'は 'for'ループの内側になければなりません。すべてのデータは1つの行、つまり' tr'で表示されます – Regolith

2

テーブルを使用してみてください、あなたはそれを間違った

<table> 
<thead> 
<tr> 
    <td>ID</td> 
    <td>User Email</td> 
    <td>Username</td> 
</tr> 
</thead> 
<tbody> 
<?php 
foreach ($results as $result) { ?> 
    <tr> 
     <td><?php echo $result['id']; ?></td> 
     <td><?php echo $result['useremail']; ?></td> 
     <td><?php echo $result['username']; ?></td> 
    </tr> 

    <?php 
} 
?> 
</tbody> 

1
echo "<table class='table_Class' >"; 
foreach($results as $result) 
{ 
echo "<tr>"; 
echo "<td>".$result['id']."</td>"; 
echo "<td>".$result['useremail']."</td>"; 
echo "<td>".$result['username']."</td>"; 
echo "</tr>"; 
} 
echo "</table>"; 
+0

賞は間違いなくアミラに行きます。ありがとうございました。 –

1

第一をやっている:あなたが

以下のようなHTML tableに設定されたレコードを埋め込む必要があります

第2位:ここでは基本的なHTMLテーブルタグrefer here

<table border="1px"> 
<thead> 
<tr> 
    <th>S.no</th> 
    <th>ID</th> 
    <th>User ID</th> 
    <th>UserName</th> 
    ....... 
</tr> 
</thead> 
<tbody> 
<?php 
foreach ($results as $result) { ?> 
    <tr> 
     <td><?php echo $i; ?></td> 
     <td><?php echo $result['user_id']; ?></td> 
     <td><?php echo $result['username']; ?></td> 
     ...... 
    </tr> 

    <?php 
} 
?> 
</tbody> 
</table> 
0
<table style="width:100%"> 
    <tr> 
    <th>ID</th> 
    <th>User Name</th> 
    <th>User Email</th> 
    </tr> 
<?php foreach($results as $result) {?> 
    <tr> 
    <td><?php echo $result['id']; ?></td> 
    <td><?php echo $result['username']; ?></td> 
    <td><?php echo $result['useremail']; ?></td> 
    </tr> 
<?php } ?> 

を読むまた、あなたのようないくつかのスタイルを追加することができます

<style> 
table, th, td { 
    border: 1px solid black; 
    border-collapse: collapse; 
} 
th, td { 
    padding: 5px; 
    text-align: left;  
} 
</style> 
関連する問題