I am trying to learn Laravel, so I apologize if my question is simple. I have 2 table (table1, table2) like this:
table1:
ID date time
1 1 1
2 4 2
3 5 3
table2:
ID V R
1 123 T
1 12 F
1 43 F
2 32 T
2 23 T
3 43 F
because I have 3 type of IDs (which could be more or less) I want to divide table2 into 3 tables using table1. like this:
table2_1: for ID:1
V R
123 T
12 F
43 F
table2_2: for ID:2
V R
23 T
23 T
table2_3: for ID:3
V R
43 F
I think I need somthing like this:
#foreach ($table1 as $t)
<table class="table">
{!! $t -> ID!!}
<thead>
<tr>
<th scope="col">R</th>
<th scope="col">V</th>
</tr>
</thead>
<tbody>
<!---Query result ---->
</tbody>
#endforeach
which in Query result I need a to select V and R from joining table1 and table2.
but I don'n know the exact code.
Any Idea how can I do this? Thanks in advance.
You may not even need to involve table1 here. Just query table2 with an ORDER BY clause, and then iterate the result set, turning out new HTML tables for each new ID value:
$result = DB::table('table2')
->orderBy('ID')
->get();
$prev_id = NULL;
foreach($result as $row) {
$curr_id = $row->ID;
if ($prev_id != NULL && $curr_id != $prev_id) {
echo "</table>";
}
if ($prev_id == NULL || $curr_id != $prev_id) {
$prev_id = $curr_id;
echo "<table class=\"table\" colspan=\"2\">";
echo "<tr><th scope=\"col\">V</th><th scope=\"col\">R</th></tr>";
}
echo "<tr><td>" . $row->V . "</td><td>" . $row->R . "</td></tr>";
}
echo "</table>";
Related
I'm new to programing. I have table
check_1,check_2,check_3 ..etc
------------------------------
1 1 1
0 0 1
1 0 1
And I want this output :
column_name, count_true
-----------------------
check_1 2
check_2 1
check_3 3
I've tried it with mysql using the union function, but when I try to apply it in laravel I have trouble with union. Is there a unionless query that can produce such output?
Thanks in advance
You can do this way. One query in db
$records = DB::table('your_table')->get();
$check1Count = $records->where('check_1', 1)->count();
$check2Count = $records->where('check_2', 1)->count();
$check3Count = $records->where('check_3', 1)->count();
......
Or
$records = DB::table('your_table')->get();
$columns = ['check_1', 'check_2', 'check_3', ...];
$data = [];
foreach($columns as $column) {
$data[] = [
'column_name' => $column,
'count_true' => $records->where($column, 1)->count();
];
}
Also you can do this way but it is many query
$check1Count = DB::table('your_table')
->selectRaw('COUNT(check_1) as count')
->where('check_1', 1)
->first()
->count;
$check2Count = DB::table('your_table')
->selectRaw('COUNT(check_2) as count')
->where('check_2', 1)
->first()
->count;
.....
A normalised approach might look like this:
response_id checkbox
1 1
1 2
1 3
2 3
3 1
3 3
You don't need union, just query all the data and process it on Laravel. Let say you query all the data using eloquent to $data variable, then you should do it like this:
//preparing a variable to hold all 24 database field value
$total = [];
foreach ($data as $d) {
//do this for all 24 database field
if ($d->check_1) {
$total[1]++;
}
if ($d->check_2) {
$total[2]++;
}
...
}
By using that way you can't check the resutl on $total[index] variable. And yes, there is a better way to store your data instead of saving all the field for each user. You can just store all checked value in database that look like this :
user_id checkbox_id
1 3
1 5
1 9
1 24
2 23
3 2
3 3
It more efficient since you don't need to save the unchecked checkbox value, if they more likely not to checked most of the checkbox.
I'm using Codeigniter V2.1.3 and I have a calendar template that have a scheduling stuff and I want to count the events in a particular date. Here is my database:
----Table: schedule -----
id | materialID | borrowerID | date_reserve |
------------------------------------------------
1 | 7 | 7 | 2013-08-16 |
2 | 10 | 10 | 2013-08-16 |
1 | 12 | 13 | 2013-08-18 |
What I want is in my calendar template the total event for the date=2013-08-16 will be 2 events. Here is my code which is not working coz it keeps on sending me only 1 event maybe you could figure out where is my mistake in here:
$query = $this->db->select('*')->from('schedule')->like('date_reserve', "$year-$month")->get();
$cal_data = array();
foreach ($query->result() as $row) {
$index = ltrim(substr($row->date_reserve,8,2), '0');
$cal_data[$index] = count($row->borrowerID). 'event(s)';
}
return $cal_data;
Any help?
If you want number of events for exact date: YYYY-MM-DD you can replace like with where:
$r = $this->db->select('id, materialID, borrowerID, date_reserve')
->where('date_reverse', $year."-".$month."-".$day)
->get('schedule')
->result();
Also you can print_r the result so you will see what the result is. Also you can echo last query: echo $this->db->last_query();
Edit:
We had to run query per each date to fetch the number of events.
$cal_data = array();
for($i=1;$i<=31;$i++)
{
$this->db->select('COUNT(id) as count,date_reserve,borrowerID');
$this->db->from('schedule');
$this->db->where('date_reserve',"$year-$month-$i");
//$this->db->group_by('date_reserve');
$query = $this->db->get();
foreach ($query->result() as $row) {
$cal_data[$index] = $row->count. 'event(s)';
}
try using group_by
public function get_results($date) {
$this->db->select('COUNT(id),date_reserve');
$this->db->from('schedule');
$this->db->like('date_reserve',$date);
$this->group_by('date_reserve');
$result = $this->db->get();
return $result->result_array();
}
I have some data on my db and I'm printing those to screen vertically but I want to print them on three different column. They can be in div, table etc.
For two column I found a solution.
If current row is even number, print location A
else location B
but if we want to print to more than two column how can we do that?
For example input must like that..
Name1 | Name2 | Name3
Name4 | Name5 | Name6
..... | ..... | .....
This way is much better than the previous I suggested. Took me a while to find it, but I knew I had done it somewhere!
echo '<table><tr>';
for ($i=0; $i < 40; $i++) {
foreach ($data as $el) {
echo '<td>' . $el . '</td>';
if ($i++ > 1 && $i++ % 3 == 0 ) echo '</tr><tr>';
}
}
echo '</tr></table>';
What I want to achieve is to move the data between 2 rows within one table.
Column A
--------
FN2
1 200x310mm
2 400x260mm[+0.84]
3 500x500mm[+11.34]
Column B
--------
0.0000
0.0000
0.0000
0.0000
This is how it should look like after the data move:
Column A
--------
FN2
1 200x310mm
2 400x260mm
3 500x500mm
Column B
--------
0.0000
0.0000
+0.84
+11.34
What I want is that the query between the [ ] is moved to column B and replaces the 0.0000
How can I achieve this?
Kind Regards
just to illustrate what Yadav said
$query = "SELECT columnID, columnA FROM table";
$result = mysql_query($query,$conn);
while ($row = mysql_fetch_array($result)){
$id = $row['columnID'];
$a = $row['columnA'];
$pos1 = strpos($a,"[")+1;
$pos2 = strpos($a,"]");
$b = substr($a,$pos1,$pos2-$pos1);
$query = "UPDATE table SET columnB = $b WHERE columnID = $id";
mysql_query($query,$conn);
}//end while
edit: Yadav obviously proposed a better answer while I was typing mine...
try this it works for you here i used id as unique key ... and test is my database
<?php
$con=mysql_connect("localhost","root","");
$db=mysql_select_db("test");
$query=mysql_query("SELECT * FROM test where columnA LIKE '%[%]'");
while($row=mysql_fetch_assoc($query))
{
if(preg_match_all('/\[(.*?)\]/',$row['columnA'],$match))
{
mysql_query("UPDATE test SET columnB='".$match[1][0]."' WHERE id=".$row['id']."");
}
}
?>
I have SQL query that produced more than 1000 rows. How it can get as columns ?
Example:
1
2
...
999
1000
how can I get as,
1 101 ....
2 102
... ...
99 199
100 200
following code displays
1 2 3 ...100
101 102 103 ...200
I want like this
1 101
2 102
... ...
99 199
100 200
$result=mysql_query("SELECT * FROM master_break");
$display = 10;
$cols = 0;
echo "";
while($fetched = mysql_fetch_array($result)){
if($cols == 0){
echo "\n";
}
// put what you would like to display within each cell here
echo "".$fetched['sName']." | ".$fetched['value']."";
$cols++;
if($cols == $display){
echo "";
$cols = 0;
}
}
// added the following so it would display the correct html
if($cols != $display && $cols != 0){
$neededtds = $display - $cols;
for($i=0;$i\n";
echo "";
}
echo "";
} else {
echo "";
}
?>
This will more than likely need to be done in the presentation layer (or display of data). The query can not produce multiple columns from 1 column, so you will ned to display the data as you need it in your code.