I'm trying to query a MySQL database for items coming from a textarea. My controller's store() function has the following code:
// [...] validation is done here and assumed ok
foreach(explode("\n", Input::get('itemlist')) as $line) {
$item = preg_split('/(?<=\d) (?=[a-z])|(?<=[a-z])(?=\d)/i', $line);
if (isset($item[1])) {
echo 'Looking for ' . $item[1];
$itemObj = DB::table('items')->select('name', 'id')->where('name', '=', trim($item[1], "\r"))->first();
var_dump($itemObj);
}
}
The data received from Input::get('itemlist') is a list of items and amount, something like:
5 apples
2 oranges
1 banana
The Looking for ... part displays the item names properly. Also, the var_dump will actually show the proper result for the last thing searched for, everything else is just 'null'.
Is there something about DB:table that stops from doing queries in a loop like this? Should I do this some other way? Thanks!
EDIT Turns out there were trailing '\r' after each line except the last one so the query was for "Some Item\r" and not "Some Item". Fixed it above!
Related
I try to pass some students data through json in wordpress. The data is taken from csv file and it has a header line and 4 lines of values. I send data to php using
JSON.stringify(inp).
The data is sent without any problem and I decode it using json_decode: $csvdata=json_decode(str_replace("\\","",$_POST['body2']));
-- I use str_replace() because without it the data is not read properly -- $csvdata is ok. I send it back to my page using wp_send_json($csvdata); and I can see my data in console. However when I try to get the values of $csvdata, whatever I try, I get a result of 3 null, null, null data.
$i=0;
foreach($csvdata as $value) {
$out[$i]=$value->student;
$i++;
}
and
wp_send_json($out);
It should get the values of my data of 'student' field. I tried almost everything with no result. Every time $out returns null, null, null
I tried also $json_decode($_POST['body2'],true) to read data as an array without any luck. Also tried html_entity_decode() or str_replace() with many combinations and always no result
Any advices???
[edit] I tried to manually recreate my csvdata like this:
$data2 = json_decode('[{"student":"1","a1":"0"},{"student":"2","a1":"2"},{"student":"3","a1":"0"},{"student":"4","a1":"3"}]');//works
$a=0;
foreach($data2 as $value) {
$out1[$a]=$value->student;
$a++;`
}
wp_send_json($out1);
Now values are read properly. I notice that on the first place when I wp_send_json($csvdata) in console log there are extra ... (there dots) at the end of each line, which may cause the problem, but I cannot figure out how to solve it...
{student: 1, a1: 0, a2: 0, a3: 0, a4: 0, …}
My data sent to php
console result of $csvdata
After 3 days of attempts I found that the error was because through JSON I was passing metadata together with my data. So I needed to replace JSON.stringify(inp) with JSON.stringify(inp.data) and everything worked.
I have a simple method that passes info to a view:
Route::get('/', function () {
$thissite = DB::table('this_site')->where('id',1)->get();
$slider1 = DB::table('front_sliders')->where('active',1)->take(10)->get();
return view('index')->with('slider1',$slider1)->with('site', $thissite );
});
All the data is passed OK and the $thissite is just one record, with one of the fields being called headline.
My problem is outputting this single variable along the lines of:
<h1><strong>{{ headline }}</strong></h1>
I have tried many variations on this but I am not getting anywhere!
So headline is a column in this_site? If so then the this should work:
<h1><strong>{{ $site->headline }}</strong></h1>
This worked:
<h1><strong>{{ $site[0]->headline }}</strong></h1>
I don't know how to specify the title for the question, and that's why i cannot find the answer by my own.
I'll try to explain what I mean.
We have a database. Then, on the list of the databases there's something like category? it's unclickable and it's bold. It contains three databases preceded by underscore and each of this databases contains tables.
It looks something like:
Category
> _something1
table1, table2, table3...
> _something2
table1, table2, table3...
> _something3
table1, table2, table3...
How it's called and how can I reach the effect above?
Here, "Category" is a prefix for database names. So your databases names are
category_something1
category_something2
category_something3
They are displayed in a collapsible/expandable tree.
From what i understood from the question, You want to write a program that will show 6 rows like this:
Category: -Something 1 table1,table2,table3
-Something 2 table1,table2,table3 -Something 3 table1,table2,table3
First of all you would need to form the rows in ASC order.
Change some thing like this :
Array(Category,
Category_something1,
Category_something2_table1,
Category_something2_table2,
Category_something2_table3,
Category_something2,
Category_something3_table1,
Category_something3_table2,
Category_something3_table3,
Category_something3,
Category_something1_table1,
Category_something1_table2,
Category_something1_table3,)
into :
Array(Category,
Category_something1,
Category_something1_table1,
Category_something1_table2,
Category_something1_table3,
Category_something2,
Category_something2_table1,
Category_something2_table2,
Category_something2_table3,
Category_something3,
Category_something3_table1,
Category_something3_table2,
Category_something3_table3,)
Then Run a loop that splits each string into 3. if second value/third is not present print as heading/category else print as item/something.
While(ArrayKey is not equal to count(Array))
{
SplitString(ArrayCurValue,"_",$VarValue1,$VarValue2,$VarValue3); //split current string into 3 vars where _ is present
if($VarValue2 == "")
{
Print "<b>",$VarValue1,"</b>";
}else{
if($VarValue3 == "")
{
Print " -",$VarValue2;
}else{
Print " -",$VarValue3;
}
}
}
I'm having problems with SQL sorting results of a query from my MySQL database. I need a way to sort invoice numbers mixed with letters and a multi digit number.
Format is: ${optional-prefix}${number part}${optional-postfix} and they are all stored in Varchar(32). It is not an option to change the number format, because the values are imported from multiple systems.
What i want to sort: (unsorted)
IoCustTextNoNumber
Io-700
IO39ABC
IO-137-kk
IO-037-kk
201-ib
201
38-kk
036
12
11-KE
IO-37-kk
00001342
IO-36-kk
11-KEk
13
035
37-kk
200
Io-701
Expected result: (sorted)
11-KE
11-KEk
12
13
035
036
37-kk
38-kk
200
201
201-ib
00001342
IO-36-kk
IO-037-kk
IO-37-kk
IO-137-kk
Io-700
Io-701
IO39ABC
IoCustTextNoNumber
Can anyone help me with a solution?
MySQL is not going to do that. You can build a custom sort in something like PHP and you do a for loop and assign things to a position. Or, You can select all that begin with lo and then update all of those to put lo into another column.
In php you could do something like:
foreach($data => row){
$test = strpos('-', $row); // If this is successful than it has a dash in the string, and it goes towards the front.
if(!$test) { // If its not a test does it begin with a number.
if($row[0] >= 0){
// Do whatever you need
}
}
}
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'];
}