Pgsql UPDATE in a web system with multiple entities - javascript

I want to update ta.nmbr and ta.totl to an existing indctr. How do I do it? is it possible to join?
This is the SELECT query.
$sql = "SELECT ind.indctr_code, ind.indctr_name, ctr_ind.ctr_indctr_id, ta.actl_trgt, ta.nmbr, ta.totl
FROM indctr AS ind
LEFT JOIN ctr_indctr
AS ctr_ind ON ind.indctr_code = ctr_ind.indctr_code
LEFT JOIN actl_trgt AS ta
ON ctr_ind.ctr_indctr_id = ta.ctr_indctr_id
LEFT JOIN qtr
ON ta.qtr_code = qtr.qtr_code";
This is the ralationship of the entities.
I was doing an inline editing table for this where the indctr_code and the indctr_name are already there. and the data for ta.nmbr & ta.totl will be editable in the table.

A plain UPDATE:
UPDATE actl_trgt
SET nmbr = 90
, totl = 100
WHERE actl_trgt = 1;

Related

How to update sequentially in order in different rows and insert a new one?

I don't really know how to approach this, thing is, I'm developing a web application and in a section I need to assign projects to other developers, every assignment/project will have a priority of how important it is. Priority 1 is the highest (more important), and priority 5 is lowest (less important).
What the system has to do is, when I add a new priority 1 (or any other priority), if there are other priorities and a priority 1, move the others down (P1 = P2, P2 = P3, P3 = P4) and add the new one as P1.
I made a little piece of code (making everything manually that will only work once but is just for you to see what I want)
//PHP CODE
//prioridad = "P1" from a button
$prioridad = validacion::limpiar_cadena($_POST['prioridad']);
$estado = "";
$pes = array();
//I get all the priorities from my user and save them in this array
//Saving an array of the user's priorities
while ($row = $resultado->fetch_assoc()){
$pes[] = $row["prioridad"];
}
//Replace current priorities with their new one (just once)
if (in_array($prioridad, $pes)){
if (in_array("P5", $pes)){
$estado = "lleno";
}
//Make priority 4 = priority 5 and same for all
//This user just had the first 3 priorities,so this one did nothing but the others updated succesfully just for this example
if (in_array("P4", $pes)){
$upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P5' WHERE idUsuario = 1 AND idProyecto = 32");
$upd->execute();
}
if (in_array("P3", $pes)){
$upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P4' WHERE idUsuario = 1 AND idProyecto = 2");
$upd->execute();
}
if (in_array("P2", $pes)){
$upd = $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P3' WHERE idUsuario = 1 AND idProyecto = 1");
$upd->execute();
}
if (in_array("P1", $pes)){
$upd= $conexion->prepare("UPDATE asignarproyectousuario SET prioridad = 'P2' WHERE idUsuario = 1 AND idProyecto = 3");
$upd->execute();
}
$insert = $conexion->prepare("INSERT asignarproyectousuario(idProyecto, idUsuario, prioridad) VALUES(4, 1, ?)");
$insert->bind_param("s", $prioridad);
$insert->execute();
}
I tried using arrays and adding a value of one to the current priority but I don't know how to make it work, separate the array and assign every value to a row in the database.
I also found queues that make exactly that "movement" of adding one priority and move the others in order, but I haven't found much documentation about it.
This is the example I saw:
$queue = new SplQueue();
$queue->enqueue('prioridad1');
$queue->enqueue('Prioridad2');
$queue->enqueue('Prioridad3');
$queue->unshift('prioridad1');
$queue->rewind(); // always rewind the queue/stack, so PHP can start from the beginning.
while($queue->valid()){
echo $queue->current()."\n"; // Show the first one
$queue->next(); // move the cursor to the next element
}
echo "\n"."\n"."\n";
var_dump($queue);
If you could give me an idea of how to do it or a different example would be very helpful.
Thanks in advance and if my english is not good enough I can try to explain it better.
technique is simple >
you need to know how JSON works
add an Extra column (text) in mysql Table >
store JSON array in that column Like >
[{"TaskName":"XYX","AssignDate":"AnyDate","Priority":"High","PriorityCount":"50"}]
add extra array in JSON / Update array whenever require
itarate the array that finds ("Priority":"High" AND "PriorityCount": "" // highest)
this might help > Getting max value(s) in JSON array

LEFT OUTER JOIN with CubeJs

I'm trying to achieve this query in CubeJS which does a LEFT OUTER JOIN with itself.
SELECT COUNT(DISTINCT EC."accId")
FROM public."orders" EC
LEFT OUTER JOIN public."orders" NC
ON NC."accId" = EC."accId"
AND NC."isFirstOutletTransaction" = true
AND NC."occurredAt" > '2020-02-01'
AND NC."occurredAt" < '2020-03-01'
WHERE EC."occurredAt" > '2020-02-01'
AND EC."occurredAt" < '2020-03-01'
AND EC."isFirstOutletTransaction"=false
AND NC."accId" is null;
I'm stuck on how to express this in the CubeJS schema. Would appreciate the help. Thanks
You can create one cube called e.g. "FirstOutletTransatction" with the appropriate condition:
sql: 'select * from public."orders" NC where NC."isFirstOutletTransaction" = true'
then define the other cube e.g. "NonFirstOutletTransatction" like:
sql: 'select * from public."orders" NC where NC."isFirstOutletTransaction" = false'
Define the relation of the two cubes in their joins and add "occurredAt" either as time dimension or in the query as a condition.

How to compare two sheets and delete/add any column with a distinct value in row 1? Google Script

I want to compare two sheets (based on header values in row 1) and delete any column with a unique value (without a match). For example, Assuming Sheet1, Row 1 data and Sheet 2, Row 1 are uniform, if a user adds/deletes a column within any sheet, I want to always match the number of columns in both sheets with their values
Screenshots of sheets headings.
IF both sheets looks like this
And a user adds a new Column N
Or delete column N
How can I ensure that both sheet matches by deleting the odd/distinct column in Sheet 1?
I have tried modifying this code below but I can't just get the unique one out. This code only look for headers with a defined value.
function deleteAloneColumns(){
var sheet = SpreadsheetApp.getActiveSheet();
var lastColumnPos = sheet.getLastColumn();
var headers = sheet.getRange( 1 ,1, 1, lastColumnPos ).getValues()[0];
for( var i = lastColumnPos ; i < 1; i--){
if( headers[i] === "alone" ) sheet.deleteColumn(i);
}
SpreadsheetApp.getUi().alert( 'Job done!' );
}
Any help to compare and delete the column with the unique value will be appreciated.
Problem
Balancing sheets based on header row values mismatch.
Solution
If I understood you correctly, you have a source sheet against which validation is run and two primary use cases: user adds a new column named differently than any other column (if you want to check that the column strictly matches the one in sheet1, it is easy to modify) in source sheet or deletes one that should be there.
const balanceSheets = (sourceShName = 'Sheet1',targetShName = 'Sheet2') => {
const ss = SpreadsheetApp.getActiveSpreadsheet();
const s1 = ss.getSheetByName(sourceShName);
const s2 = ss.getSheetByName(targetShName);
const s2lcol = s2.getLastColumn();
//keep all vals from source to reduce I/O
const s1DataVals = s1.getDataRange().getValues();
const s2Vals = s2.getRange(1, 1, 1, s2lcol).getValues();
const h1Vals = s1DataVals[0];
const h2Vals = s2Vals[0];
//assume s1 is source (validation) sheet
//assume s2 is target sheet that a user can edit
//case 1: target has value not present in source -> delete column in target
let colIdx = 0;
h2Vals.forEach(value => {
const isOK = h1Vals.some(val => val===value);
isOK ? colIdx++ : s2.deleteColumn(colIdx+1);
});
//case 2: target does not have values present in source -> append column from source
h1Vals.forEach((value,index) => {
const isOK = h2Vals.some(val => val===value);
!isOK && s2.insertColumnAfter(index);
const valuesToInsert = s1DataVals.map(row => [row[index]]);
const numRowsToInsert = valuesToInsert.length;
s2.getRange(1,index+1, numRowsToInsert,1).setValues(valuesToInsert);
});
};
Showcase
Here is a small demo of how it works as a macros:
Notes
Solving your problem with two forEach is suboptimal, but I kept number of I/O low (it can be lowered further by, for example, moving deleteColum out of the loop while only keeping track of column indices).
The script uses ES6 capabilities provided by V8, so please, be careful (although I would recommend migrating as soon as possible - even if you encounter bugs / inconsistencies , it is worth more than it costs.
UPD made script more flexible by moving sheet names to parameter list.
UPD2 after discussing the issue with deleteColumn() behaviour, the answer is updated to keep column pointer in bounds (for those curious about it - forEach kept incrementing the index, while deleteColumn reduced bounds for any given index).
Reference
insertColumnAfter() method reference

how to check if a word is repeated in string php [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
I have a database that have multiple fields. i've to display one of its field in in html table. i can do it easily. but the problem is that the field i want to show may contain repeated words in a row. for eg
john;mike;john
john
mike;mike;mike
now what i want to do is that i have to trim repeated words i.e if a word is more than once in a row so it should be displayed only once. e.g above will be
john;mike
john
mike
i.e i don't want to show repetition when displaying record in html table. i want to make it dynamic i.e not just for 5 to 6 word. but to condemn repetition even if there thousands of records.
considering whole value of a row as a string how could i remove this using php ..
Thanks for your support in advance..
$variable="mike;john;mike";
$each=explode(';',$variable);
//print_r($each);
$new=array_unique($each);
echo implode(';',$new); //mike;john
If you'll treat each line or row as a sentence you can split on the separator, the semicolon in your examples. So if "UD;RUP;UD;UD" is a line you do something like:
line = "UD;RUP;UD;UD";
lineArray = new Array();
lineArray=line.split(";");
Then you want to create a new array that will hold the unique values you parse from your string:
newArray = new Array();
Now you iterate over the first array and see if you already have it stored. If you don't, store it by pushing to your new array:
for(i=0;i<lineArray .length;i++)
{
if((i==lineArray .indexOf(lineArray [i]))||(lineArray .indexOf(arr[i])==lineArray .lastIndexOf(arr[i])))
newArray.push(lineArray [i]);
}
And finally join the newArray elements:
newArray.join(";");
You can try with this
<?php
$consulta=Array('azul,azul,verde,amarillo','verde','azul,verde','azul');
$arrall=Array();
foreach ($consulta as $value) {
$arrnew= explode(",", $value);
if(count($arrnew!=0)){
foreach ($arrnew as $value) {
array_push($arrall, $value);
}
}
}
$result = array_unique($arrall);
I would say, the key is to create a unique index for each record.
<?php
$records = array(
'john;mike;john',
'john',
'mike;sally;mike;mike'
);
foreach($records as $index => $record) {
$records[$index] = implode(';',array_unique(explode(';',$record)));
}
print_r($records);
?>
This is in plain JavaScript to show the concept
var records = [
'john;mike;john',
'john',
'mike;sally;mike;mike'
]
var tuple;
for(var r in records) {
var tuple = records[r].split(';');
var index = {};
for(var t in tuple) {
index[tuple[t]] = true;
}
records[r] = Object.keys(index).join(';');
}
console.log(records);
I hope this helps :)
Try this:
/**
* #param array|string $values
* #param string $delimiter
*
* #return bool|string
*/
function filterValues($values = array(), $delimiter = ';') {
if(is_string($values) && $values) {
$explodedValues = explode($delimiter, $values);
return implode($delimiter, array_unique($explodedValues));
} else if(is_array($values) && $values) {
return implode($delimiter, array_unique($values));
} else {
return false;
}
}
$string= 'mike;bike;mike';
$array = array('mike', 'bike', 'mike');
echo filterValues($string);
echo filterValues($array);

Construct a Javascript Template and inserting values into it

If anybody knows , please help me , i am very badly struck .
From AJAX Call i am constructing this data in my server (This data is fetched from the server so there can be any number of such rows data )
{data:[{one:"1",two:"2"},{one:"3",two:"3"}]}
My question is that , is it possible to construct a similar array inside javascript dynamically ??
For example , depending upon the number of rows , i want to construct a similar jaavscript array dynamically
(For example depneding on data.length , i want to construct this type dynamically
var data = {
jobs:[
{one:"1",two:"2"},
{one:"3",two:"3"}
]
};
Please help me .
This will dynamically create a list of dictionary, which is what I think you are looking for:
var row = {};
row['one'] = "1";
row['two'] = "2";
data.push(row);
row = {};
row['one'] = "3";
row['two'] = "3";
data.push(row);
// outputs [{"one":"1","two":"2"},{"one":"3","two":"3"}]
alert( JSON.stringify( data ) )

Categories

Resources