This question already has answers here:
How can I use mysqli_fetch_array() twice?
(4 answers)
Closed 1 year ago.
I have two table. One is an article list and the other one is a category list. In the category table, I have use fetch_assoc() to get the data from my DB and list in the table. But I want to fetch the article's data in the article table. How do I use fetch_assoc() twice in the same php file?
<table>
<caption class="table_title">Category Management</caption>
<thead>
<tr class="table_header">
<th>ID</th>
<th>Ttile</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php while($row = $categoryResult->fetch_assoc()) {?>
<tr class="table_content">
<td><?php echo escape($row['id'])?></td>
<td><?php echo escape($row['title'])?></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
</table>
articles table
<tbody>
<?php while($row = $result->fetch_assoc()) {?>
<tr class="table_content">
<td></td>
<td></td>
<td></td>
<td>Edit</td>
<td>Delete</td>
</tr>
<?php }?>
</tbody>
Replace
while($row = $categoryResult->fetch_assoc()) {
with
foreach($categoryResult as $row)
It does the same but the foreach approach uses an automatic iterator that will always start from the beginning of the result set.
$categoryResult = $mysqli->query();
// OR
$categoryResult = $stmt->get_result();
// from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
// again, from start to end as associative array
foreach($categoryResult as $row) {
// ...
}
If for some reason, you must use while loop and the manual approach then you need to ensure that you always reset the internal pointer to the first records before starting your loop. However, manual looping is not recommended. It is easier to make more errors when doing this manually with while
$categoryResult->data_seek(0);
while($row = $categoryResult->fetch_assoc()) {
// ...
}
Related
This question already has answers here:
How to view query error in PDO PHP
(5 answers)
How can I get useful error messages in PHP?
(41 answers)
Closed 2 years ago.
sorry to bother you guys, but been trying everything i could to get some data from database, and all the time it show's me blank, so i'm getting blank flashes as well :)
So what i need to to is,
as you see on the pic, i have a column ownerId with repeated data, this is ok, i work's like that, now what i want extract from the database is to show the itemId and count where ownerId = ?.
So have my query as bellow.
<table id="zctb" class="display table table-striped table-bordered table-hover" cellspacing="0" width="100%">
<thead>
<tr>
<th>#</th>
<th>Item Name</th>
<th>Item ID</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT ownerId, itemId, count from user_item where ownerId = :editid";
$query = $dbh2 -> prepare($sql);
$query->bindParam(':editid',$editid,PDO::PARAM_INT);
$query->execute();
$result=$query->fetch(PDO::FETCH_ASSOC);
$cnt=1;
if($query->rowCount() > 0){
foreach($results as $result){
?>
<tr>
<td><?php echo htmlentities($cnt);?></td>
<td><?php echo htmlentities($result->item_name);?></td>
<td><?php echo htmlentities($result->itemId);?></td>
<td><?php echo htmlentities($result->count);?></td>
</tr>
<?php
$cnt=$cnt+1;
}
}
?>
</tbody>
</table>
but this don't return anything not even not data found on the table.
any idea?
1) Check if you have a value in the variable $editid
2) you have an error in iterating over an array:
foreach ($results as $result) // you don't have a $results variable
I'm trying to loop in a table row 16 times with 2 elements of array. I know I can't loop 16 times with 2 elements of data but I want to display a table which has 16 row and only first 2 row will have data and others will leave blank.
Same as the image below
I have tried this code but its not what I want
<th>No</th>
<th>Id</th>
<th>Name</th>
for($i=1; $i<=16; $i++)
{
<tr>
<td> echo $i </td>
foreach($array as $a){
<td> $a->id </td>
<td> $a->name </td>
}
</tr>
}
You're looping through your entire array, 16 times. I assume that your array only has 2 elements in it. I'm going to assume it may not always have 2 elements in it, and if it has, say, 8 elements in it, you want the first 8 rows populated with data, but you always want 16 rows to display?
Additionally, I'm assuming that your $array variable is numerically indexed.
If all of that is true, then what you want is to eliminate your foreach, and just access the array element by index:
<thead>
<tr>
<th>No</th>
<th>Id</th>
<th>Name</th>
</tr>
</thead>
<tbody>
<?php for ($i=0; $i<=15; $i++) {
if (!empty($array[$i])) { ?>
<tr>
<td><?= $i+1; ?></td>
<td><?= $array[$i]->id; ?></td>
<td><?= $array[$i]->name; ?></td>
</tr>
<?php }
else { ?>
<tr>
<td></td>
<td></td>
<td></td>
</tr>
<?php } ?>
<?php } ?>
</tbody>
Note that PHP arrays are zero-based, hence why I did 0-15 instead of 1-16.
i'm new on using codeigniter, i want to ask how to make dynamic table so when i select data from anytable form database it can be fit with the table, even the field is different.
so, in normally i show table like this :
<table class="table table-striped">
<thead>
<tr>
<th scope="col">#Number</th>
<th scope="col">Field</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($data as $row) {?>
<tr>
<th scope="row"><?php echo $no++?></th>
<td><?php echo $row->COLUMN_NAME ?></td>
</tr>
<?php } ?>
</tbody>
</table>
but the problem when i using 3 field or more field it can't be fit, so any suggestion for it?
Your problem:
You are fetching data from database.
And want to display it in table but, not sure how many columns are there.
Solution:
Say, you have a multi-dimensional array with n records.
First get the first element (which is a database row, a table row)
Get count of it.
Now loop over the array.
Use foreach() language construct.
It will take care of every thing.
Note: This solution assumes that the individual array (database records) are having same number of columns.
<?php
if (! empty($arr)) {
foreach ($arr as $elem) {
?>
<tr>
<?php
if (! emtpy($elem)) {
foreach($elem as $td) {
?>
<td><?php echo $td;?></td>
<?
}
}
</tr>
<?
}
}
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 7 years ago.
Improve this question
The While Loop is Only Displaying only Last entry
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
}
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>
";
?>
</table>
Your loop isn't printing anything to the page. It's setting values to variables. And it's over-writing those variable every time. So when the loop is done, only the last values are still set.
Then you just echo that last value. Once.
Instead, echo the output inside the loop so you can have one element of output for each loop iteration:
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
Note, however, that there are a couple of other things wrong here:
Your code is vulnerable to XSS attacks.
You are displaying user passwords. Never, ever do that. Your system shouldn't even have user passwords in a readable format. User passwords should be obscured using a 1-way hash and should never be retrievable by anybody.
As you have it, you are only printing a table row once, and using the last values of the row. All your loop does is assign values to $ID, $User, and $Password, and each loop pass just overwrites the old values. To fix this, you need to move the echo statements into the body of the loop.
This will let you print the current values over each iteration, instead of only printing the last. Here's the code that will work for what you want.
<table border="5" bgcolor="white" width="300" align="center">
<tr>
<th bgcolor="grey">User ID</th>
<th bgcolor="">User Name</th>
<th bgcolor="grey">User Password</th>
</tr>
<?php
$Load = "select * from sudents";
$fetch = mysql_query($Load);
while ($rows = mysql_fetch_array($fetch))
{
$ID=$rows['id'];
$User=$rows['user'];
$Password=$rows['password'];
echo "<tr>
<td>$ID</td>
<td>$User</td>
<td>$Password</td>
</tr>";
}
?>
</table>
Welcome to Stack Overflow #Cat I see two problems.
First: no echo, var_dump() or print found.
Second: the while loop will overwrite anything as long as it iterates, so in your case i would push to an array with each result found, like this:
<?php
$q = "SELECT id, user, password FROM sudents";
$fetch = mysql_query($q);
$final = [];
while ($row = mysql_fetch_array($fetch)) {
final[] = $row;
}
?>
<table>
<tr>
<th>User ID</th>
<th>User Name</th>
<th>User Password</th>
</tr>
<?php foreach($final as $r) {?>
<tr>
<td><?= $r['id'] ?></td>
<td><?= $r['user'] ?></td>
<td><?= $r['password'] ?></td>
</tr>
<?php } ?>
</table>
Ok, so I've read loads of articles and I think I'm at risk of duplicating, but cant work this one out.
I have an array that is being returned by a PHP function, I've called it getLeague();
The structure of the array is:
body[0]->position
body[0]->teamname
body[0]->points
and obviously the results increment from 0 -16 as that's the amount of teams in my league.
I'm trying to tabulate the array by calling getLeague() and iterating over the returned array to print into a table.
I'm trying at the minute to work out the basic for each loop, after that I'll shoehorn it into a table. Can you help me with the foreach? I've tried:
<table class="table table-striped">
<thead>
<tr>
<th>Position</th>
<th>Team</th>
<th>Points</th>
</tr>
</thead>
<tbody>
<?php
$rows = getLeague();
foreach ($rows as $row):
?>
<tr>
<td><?= $row->body->position; ?></td>
<td><?= $row->body->teamname; ?></td>
<td><?= $row->body->points; ?></td>
</tr>
<? endforeach ?>
</tbody>
</table>
Any help appreciated.
Without seeing more on that data structure, I can't say for certain, but I think you want:
foreach ($rows->body as $row):
And:
$row->position