Im new to php and developing. please find below the code i used.
$data['ques']=$this->questions_model->displayquestionbyid($questionid);
var_dump($data);
The out put prints as
array(1) { ["ques"]=> array(1) { [0]=> object(stdClass)#19 (6) { ["questionid"]=> string(1) "1" ["votecount"]=> string(1) "0" ["username"]=> string(3) "hip" ["catogoryid"]=> string(1) "2" ["questionTitle"]=> string(4) "e2e2" ["description"]=> string(6) " deded" } } }
How do i take value of username and save it under php variable.Thank you
It's hard to tell from the output format you gave, but you probably want to do:
$data = $this->questions_model->displayquestionbyid($questionid);
$username = $data[0]->username;
$data = $this->questions_model->displayquestionbyid($questionid);
$username = $data['ques'][0]->username;
check if this works and please debug using print_r() instead.
Related
I get from my DB data in format like this:
array(12) {
[0]=>
array(4) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "78984"
["month"]=>
string(1) "6"
["hours"]=>
string(10) "10580.0833"
}
[1]=>
array(4) {
["id"]=>
string(1) "2"
["count"]=>
string(5) "64174"
["month"]=>
string(1) "6"
["hours"]=>
string(9) "6866.8333"
}
[2]=>
array(4) {
["id"]=>
string(1) "3"
["count"]=>
string(5) "31032"
["month"]=>
string(1) "6"
["hours"]=>
string(9) "3700.9167"
}
[3]=>
array(4) {
["id"]=>
string(1) "1"
["count"]=>
string(5) "91114"
["month"]=>
string(1) "7"
["hours"]=>
string(10) "11859.6000"
}
...
Each of array inside has a key: "id". It mostly look values from 1 to 3. I would like to create a new array based on this "ids" that would look like this:
array("number of unique ids") {
[0]=> "for id = 0"
array(12) {
int() count/hours
int() count/hours
int() count/hours
...
}
[1]=> "for id = 1 and so on..."
array(12){
....
}
I`ve been trying to do it like this:
$data1 = [];
$data2 = [];
$data3 = [];
foreach($data as $key => $record){
if($record['id'] == 1){
array_push($data1, $record['count']/$record['hours']);
}else if($data['id_zmiana'] == 2){
array_push($data2, $record['count']/$record['hours']);
}else{
array_push($data3, $record['count']/$record['hours']);
}
}
$raport = array_merge($data1, $data2, $data3);
And that would work, but it doesn`t look good in my opinion, beacuse what if the id change to some big number.
Yeah, having separate arrays for each ID is not a good idea. How about:
$raport = [];
foreach ($data as $record) {
$raport[$record['id']][] = $record['count'] / $record['hours']);
}
Simple, and straight to the point.
Haven't tested it. Next time try to use var_export() to export the data you put in your question. That way we can actually use it, without having to rewrite it completely.
I have a MySQL database called 'plan_test', which contains tables called (5a, 5b, 5c...). These tables have the same columns and multiple rows. I've setup an user with the permissions to do everything with the database. Here is my php code, which should return a json formated String, that I can read later on:
<?php
$servername = "localhost";
$username = "tester";
$password = "abc123";
$dbname = "plan_test";
$sql = "select * from plan_test.5a";
$con = mysqli_connect($servername,$username,$password,$dbname);
mysqli_set_charset($con,"utf-8");
$result = mysqli_query($con,$sql);
$response = array();
while($row = mysqli_fetch_array($result)){
array_push($response,array("Klasse"=>$row[0],"Tag"=>$row[4],"Monat"=>$row[5],"Stundenanfang"=>$row[7],"Stundenende"=>$row[8],"Art"=>$row[9],"Fach"=>$row[10],"Lehrer"=>$row[11],"sFach"=>$row[12],"sLehrer"=>$row[13],"Raum"=>$row[14],"Bemerkung"=>$row[15]));
}
echo json_encode(array("test"=>$response));
mysqli_close($con);
?>
I am getting a blank site out of it and when I run
var_dump($response);
I get this:
array(3) {
[0]=>
array(12) {
["Klasse"]=>
string(2) "5a"
["Tag"]=>
string(2) "19"
["Monat"]=>
string(1) "5"
["Stundenanfang"]=>
string(1) "1"
["Stundenende"]=>
string(1) "2"
["Art"]=>
string(10) "Vertretung"
["Fach"]=>
string(1) "E"
["Lehrer"]=>
string(2) "VH"
["sFach"]=>
string(1) "E"
["sLehrer"]=>
string(2) "AL"
["Raum"]=>
string(3) "113"
["Bemerkung"]=>
string(1) "�"
}
[1]=>
array(12) {
["Klasse"]=>
string(2) "5a"
["Tag"]=>
string(2) "19"
["Monat"]=>
string(1) "5"
["Stundenanfang"]=>
string(1) "3"
["Stundenende"]=>
string(1) "4"
["Art"]=>
string(10) "Vertretung"
["Fach"]=>
string(4) "SCHW"
["Lehrer"]=>
string(2) "WE"
["sFach"]=>
string(4) "SCHW"
["sLehrer"]=>
string(2) "TO"
["Raum"]=>
string(3) "AH2"
["Bemerkung"]=>
string(1) "�"
}
[2]=>
array(12) {
["Klasse"]=>
string(2) "5a"
["Tag"]=>
string(2) "19"
["Monat"]=>
string(1) "5"
["Stundenanfang"]=>
string(1) "3"
["Stundenende"]=>
string(1) "4"
["Art"]=>
string(10) "Vertretung"
["Fach"]=>
string(4) "SCHW"
["Lehrer"]=>
string(2) "RR"
["sFach"]=>
string(4) "SCHW"
["sLehrer"]=>
string(2) "WI"
["Raum"]=>
string(3) "AH1"
["Bemerkung"]=>
string(1) "�"
}
}
For some reason this array was not encoded to a json format.
How do I encode this array to a json String?
Btw that undefined String from 'Bemerkung' is meant to be there.
It was a malformed UTF-8 characters..
thx #Vatev
You are using the mysqli_fetch_array function, you can guess from his name that this function will return an PHP array ;)
You need to transform your PHP array into a json string, the json_encode
function will do the job for you.
From your php you need to add the header content-type: application/json to specify in the response returned to the client that the body is in json. With that the client browser will be able to properly read the data.
What is the value in database for column Bemerkung at the third line. I think it's an accent or something and you maybe have an encoding problem
I think , you must use mysqli_fetch_all() like this.
$servername = "localhost";
$username = "tester";
$password = "abc123";
$dbname = "plan_test";
$sql = "select * from plan_test.5a";
$con = mysqli_connect($servername,$username,$password,$dbname);
mysqli_set_charset($con,"utf-8");
$result = mysqli_query($con,$sql);
$response = mysqli_fetch_all();
echo json_encode($response);
mysqli_close($con);
I couldn't test the script , but it must solve your problem.
You never set $response to be the encoded version, you echo the encoded version of $response. That is why var_dump() shows an array.
And to output actual JSON you'll have to add the appropriate Content-Type header:
header('Content-Type: application/json');
echo json_encode(array("test"=>$response));
need some help with splitting mysql single column query array into different php variables here.
example:
here's the query, it's pretty simple to be honest.
but, i'm running out of ideas right now.
$string = "select Description from tblQuestion
where Employeeid = '$param'"
$query = $this->db->query($string);
$result = return $query->result_array();
btw, i am using Codeigniter and i tried to var_dump and the results are like this.
array(9) { [0]=> array(1) { ["Description"]=> string(5) "tidak" } [1]=> array(1) { ["Description"]=> string(5) "tidak" } [2]=> array(1) { ["Description"]=> string(5) "tidak" } [3]=> array(1) { ["Description"]=> string(5) "tidak" } [4]=> array(1) { ["Description"]=> string(5) "tidak" } [5]=> array(1) { ["Description"]=> string(5) "tidak" } [6]=> array(1) { ["Description"]=> string(5) "tidak" } [7]=> array(1) { ["Description"]=> string(5) "tidak" } [8]=> array(1) { ["Description"]=> string(5) "tidak" } }
i tried to use json_encode and the result is
[{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"},{"Description":"tidak"}]
the question is.
how do i convert this stack of arrays into different variables like this?
$var0 = "tidak";
$var1 = "tidak";
$var2 = "tidak";
$var3 = "tidak";
and on and on....
thanks in advance.
cheers!
Put the results in a foreach loop and assign the values to a dynamic variable...
sample code like,
foreach($results as $key=>$val){
$str = 'var'.$key;
$$str = $val['Description'];
}
echo $var0;
array(2) {
["success"]=>
string(1) "1"
["return"]=>
array(125) {
[0]=>
array(11) {
["marketid"]=>
string(3) "141"
["label"]=>
string(6) "42/BTC"
["primary_currency_code"]=>
string(2) "42"
["primary_currency_name"]=>
string(6) "42Coin"
["secondary_currency_code"]=>
string(3) "BTC"
["secondary_currency_name"]=>
string(7) "BitCoin"
["current_volume"]=>
string(10) "0.11628537"
["last_trade"]=>
string(12) "223.00000000"
["high_trade"]=>
string(12) "256.88999999"
["low_trade"]=>
string(12) "205.00000000"
["created"]=>
string(19) "2014-01-12 19:35:49"
}
}
I was trying to process this in PHP
$result = api_query("getmarkets");
$json = json_decode($result);
var_dump($result);
I tried many times to start processing this data but how would I start this
I was thinking something like $json['return'][0]['marketid'] would grab the market id.
Looks like it is already php object, so no need to parse as json.
try...
$result['return'][0]['marketid']
... and delete ...
$json = json_decode($result);
When I dump the variable it has a data:
var_dump($result);
object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "test#mail.com" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }
But when I use foreach no value has return except if I just echo $data; but I want to be specific to the value I want to get.
foreach($result as $data){
echo $data->use_id;
echo $data->username;
}
Why there is no returned value?
Try this : json_decode converts json string to array. Ref: http://php.net/manual/en/function.json-decode.php
$array = json_decode($result, true);
echo "<pre>";
print_r($array);
You can use foreach to $array and echo the result.
You don't need to iterate over the object. Just do this.
echo $result->user_id;
echo $result->username;
With the sample you provided you are treating each property in $result as an object and looking for user_id and user_name (which don't exist).
As you wrote:
var_dump($result);
object(stdClass)#2 (5) { ["user_id"]=> string(1) "1" ["username"]=> string(6) "user_name" ["email"]=> string(14) "test#mail.com" ["password"]=> string(32) "password" ["test"]=> string(1) "1" }
So the result is an object itself. so if you want specify and fetch exact variable, there's no need to iterate it!