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
Related
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 . .. ');
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.
i have a agent table
& where all agent details hai been display.
So along with the details i want to show count the agent name has been displayed in the table in same table.
i tried this query
Select count(name) from agent;
but it shows whole table count.
i want result to be this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>1</td>
</tr>
<tr>
<td>amit</td>
<td>4</td>
</tr>
<tr>
<td>sushant</td>
<td>8</td>
</tr>
</table>
2/5
Saurabh 4/5
bt wat i m getting is this:
<table border="1">
<tr>
<td>name</td>
<td>count</td>
</tr>
<tr>
<td>bhaskar</td>
<td>13</td>
</tr>
<tr>
<td>amit</td>
<td>13</td>
</tr>
<tr>
<td>sushant</td>
<td>13</td>
</tr>
</table>
so how to show particular name count in table row in php page??
Try this
select count(name)as count,name from agent group by name
Update code
<?php $query=mysql_query("SELECT count(name) as count,phone,email,t_id,name FROM transfer WHERE agent_id='$id' group by name ORDER BY t_id DESC"); while($row=mysql_fetch_array($query)) {
echo $row['name'];
echo $row['count'];
}
?>
I've been using this PLUGIN for representing my data from mysql database.
I am using this script EXAMPLE for server side processing.
Now this script is working perfectly why you have one table. But problem is occurring when I have multitable query.
For example I have:
$sWhere = "a.ID=b.ID AND b.Name=c.Name";
This is only where variable. If you go to the link that I gave you can see the php script that is used to fetch data. When I put more then one table I get unique table error. And the search functions can't work.
Can someone show me how to use this script to be able to have multiple tables included in one query.
If you need more source let me know.
EDIT:
My HTML:
<table id="table_my" width="100%" border="1" cellspacing="2" cellpadding="5" class="chart_1">
<thead>
<tr class="even">
<th>A</th>
<th>B</th>
<th>C</th>
<th>D</th>
<th>E</th>
<th>F</th>
<th>G</th>
</tr>
</thead>
<tbody>
</tbody>
</table>
There should be a change in the HTML Markup, when that multitable query executes. Why don't you compare both and check for the difference in the HTML Markup?
The markup for DataTable jQuery plugin should be this way:
<table>
<thead>
<tr>
<th>Column Name</th>
<th>Column Name</th>
<th>Column Name</th>
</tr>
</thead>
<tbody>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
<tr>
<td>Row Value</td>
<td>Row Value</td>
<td>Row Value</td>
</tr>
</tbody>
</table>
Also remember, it doesn't work with more than one tbody or thead tag and tables without these tags. Also, if you have used colspan and rowspan attributes, it doesn't work.
from www.facebook.com/Heanock Z behere Ethiopia
*
Answer for the above problem
*
i just use like i am using in php or other language and i have two tables to be joined
tbl_employees
**
emp_id
** int(11) auto_increment primary key,
emp_name varchar(50),
sex enum('female','male'),
emp_dept varchar(50),
emp_salary double,
job int(11) = is foreign key that i will share from the second table
tbl_job
**
job_id
** int(11) primary key,
job_name varchar(50),
job_cat varchar950)
lastly here is my code in php mysql plus datatable
Emp ID
Emp Name
Sex
Department
Salary
Job
Edit
Delete
$stmt = $db_con->prepare("SELECT emp_id,emp_name,sex,emp_dept,emp_salary,job_name FROM tbl_employees,tbl_job where job=job_id ORDER BY emp_id DESC");
$stmt->execute();
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
?>
<tr>
<td><?php echo $row['emp_id']; ?></td>
<td><?php echo $row['emp_name']; ?></td>
<td><?php echo $row['sex']; ?></td>
<td><?php echo $row['emp_dept']; ?></td>
<td><?php echo $row['emp_salary']; ?></td>
<td><?php echo $row['job_name']; ?></td>
if you have any question related with above problem write me # heanocklove#gmail.com or My facebook Page listed Above
Thanks very much
I need to put tables in my datatable, but it doesn't look like datatables likes it. Does anyone know how I can get the functionality I need? I tried putting in divs that look like tables instead of tables, but it still broke. I think because the tr only has one td.
My code:
foreach($campaigns as $campaign):?>
<tr>
<td><?php echo $campaign['name']?></td>
<td><?php echo date('m/d/Y', strtotime($campaign['created_date']))?></td>
<td><?php echo $campaign['country_id']?></td>
<td><?php echo $campaign['domain_getter_type']?></td>
<td><?php echo $campaign['domain_metrics']?> <?php echo $campaign['metric_filter_setting_id']?></td>
<td><?php echo $campaign['domain_contact']?></td>
<td>0</td>
</tr>
<!--campaign runs-->
<tr>
<td colspan="99">
<table>
<thead>
<tr>
<th>dfad</th>
<th>t3wtaw3 On</th>
<th>dfw</th>
<th>Domain dawfd</th>
<th>p3p3</th>
</tr>
</thead>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
<tr>
<td>1</td>
<td>2</td>
<td>3</td>
<td>4</td>
<td>5</td>
</tr>
</table>
</td>
</tr>
<?php endforeach;?>
After each row of data, I want to put a hidden table you can show or hide, that holds additional information about each specific run of that campaign.
I get this error in chrome: Uncaught TypeError: Cannot read property 'className' of undefined
Assuming this question is referring to the jQuery DataTables JavaScript plugin (assuming because I see no mention of JS in the example code provided), I would follow the author's example that he outlines in his blog here: http://datatables.net/blog/Drill-down_rows
I have used this method before and it works quite well.
It does assume that you are initializing the table as empty when it renders and then fetches the data with AJAX, but I see no reason it could not be easily modified to be generated server side.
Another option would be using the master / detail example as a guide, you can find that here along with the sample code: http://datatables.net/examples/api/row_details.html