Can I create database tables using controller in laravel? - php

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 . .. ');

Related

Split mysql retrieved data into headers and rows

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.

how to show the count of particular name? & show it page using php sql?

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'];
}
?>

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]

jquery ui sortable get table tr grouped

i have some problems with my table i have this table for egs
<tr style="">
<td>1</td>
<td>item 1</td>
</tr>
<tr style="">
<td>1.1</td>
<td>item 1.1</td>
</tr>
<tr style="">
<td>1.2</td>
<td>item 1.2</td>
</tr>
<tr style="">
<td>1.1.1</td>
<td>item 1.1.1</td>
</tr>
i use jquery ui sortable but the problem is that if i want to move the item 1 the item1.2 and item 1.1.1 will also follow something like a group
fiddle link http://jsfiddle.net/kN2XL/
You should have a look at nestedSortable to implement this kind of functionality. Example
Unfortuately, you will however need to change your html from tables into a hierarchy, like <ul> <li> etc.

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