access values after comma separator from single field of mysql (Codeigniter) - php

I have one problem while retrieveing value from mysql using codeigniter.
I have one table name task and in that there is one field of assigneduserid.
Table - Task:
id | title | Title | assigneduserid
--------------------------------------------------------
1 | Workspace |PMS | 23,21
Table - User
id | title
-----------------
23 | Ashish
21 | Ritesh
In assigneduserid field there are two values in that field that is 23,21.
I want to display task title in both user login.
but it displays only to Ashish login.
how can i solve it??
Following is my model:-
function getTask($id, $is_master_admin) {
$this->db->select('task.*, workspace.title as workspacetitle, user.title as usertitle');
$this->db->join(WORKSPACE, WORKSPACE . '.id = ' . TASK . '.workspaceid', 'inner');
$this->db->join(USER, USER . '.id = ' . TASK . '.userid', 'inner');
$this->db->from(TASK);
$this->db->group_by('task.id');
if (!$is_master_admin) {
$this->db->where(USER . '.id', $id);
}
$this->db->where(TASK . '.tasktypeid', '1');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result();
} else {
return false;
}
}
Can any one please help??

function getTask($id, $is_master_admin) {
$data= $this->db->group_by('task.id')->get('task')->result();//check what value your getting in data and then use foreach and select the user from the user table
foreach($data as $value):
$array= $this->db->where('id',$value->id)->get('user')->result();
endforeach;
}

Try, Haven't checked it,
$tsql = " SELECT task.*, workspace.title AS workspacetitle, GROUP_CONCAT(user.title ORDER BY id) AS usertitle ".
" INNER JOIN ". WORKSPACE ." ON ". WORKSPACE .".id = " . TASK . ".workspaceid ".
" INNER JOIN USER ON FIND_IN_SET(".USER . ".id, ". TASK .".assigneduserid ) > 0 ".
" FROM ". TASK ;
if (!$is_master_admin) {
$tsql .= " WHERE ". USER . ".id =". $id;
}
$tsql .= " WHERE ". TASK . ".tasktypeid =1 ";
$tsql .= " GROUP BY ". TASK .".id ";
$query = $this->db->query($tsql);

Related

Pull values from a row based on ID

Hoping someone can shed light on this. I am trying to pull the value from 2 fields from a row and based on the row being expired, exclude those 2 values from a drop down list.
I have a table (schedule)
gameID
homeID
visitorID
gameTimeEastern
weekNum
each week there are matchups where 2 teams play each other. Those 2 teams are in a row based on gameID with a specific start time (gameTimeEastern).
I have a function that determines when the matchup is locked, meaning the game has started:
function gameIsLocked($gameID) {
//find out if a game is locked
global $mysqli, $cutoffDateTime;
$sql = "select (DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > gameTimeEastern or DATE_ADD(NOW(), INTERVAL " . SERVER_TIMEZONE_OFFSET . " HOUR) > '" . $cutoffDateTime . "') as expired from " . DB_PREFIX . "schedule where gameID = " . $gameID;
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$row = $query->fetch_assoc();
return $row['expired'];
}
$query->free;
die('Error getting game locked status: ' . $mysqli->error);
This basically determines if the row is expired (gameTimeEastern has passed). I then have a drop down on a form that has a list of all the teams from each matchup for that week.If the row is expired, then I do not want to include the homeID or visitorID from that row in the drop down.
On my page I am trying to show those teams from the expired row but it is failing as the page stops processing when it hit this:
//get expired teams
$expiredGames =gameIsLocked(gameID);
// echo 'Expired games are GAME ' . $expiredGames . '<br>';
for ($eti=1; $eti<=$gameID; $eti++)
{
if ($gameID[$eti]>''){
$sql = "select * from " . DB_PREFIX . "schedule WHERE gameID = '" . $gameID[$eti] . "';";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$result = $query->fetch_assoc();
$expiredHomeTeam = $result['homeID'];
$expiredVisitorTeam = $result['visitorID'];
}
}
echo 'Expired teams for GAME '.$gameID.' are '.$expiredHomeTeam.' and '.$expiredVisitorTeam.'<br>';
}
NEW CODE - Actually giving me the first result
//get expired teams
$expiredGames =gameIsLocked(gameID);
$sql = "select * from " . DB_PREFIX . "schedule WHERE weekNum = '6';";
$query = $mysqli->query($sql);
if ($query->num_rows > 0) {
$result = $query->fetch_assoc();
$expiredHomeTeam = $result['homeID'];
$expiredVisitorTeam = $result['visitorID'];
}
echo 'Expired teams for GAME ' . $expiredGames . ' are '.$expiredHomeTeam.' and '.$expiredVisitorTeam.'<br>';
Ended up using the SQL query to schedule to get results I needed. Thanks for the direction. The logic was already there, just needed to add an if statement to how I populated the array for teams in the drop down.

PHP & Mysql Not ordered correctly

I have latest MySQL version (5.5) and this is the screenshot of groupid field
I didn't touch anything yet, but some cells are not ordered correctly like this
But if I click groupid name in the top, it will ordered correctly like this:
Below PHP code output is like first screenshot above, that are not ordered correctly. Please help how to make the output ordered correctly, like it is displayed in the second screenshot above,
Maybe add code like this : order by id asc, but which is the right place to add it below?
$group_ids = explode(" ", $options['groupchoice_ids']);
$groupsql = "SELECT id, title FROM " . TABLE_PREFIX . "thegroup WHERE";
$first = true;
foreach($group_ids as $value)
{
if (!$first)
{
$groupsql = $groupsql . " OR ";
}
else
{
$first = false;
}
$groupsql = $groupsql . " id = '" . $value . "' ";
}
$kh_optionsgroup = '<select name = "accounttype">';
$checksec = $db->query_read($groupsql);
if ($db->num_rows($checksec))
{
while ($lboard = $db->fetch_array($checksec))
{
$kh_optionsgroup = $kh_optionsgroup . "<option value
='" . $lboard['id'] . "'>" . $lboard['title'] . "</option>";
}
}
$verifystring = '$human_verify';
$kh_optionsgroup = $kh_optionsgroup . "</select>";
At the end of your query, you need to set an order, like so:
$groupsql="SELECT id, title FROM " . TABLE_PREFIX . "thegroup WHERE";
$first=true;
foreach($group_ids as $value){
if(!$first){
$groupsql = $groupsql." OR ";
}else{
$first = false;
}
$groupsql = $groupsql." id = '".$value."' ORDER BY groupid ASC";
}
ORDER BY id ASC
This will make the query return its results in ascending order from the groupid column. Simply change ASC to DESC if you want it to go descendinng (high->low).

PHP: Merge two MySql array result

i fetch id and name from categories table like this :
|id|name| tag |desc|order|status|
|1 |test| NULL|NULL| 1 | 1 |
$i = 0;
$allcats = Access::FETCH("SELECT * FROM " . CATS . "");
$cats = array();
foreach($allcats AS $row2){
$cats[$i] = array("name" => $row2['name'], "id" => $row2['id']);
$i++;
}
i fetch category id for each news :
|id|catid|storyid|
|1 | 1 | 5 |
$groupcats = Access::FETCH("SELECT * FROM " . GROUPCATS . " WHERE storyid = ?", $row['postid']);
Now, i need to print name of cat for storyid. i,e : i need to print test(catname) for story id 5.
How do can i print this?
Easier to do with with a join rather than in php. Something like,
$joinedcats = Access::FETCH("SELECT name FROM " . CATS .
" JOIN " . GROUPCATS . " ON catid = id");
foreach($joinedcats as $row) {
echo $row['name'];
}
See https://dev.mysql.com/doc/refman/5.5/en/join.html
You can use join to get needed rows for each story:
"SELECT * FROM " . GROUPCATS . " LEFT JOIN " . CATS . " USING(categoryid) WHERE storyid = ?"

mysql advance search query with mulitivalued field

i have a search function to search by different criteria from a db table.
here is the table' field with some data
id first_name last_name province_id
1 ali alii 2,12
2 ahmad ahmadi 22,1,4
a person can be in different province, and its saved in table separating by "," in province field, i know it is not normalized.
i use followed function for search
$WHERE_ARR = array();
if(trim($first_name)!=""){
$WHERE_ARR['first_name'] = " p.first_name like '%".$first_name."%' ";
}
if(trim($last_name)!=""){
$WHERE_ARR['last_name'] = " p.last_name like '%".$last_name."%' ";
}
if(trim($province)!=""){
$WHERE_ARR['province'] = " p.province_id = ".$province." ";
}
$WHERE_STR = implode(' AND ', $WHERE_ARR);
$WHERE_STR = (trim($WHERE_STR)!="")?" AND " . $WHERE_STR:'';
$QUERY = "
SELECT p.*,pr.province
FROM ndi_participants p,ndi_provinces pr
WHERE pr.province_id = p.province_id
". $WHERE_STR . "
ORDER BY pr.province ASC
LIMIT ".$start.", " . $perpage . "
";
return $this->db->query($QUERY);
for sure it is not exact when i search by province field.
how can i search by province field when it is multivalued separating by ","
note: i am using codeigniter
thanks for your helping
Try FIND_IN_SET mysql function to search in provience_id
if(trim($province)!=""){
$WHERE_ARR['province'] = " p.province_id = FIND_IN_SET(".$province.",p.province_id)";
}

Product filtering with MySQL and PHP for opencart

I am trying to make dynamic product filtering for opencart in which user could add filters, assign them to categories and assign filter values for products.
Currently I Have these mysql tables:
Filters - ID NAME
Filters_categories - ID FILTER_ID CATEGORY_ID
Filters_values - ID FILTER_ID VALUE
Filters_products - ID FILTER_ID VALUE_ID PRODUCT_ID
The problem is with this structure that i can't get products when more than one filter is activated because i get MySQL query which looks something like this:
SELECT product_id FROM Filters_products WHERE ((name_id='1' OR name_id='2') AND filter_id='1') AND ((name_id='3' OR name_id='4') AND filter_id='2')
And it doesn't make any sense. I can't figure out how should i change my database structure to make dynamic filtering possible. How can I solve this situation?
Thank you.
SELECT product_id FROM Filters_products WHERE (screen_id='1' OR screen_id='2') and (connection_id='1')
Use this awesome extension,
http://www.opencart.com/index.php?route=extension/extension/info&extension_id=10098
Hope this helps.
in file /catalog/model/catalog/product.php find line nr. 91 and replace "if {}" block whit this code...
if (!empty($data['filter_category_id'])) {
if (!empty($data['filter_sub_category'])) {
$sql .= " AND cp.path_id = '" . (int)$data['filter_category_id'] . "'";
} else {
$sql .= " AND p2c.category_id = '" . (int)$data['filter_category_id'] . "'";
}
if (!empty($data['filter_filter'])) {
$implode = array();
$filters = explode(',', $data['filter_filter']);
foreach ($filters as $filter_id) {
$filterSQL = $this->db->query("SELECT * FROM " . DB_PREFIX . "filter WHERE filter_id = ". $filter_id);
$implode[$filterSQL->row['filter_group_id']][] = (int)$filter_id;
}
foreach($implode AS $implode2) {
$sql .= " AND (";
foreach($implode2 AS $filterID) {
$sql .= "(SELECT count(*) FROM product_filter WHERE product_filter.product_id=p.product_id AND product_filter.filter_id=$filterID) OR ";
}
$sql = substr($sql, 0, -4);
$sql .= ")";
}
}
}

Categories