While retrieving values from array without index in PHP I am getting nothing.
From my database value is stored like ["SCHOOL"] and I want to get just SCHOOL from this.
It's not treated as array instead it's treated as a string.
What will be the solution if want to treat this as array and get value of that array.
My Code is as follows :
$stream_arr = $res_tbl['streams'];
which gives result as ["SCHOOL"]
and I want just SCHOOL from this.
I am using code as $stream = $stream_arr[0];
and I get '[' this as result.
If I assign manual value to $stream_arr = ["SCHOOL"],
above code works and returns SCHOOL as expected.
But for results from my database is not working where result is same as the one I manually assigned.
To eliminate braces from the string you can use below code.
$stream_arr = '["SCHOOL"]';
$temp=explode('"',$stream_arr);
$val=$temp[1];
echo $val; //it will dispaly 'SCHOOL'.
But check the insert query(from the place you store data into db) that why it is storing with braces? if it is done by you as required then proceed else first fix that issue instead of eliminating braces in php code.
First I have to say that I am a total newbie, I have literally 0 experience with PHP.
Here is my problem. I have a sensor, that sends data into mysql database in following form:
http://192.168.1.2/add.php?i=mit=0106,22:5113/07/2016,liv=175cm,livp=000%,b=12.0V,t=36;
Now I am able to display this in my mysql table:
"mit=0106,22:5113/07/2016,liv=175cm,livp=000%,b=12.0V,t=36;"
but I would like to display each argument separately.
Do you know how to change the separator from "&" to ","?
Ideal solution would be to change the url command to this form:
http://192.168.1.2/add.php?i=mit=0106&22:5113/07/2016&liv=175cm&livp=000%&b=12.0V&t=36;
but unfortunately I can reprogramm the sensor, so I need to change the separator from "&" to ",".
arg_separator.input is the configuration directive you are looking for:
arg_separator.input string
List of separator(s) used by PHP to parse input URLs into variables.
Note:
Every character in this directive is considered as separator!
Be aware the setting mode is PHP_INI_PERDIR, meaning it "can be set in php.ini, .htaccess, httpd.conf or .user.ini (since PHP 5.3)" (http://php.net/manual/en/configuration.changes.modes.php)
If you can't change this setting, you might look at the query string as a whole (see $_SERVER variable), and split it at the comma yourself, using explode or something like that.
If I got it right, you have to store all name=value pairs from that string into separate columns in database table.
In this case you can't use explode() because of first value that contains comma.
Assuming that you already have created columns called mit, liv, livp, b and t with required data-type, and your string is double-checked before it goes to database to avoid SQL injections, you can do something like this:
# your input string
$input = "mit=0106,22:5113/07/2016,liv=175cm,livp=000%,b=12.0V,t=36;";
# loop through it and determine all name=value pairs
while (strlen($input)>2) {
preg_match ('/([a-z]+)\=(.*?)(?:,[a-z]+=|;)/', $input, $m);
# store names here
$d1[] = "'{$m[1]}'";
# and values here
$d2[] = "'{$m[2]}'";
$input = substr($input, strlen($m[1]) + strlen($m[2]) + 2);
}
# join arrays into string
$d1 = join(",", $d1);
$d2 = join(",", $d2);
# and put them into query
$sql = "INSERT INTO mytable ($d1) VALUES ($d2);";
echo $sql;
This will print out a query like this
INSERT INTO mytable ('mit','liv','livp','b','t')
VALUES ('0106,22:5113/07/2016','175cm','000%','12.0V','36');
How that while loop works?
It uses $input as argument and look for name=value followed by another name= (for inner pairs) or ; (for last pair) to determine its end. Than it extracts name into separate array d1 and value into another array called d2. Removes first match from the beginning of the string and takes another turn in loop until the $input string is empty. Course, elements into those two arrays are stored surrounded with single quotes for later use in query.
At the end, I joined both arrays (separately) with comma between elements and put them into query string called $sql.
Please note (again) that it's dangerous to send data this way directly from URI's GET parameter into database with no previous validation of data.
Not sure what you mean exactly but you can use str_replace() to help you or possibly you can convert this into an array using explode() then do what you need to do with the data and put it back into a string using implode().
edit: thanks, fixed the mixup.
echo "http://192.168.1.2/add.php?i=" . str_replace(",","&","mit=0106,22:5113/07/2016,liv=175cm,livp=000%,b=12.0V,t=36;");
So I am working with PHP to pass a PHP array over a jQuery Ajax request to another PHP page. This is quite the task. For some reason, my json_encode is returning an array instead of a string, I am not quite sure why. Here is my PHP code:
$new_spreadsheet = nl2br($_POST['spreadsheet']);
$new_spreadsheet = explode('<br />', $new_spreadsheet);
array_shift($new_spreadsheet);
$new_spreadsheet = array_values($new_spreadsheet);
echo json_encode($new_spreadsheet);
I would show you the output, but it is really long. Basically this is outputting a PHP array which consists of each row on the spreadsheet. This is what I want to have, but the problem is that I don't want the quotes and everything in the array. I am pretty sure I need to run json_decode, but when I do that my code returns an error saying that the parameter needs to be a string. I can tell something is not right, but am not quite sure what I need to change. I would appreciate any advice.
Update: At this point, when I try to loop through the array and print each value, the first array index is equal to a double quote like so: ". There are double quotes in random values throughout the area. I am not quite sure about what is causing this.
If I echo the rows from within the json_encoded PHP array onto the console, I get the following output:
"
correct value
correct value
correct value
"
You're using JSON, which means you have to adhere to somewhat more stringent syntax rules than Javascript's. e.g.
<?php
$arr = array('This' => 'is', 'an' => 'array in php');
echo json_encode($array);
?>
output:
{"This":"is","an":"array in PHP"}
There is NO way to avoid getting quotes on the values, as they're a fundamental requirement of JSON (and Javascript). If you don't want quotes, then don't use JSON.
try only br.
$new_spreadsheet = explode("<br>", $new_spreadsheet);
It will work. and json_enode can never return an array. try var_dump and check.Also make sure before you post, use htmlspecialcharacters.
Right I had to make a variable as shown below.
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
But now the code wont run when shown anywhere it shows like $quizinfo['Q1_ID'] Instead of getting the variable that the php got earlier that behind it.
I need to be done like that as there 15 questions in a quiz and the ids I held and I change the number every time to get the new ID number but it not getting them how do I fix it?
checking it I put in.
echo $quizinfo['Q1_ID'];
And it worked correctly so what do I need to do to make it work?
Try:
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
It should work.
When you write:
$questionID = '$quizinfo[\'Q' . $ques_num . '_ID\']';
$quizinfo[…] is not interpreted. It is taken as a string.
See also:
Php variables inside strings
PHP - concatenate or directly insert variables in string
PHP: variables in strings without concatenation
PHP: Beware of Variables Inside Strings
Mixing PHP variable with string literal
Since it is just php, why do you escape it?
$questionID = $quizinfo['Q' . $ques_num . '_ID'];
should do it. Also, You have ]' which should be replaced by '] at the end.
I think you are looking to use a variable for your array index:
$index = "Q{$ques_num}_ID";
echo $quizinfo[$index];
The problem you're experiencing is that you've turned the whole expression into a string with a number inside it, and so no array look-up was being performed.
As an aside: having indices that are human-readable is nice for humans, but not necessary for a computer. Unless you have a particular need to do it this way, I'd change this so it is just numerically indexed - it'll simplify your code.
My problem is that the database fields have been badly designed by previous people before me. We are accessing the table rows horizontally.
I am working with PHP and jQuery right now and I am passing data back and forth through AJAX. In PHP I am accessing this array like so:
$attendance['date_lesson_' . $lessonCount]
where lessonCount is a number from 1-10 that is incremented i.e. if lessonCount = 1 then above would be $attendance['date_lesson_1']
I am passing the attendance array through json_encode
How do I therefore access this field data_lesson_ 1 up to 10 in jQuery?
I am trying to do:
var lessonCount = 1;
attendance[i].date_lesson_+lessonCount
//do some stuff with attendance.date_lesson_
lessonCount++;
It was easy in PHP cause you concatenate strings with the dot "." but this isn't a string I'm dealing with in jQuery/JavaScript so how on earth would I do this?
Yes, I know. This is awkward. I agree 100%.
You’ll need to use bracket notation:
attendance['date_lesson_' + lessonCount]
Dot notation can only be used with identifier names as property names.
Just a guess, I don't know how that json looks like.
attendance["date_lesson_"+lessonCount]