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>
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
im having trouble getting a result value of a query and the error says.
Notice: Undefined index: prelim in C:\xampp\htdocs\gradingxworking\teacher\student.php on line 85 can someone help me to fix this or some clue to fix this? im just starting to learn php.
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<?php $grade = $student->getstudentgrade($row['studid']);?>
//code that cause error line 85--> <td class="text-center"><?php echo $grade['prelim']; ?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr><td colspan="8" class="text-center text-danger"><strong>*** No Result ***</strong></td></tr>
<?php endif; ?>
</tbody>
the function:
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid";
$r = mysql_query($q);
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
return $data;
}
As #Bara suggested, you need to check the array first before accessing it.
if(isset($grade['prelim']))
I suspect, there is no data for specific studid. Lets see you function.
$data = array();
while($row = mysql_fetch_array($r)){
$data[] = array(
'prelim' => $row['prelim']
);
}
Now, you have created new array $data. But, if there is no record ? your while loop won't be executed and your $data array won't have anything. right ? So, to handle that, you need to check whether there is any data in your array.
Now, second point, which #Mossavari made is also correct. You need to use
$grade[0]['prelim'];
instead of
$grade['prelim'];
after doing
<?php $grade = $student->getstudentgrade($row['studid']);?>
you need to check what $grade holdes. and its better before trying to fetch data from array to make a check like this :
if(isset($grade['prelim']))
Based on your getstudentgrade function you'll have multidimensional array result, you may need to change $grade['prelim'] to $grade[0]['prelim']
Since, every student will have only 1 grade. So, why to use array.
<?php
function getstudentgrade($studid){
$q = "select * from studentsubject where studid=$studid LIMIT 0,1";
$data = "";
$r = mysql_query($q);
while($row = mysql_fetch_array($r)){
$data = $row['prelim'];
}
return $data;
}?>
PHP
<tbody>
<?php $c=1; ?>
<?php foreach($mystudent as $row): ?>
<tr>
<td><?php echo $c; ?></td>
<td class="text-center"><?php echo $row['studid']; ?></td>
<td class="text-center"><?php echo $row['lname'].', '.$row['fname']; ?></td>
<td class="text-center"><?php echo $grade = $student->getstudentgrade($row['studid']);?></td>
</tr>
<?php $c++; ?>
<?php endforeach; ?>
<?php if(!$mystudent): ?>
<tr>
<td colspan="8" class="text-center text-danger">
<strong>*** No Result ***</strong>
</td>
</tr>
<?php endif; ?>
</tbody>
I got a foreach which loops through my database for each user.
<?php
foreach($users as $user) {
?>
<tr>
<td>
<?php
for ($i=1; $i < 52; $i++) {
echo $i;
}
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
This loop through database and echos the username, now I tried to build a for loop inside this so that every user has a number, I took as an example a very basic for loop ( it will be changed later).
The problem is that I get all numbers printed out for each user, but I just want to print out the number once for a user. How do I solve this.
If you want unique index number for user, you do not need extra loop.
Just add an increment variable:
And increment it in existing loop.
<?php
$i=0;
foreach($users as $user) {
++$i; // For printing first user, it will be 1 not 0.
// Therefore, increment is placed before printing name.
?>
<tr>
<td><?php echo $i;?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php
}
?>
This should be enough to achieve what you're trying to do :
Your for() isn't needed since foreach() already create a loop, you just have to use this loop to increment a value (here called $i) then display it.
Also you should avoid to open your php tags ten thousands times for a better visibility into your code :)
<?php
$i = 0;
foreach($users as $user) {
$i++;
echo'<tr>
<td>'.$i.'</td>
<td>'.$user['firstname'].'</td>
</tr>';
?>
<?php
$i=1;
foreach($users as $user) {
?>
<tr>
<td>
<?php
echo $i++;
?>
</td>
<td>
<?php echo $user['firstname']; ?>
</td>
</tr>
<?php
}
?>
Try to use the key in foreach
<?php foreach ($users as $key => $user) : ?>
<tr>
<td><?php echo $key; ?></td>
<td><?php echo $user['firstname']; ?></td>
</tr>
<?php endforeach; ?>
I am having a problem with api Yandex.
I have to get data from api and make with php the table:
Source Users New Pages Bounce Goal1 Goal2
organic s1v1 s1v2 s1v3 s1v4 s1v5 s1v6
referral s2v1 s2v2 s2v3 s2v4 s2v5 s2v6
(none) s3v1 s3v2 s3v3 s3v4 s3v5 s3v6
After json_decode I have:
{"data":[
{"dimensions":[{"name":"organic"}],"metrics":[s1v1,s1v2,s1v3,s1v4,s1v5,s1v6]},
{"dimensions":[{"name":"referral"}],"metrics":[s2v1,s2v2,s2v3,s2v4,s2v5,s2v6]},
{"dimensions":[{"name":"(none)"}],"metrics":[s3v1,s3v2,s3v3,s3v4,s3v5,s3v6]},
]}
But I can't correctly parse it into a table. So far I have written code only for the first column Source, and then stuck:
<?php
$metrika_o = json_decode($metrika);
echo "<table>
<tr>
<td><strong>Source</strong></td>
<td><strong>Users</strong></td>
<td><strong>New</strong></td>
<td><strong>Pages</strong></td>
<td><strong>Bounce</strong></td>
<td><strong>Goal1</strong></td>
<td><strong>Goal2</strong></td>
</tr>";
foreach($metrika_o->data as $data)
foreach($data->dimensions as $source)
:
?>
<tr>
<td><?php echo $source->name?></td>
</tr>
<?php endforeach;
echo "</table>";
?>
The Source number is constantly changing, the set of columns is fixed.
Please help me to solve this task
You are on the right way:
<?php foreach($metrika_o->data as $data): ?>
<tr>
<td><?php echo $data->dimensions[0]->name; ?></td>
<?php foreach(explode(',',$data->metrics) as $col):
<td><?php echo $col; ?></td>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>