How to look ahead in a mysql result - mysql

I have an array containing the results of a mysql query on "Category", "Group" and "Subgroup" for stock items. I need to build a dynamic menu but the content of the current line of the array I am building depends on a column in the next line.
I have looked up and found many examples but all of them check the whole line of the array (the mysql row) I only want to check one item in the line of the array (one mysql column). I need something like
for ( $ix = 0; $ix < (length(current_result) - 1); $ix++) {
if (current_result[$ix, Catagory] = current_result[$ix + 1, Catagory]) {
echo some code
} else {
echo some other code
}
}
some code to handle the last line of the array

Assuming you have an associative array of rows, you're simply missing proper array syntax:
for( .... ){
$thiscat = $current_result[$ix]['Category'];
$nextcat = $current_result[$ix+1]['Category'];
}

Related

Why does encoding an array into json format result in an json object?

This is strange, I've used json encoding arrays and the output is something like [{" etc, but in another code this time the output is "{"1":{".. causing multiple errors. I don't understand what is going on.
this is the part of the code:
$json_arr = json_decode($json_str, true);
$fecha = date("Y-m-d H:i:s");
foreach (array_column($json_arr, 'f') AS $k => $fecha) {
if($fecha < $ahora){
unset($json_arr[$k]);
}
}
$json_str = json_encode($json_arr,true);//this will be inserted in the DB table
but the $json_str is in the form of "{"1":{".. but I need in the form of [{".
Here some of the images when debugging:
in orange, the json_str is readed from the BD table
after decoding, you see the json_arr is an array of three elements
after deleting some row, you see json_arr is still an array
after encoding the json_arr, I get the "{"1":{" format, in other cases of encoding arrays I had the [{" format, which is what I need.
You are starting with an array of 3 arrays, with the indices of 0, 1, and 2.
Then you are deleting the first one. If you compare your 2nd and 3rd screenshots, specifically the popup portion, you'll see that what you have lost is index 0. Your array now starts with index 1.
But in JavaScript, an array can't start with index 1. It has to start with index 0, so PHP is encoding it as an object, instead of an array.
If you use the PHP function array_values() it will re-index your array and you should be good to go.

Using ELK, I'm unsure how to use right filter in logstash config file for pairs

I have a dollarVars term filled with things in this format...
Message 1:
Stuff1,Header1|Value1|Header2|Value2|Header3|Value3|
Message 2:
Stuff1,Header2|Value2|Header4|Value4|Header5|Value5|
Message 3:
Stuff1,Header3|Value3|Header4|Value4|Header5|Value5|
Etc.
My current filter looks like:
filter {
dissect {
mapping => {
"message" => "%{stuff1},%{dollarVars}"
}
}
mutate {
convert => {
"stuff1"=>"integer"
}
}
date{
match => ["dateTime", "yyyy/MM/dd HH:mm:ss"]
}
}
What can I do to take my dissected dollarVars and break it up into the header with its respective value (the one to the right of each header)?
I suspect this would use the csv filter somehow using "|" instead of "," and somehow telling it each dollarVar odd column is a header and each even column goes with its respective (n-1) odd column.
I think you're going to want to split that dollarVars using the split function within mutate.
From there, I'd break out into ruby via the ruby filter to loop through the resultant split array and build out the new fields:
ruby {
code => "
i = 0
while i < dollarVars.count
event.set(dollarVars[i++]) = dollarVars[i++]
end
"
}
Here is what fixed what I was trying to do. No mutate needed, just this ruby code.
ruby {
code => "
i = 0
vars = event.get('dollarVars')
v_array = vars.split('|')
while i < v_array.count
event.set(v_array[i],v_array[i+1])
i += 2
end
"
}

Get values from multidimensional array | JSON

I am trying to access the values of this array in PHP. It's a multidimensional array.
I need to get values from the array and insert it in the DB.
Inserting is the second part of the problem. First parts is getting the values from it.
JSON -
{
"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}]}
Here the "itempicture" is the table name and all the keys, i.e 'itemcode', 'itemimage, etc are the SQL columns'".
I need to get the values of the SQL columns and insert it into the DB.
So far i have tried this -
$data = file_get_contents($url);
$json_array = (array)(json_decode($data));
print_r($json_array);
foreach($user->itempicture as $mydata)
{
echo $mydata->itempicture . "\n";
foreach($mydata->itempicture as $values)
{
echo $values->itempicture . "\n";
}
}
Using MYSQL in Object-oriented method to insert it in DB, with a simple query like "INSERT INTO table_name
VALUES (value1, value2, value3, ...)"
So table name will be the "itempicture" present in the array and values will be the values of the keys in the array.
This may be of help. Instead of looping through the properties with a foreach and printing, you'll want to use them to build an array to use as parameters for a prepared query, or build the query directly. But this shows you how to access those properties -
<?php
$j='{
"itempicture":[
{
"status":"3"
},
{
"ItemCode":"001",
"ItemImage":"image1",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
},
{
"ItemCode":"002",
"ItemImage":"image2",
"ItemCategory":"shirt",
"ShowOnPOS":"Yes",
"LastModifiedOn":"2018-06-02 11:53:57"
}]}';
$jo=json_decode($j);
print("status ".$jo->itempicture[0]->status."\n");
for($i=1;$i<count($jo->itempicture);$i++){
foreach($jo->itempicture[$i] as $prop=>$val){
print($prop." = ".$val."\n");
}
}
?>
There are some issues I have seen in your code:
You cast the result of json_decode($data) to an array, an then later you try to use property-access to access the "itempicture" property. I have removed this cast.
In the first foreach use the variable $user, I guess you meant to use $json_array.
The inner loop is not really used, you can access the properties directly
After having fixed this issues the code looks like that and works with your given JSON. I have just echo-ed the values since you have not told us which DB-abstraction you use, but you can use the values to populate the database.
$data = file_get_contents($url);
$json_array = (json_decode($data));
print_r($json_array);
foreach($json_array->itempicture as $mydata)
{
if (!property_exists($mydata, 'ItemCode')) {
// Ignore entries which are invalid (have no ItemCode property)
continue;
}
echo implode(' - ', [
$mydata->ItemCode,
$mydata->ItemImage,
$mydata->ItemCategory,
$mydata->ShowOnPOS,
$mydata->LastModifiedOn,
]), PHP_EOL;
}

Append value to JSON decode array parameter stored in MySQL

Im trying to work out how to append a zero to a specific JSON decoded array value for multiple records stored in a MySQL table according to some conditions.
for example, for table 'menu', column 'params'(text) have records containing JSON decoded arrays of this format:
{"categories":["190"],"singleCatOrdering":"","menu-anchor_title":""}
and column 'id' has a numeric value of 90.
my goal is to add a zero to 'categories' value in menu.params whenever (for example) menu.id is under 100.
for this records the result being
{"categories":["1900"],"singleCatOrdering":"","menu-anchor_title":""}
so im looking for a SQL Query that will search and find the occurrences of "categories": ["999"] in the Database and update the record by adding a zero to the end of the value.
this answer is partially helpful by offering to use mysql-udf-regexp but its referring to REPLACE a value and not UPDATE it.
perhaps the REGEXP_REPLACE? function will do the trick. i have never used this library and am not familiar with it, perhaps there is an easier way to achieve what i need ?
Thanks
If I understand your question correctly, you want code that does something like this:
var data = {
"menu": {
"id": 90,
"params": {
"categories": ["190"],
"singleCatOrdering": "",
"menu-anchor_title": ""
}
}
};
var keys = Object.keys(data);
var columns;
for (var ii = 0, key; key = keys[ii]; ii++) {
value = data[key];
if (value.id < 100) {
value.params.categories[0] += "0";
alert(value.params.categories[0]);
}
}
jsFiddle
However, I am not using a regular expression at all. Perhaps if you reword the question, the necessity of a regex will become clearer.

Compare data against sql column

I have collection of numbers like: 11111, 12345, 12346 stored in a list in c# code. I need to compare this list against sql database column of numbers similar to this and find out if matching numbers exist. Below is what i am doing:
foreach (number in numbers)
{
//get column data through sql reader and iterate through it:
foreach(column in columnData)
{
if(number == column)
{
// do something
}
}
my question is this right approach? Or is there a better way to do this? As it looks like this requires lots of processing.
I would so something like this..
var matches = columnData.Where(z=> numbers.Contains(z=>z.columnData)).ToList();
or
var matches = columnData.Select(z=> z.columnData).Intersect(numbers);