The below code works for the first $myurl1 only after setting for the first time, it doesnot set for the other statements, can anybody help on this.
$myurl="http://alberta.domain.com/";
$myurl2="http://toronto.domain.com/";
if ($myurl1) {
osc_set_preference('subdomain_type', 'region', 'osclass');
osc_reset_preferences();
} elseif ($myurl2) {
osc_set_preference('subdomain_type', 'city', 'osclass');
osc_reset_preferences();
} else {
osc_set_preference('subdomain_type', 'country', 'osclass');
osc_reset_preferences();
}
Since $myurl1 is not empty, if will only execute the first if statement. elseif or else will not be executed
Related
A user can enter a letter range like "A-D", by which a query must find all records that start with any of those letters. What I eventually need is a constraints block that looks like this:
$constraints = [
$query->expr()->eq(
'composition.sys_language_uid',
$query->createNamedParameter($language, \PDO::PARAM_INT)
),
$query->expr()->orX(
$query->expr()->like(
'composition.title',
$query->createNamedParameter('A%')
),
$query->expr()->like(
'composition.title',
$query->createNamedParameter('B%')
),
$query->expr()->like(
'composition.title',
$query->createNamedParameter('C%')
),
$query->expr()->like(
'composition.title',
$query->createNamedParameter('D%')
)
)
];
which is a structure, that works well, when I use it as a test. So I know I need to strive for a solution like this.
But, of course, since the letters given are not fixed, but variable, the block within ->orX() needs to be calculated programmatically. This is, where my problem lies.
I tried this:
// A custom helper function that splits a letter range string like "A-D"
// and returns an array like ['A','B','C','D']
$compareLetters = Helper::returnItemOrListAsArray($letter, true);
}
// Create the query
$query = $allDbConnections['composition']->createQueryBuilder();
// Collect constraints
$addConstraints = [];
// Compare first letter against given compareLetters
foreach($compareLetters as $l) {
$addConstraints[] = $query->expr()->like(
'composition.title',
$query->createNamedParameter($l . '%')
);
}
Trying to insert the resulting array like this:
$constraints = [
$query->expr()->eq(
'composition.sys_language_uid',
$query->createNamedParameter($language, \PDO::PARAM_INT)
),
$query->expr()->orX(
implode(',', $addConstraints)
)
]
throws an exception:
Operand should contain 1 column(s)
Currently I have no idea, how to do this differently nor how to interpret the exception. Any hint would be most welcome!
Not sure whether it's a good approach, but I would try out range() function to generate the query, something like this:
<?php
$userInput = 'A-D';
list($start, $end) = explode('-', $userInput);
$selection = [];
foreach (range($start, $end) as $letter) {
$selection[] = $query->expr()->like(
'composition.title',
$query->createNamedParameter($letter . '%')
);
}
$constraints = [
$query->expr()->eq(
'composition.sys_language_uid',
$query->createNamedParameter($language, \PDO::PARAM_INT)
),
$query->expr()->orX(...$selection)
];
So more or less what you already did. Just you are not using the spread operator in your example when calling orX()
i'm new to codeigniter.
i need some help to update some rows.
here is my model:
public function change()
{
$aa = $this->input->post('id');
$bb = $this->input->post('app');
$query = $this->db->query('select kodeunit from user where email = "hehe#gmail.com"');
foreach ($query->result() as $row)
{
$kode = $row->kodeunit;
}
for($i=0;$i<sizeof($aa);$i++)
{
$data = array(
'approval' => $bb[$i]
);
$this->db->where('id_team', $aa[$i]);
$this->db->where('kodeunit', $kode);
$this->db->update('detail_tim', $data);
}
}
when i tried to update only one row, it worked with this way. but when im trying to update some rows, it doesnt change at all.
please help me to fix this problem thanks
Don't use sizeof() in your for loop, instead use $aa directly if it's an integer or count($aa) if it's an array
for($i=0;$i<count($aa);$i++)
it'll be better if you make it to other variable
$max = count($aa);
for($i=0;$i<$max;$i++)
i want to sql query like this
SELECT * FROM `tblName`
where `id`='007' and
`doj` != '2014-07-26' and
`doj` != '2014-08-04'
i want same column 'doj' used in where clause.
please help me?
Thanks in advance.
<?php
$dates = array(
'2014-07-26',
'2014-08-04'
);
$this->db->from('tblName');
$this->db->where('id','007');
// first variant
foreach($dates as $date){
$this->db->where('doj !=',$date);
}
// or second variant
$this->db->where_not_in('doj',$dates); // <- seems to be better
// finally get result
$result = $this->db->get();
if($result->num_rows()){
var_dump($result->result_array());
} else {
echo 'no rows found';
}
// check query
echo $this->db->last_query();
?>
I'm trying to run a "where" query to find a domain model object that has no association with another domain model object or if it does, that domain model object has a specific property value. Here's my code:
query = Model.where({
other == null || other.something == value
})
def list = query.list()
However, the resulting list only contains objects that match the second part of the OR statement. It contains no results that match the "other == null" part. My guess is that since it's checking a value in the associated object its forcing it to only check entries that actually have this associated object. If that is the case, how do I go about creating this query and actually having it work correctly?
You have to use a LEFT JOIN in order to look for null associations. By default Grails uses inner join which will not be joined for null results. Using withCriteria as below you should get the expected results:
import org.hibernate.criterion.CriteriaSpecification
def results = Model.withCriteria {
other(CriteriaSpecification.LEFT_JOIN){
or{
isNull 'id'
eq 'something', value
}
}
}
UPDATE
I know aliasing is not possible in DetachedCritieria where one would try to specify the join as in createCriteria/withCriteria. There is an existing defect regarding adding the functionality to DetachedCriteria. Just adding the work around for where query as mentioned in defect.
Model.where {
other {
id == null || something == value
}
}.withPopulatedQuery(null, null){ query ->
query.#criteria.subcriteriaList[0].joinType = CriteriaSpecification.LEFT_JOIN
query.list()
}
I would rather use withCriteria instead of the above hack.
this might work:
query = Model.where({
isNull( other ) || other.something == value
})
If that wouldn't work, try something like:
other.id == null || other.something == value
UPDATE:
or with good'ol criteria query:
list = Pack.withCriteria{
or{
isNull 'other'
other{ eq 'something', value }
}
}
I am using codeIgniter..
I want to update a table column is_close when id=$ticket_id of my table= tbl_tickets.
I am doing this :-
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
and I have also done this :-
$sql = "UPDATE tbl_tickets SET is_close={1} WHERE id='$title_id'";
$this->db->query($sql);
both are not working,i.e., my table is not updating the value to 1 and also no error is being shown in the broswer. :(
Edited: Included my model part :
function setClosePost($title_id){
$sql = "UPDATE tbl_tickets SET is_close=0 WHERE id='$title_id'";
$this->db->query($sql);
// $data=array(
// 'is_close'=>1
// );
// $this->db->where('id',$title_id);
// $this->db->update('tbl_tickets',$data);
}
My controller :-
function closePost(){
$this->load->model('helpdesk_model');
$this->helpdesk_model->setClosePost($this->input->post('title_id'));
}
first of all use a get method to check if ticket_id is exist or not.
another thing is always use return in your functions in models so you can check them by if(function_name){...}else{...}
then if your get method returned data correctly try
Model Method
public function set_closed($ticket_id){
$this->db->set(array(
'is_close'=>1
)); // pass fields in array
$this->db->where('id',$ticket_id);
$this->db->update('tbl_tickets'); // table name
return true;
}
then check that in your controller
if($this->Ticket_model->set_closed($ticket_id) == true){
echo 'ticket set to closed correctly';
}else{
echo 'there is some error on updating database'.$this->db->error(); // to checkout db error .
}
First, check $title_id before passing:
var_dump($title_id);
Then, try do "select a row with this id" before updating and after.
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
$data=array(
'is_close'=>1
);
$this->db->where('id',$title_id);
$this->db->update('tbl_tickets',$data);
$query = $this->db->get_where('tbl_tickets', array('id' => $id));
foreach ($query->result() as $row)
{
var_dump($row->is_close);
}
Then, give your table structure.
Just try like this
$sql = "UPDATE tbl_tickets SET is_close='1' WHERE id=".$title_id;
$this->db->query($sql);
just try like this
**function edit($close,$id) {
$sql = "UPDATE tbl_tickets SET is_close= ? WHERE id = ? ";
$this->db->query($sql, array($close,$id));
}**
To handle this type of errors, i mean if reflection is not happen in database, then use below steps to resolve this type of error.
1) use $this->db->last_query() function to print query, using this we can make sure our variable have correct value (should not null or undefined), using that we can make sure also SQL query is valid or not.
2) If SQL query is valid then open phpmyadmin & fire same query into phpmyadmin, it will return error if query columns or table names are invalid.
Use this way, its best way to cross check our SQL queries issues.
I hope it will work.
Thanks
You are trying to update integer(INT) type value, just cross check with your column datatype if that is varchar then you have to put value in a single or double quote.
Like this
$data=array('is_close'=> '1');