codeigniter looping data in foreach using for - php

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

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 } ?>

$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>

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>

Related

Superglobal GET using PHP

I am new to php and I couldn't find the answer on stack. I am experimenting and trying to figure out how I can use the superglobal GET to display an image and also change the *count depending on how the user modifies it in the URL.
I would like to change the Beach image count times.
So the URL would look something like:
website.com/table?beach_image=2&count=4
and would display the beach image #2, 4 times:
bathing suit
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$count = 1;
$beach_image = filter_input(INPUT_GET,"beach_image");
?>
for my html:
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td>0</td>
<td>Sunglasses</td>
</tr>
<tr>
<td>1</td>
<td>Sunscreen</td>
</tr>
<tr>
<td>2</td>
<td>Bathing Suit</td>
</tr>
<tr>
<td>3</td>
<td>Hat</td>
</tr>
</table>
<div>
<img src="images/<?= $beach[$beach_image]?>">
</div>
You can do it like below
<?php
$beach = ['sunglasses.jpg','suncreen.jpg','bathing-suit.jpg','hat.jpg'];
$keysArr = array_fill(0,count($beach),0);//array_combine(array_keys($beach),array_fill(0,count($beach),0));
$beach_image = filter_input(INPUT_GET,"beach_image");
$count = filter_input(INPUT_GET,"count");
$keysArr[$beach_image] = $count;
?>
<table>
<tr>
<th>Beach Image</th>
<th>Beach</th>
</tr>
<tr>
<td><?php echo $keysArr[0] ;?></td>
<td>Sunglasses</td>
</tr>
<tr>
<td><?php echo $keysArr[1] ;?></td>
<td>Sunscreen</td>
</tr>
<tr>
<td><?php echo $keysArr[2] ;?></td>
<td>Bathing Suit</td>
</tr>
<tr>
<td><?php echo $keysArr[3] ;?></td>
<td>Hat</td>
</tr>
</table>
<div>
<?php for($i=1;$i=$count;$i++){?>
<img src="images/<?= $beach[$beach_image]?>">
<?php }?>
</div>

Codeigniter-returning value to output in table with foreach

I am using Codeigniter, and I want to return some information for an advisor. For some reason, the model isn't returning anything from the database (I'm assuming since I placed text throughout the table in view and nothing came up). The query calls information from two different tables, and I used the query directly in the database to make sure it works.
At this point, I'm sure that it is something small that I'm missing. Any insight is appreciated.
Model
function profileInfo($user_id, $user_type)
{
if($user_type == 'advisor')
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone, major, office_loc');
$this->db->from('users, advisor');
$this->db->where('users.user_id', $user_id);
$this->db->where('users.user_id = advisor.user_id');
$query = $this->db->get();
return $query->result();
}
elseif($user_type == 'advisee')
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone, major, classification');
$this->db->from('users, advisee');
$this->db->where('users.user_id', $user_id);
$this->db->where('users.user_id = advisee.user_id');
$query = $this->db->get();
return $query->result();
}
else
{
$this->db->select('user_fullname, CWID, user_name, user_email, user_phone');
$this->db->from('users');
$this->db->where('user_id', $user_id);
$query = $this->db->get();
return $query->result();
}
}
Controller
function profilePage()
{
$CWID = $this->session->userdata('id');
$userType = $this->session->userdata('user_type');
$data = array('view' => 'viewProfile', 'userid' => $CWID, 'usertype' => $userType, 'user_info' => $this->Users_model->profileInfo($CWID, $userType));
$this->load->view('admin', $data);
}
View
<?php if($usertype =='advisor'): ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<tr>
<th>Major: </th>
<td><?php echo $row->major ?></td>
</tr>
<tr>
<th>Office Location: </th>
<td><?php echo $row->office_loc ?></td>
</tr>
<?php endforeach ?>
</table>
<!--Provides view for a user that is an advisee-->
<?php elseif($usertype =='advisee'): ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname ?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<tr>
<th>Major: </th>
<td><?php echo $row->major ?></td>
</tr>
<tr>
<th>Classification: </th>
<td><?php echo $row->classification ?></td>
</tr>
<?php endforeach ?>
</table>
<!--Provides view for all other user types-->
<?php else: ?>
<table>
<?php foreach ($user_info as $row): ?>
<tr>
<th>Name: </th>
<td><?php echo $row->user_fullname ?></td>
</tr>
<tr>
<th>CWID: </th>
<td><?php echo $row->CWID ?></td>
</tr>
<tr>
<th>User Name: </th>
<td><?php echo $row->user_name ?></td>
</tr>
<tr>
<th>Email: </th>
<td><?php echo $row->user_email ?></td>
</tr>
<tr>
<th>Phone: </th>
<td><?php echo $row->user_phone ?></td>
</tr>
<?php endforeach ?>
</table>
<?php endif ?>

Working with multiple xml with same format

I am working on a xml to table project which would use PHP.
<? $xml = new SimpleXMLElement('http://example.com/genXML.php?product=388', 0, TRUE);
?>
<table>
<thead>
<tr>
<th>Symbol</th>
<th>Name</th>
<th>Price</th>
<th>Change</th>
<th>Percentage</th>
<th>High</th>
<th>Low</th>
</tr>
</thead>
<tbody>
<?php foreach ($xml->product as $check) :?>
<tr>
<td><?php echo $check->symbol; ?></td>
<td><?php echo $check->name->chinese; ?></td>
<td><?php echo $check->price; ?></td>
<td><?php echo $check->change; ?></td>
<td><?php echo $check->pct_change; ?></td>
<td><?php echo $check->high; ?></td>
<td><?php echo $check->low; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
As product will have their own unique code, I would like the programe to show all the details in one table.
Is it possible to use MYSQL Database to store the product code that i need to ref. and show them out on a single web page? Thanks!

How can I use WHILE loop to get as many sets of data into a table as are available?

When we run a query in ssms, we can get one or more rows of data per associateID.
However, on a table data,
<table>
<tr>
<td>...</td>
</tr>
</table>
We believe that the solution to our problem is to loop the data and dump contents in a table.
Any ideas how I can accomplish this?
Here is the code I am trying to loop:
<?php
//I need the loop here.
<table>
<tr>
<td class="dataItem" id="SignCode"></td>
<td class="dataItem" id="SignType"></td>
<td class="dataItem" id="SignSize"></td>
<td class="dataItem" id="SignColor"></td>
<td class="dataItem" id="Facing"></td>
<td class="dataItem" id="HorizClear"></td>
<td class="dataItem" id="VertClear"></td>
<td class="dataItem" id="Angle"></td>
<td class="dataItem" id="ReflCoat"></td>
<td class="dataItem" id="Condition"></td>
<td class="dataItem" id="Status"></td>
</tr>
</table>
?>
The data is a combination of query and the Javascript array. The query is
$tsql="select * from mytable where associateId='$aid'";
Then all the form ids are in Javascript like this example:
dojo.byId("SignType").innerHTML = obj["SignType"];
If you show us your php code we can give you a better answer but your code should be somthing like this:
<table>
<?php
// Your loop
while($row = /*fetchData()*/) {
?>
<tr>
<td class="dataItem" id="SignCode"><?php echo $row['SignCode']; ?></td>
<td class="dataItem" id="SignType"><?php echo $row['SignType']; ?></td>
<td class="dataItem" id="SignSize"><?php echo $row['SignSize']; ?></td>
<td class="dataItem" id="SignColor"><?php echo $row['SignColor']; ?></td>
<td class="dataItem" id="Facing"><?php echo $row['Facing']; ?></td>
<td class="dataItem" id="HorizClear"><?php echo $row['HorizClear']; ?></td>
<td class="dataItem" id="VertClear"><?php echo $row['VertClear']; ?></td>
<td class="dataItem" id="Angle"><?php echo $row['Angle']; ?></td>
<td class="dataItem" id="ReflCoat"><?php echo $row['ReflCoat']; ?></td>
<td class="dataItem" id="Condition"><?php echo $row['Condition']; ?></td>
<td class="dataItem" id="Status"><?php echo $row['Status']; ?></td>
</tr>
<?php } ?>
</table>
The code should look something like this:
(It is still unclear how you acquire the data from the DB.)
<?php
$resultSet = ... // <-- somehow aquire a result-set you can loop through
?>
<table>
<?php while ($row = <somehow_get_next_row_from_$resultSet_as_associative_array>) {?>
<tr>
<td class="dataItem" id="SignCode"><?php echo($row["signCode"]); ?></td>
<td class="dataItem" id="SignSize"><?php echo($row["signSize"]); ?></td>
<td class="dataItem" id="SignColor"><?php echo($row["signColor"]); ?></td>
...
<td class="dataItem" id="Status"><?php echo($row["status"]); ?></td>
</tr>
<?php } ?>
</table>
In case the value for id (e.g. "SignCode" etc) is the same as the name of the DB column-name AND the query returns the columns in the order you want the HTML-table columns to be, then the part between <tr> and </tr> could be further simplified as:
...
<tr>
<?php forEach ($row as $key => $value) {?>
<td class="dataItem" id="<?php echo($key); ?>"><?php echo($value); ?></td>
<?php } ?>
</tr>
...

How to add a class to last table row

I want to add a class to every last <tr> of a main category. I don't want to add the classname to every <tr>. What can I change in my script?
Like this:
<table>
<tr>
<th></th>
</tr>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
<th></th>
<tr>
<td></td>
</tr>
<tr class="test">
<td></td>
</tr>
</table>
<table class="data forum">
<? foreach ($this->mainCategories as $mainCategory): ?>
<tr>
<th><strong><?= $this->escape($mainCategory->fcName) ?></strong></th>
<th> </th>
<th><strong>Topics</strong></th>
<th><strong>Laatste topic</strong></th>
</tr>
<? foreach ($mainCategory->getSubCategories() as $category): ?>
<tr>
<td><?= $this->escape($category->getName()) ?></td>
<td><?= $this->escape($category->fcDescription) ?></td>
<? if ($this->escape($category->numTopics) > 0): ?>
<td><?= $this->escape($category->numTopics) ?></td>
<td><?= date_create($this->escape($category->last_topic))->format('d-m-Y H:i') ?></td>
<? else: ?>
<td> </td>
<td> </td>
<? endif ?>
</tr>
<? endforeach ?>
<tr>
<td class="split"></td>
</tr>
<? endforeach ?>
</table>
Grab the count of your $this->mainCategories variable and test for the last one against a counter variable...
$count = count($this->mainCategories;
$current = 0;
<?
foreach ($this->mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
Update:
Let's look at a simplified version as a proof of concept:
<?php
$mainCategories = array("Eins", "Zwei", "Drei");
$count = count($mainCategories);
$current = 0;
foreach ($mainCategories as $mainCategory):
$current++;
?>
<tr<?= $current == $count?' class="someClass"':''?>>
<td><?= $mainCategory ?></td>
</tr>
<?php endforeach ?>
This produces the following HTML:
<tr>
<td>Eins</td>
</tr>
<tr>
<td>Zwei</td>
</tr>
<tr class="someClass">
<td>Drei</td>
</tr>

Categories