My mysql table look like
ranks are rank1,rank2,rank3 on id 1
but i want to show like that
id 1 -> rank1
id 1 -> rank2
id 1 -> rank 3
My sql Query
$this->db->select('rank')->from('rank_restriction');
$data = $this->db->get();
$query = $data->result_array();
foreach ($query as $result) {
if (!empty($result['rank'])) {
$result['rank'] = explode('||', $result['rank']);
}
return $query
}
Related
I want to convert a normal mysql select query to codeigniter
SELECT count(*) leave_status, teacher_id FROM teacher_attendance WHERE teacher_id='1' & leave_status='1' group by teacher_id
this is what i have tried
$attendance = $this->db->get_where('teacher_attendance', array('year' => $running_year, 'timestamp' => $timestamp, 'teacher_id' => $row['teacher_id']))->result_array();
what i want to do is get the count of leave_status by teacher id
$this->db->select("count(*) as leave_status, teacher_id");
$this->db->from("teacher_attendance");
$this->db->where("teacher_id = 1 AND leave_status='1'");
$this->db->group_by("teacher_id");
$query = $this->db->get();
if ($query->num_rows() > 0) {
$result = $query->result_array();
}
Say I have a table 'table_name' with two columns like this:
table_name
------------
my_id | data
------|-----
id1 | 312
id1 | 523
id1 | 128
id2 | 239
id2 | 479
id2 | 121
id3 | 639
id3 | 429
id3 | 131
id4 | 473
id4 | 872
id4 | 662
id4 | 174
id4 | 272
I tried around a while and I now found a way to select the ids I want to use:
SELECT DISTINCT my_id from table_name WHERE (my_id REGEXP 'id[234]')
This gives me the ids 'id2', 'id3' and 'id4'. How would I formulate a query that gives me my desired output, based on the selected ids as below?
id2 | id3 | id4
---------------
239 | 639 | 473
479 | 429 | 872
121 | 131 | 662
NaN | NaN | 174
NaN | NaN | 272
If your table really only has those two columns and there is no related data that can be used to order the data column like you have shown, then there is no good way to achieve the ordering you're looking for.
Relational data doesn't have any intrinsic "order" to it, even if you've inserted that data in a particular order or displayed it in a certain way here. If your table really only has those two columns, it will be difficult to match the "first" id2 value with the "first" id3 value and so on to get to your desired output. We could potentially rank them in a query and match them based upon that rank (ascending or descending) or even provide some random order capability, but there is no documented (i.e. reliable) way to get the order that you've provided.
While a table does have a physical order to the underlying bits in storage, and some products allow you to access them via a physical rowid (Oracle), no RDBMS that I know of (including MySql) guarantees the order in which the rows will be returned unless an order by clause is used in your select statement. This is literally by design and is considered a "feature" that came out of E.F. Codd's research.
First make a function that select distinct of the id's
public function get_id_of_table_name() {
$sql = 'SELECT DISTINCT my_id ';
$sql .= 'FROM table_name ';
$query = $this->db->query($sql);
$result = $query->row();
foreach ($result as $key => $value)
{
$result->data = $this->get_data($result->my_id);
}
return ( $query->num_rows() ) ? $result : FALSE;
}
And create the second function that will gather all the data's base on what id's
public function get_data($my_id) {
$params = [];
$sql = 'SELECT data ';
$sql .= 'FROM table_name ';
$sql .= 'WHERE my_id = ? ';
$params[] = $my_id;
$query = $this->db->query($sql, $params);
$result_data = $query->result();
$data = [];
foreach ($result_data as $result)
{
array_push($data, $result->data);
}
return $data;
}
You can put a where clause in the first function if you want.
$sql .= 'WHERE my_id = ? ';
and add the params
$params[] = $my_id;
You can also add parameters my_id as array in the first function so that you can get what id you want to get
$id = ["id2","id3","id4"];
public function get_id_of_table_name($id) {
$params = [];
$sql = 'SELECT DISTINCT my_id ';
$sql .= 'FROM table_name ';
$sql .= 'WHERE 1 ';
foreach ($id as $my_id)
{
$sql .= 'OR (my_id = ?) ';
$params[] = $my_id;
}
$query = $this->db->query($sql, $params);
$result = $query->row();
foreach ($result as $key => $value)
{
$result->data = $this->get_data($result->my_id);
}
return ( $query->num_rows() ) ? $result : FALSE;
}
I'm trying to do a filter search functionality in codeigniter. I have a table named products and my system has the functionality to filter these products by category and by date. I have a mysql code in mind which looks something like this:
SELECT * from products WHERE product_category='Cloth'
INTERSECT
SELECT * from products WHERE ('insert date logic here')
So it should return records (via id) from the same table named products. However, there's no INTERSECT in mysql so I don't know how to do this. Any help would be greatly appreciated!
This is my code just for the part of the product category
$this->db->limit($limit,$start);
$query = $this->db->query('SELECT * from product_advertised WHERE quantity > 0 AND prodcatid='.$prodcats[0].' LIMIT '.$start.','.$limit);
if(sizeof($prodcats > 1)) {
$query_str = "SELECT * FROM product_advertised WHERE quantity>0 AND ";
$str="";
for($i = 0;$i < sizeof($prodcats);$i++) {
if($i != sizeof($prodcats)-1) {
$str = $str. "prodcatid=".$prodcats[$i]." OR ";
}
else {
$str = $str. "prodcatid=".$prodcats[$i]." LIMIT ".$start.",".$limit;
}
}
$query_str .= $str;
$query = $this->db->query($query_str);
}
if($query->num_rows() > 0) {
foreach($query->result() as $row) {
$data[] = $row;
}
return $data;
}
return false;
Why wouldn't you just use and?
SELECT *
from products
WHERE product_category = 'Cloth' AND
('insert date logic here');
If I have a variable in php like,
$var = 'aaa';
And my mysql database has 2 columns
| aaa | bbb |
Is there a way I can select bbb using $var, basically select the column next to $var on the right?
Use mysql_query("SHOW COLUMNS FROM {table}",$db) to get a list of columns and use that to figure out the name of the column next to aaa. You can then use that in further queries.
Implementing this results in something in the lines of (untested):
$columns = mysql_query("SHOW COLUMNS FROM {table}",$db) or die("mysql error");
if (mysql_num_rows($columns) > 0) {
while ($row = mysql_fetch_assoc($columns)) {
if ($prev == 'aaa') {
$nextcol = $row['Field']; // 'bbb' in this case
break;
}
$prev = $row['Field'];
}
}
I have two set of arrays in different MYSQL table. This what I want to do
What I Want TO Do
TABLE_ONE connect to the table.
get the value we want from session_id
THEN get the array associated with the value (session_id)
explode the array to get individual values.
NOW::::: - GO TO TABLE_TWO
TABLE_TWO Go straight to the first value from array (TABLE_ONE)
Explode the array associated with it.
Delete the number that's equal to the session_id
_____________________________________________________
And so fort....
More visual explanation below:
session_id = 4
TABLE_ONE:
id array1
1 4
2 1
3 2,5
4 1,3,4,5
5 4,5
TABLE_TWO:
id array2
1 4,6,9,2
2 3,7,8,2
3 7,12,4,9
4 1,5,4,8
5 3,6,12,3,5,4
So, because session_id = 4, we go to TABLE_ONE id 4. The array of id-4 is 1,3,4,5.
So now we know 4 can be found in id 1,3,4,5 of TABLE_TWO
We should now explode TABLE_TWO's array and delete 4 from there array. Implode the array and save the new value to database.
THIS IS WHAT I HAVE DONE - it would delete only '4' from them id-3 and delete all the values in id-4. Please help!!
$SESSION = 4;
$depSQL = mysql_query("SELECT array1 FROM TABLE_ONE WHERE id='$SESSION' LIMIT 1");
while($row=mysql_fetch_array($depSQL)) { $depARRAY = $row["array1"]; }
$explodedDEP = explode(",", $depARRAY);
foreach ($explodedDEP as $key1 => $value1) {
$stSQL = mysql_query("SELECT array2 FROM TABLE_TWO WHERE id='$value1'");
while($get=mysql_fetch_array($stSQL)) { $stARRAY = $get["array2"];}
$explodedST = explode(",", $stARRAY);
foreach ($explodedST as $key2 => $value2) {
if ($value2 == $SESSION) {
unset($explodedST[$key2]);
}
}
$newST = implode(",", $explodedST);
$sql = mysql_query("UPDATE TABLE_TWO SET array2 ='$newST' WHERE id='$value2'");
}
exit();
Please help!!! I'm really struggling on it.
I have tried it for hours now and i havent really got any where.
I think the problem is with the inserting to database.
Please help.
You can avoid one loop by using the array1 in your second SQL directly since it is already a comma seperated list.
Try this:
EDIT: Updated the code after testing.
$SESSION['ID'] = 4;
$depSQL = mysql_query("SELECT array1 FROM TABLE1 WHERE id='".$SESSION['ID']."' LIMIT 1");
while($row=mysql_fetch_array($depSQL))
{
$depARRAY = $row["array1"];
}
$stSQL = mysql_query("SELECT id, array2 FROM TABLE2 WHERE id IN ($depARRAY)") or die("Query Error");
while($get=mysql_fetch_array($stSQL)) {
$stARRAY = $get["array2"];
$id = $get["id"];
$explodedST = explode(",", $stARRAY);
foreach ($explodedST as $key2 => $value2) {
if ($value2 == $SESSION['ID']) {
unset($explodedST[$key2]);
}
}
$newST = implode(",", $explodedST);
echo $id . " " . $newST . "<BR/>" ;
$sql = mysql_query("UPDATE TABLE2 SET array2 ='$newST' WHERE id='$id'");
}
exit();
$SESSION is an array you can not assign value like this $SESSION = 4;
assign value like this
$SESSION['id'] = 4;
if ($value2 == $SESSION['id']) {