Reputation: 97
i want to looping data in foreach using for in html so i don't want to type <input type>
one by one.
Edit :
Sorry I did not inform you completely,so in my database i have columns like this.
//my database
team_id
//Participants 1
name_1
phone_1
email_1
//Participants 2
name_2
phone_2
email_2
//Participants 3
name_3
phone_3
email_3
//view
//$data is from my controller
<?php foreach($data as $rowdata) {
//1
$name_1=$rowdata->name_1;
$phone_1=$rowdata->phone_1;
$email_1=$rowdata->email_;
//2
$name_2=$rowdata->name_2;
$phone_2=$rowdata->phone_2;
$email_2=$rowdata->email_2;
//3
$name_3=$rowdata->name_3;
$phone_3=$rowdata->phone_3;
$email_3=$rowdata->email_3;
}?>
<?php for($i=1;$i<=3;$i++){ ?>
<tr>
<td>Name</td>
<td><?php echo $name_$i ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?php echo $phone_$i ?></td>
</tr>
<tr>
<td>Email</td>
<td><?php echo $email_$i ?></td>
</tr>
so, how i can looping like that using for,Thanks
Upvotes: 1
Views: 994
Reputation: 1527
I don't know syntax is ok but logic is below
<table>
<?php
$i=1;
foreach($data as $rowdata) {
?>
<tr>
<td>Name</td>
<td><?= $rowdata->name.'_'.$i; ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata->phone.'_'.$i; ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata->email.'_'.$i; ?></td>
</tr>
<?php $i++; }?>
</table>
Upvotes: 1
Reputation: 1539
$data is an array of objects, so don't need to use 2 loops for getting and print data.You can do this in one loop.
<table>
<?php foreach($data as $rowdata) { ?>
<tr>
<td>Name</td>
<td><?= $rowdata->name ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata->phone ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata->email ?></td>
</tr>
<?php }?>
</table>
Upvotes: 1
Reputation: 1262
In your comments, you answered that $data
is the result from your db.
I assume that your db has 3 columns Name
, Phone
and Email
and NOT Name_1
, Name_2
, Phone_1
etc...
Code:
<?php foreach($data as $rowdata) { ?>
<tr>
<td>Name</td>
<td><?= $rowdata['name'] ?></td>
</tr>
<tr>
<td>Phone</td>
<td><?= $rowdata['phone'] ?></td>
</tr>
<tr>
<td>Email</td>
<td><?= $rowdata['email'] ?></td>
</tr>
<?php } ?>
Upvotes: 3