SQR replace by position rather than multiple IF LET ENDIF - sqr

In translating codes from one database to the next, I have to use an IF LET ENDIF for each code.
IF $CODE = 'A11'
LET $CODE = 'AAA'
END-IF
IF $CODE = 'B11'
LET $CODE = 'BBB'
END-IF
IF $CODE = 'C11'
LET $CODE = 'CCC'
END-IF. . .ad nauseum
Is there a way in SQR to do a positional replace?
REPLACE ('A11', 'B11', 'C11') IN ('AAA', 'BBB', 'CCC')
Thanks,
David

Unfortunately, both the REPLACE and TRANSLATE sub-commands of the LET Statement only allow a single transformation.
As an alternative to multiple if-then-else's, you could use the Evaluate statement:
Evaluate $Code
When = 'A11'
Let $Code = 'AAA'
When = 'B11'
Let $Code = 'BBB'
When-Other
End-Evaluate
This would make the code easier to review and less verbose.

Related

Codeigniter update_batch in Core PHP

everyone.
In the codeigniter there is update_batch function by using it we are able to bulk update multiple rows.
$this->db->update_batch('table_name', $update_data_array, 'where_condition_field_name');
I want similar functionality in core PHP in one function or in one file. Is there any workaround?
Is there any way to extract update_batch function from Codeigniter in one file/function?
I tried to extract this function but it is very lengthy process and there will be many files / functions should be extracted.
Please help me in this regard
Thanks in advance
You can also insert multiple rows into a database table with a single insert query in core php.
(1)One Way
<?php
$mysqli = new mysqli("localhost", "root", "", "newdb");
if ($mysqli == = false) {
die("ERROR: Could not connect. ".$mysqli->connect_error);
}
$sql = "INSERT INTO mytable (first_name, last_name, age)
VALUES('raj', 'sharma', '15'),
('kapil', 'verma', '42'),
('monty', 'singh', '29'),
('arjun', 'patel', '32') ";
if ($mysqli->query($sql) == = true)
{
echo "Records inserted successfully.";
}
else
{
echo "ERROR: Could not able to execute $sql. "
.$mysqli->error;
}
$mysqli->close();
? >
(2)Second Way
Let's assume $column1 and $column2 are arrays with same size posted by html form.
You can create your sql query like this:-
<?php
$query = 'INSERT INTO TABLE (`column1`, `column2`) VALUES ';
$query_parts = array();
for($x=0; $x<count($column1); $x++){
$query_parts[] = "('" . $column1[$x] . "', '" . $column2[$x] . "')";
}
echo $query .= implode(',', $query_parts);
?>
You can easily construct similar type of query using PHP.
Lets use array containing key value pair and implode statement to generate query.
Here’s the snippet.
<?php
$coupons = array(
1 => 'val1',
2 => 'va2',
3 => 'val3',
);
$data = array();
foreach ($coupons AS $key => $value) {
$data[] = "($key, '$value')";
}
$query = "INSERT INTO `tbl_update` (id, val) VALUES " . implode(', ', $data) . " ON DUPLICATE KEY UPDATE val = VALUES(val)";
$this->db->query($query);
?>
Alternatively, you can use CASE construct in UPDATE statement. Then the query would be something
like:
UPDATE tbl_coupons
SET code = (CASE id WHEN 1 THEN 'ABCDE'
WHEN 2 THEN 'GHIJK'
WHEN 3 THEN 'EFGHI'
END)
WHERE id IN(1, 2 ,3);

Adding Multiple Variables to a View in Laravel

$header = DB::select("SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tableOne'");
$secheader = DB::select("SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tableTwo'");
$variables = DB::table('tableThird')->get();
$variables = json_decode(json_encode($variables, true));
$tbHeading = json_decode(json_encode($header, true)); //json object
return view('admin/crosstabdata', compact('secheader','tbHeading','variables'));
When I print all three variables to view (crosstabdata.blade.php) file, it said:
secheader variable does not exist.
Your code should work. Maybe the problem is in the view.
As an alternative, you can pass varaibles to your views like this:
$secheader = /** ... */;
$variables = /** ... */;
$tbHeading = /** ... */;
return view('admin.crosstabdata')
->with('secheader', $secheader)
->with('variables', $variables)
->with('tbHeading', $tbHeading);
Then in your view you can access them like $secheader, $variables & $tbHeading.
try this way:
return View::make('admin.crosstabdata')
->with(compact('secheader', 'tbHeading', 'variables'));
Try this;
$header = DB::select("SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tableOne'");
$data['secheader'] = DB::select("SELECT COLUMN_NAME
FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME = 'tableTwo'");
$variables = DB::table('tableThird')->get();
$data['variables'] = json_decode(json_encode($variables, true));
$data['tbHeading'] = json_decode(json_encode($header, true));
return view('admin/crosstabdata', $data);
Hope this approach helps out.
You can pass an associative array to the with() method, so your return statement can look like this:
return view('admin/crosstabdata')->with(['secheader' => $secheader, 'tbHeading' => $tbHeading, ...]);
i didn't try to pass three vatiables directly but here is a way to pass two variables
return view('admin/crosstabdata', compact('secheader'))->with('tbHeading', $tbHeading);
also i saw this question in stack over flow i think it might help you its about making it as an array and passing it as one variable see here
Laravel - Pass more than one variable to view

PHPExcel - read comment from a cell?

Sorry, for my bad english.
I have written the following code.
foreach ($sheetData as $theData)
if ($theData['A'] != "ID")
{
$id = $theData['A'];
$vorname = $theData['B'];
$nachname = $theData['C'];
$bew_foto = $theData['D'];
$emailadr = $theData['E'];
$sql = "INSERT INTO datensatz (
id,
vorname,
nachname,
emailadr)
VALUES (
$id,
'$vorname',
'$nachname',
'$emailadr')";
$ergebnis = $mysqli->query($sql);
echo "Datensatz erfolgreich eingetragen!<br>";{
}
}
How can I make it, that I can read a comment in (to example) column B and write it in a database or give it out with an echo? I will read the comments in all rows 'nachname' (C). How can I make this?
To read a comment in cell A2, it's as easy as
$objPHPExcel->getActiveSheet()
->getComment('A2')
->getText();

Laravel4 raw statement in the middle of complex where

I have a quite complex search method which handles the $input array from a controller, the thing is that I want to perform a custom SQL statement in the middle of it, for example:
$input['myField'] = array('condition' => 'rawStatement', value => 'AND WHERE LEFT(field,9) = 10`
and that would apply into my busy-conditions-method-builder
You can see the method at
http://pastebin.com/BNUKk2Xd
I'm trying to apply it on lines 52-54 but cant seem to get it working.
I know this is an old question but looking at your pastbin you have to chain your query like.
$query = User::join('user_personal','users.id','=','user_personal.user_id');
# Join user askings
$query = $query->leftJoin('user_askings','users.id','=','user_askings.user_id');
$query = ..........
$query = $query->orderBy('users.profile_score','DESC');
$query = $query->groupBy('users.id')->paginate(32);
return $query;

why is this not possible in doctrine

I have to do some queries, while i was trying different ways, i found out that the next lines are not "recognized" by doctrine (it gives errors):
for example, when i want to compare if some data in the db is equal to a literal, here the condition:
('u.gender = M')
this is how my table look like:
id gender
1 M
2 M
3 F
it throws a semantical error. Also when comparing dates that way.
I would like to know why this is not recognized by doctrine, while comparing directly with numbers is accepted:
condition: ('u.age = 15')
First option you can do this way-
$M = 'M';
$age = 15;
$query = $this->createQueryBuilder('t')
->where('u.gender = :M AND u.age = :age')
->setParameters(array('M'=> $M,'age'=>$age);
another way to do this-
$query = $this->createQueryBuilder("t")
->where("u.gender = 'M' AND u.age = 15");
So i guess the answer to my question would be that it was not working because doctrine didn't recognize M as a string. That is why it was necessary to use inverted commas like #Mehedi said.
Another way of solving this was to use the query builder:
$v = 'M';
$condition = $this->qb->expr()->eq('u.gender', $this->qb->expr()->literal($v));
but i guess that is just long and hard to read. So the shortest thing would be just:
$condition = ("u.gender = 'M'");

Resources