I'm working on a project with laravel and I have to ask to database some queries that have to return an array. This is the code that I have
Logged as a teacher:
$teacher= Teacher_Admin::where('id_user', Auth::user()->id)->first();
$grades = Grade::where('id_depar', $teacher->id_depar)->get(); //3 grades
$studients_grades = Studient_Grades::where('id_grade', $grades->id)->get(); //5 studient
$studients = Studient::where('id', $studients_grades->id_studient)->get(); //5 studient
$user = User::where('range', 2)->get();//5 users
What I'm trying to do is look for all the studients that are enrolled on a grade that is on the department of the teacher, e.g. Departments: IT, Chemistry. In IT department we have two Grades: Frontent Development and Backend Development. And I have 30 studient per grade. How I can get the 60 studients?
If you need more code please ask, this is my first question and I'm not sure how to do this
Thanks in advice
Finally I got it. When I start receiving get() answers, it returns a multiple array so I have to parse to something like this
$teacher= Teacher_Admin::where('id_user', Auth::user()->id)->first();
$grades = Grade::where('id_depar', $teacher->id_depar)->get();
$id_grades = []
foreach ($grades as $grade){
$id_grades = $grade->id
}
$studients_grades = Studient_Grades::whereIn('id_grade', $id_grades)->get();
I was able to work with this :) Hope to help someone someday
i'm not good enough at english and PHP. So I have a question: How to get result by 'GET' request?
i have an url like this:
laporanpemesanancetak.php?TANGGAL_PERIODE1=2016-05-01&TANGGAL_PERIODE2=2016-05-23
and my GET method like:
if (isset($_GET["$TANGGAL_PERIODE1"])) {
$TANGGAL_PERIODE1 = $_GET["TANGGAL_PERIODE1"];
$TANGGAL_PERIODE2 = $_GET["TANGGAL_PERIODE2"];
$query = mysql_query("select p.PURCHASE_ORDER_PEMESANAN,p.TANGGAL_PEMESANAN,s.NAMA_SUPPLIER,i.NAMA_BAHAN_BAKU,p.JUMLAH_PEMESANAN,i.SATUAN,k.NAMA_KARYAWAN
from pemesanan p,supplier s,inventori i,karyawan k
where p.NPWP_SUPPLIER = s.NPWP_SUPPLIER and p.ID_BAHAN_BAKU = i.ID_BAHAN_BAKU and p.NIP_KARYAWAN = k.NIP_KARYAWAN and p.TANGGAL_PEMESANAN between '$TANGGAL_PERIODE1' and '$TANGGAL_PERIODE2' order by p.PURCHASE_ORDER_PEMESANAN");
while ($row = mysql_fetch_array($query)) {
$pdf->Cell(27,8,$row["PURCHASE_ORDER_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["TANGGAL_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_SUPPLIER"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_BAHAN_BAKU"],1,0,"C");
$pdf->Cell(27,8,$row["JUMLAH_PEMESANAN"],1,0,"C");
$pdf->Cell(27,8,$row["SATUAN"],1,0,"C");
$pdf->Cell(27,8,$row["NAMA_KARYAWAN"],1,0,"C");
$pdf->Ln();
}
}
and the result is show nothing, i've tried using
if (isset($_GET["$TANGGAL_PERIODE1"])&&$_GET["$TANGGAL_PERIODE2"])
but the result is same, i've tried my sql query into mysql and it works.
Can someone help me please? it would be great :)
$_GET["$TANGGAL_PERIODE1"]
Note the $ in the key. This will try to evaluate the variable $TANGAL_PERIODE1 rather than look a key of the $_GET array named TANGAL_PERIODE1
Be sure to not ignore E_NOTICE and E_WARNING messages when developing. These are important clues, this would generate a notice of an undefined variable.
Am quite frustrated with this and could use some savvy minds.
Am building a relatively simple API.
Using PHP have created the stdClass()'s and json_encode.
On the host server the data echos perfectly.
On the client side am getting a persistent foreach invalid argument error.
$thefez= new stdClass();
$thefez->muid=$id;
$thefez->bandname=$bandname;
$thefez->core=new stdClass();
$thefez->core->joined=$since;
$thefez->core->bandbio=$bio;
$thefez->core->genre=$genre;
$thefez->core->subgenre=$subgenre;
echo json_encode($thefez);
The Result (Host)
{"muid":"IM5LGM02MFS8RJLKGY9W","bandname":"Marbles For Zen","core":
{"joined":"Sun 01 March 2015","bandbio":"Zen And Marbles","genre":"Rhythm Blues",
"subgenre":"Dixie Rhythm"}}
{"muid":"IMA3YNBKZQDNR9RBCSRI","bandname":"Frankie Storm","core":
{"joined":"Sat 21 February 2015","bandbio":"Just registered. Bio coming soon.","genre":"Popular","subgenre":""}}
ISSUE:
Using json_decode and foreach simply want to echo the items in the array.
json_decode(file_get_contents('http://api.mutrs.me/?artists'), TRUE);
foreach($result as $item){
$item->muid;
}
Host:
Checked json_last_error it returns 0
Checked json_last_error_msg it returns No Error
Client:
Checked json_last_error it returns 4
Checked json_last_error_msg it returns Syntax Error
jsondecode converts to array not an standard class object
try this
first $result = json_decode($object , true);
then for your item
$varname = $item['muid'];
Test this compared to Your Code:
<?php
$data = '{"muid":"IM5LGM02MFS8RJLKGY9W","bandname":"Marbles For Zen","core":{"joined":"Sun 01 March 2015","bandbio":"Zen And Marbles","genre":"Rhythm Blues","subgenre":"Dixie Rhythm"}}';
var_dump(json_decode($data , true));
?>
EDIT#4: json_decode is failing and returning null on a seemingly valid json string. See below for more info
I am new to JSON/JSONP and I'm running into constant trouble accessing the values in the returned JSON with PHP. I have stripped the JSONP callback without issue using code I found on this board. I am getting a JSONP result from http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=love and struggling to access the first result for the meaning. It's a quite complex result, and I need to access the first meaning (in the node "text") from the below JSON result.
http://pastebin.com/hBTeBTUL
My best attempt was:
if (isset($json->primaries[1]->entries[1]->terms[1]->text))
The above was the best I could do, I just keep getting errors trying to return that text node saying it is undefined. I'd prefer to work with objects rather than associative arrays too, if possible, so please avoid telling me to set it to return assoc array.
Any help would be greatly appreciated. I'm really stuck :P
EDIT:
$json->primaries[1]->entries[1]->terms[0]->text didn't seem to work either. Here is the complete script. Ignore the $params array as it is not used, was going to use it to generate the query.
The script has been edited since when I first posted, I had an invalid JSON object, but the error seems to be fixed as it will now parse through JSON formatters.
The error i'm getting trying to print the value out is
PHP Notice: Trying to get property of non-object in /home/outil2/Plugins/GDefine.php on line 23
EDIT#2: added json_decode which was in my original solution, but got lost in the second version
<?php
class GDefine extends Plugin {
public static $enabled = TRUE;
public function onReceivedData($data) {
if ($data["message"][0] == ".def") {
$params = array (
"callback" => "a",
"sl" => "en",
"tl" => "en",
"q" => $data["message"][1]
);
$jsonp = file_get_contents(
"http://www.google.com/dictionary/json?callback=a&sl=en&tl=en&q=" . $data["message"][1]);
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
var_dump($json);
print_r($json->primaries[1]->entries[1]->terms[0]->text);
if (isset($json->primaries[1]->entries[1]->terms[0]->text)) {
$text = $this->bold("Google Definition: ");
$text .= $this->teal($json->primaries[1]->entries[1]->terms[0]->text);
$this->privmsg($data["target"], $text);
} else {
$this->privmsg($data["target"], "error error error");
}
}
}
}
EDIT #3: this is the string I'm trying to json_decode, after using substr to remove the callback function, but am getting a NULL value returned on var_dump($json)
{"query":"love","sourceLanguage":"en","targetLanguage":"en","primaries":[{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Noun","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loves","language":"und","labels":[{"text":"plural"}]}]},{"type":"meaning","terms":[{"type":"text","text":"An intense feeling of deep affection","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"babies fill parents with intense feelings of \x3cem\x3elove\x3c/em\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e their country","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A deep romantic or sexual attachment to someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it was \x3cem\x3elove\x3c/em\x3e at first sight","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"they were both \x3cb\x3ein \x3cem\x3elove\x3c/em\x3e with\x3c/b\x3e her","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we were slowly \x3cb\x3efalling in \x3cem\x3elove\x3c/em\x3e\x3c/b\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A personified figure of \x3cem\x3elove\x3c/em\x3e, often represented as Cupid","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A great interest and pleasure in something","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"his \x3cb\x3e\x3cem\x3elove\x3c/em\x3e for\x3c/b\x3e football","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"we share a \x3cb\x3e\x3cem\x3elove\x3c/em\x3e of\x3c/b\x3e music","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Affectionate greetings conveyed to someone on one\x27s behalf","language":"en"}]},{"type":"meaning","terms":[{"type":"text","text":"A formula for ending an affectionate letter","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"take care, lots of \x3cem\x3elove\x3c/em\x3e, Judy","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A person or thing that one \x3cem\x3eloves\x3c/em\x3e","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"she was \x3cb\x3ethe \x3cem\x3elove\x3c/em\x3e of his life\x3c/b\x3e","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"their two great \x3cem\x3eloves\x3c/em\x3e are tobacco and whiskey","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"A friendly form of address","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"it\x27s all right, \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Used to express affectionate approval for someone","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"don\x27t fret, there\x27s a \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"(in tennis, squash, and some other sports) A score of zero; nil","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"\x3cem\x3elove\x3c/em\x3e fifteen","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"he was down two sets to \x3cem\x3elove\x3c/em\x3e","language":"en"}]}]}]},{"type":"headword","terms":[{"type":"text","text":"love","language":"en","labels":[{"text":"Verb","title":"Part-of-speech"}]},{"type":"phonetic","text":"/lÉv/","language":"und"},{"type":"sound","text":"http://www.gstatic.com/dictionary/static/sounds/de/0/love.mp3","language":"und"}],"entries":[{"type":"related","terms":[{"type":"text","text":"loved","language":"und","labels":[{"text":"past participle"}]},{"type":"text","text":"loves","language":"und","labels":[{"text":"3rd person singular present"}]},{"type":"text","text":"loving","language":"und","labels":[{"text":"present participle"}]},{"type":"text","text":"loved","language":"und","labels":[{"text":"past tense"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Feel a deep romantic or sexual attachment to (someone)","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"do you \x3cem\x3elove\x3c/em\x3e me?","language":"en"}]}]},{"type":"meaning","terms":[{"type":"text","text":"Like very much; find pleasure in","language":"en"}],"entries":[{"type":"example","terms":[{"type":"text","text":"I\x27d \x3cem\x3elove\x3c/em\x3e a cup of tea, thanks","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"I just \x3cem\x3elove\x3c/em\x3e dancing","language":"en"}]},{"type":"example","terms":[{"type":"text","text":"a fun-\x3cem\x3eloving\x3c/em\x3e girl","language":"en"}]}]}]}]}
I json_decode that and it returns NULL :(
You're trying to access an object that doesn't exist. Your code:
if (isset($json->primaries[1]->entries[1]->terms[1]->text)) // Doesn't exist
There's no terms[1] in entries[1] in primaries[1]. There's just 1 item; terms[0]. I think this will work for example:
if (isset($json->primaries[1]->entries[1]->terms[0]->text))
The first item in the array is indexed by 0 not 1, maybe that's your mistake.
Edit:
You also need to decode the JSON, change:
$json = substr($jsonp, 2, strlen($jsonp)-12);
to:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
Edit:
You need to escape some unescaped characters in the JSON as well. Add this to your code:
Change:
$json = json_decode(substr($jsonp, 2, strlen($jsonp)-12));
to:
$json = substr($jsonp, 2, strlen($jsonp) - 12);
$json = str_replace("\\", "\\\\", $json);
$json = json_decode($json);