Split mysql retrieved data into headers and rows - php

I retrieve data from DB and I need to insert all of them in a table.
I have some constant headers and some that depends on the query results.
Below you can see a table; how i want it to look like:
I did prepare the HTML structure:
<table class="table-fill">
<thead>
<tr>
<th>ID</th>
<th>CODE</th>
<th colspan=2>TREND A</th>
<th colspan=2>TREND B</th>
<th colspan=2>TREND C</th>
</tr>
<tr>
<th></th>
<th></th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
<th>Items</th>
<th>Revenue</th>
</tr>
</thead>
<tbody class="table-hover">
<tr>
<td>1</td>
<td>A</td>
<td>10</td>
<tD>150</td>
<td>5</td>
<tD>200</td>
<td>8</td>
<tD>120</td>
</tr>
</tbody>
No problem so far. The hard part is to use actual data from DB and insert them to the table.
Headers "trend a", "trend b" etc are not constants. The below tables will be displayed on different countries. Some countries have specific trend names. Let's say England has "Trend A", "Trend B" and "Trend D". Italy has only "Trend A" etc.
Is it possible to write php code and in some way auto generate the table headers based on the query results?
And then split the results by trend so that correct values will go to correct trend?
I know how to do a simple table like:
$sql_r = "SELECT o.id, o.code, tl.name, ts.items, ts.revenue
FROM overview o, trend_stats ts, trend_lang tl
WHERE o.id_shop = '1'
AND o.id = ts.id
AND ts.id_trend = tl.id_trend";
$data_to_output = Db::getInstance()->ExecuteS($sql_r);
foreach ($data_to_output as $data) {
echo '
<tr>
<td>'.$data['id'].'</td>
<td>'.$data['code'].'</td>
<td>'.$data['trend'].'</td>
<td>'.$data['items'].'</td>
<td>'.$data['revenue'].'</td>
</tr>
';
}
But I'm not sure how to split and structure them the way I want them to be. If there's any tutorial or anything online related to what I'm asking I'm happy to check it out. Thank you

I don't think you can do that other than rewrite your headers. The fact that you want to retrieve your informations from your database tells that you'll have to work with any kind of loop. So you first have to display your headers and then display your informations. The only way is to store somewhere in your loop the value that will change your headers when it changes, and add another line of header when the value actually changes. You can do that with just a simple if condition.

Related

Modify WooCommerce orders page

I need to modify my WooCommerce orders page but I don't have the experience for this. At the moment (like in normal case) my orders page looks like this:
Yesterday I've added a second id attribute to each order where I can set a custom customer id (user_id in Wordpress). This field is named "second_customer".
I'm doing this because I want to show the order taken by customer one to an other customer too in case the "second_customer" has his user_id as value.
Now I need to modify the orders.php and modify it so that the orders gets loaded to for the "second_customer" on his account and the normal way too. So my question is: How can I get exacly the row like in the picture (the order) for the user id defined in my custom attribute?
As your question:
How can I get exacly the row like in the picture
use the table HTML elements like will result like the screen shoot above with 5 headings rows with table headings- and table rows- :
<table style="width:100%">
<tr>
<th>Product Id</th>
<th>Date</th>
<th>Status</th>
<th>Address</th>
<th>Name</th>
</tr>
<tr>
<td>#356</td>
<td>30 September</td>
<td>Customer</td>
<td>10 Address st, China</td>
<td>John Doe</td>
</tr>
<tr>
<td>#357</td>
<td>30 September</td>
<td>Customer</td>
<td>32 Address st, Porland</td>
<td>Sarah smith</td>
</tr>
<tr>
<td>#358</td>
<td>29 September</td>
<td>Customer</td>
<td>55 Address st, New York</td>
<td>Ben Barber</td>
</tr>
</table>

Can I create database tables using controller in laravel?

How can i create database tables using controllers in laravel. Is there any way?
Actually I want to create a table using another table column values.
Eg:
I have two tables called table1 and table2.
<table>
<thead>
<td>id</td>
<td>name</td>
</thead>
<tr>
<td>1</td>
<td>raj</td>
</tr>
<tr>
<td>2</td>
<td>john</td>
</tr>
<tr>
<td>3</td>
<td>sam</td>
</tr>
</table>
and I want the names as field names in table2
<table>
<thead>
<td>raj</td>
<td>john</td>
<td>sam</td>
</thead>
</table>
And I want to create table2 using controller
Why do you want to do that!? I see no reasons!
Anyway you can use raw query in controller :
DB::raw(' CREATE TABLE . .. ');

Add td to specific column in table using php

I'm still new to php and im trying to make a scheduler script with php and trying to add a tag to a specific column
My code:
<table>
<th>Column 1</th>
<th>Column 2</th>
<th>Column 3</th>
<tr>
<td>3</td> /* I'm trying to get this td in column 3 */
<td>1</td> /* I'm trying to get this td in column 1 */
<td>2</td> /* I'm trying to get this td in column 2 */
</tr>
</table
Any help would be appreciated
EDIT
To make things clear i simplified my code.
I could not understand your Question clearly that you want to generate dynamic code to add td by php if yes, below code may useful for you
<?php foreach($td_array as $key=>$val){
echo "<tr>";
echo "<td>".$key."</td><td>".$val."</td>";
echo "</tr>";

xPath, getting tabular data

I have a HTML thus like:
<table>
<tr>
<th>Name</th>
<th>Email</th>
<th>Age</th>
</tr>
<tr>
<td>Joe Bloggs</td>
<td>joe#bloggs.com</td>
<td>40</td>
</tr>
<tr>
<td>John Doe</td>
<td>john#doe.com</td>
<td>40</td>
</tr>
</table>
Is there a way using xPath to get the first 2 columns, i.e the Name and Email fields?
I can get the table data using $data = $xpath->query( '//table'); just unsure how to get only the first 2 columns.
Many thanks
Get the first two td's:
//table/tr/td[position() <= 2]

PHP table creation with arrays

I have a table with a first column called: "Fruit", and a last column called: "Total"
And any columns in between are dynamically created by the number of students.
Below is what I'm looking to dynamically make, from what the database shows so far.
<table border="1">
<tr>
<th>Fruit</th>
<th>Sally</th>
<th>John</th>
<th>Total</th>
</tr>
<tr>
<td>apples</td>
<td>5</td>
<td>3</td>
<td>8</td>
</tr>
<tr>
<td>bananas</td>
<td>3</td>
<td>5</td>
<td>8</td>
</tr>
<tr>
<td>Oranges</td>
<td>3</td>
<td>3</td>
<td>6</td>
</tr>
<tr>
<td></td>
<td>11</td>
<td>11</td>
<td>22</td>
</tr>
</table>
Here's the difficulties I run into. The fruit names, students, and fruit consumed are pulled from the database.
And I don't know how many fruit rows nor student columns there will be.
This is as far as I got:
<table>
<tr>
<th>Fruit</th>
<?php $sqlS = db("SELECT s.*, sf.consumed FROM `tbl_students` s, `tbl_students_fruit` sf WHERE s.studentid = f.studentid ORDER BY s.studentname ASC");
while($student = mysql_fetch_array($sqlS)){ ?>
<th><?php echo $student['studentname'];?></th>
<?php } ?>
<th>Total</th>
</tr>
<?php $sqlF = db("SELECT * FROM `tbl_fruit` ORDER BY fruitname ASC");
while($fruit = mysql_fetch_array($sqlF)){ ?>
<tr>
<td><?php echo $fruit['fruitname'];?></th>
<td></td>
<td></td>
</tr>
<? } ?>
</table>
As you can see, I'm creating the Fruit rows, and Student columns. But it's incomplete. I've only created the column headers, and not the columns below the headers. From this point I am stuck on the guts of the table.
I am sure arrays are the way to go with this monstrosity. But the only way my feeble brain can make this work is to have more queries, which I'm sure is a very wrong way to do this.
If there were 3 students or 15 students, I can make them appear in the table th columns, but not in rows in their columns.
How does one traverse dynamic columns this way?
And if my demo above is confusing, I don't blame you!
well, I am assuming that your db structure is as follows:
[tbl_students]
studentid, studentname
[tbl_fruit]
fruitid, fruitname
[tbl_students_fruit]
id, fruitid, studentid, consumed
http://pastebin.com/CxPUeXR0
I haven't tested it, so good luck

Categories