I want to populate all the data from my database to a table using <?php foreach; ?> but I can't get it to work. Please check my code:
controller
$data['query'] = $this->db->query('SELECT * FROM users');
$this->load->view('users_view',$data);
View
<?php foreach ($query->result_array() as $row)
{ ;?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?>
I would reccommend the alternative syntax for your foreach in the view...
It looks like you should be having some syntax errors the way you have it.
<?php foreach ($query->result_array() as $row): ?>
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php endforeach; ?>
EDIT - to point out your syntax issues.
<?php foreach ($query->result_array() as $row)
{ ;?> //<---- semicolon?
<tr>
<td><?php echo $row['usrID'];?></td>
<td><?php echo $row['usrName'];?></td>
<td><?php echo $row['usrPassword'];?></td>
</tr>
<?php } ?>
<?php endforeach; ?> //<--- no need for this when using regular foreach
This first semicolon would be like writing a foreach like this...
foreach ($array as $ele) { ; //<--this would throw an error just like it should above.
//do stuff
}
Opinion aside from the point of the question
You should be processing your data in the controller and simply sending an array of data to be displayed in the view, not the whole query object.
so...
Controller
$query = $this->db->query('SELECT * FROM users');
$data['users'] = $query->result_array();
$this->load->view('users_view',$data);
Related
I create a code to display from a table
$sql = "SELECT * FROM table";
$stmt = $dbcon->prepare($sql);
$stmt->execute();
$result = $stmt->fetchAll();
to display the result, I use the following ode:
<?php foreach($result as $readrow){ ?>
<div class="table-row">
<td><?php echo $readrow['id'];?></td>
<td><?php echo $readrow['area'];?></td>
<td><?php echo $readrow['color'];?></td>
<?php } ?>
Is there any way, the table can be displayed with the header?
There are many duplicated questions but all answers are beyond any reason, offering complicated solutions involving running extra queries etc.
While the solution is right here: when using fetchAll(), you already have all the column headers in the $result variable
$headerNames = $result ? array_keys($result[0]) : [];
now you can foreach over $coulmnNames to get the table header and the foreach over $result to display the results.
<table class='table'>
<tr>
<?php foreach($coulmnNames as $name): ?>
<th><?= $name ?></th>
<?php endforeach ?>
</tr>
<?php foreach($result as $row){ ?>
<tr class="table-row">
<?php foreach($result as $value){ ?>
<td><?= $value ?></td>
</tr>
<?php endforeach ?>
</table>
I have a number of rows (ingredients) in a table in my database, I'm using the following code to iterate and populate my view with the name of each, but I want to be able to display the children/parents (columns in the DB) of each item, I'm able to log out the values correctly but when I try to echo as below I'm receiving "Message: Undefined index: parents" & "Message: Undefined index: children";
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<td><?php echo $key['name']; ?> </td>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<td>
</tr>
<?php } ?>
I'm confused as I have no issue logging it out.
I assumed it may be due to null values so I assigned child/parent a string value for each row as follows;
but the error persists.
Here is the logged output in the browser;
Output of <?php ChromePhp::log($key); ?>
EDIT: Here is how I'm building the $ingredients array;
<?php
function get_ingredient()
{
$qry = "SELECT CONCAT(UPPER(LEFT(i.name, 1)), LCASE(SUBSTRING(`name`, 2))) as name,i.id as id,'ingredient' as type, i.parents as parents, i.children as children FROM `ingredient` i WHERE i.is_del=0 order by i.name asc";
$qry = $this->db->query($qry);
if ($qry->num_rows() > 0) {
$ingr = $qry->result_array();
} else {
$ingr = array();
}
}
?>
I assume I'm missing something fundamental, any advise would be most appreciated. Cheers.
My array had two sets of data, I have resolved this by limiting the array to one set.
<?php foreach ($ingredient as $row => $key) { ?>
<tr>
<td><?php echo $row + 1; ?> </td>
<?php if(isset($key['name']) && !empty($key['name'])):?>
<td><?php echo $key['name']; ?> </td>
<?php endif;?>
<?php if(isset($key['parents']) && !empty($key['parents'])):?>
<?php ChromePhp::log($key['parents']); ?>
<td><?php echo $key['parents']; ?> </td>
<?php endif;?>
<?php if(isset($key['children']) && !empty($key['children'])):?>
<?php ChromePhp::log($key['children']); ?>
<td><?php echo $key['children']; ?> </td>
<?php endif;?>
</tr>
$key looks like an object so you have to access it with -> so to get value of name use $key->name,$key->parents
I am trying to populate html table with data from mysql.But i am stuck on this part where each time i add some data it keeps repeating. On the picture below you can see that Test 1 repeat each time for every P20,P21,P24,P22,P23 and i needed to be one TEST1 for all of them. When i add Test 2 with value 19000 its making new P20 and all data come from Test 1 to Test 2. Can someone help me how to fix this.Any hint or suggestion wil be appreciated. Thank you all very much (Sorry for my bad english)
This is code that runs this
$sql = 'SELECT DISTINCT Pers.naam, Rol.funkcija,pdata.broj
FROM ids
left JOIN Pers ON ids.persid = Pers.id
left JOIN Rol ON ids.rolid = Rol.id
left JOIN pdata ON ids.pdataid = pdata.id
';
$query = $conn->prepare($sql);
$query->execute();
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php
foreach ($testing as $test):
?>
<th>
<?php
echo $test['naam'] . '<br />';
?>
</th>
<?php
endforeach;
?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($testing as $test): ?>
<tr>
<td><?php echo $test['funkcija']; ?></td>
<?php endforeach; ?>
<?php
foreach ($testing as $test):
?>
<td><?php echo $test['broj']; ?></td>
<?php
endforeach;
?>
</tr>
</table>
I would like to have it like this
There is some refactoring required to get to the output you want.
You seem to be enumerating quite a lot of data from your SQL result, so there is some grouping required to make things easier.
To get the appropriate number to each "naam" and "funkcija" you can use array_filter which however could theoretically return several numbers in each case.
You'll also have to nest a couple foreach instead of running them after one another.
If I understand your data structure correctly, this should at least give you a good starting point for the output you want:
<?php
// ...
$testing = $query->fetchAll(PDO::FETCH_ASSOC);
$naams = array_unique(array_column($testing, "naam"));
$funkcijas = array_unique(array_column($testing, "funkcija"));
?>
<table>
<tr>
<th>P Small <small>(NONE)</small></th>
<?php foreach ($naams as $naam): ?>
<th>
<?php echo $naam; ?>
</th>
<?php endforeach; ?>
</tr>
<tr>
<th>TESTING LINES</th>
</tr>
<?php foreach ($funkcijas as $funkcija): ?>
<tr>
<td><?php echo $funkcija; ?></td>
<?php foreach ($naams as $naam): ?>
<?php
$data = array_filter(
$testing,
function ($v) use ($naam, $funkcija)
{
return $v["naam"] === $naam && $v["funkcija"] === $funkcija;
}
);
foreach ($data as $value): ?>
<td><?php echo $value["broj"]; ?></td>
<?php endforeach; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</table>
this is my php code for retrieving data from database using prepare statement
if ($conn) { //database connected success
$view_sql = "SELECT * FROM certificate_verify";
$stmt_view = $conn->prepare($view_sql);
$stmt_view->execute();
$results_view = $stmt_view->fetchAll();
foreach($results_view as $row) {
$view_first_name = $row['first_name'];
$view_last_name = $row['last_name'];
}
I need to fetch all data from database to display in the following display page
<table>
<tr>
<td><?php echo $view_first_name; ?></td>
<td><?php echo $view_last_name; ?></td>
</tr>
</table>
but I am getting only last row data (not all data from database) why it is happening and what is wrong with my code?
Use foreach loop inside your table as otherwise you data ovetlap inside foreach loop and you get only last value
<?php
$results_view = $stmt_view->fetchAll();
?>
<table>
<?php foreach ($results_view as $row) { ?>// start your loop here
<tr>
<td><?php echo $row['first_name']; ?></td>
<td><?php echo $row['last_name']; ?></td>
</tr>
<?php } ?>// loop end
</table>
replace your foreach(...){...} code with this:
echo "<table>";
foreach ($results_view as $row)
{
echo "<tr>";
foreach ($row as $cell)
{
echo "<td>".$cell."</td>";
}
echo "</tr>";
}
echo "</table>";
Obviously,
Because every time you iterate your foreach loop, your $view_first_name and $view_last_name get overwritten with new values coming from $row.
You need to make $view_first_name and $view_last_name Array and iterate those over tr.
i have this code controler CI:
function cek_xml() {
$response = $_SESSION ['nim'];
// fetch data
$respons = $this->curl->simple_get($url = "http://localhost/restful/index.php/restful/buku/nim/$response/format/xml");
if (empty($response)) {
show_error('can`t access :' . $response);
}
$data['cuaca'] = $this->format->factory($respons, 'xml')->to_array();
$this->load->view('data_buku_XML', $data);
}
in view :
<?php $no=1;?>
<?php //$this->benchmark->mark('rest_start'); ?>
<?php foreach ($cuaca as $row) { ?>
<?php foreach ($row as $row) { ?>
<tr class="<?php echo ($no % 2 == 0) ?>">
<td><?php echo $no; ?></td>
<td><?php echo $row['title']; ?></td>
<td><?php echo $row['loan_date'] ?></td>
<td><?php echo $row['due_date'] ?></td>
<?php $no=$no+1;?>
</tr>
<?php } ?>
<?php } ?>
The problem is when i get the title, loan date, due date more than one variable it's okay.
But if get 1 title it's will show:
Message: Illegal string offset 'title'
But if i put // like in one foreach:
// foreach ($row as $row) {
It will show 1 variable title but error in more one variable ...
You are reusing same name for variables
Change the variable name
<?php foreach ($row as $r)
and use as
<td><?php echo $r['title']; ?></td>