Row within a row with JSON - php

I have a webpage where it shows the lists of Projects and the monitoring of its progress/finances for every quarter. As shown below:
As you can see, my table is comprises of Project Name and a lists of sub-title's underneath it. And a series of columns per each quarter. Thru PHP I was able to populate the list of sub-titles under the Project Name, which also being fetched from the server side. Here's the code:
$sql = mysqli_query($con," My SELECT Statement ");
$i=0;
while($row = mysqli_fetch_assoc($sql)){
$ptitle = $row['Title'];
$iname = $row['Item'];
if($i%1)
{
?>
<?php } else { ?>
<tr>
<?php } ?>
<td width="25%"><?php echo $ptitle; ?></td>
<td></td>
</tr>
<tr>
<td><?php echo "<ul style='list-style-type: none;'><li>".nl2br($iname)."</li></ul>"; ?></td>
<td contenteditable="true" name="v1"></td>
Note: ptitle = ProjectName and iname = Semi-title underneath the Project's name.
Now, as you can see, the Project Name column literally "conquer" a single row on the left. Yet, the rows under the column of each quarter, should have its own separately, and must be parallel to the every sub-title underneath the Project Name. (Please refer to the image above for this) the only problem am encountering is... how can I make an editable row from inside a row, without affecting mysqli result? coz basically my table right now is kinda look like this:
Anyone who's more experience on this? I need your help.
PS: ...and oh! You might be wondering why do I include JSON in the title? It is because, I originally use JSON for editing those table rows before I even use mysqli_fetch_array. But when I include the results of the array inside the <table> tag, everything's changed and JSON is no longer working. So as of now, I am force to do it manually, meaning typing each <td contenteditable=true> in all of those rows. Yet, its not the desired output since I need another row within an existing row. Ideas? Anyone?
Figure Two:
Figure Three:

Count the number of lines in $iname, and then use a loop to create that many rows of contenteditable cells. You can also use this in the rowspan attribute of the <td> containing the title and subtitles.
$rows = substr_count($iname, "\n") + 1;
for ($i = 0; $i < $rows; $i++) {
echo "<tr>";
if ($i == 0) { ?>
<td rowspan='<?php echo $rows;?>' width='25%'><?php echo $ptitle . "<br>" . nl2br($iname);?></td>
<?php }
?>
<td contenteditable="true" name="v1"></td><td contenteditable="true" name="v2"></td>...
</tr>
<?php }
DEMO

Related

Echo multiple values from a single database entry after LEFT JOIN to pull names from a different table

I hope the title makes sense and sorry if this is a duplicate I wasn't certain what to search for to find the correct answer.
I have a table on a website that pulls info from a database. So my issue is I have two tables in the database, the_traveler and the_discipline. What I am doing is pulling the value from the traveler that looks like this 89,43 and after I LEFT JOIN with the_disciplines table, to get the name that correlates with the numbers, like so:
$query ="SELECT t.name,t.traveler_id, t.disciplines, dis.name as discname
FROM ".TABLEPRIFIX."traveler t
LEFT JOIN ".TABLEPRIFIX."discipline dis ON t.disciplines = dis.id";
$disname = $obj->queryResult($query);
I am only able to echo out the first numbers name by doing this:
<?php
foreach($disname as $traveler)
{
<tr>
<td><?php echo $traveler['discname']; ?></td>
</tr>
<?php
}
?>
My question is how would I change my code to get all the numbers from the database to echo in each row. I have tried to implode the numbers.
If you want to get a single row with all values you must change the code to:
<tr>
<?php
foreach($disname as $traveler)
{
<td><?php echo $traveler['discname']; ?></td>
<?php
}
?>
</tr>

populate html table in PHP

I am at the end of my project, I have all the data from my database and everything is working fine I just can not work out how to display it in a table.
I have generated a table which is the (number of projects) * (the number of people).
The data I have collected is user_id, project_id and hours.
But how do I insert '6' (hours) into user_x's row in the column of the correct project?
I can only think to make x arrays (for the number of projects) of the length for the number of users and evaluate the project code to select the correct array and then use the user id to get the correct position to place the value and simply spit out the array into the td tag
This is incredibly messy, I wonder if I'm going about it completely wrong.
If this is indeed the best way I need to recursively create arrays and write code which references variables that might not even exist. Sounds insane to me
//for the length of projects create arrays that are the length of users and fill with 0's
for ($v = 0; $v < $rows_x; $v++){
$name = "variable{$v}";
$$name = array_fill(0, $rows_u, '0');
EDIT: What I am trying to do is show the number of hours that are booked to projects between two dates. I have gotten all the data correctly but now I need the data to land into a table so you can easily see which user booked to what project.
In an excel world I could use the project number to select the Y axis and the user id to select the X axis. However I don't know the best way to do this in php.
Surely creating an array for each project the length of the users and filling with data if there is data is not the best way.
I don't know if this is what you're looking for, but I insert my values into a table in the process of retrieving them.
You can echo the values into a <td> as long as they are in your SQL SELECT statement.
<table>
<thead>
<tr>
<th>Values1</th>
<th>Values2</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT value1, value2
FROM tbl_Values";
if (!$res = $link->query($sql)) {
trigger_error('Error in query ' . $link->error);
} else {
while ($row = $res->fetch_assoc()) {
?>
<tr>
<td>
<?php echo $row['value1']; ?>
</td>
<td>
<?php echo $row['value2']; ?>
</td>
</tr>
<?php
}
}
?>
</tbody>
</table>
This is the way I put data into my tables, without using arrays.

Linking two PHP objects together

I have these two objects:
$userinfo->pilotid;
$departures->total;
I'm trying to get $departures->total for specific pilotid in $userinfo = $userinfo->pilotid.
However, I'm not sure how can I link them so it echoes A for B. I have something like this but it does not display anything.
<?php echo $pilotid->$departures->total; ?>
Additionally, the first object is called like this:
$pilotid = Auth::$userinfo->pilotid;
This is the structure of the table where the objects are gathered from, using a query.
Stemming from the data provided by the OP, I am assuming, that $departures has a 1:n relationship with $userinfo, $userinfo being the 1 containing the pilotid.
So, in oder to find out how many departures that pilot had in total, there's two possible ways, one by using a subquery, which would mean something like this:
SELECT (SELECT COUNT(*) FROM `departures` WHERE `pilot_id` = ID) as total, * FROM pilots;
In this case, your total would be in the total column of your $userinfo query.
The second attempt makes use of actual PHP. In this scenario, you do the counting yourself.
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$row->departures = array();
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$userinfo[$row->pilotid]->departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
If this isn't what you're looking for, I will be needing more information from you, however like this you will be able to get the departures of the pilots either by making use of the total variable in the $userinfo object, or by simply calling count on the departures array.
Another variant, which keeps the actual departures and the pilots apart would look like this:
First step: Getting the pilot information:
$userinfo = array();
while($row = fetch()) {
$row->total = 0;
$userinfo[$row->pilotid] = $row;
}
These lines will give you the pilot data keyed to their IDs in an array.
Step two. Glueing the departures to the pilots.
$departures = array();
while($row = fetch()) {
if(isset($userinfo[$row->pilotid])) {
$departures[] = $row;
++$userinfo[$row->pilotid]->total;
}
}
I hope you will find these suggestions useful.
Edit:
After a few additional information from the OP, I suggest changing the query used to access the information in question.
This is the original code by the OP
$dep_query = "SELECT COUNT(pilotid) as total, depicao, pilotid FROM phpvms_pireps GROUP
BY depicao, pilotid ORDER BY total DESC LIMIT 5";
$fav_deps = DB::get_results($dep_query);
foreach($fav_deps as $departure)
{
$dep_airport = OperationsData::getAirportinfo($departure->depicao);
$pilotid = Auth::$userinfo->pilotid;
?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $pilotid->{$departures->total}; ?></td>
</tr>
<?php
}
?>
First thing we'll change is the query used to get the departures. Why fetch all the information, if we actually only want the one of the pilot in question?
$pilotid = $userinfo->pilotid; //As per Chat discussion
$dep_query = "SELECT COUNT(depicao) as total, depicao FROM phpvms_pireps WHERE pilotid = $pilotid GROUP BY depicao ORDER BY total DESC LIMIT 5";
This query will return the Top 5 of the departures from the different airports, which have been run by the pilot in question. As for the rest:
$fav_deps = DB::get_results($dep_query);
if(is_array($fav_deps)) { //For the general use
foreach($fav_deps as $departure) {
$dep_airport = OperationsData::getAirportinfo($departure->depicao); ?>
<tr class="awards_table1">
<td width="10%"><?php echo $departure->depicao; ?></td>
<td width="10%"><img src="<?php echo Countries::getCountryImage($dep_airport->country); ?>" /></td>
<td width="60%"><?php echo $dep_airport->name; ?></td>
<td width="20%"><?php echo $departure->total; ?></td> //Here is the actually changed Layout code
</tr>
<?php
}
} else echo "This pilot didn't have any departures yet.";
?>
With these alterations, your code should output the desired result. It is completely untested though. However it should give you the right idea.
I think what you need is this (note the curly brackets):
<?php echo $pilotid->{$departures->total}; ?>
Unless I'm misunderstanding the question...

Generate html table with variable rows and cells

I am trying to generate a table that takes data from a mysql database. There are 8 columns but the number of rows is variable depending on the amount of info from the database. The problem I'm running into is that I have two things that are variable and I don't know how to use two while loops (or if that's the right choice).
The code below can generate a table with 8 columns and a variable amount of rows but I don't know how to replace thing with sequential integers. I would like each cell to have just one integer in it sequentially like a while loop until the numbers = $end.
CODE:
<?php
$end=82; //will get all this data from a dabase
$rows=ceil($end/8);
$x=0;
$start=0;
?>
<table>
<?php
While ($x<=$rows){
echo"
<tr>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
<td>
thing
</td>
</tr>
";
$x++;
}
?>
</table>
This will output eight numbers per row starting at 1 and going to $end. I am unsure of what $maxprobs is here.
<?php
$end=82; //will get all this data from a dabase
$rows=ceil($end/8);
$x=0;
$start=0;
?>
<table>
<?php
while($x<=$rows) {
echo "<tr>";
for ($y = 0; $y < 8; $y++, $start++) {
if ($start <= $end) echo "<td>$start</td>";
}
echo "</tr>";
$x++;
}
?>
</table>
I have to ask - if you're pulling rows from a MySQL database, and you want your table to have a dynamic number of rows depending on the data, is there any specific reason you're not doing it the regular way, with a while loop and a mysql_fetch function?
$data = mysql_query("SELECT row1, row2, ... , row8 FROM table WHERE ...); // Sample query, edited for brevity.
echo "<table>";
while ($result = mysql_fetch_object($data))
(
echo "<tr>";
echo "<td>".$data->row1."</td>;
echo "<td>".$data->row2."</td>;
... // Edited for brevity. Include as many columns as you queried.
echo "<td>".$data->row8."</td>;
echo "</tr>";
}
echo "<table>";
Unless you have a really specific reason not to do it this way, I'd use this method. It's flexible and it will print as many rows as your query returns (meaning that you only need to change your query, not your code).
Additional information on mysql_fetch_object and mysql_query can be found here and here, respectively.
Take a look at the php website, nearly all their entries have examples, the database stuff shows a while loop, this is the best way to do it..
write the table opening tag, plus any headers..
while able to get more database data, get it, and do
display the table row open tag
display the row, by looping through the fields, and outputting them between cell tags
dispaly the end of row tag
now you've finished with your data, write the end of the table
There are a lot of examples about.
for the looping through the field, thats why they invented a "foreach" , eg wether you have 0, or 10000, you can go through each one, even if its not consecutive, so for each "fruit" in apple, pear, bannana..

HTML PHP put a cell under a specific header according to query

I have various tasks under various departments. The department name and id come as table header (<th>). I have displayed them.
Now I query the tasks and if the task belongs Dept A I want it under <th id='A'> and if task belogs to Dept B it should be under <th id='B'>.
My problem is that some depts may not have task for this particular row and so that cell should be empty. The next cell may have value. So how to tell html to put an empty cell when necessary ie, I want to do something like this:
if(th id == 'DeptA' and resultSet.deptId = 'DeptA')
then <td> resultSet(i); </td>
else <td> </td>
I am a bit new to HTML and PHP. All help is greatly appreciated.
How about this:
<?php
echo '<table>';
foreach(...){
echo '<tr><td>';
if(th id == 'DeptA' && resultSet.deptId == 'DeptA'){
echo resultSet(i);
}
echo '</td><td>';
if(th id == 'DeptB' && resultSet.deptId == 'DeptB'){
echo resultSet(i);
}
echo '</td></tr>';
}
echo '</table>';
?>
<table> - starts the table
<tr> - is a table row
<td> - is a table column

Categories