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
Related
I'm trying to display an html table in which I'd like to show some php variables.
My code is the following:
<?php
$title = the_title();
?>
<table>
<thead>
<tr>
<th>Name</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $title?></td>
</tr>
</tbody>
</table>
In the output I can see the word "Name" as expected but it doesn't print the $title variable (there is a empty space instead).
I've already read some of the questions on the site but they doesn't help me because they suggest to write the same code that I wrote.
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>
<?
}
}
I have this json:
[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]
I wanna show in my table into my HTML page, but I have this error:
Invalid argument supplied for foreach()
This is the part of code where I getting the error:
<table cellpadding="0" cellspacing="0">
<thead>
<tr>
<th>ID Complejo></th>
<th># Cancha></th>
<th>Cantidad Turnos></th>
</tr>
</thead>
<tbody>
<?php foreach ($cantidadturnos as $turno): ?>
<tr>
<td><?= h($turno->idComplejo) ?></td>
<td><?= h($turno->canchaFK) ?></td>
<td><?= h($turno->cantidadTurnos) ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
the var_dump of cantidadturnos is the json that i put above.
Thanks for help
The json is in the form of a JavaScript object. PHP cannot read it so it needs to parse the json with json_decode(), like this:
$cantidadturnos = json_decode('[{"nombre":"Example1","idcomplejo":"1","canchafk":"1","cantidadturnos":"35"},
{"nombre":"Example2","idcomplejo":"3","canchafk":"6","cantidadturnos":"29"},
{"nombre":"Example3","idcomplejo":"4","canchafk":"7","cantidadturnos":"6"},
{"nombre":"Example4","idcomplejo":"5","canchafk":"8","cantidadturnos":"4"},
{"nombre":"Example5","idcomplejo":"5","canchafk":"9","cantidadturnos":"2"}]');
Now $cantidadturnos is in the form of an array the foreach can work with like this:
foreach ($cantidadturnos as $turno) {
// do something
}
what would be the proper implementation of displaying an empty/blank table or just the table header if query result is empty?
**note/conditions
no page redirection
no creation of two tables, one for query with empty result and one for query with results
or is there a much, much better way to do this?
here is a sample code:
<?php if(isset($result)){ ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $key => $data){?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php }?>
</table>
<?php } ?>
the problem here is that it still throws an error on foreach loop.
When the query result returns false return an empty array instead
I hope this help , and ready for more help if needed
About the exception:
You check if $results is set, but then you try to loop over $result (without a trailing s). I believe you have a typo, which might be a part of the problem. Fixing the typo and making sure that the result is not empty (it might be set, but still empty) will probably fix the exception being thrown.
About always showing the header:
To display the header no matter what, move the if-statement to just before the the loop.
FYI - About well formatted HTML-tables:
To declare a header on a table, you usually make use of the <thead> element - to separate it from the content of the table. An example of a well-formatted HTML-table:
<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>John</td>
<td>john#email.com</td>
</tr>
</tbody>
</table>
More on formatting tables
In your case, I would put the loop around the <tr> element within the <tbody>.
<?php if(isset($result) && !empty($result)): ?>
<table>
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php foreach($result as $data): ?>
<tr>
<td><?php echo $data['name'];?></td>
<td><?php echo $data['email_add'];?></td>
</tr>
<?php endforeach ?>
</table>
<?php else: ?>
No results found.
<?php endif ?>
Note that I prefer to use the long-form of statements for PHP templates, as it greatly improves readability.