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.
Related
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.
In my php script,i am using a php variable inside an sql query.The php variable acquires its value through a post variable as follows:
$desc0=$_POST['desc0'];
$desc1=$_POST['desc1'];
$desc2=$_POST['desc2'];
$desc3=$_POST['desc3'];
$desc4=$_POST['desc4'];
$desc5=$_POST['desc5'];
$desc6=$_POST['desc6'];
$desc7=$_POST['desc7'];
$desc8=$_POST['desc8'];
$desc9=$_POST['desc9'];
The query is:
for($i=0;$i<10;$i++)
{
$q="insert into photos(name,category,description) values ('{$name{$i}}','$category','{$desc{$i}}')";
}
The problem is that on submitting the form i am getting an error which says
"undefined variable desc".
Therefore its not taking the values from the previously defined variables?
Any help?
First of, you code is completely unsafe - you should not pass user data directly into your query. There are many topics about it, and this is a good start.
Next, you don't need to store your data in such weird way. What if you'll want to pass 20 photos? In HTML, name your fields like photos[] - and in PHP, your values will be correctly parsed as an array $_POST['photos'], so you will be able to work with array:
$photos = $_POST['photos'];
foreach($photos as $photo)
{
//$photo contains certain item, so handle it with your logic
}
Finally, your issue is because of non-obvious PHP possibility for array-dereference with curly brackets. So your $desc{$i} is an attempt to access $i-th index for non-existent array $desc. Either use $desc$i or use concatenation to separate your variables.
You must change $desc{$i} to ${"desc" . $i}
I've got this problem when trying to construct a variable name
(which should output a corresponding array element.)
I start with setting the variables for testing purposes, normally the piece variable would hold the first element of an array $piece.
$wordnumber = 0;
$piece[0] = "forever";
I later echo them right before my problem to see if they're still ok.
echo "$piece[0]";
echo "$wordnumber";
The output is ok.
forever0
But then comes the problem, as I'm trying to make a function that automatically handles every single array element, so I want it to construct the next corresponding variable every time. However somehow it has no value after construction.
$name = ${'piece[' . $wordnumber . ']'};
echo "$name";
outputs nothing...
I've tried a lot of different formatting, I really don't know why I'm failing so hard here.
The code isn't part of any function right now btw.
Update:
$name = $piece[$wordnumber] solves the problem
I'm curious though why my previous formatting didn't work as expected.
Update: Question solved by André, the problem was that $piece[0] wasn't actually part of an array. So $piece was the actual variable. After storing an actual array $piece = array("Redish", "Yellow", "Green"); at start and using global $piece; in my function everything started working like a charm.
I haven’t tested the code, but I think your first approach didn’t work because your variable’s name is “piece”, not “piece[0]”. In other words, ${'piece'}[0] should work, but ${'piece[0]'} is wrong. Try add this in the very beginning of your script and PHP should display you some complaints:
error_reporting(E_ALL);
ini_set("display_errors", 1);
Try just $name = $piece[$wordnumber]; and echo $name
this will output "forever"
I have a multi-dimensional array with 4 entries in each value - (1st name, last name, email, password). I am trying to format the password value so I can insert it into a database. As you can see, my results are not what I need. Why is this the result and what should I do to get my intended result? Thanks
php > $newlines[1][3] = "PASSWORD($newlines[1][3)]";
php > echo $newlines[1][3];
PASSWORD(Array[3)]
You have a typo:
php > $newlines[1][3] = "PASSWORD($newlines[1][3)]";
^
But this not the only problem. You are accessing a multi-dimensional array and therefore, you have to put the array access into brackets {}. Otherwise, PHP would only subsitute the variable up to the first index (i.e. $newlines[1]). See also variable parsing.
And as $newlines[1][3] is most likely a string, you should also put quotation marks around it:
php > $newlines[1][3] = "PASSWORD('{$newlines[1][3]}')";
or even better in my opinion:
php > $newlines[1][3] = "PASSWORD('" . $newlines[1][3] . "')";
"PASSWORD($newlines[1][3)]"
Should be
"PASSWORD({$newlines[1][3]})"
This appears to be a quotation mark placement problem. You want the result of the function Password() rather than the string "Password(-arguments-)".
Drop the quotations around the right side:
php > $newlines[1][3] = password($newlines[1][3]);
How come when I echo $p, the variable which Im trying to fetch using this loop doesnt get displayed in the path.
$name_image2="picture.jpg";
for ($i=2; $i<=$nr_of_pics; $i++){
$img='name_image'.$i;
echo $$img; gives me this: 'picture.jpg' which is correct.
but when echoing $p like this:
$p="/SV/main/temp_images/$$img"; echo $p;
I get this: SV/main/temp_images/name_image2 --> the variable 'name_image2' doesnt get called here, why?
I want it to say: SV/main/temp_images/picture.jpg
Thanks
$p = "/SV/main/temp_images/" . $$img;
Ought to fix it.
Also, I would recommend learning how to use arrays. They are a much better way to have a set of data instead of variable variables.
Try $p="/SV/main/temp_images/{${$img}}";
When PHP is parsing the string and comes to a $, it looks at the next character to see if it makes a valid variable name. If not, it moves on. In this case, that means that the second $ is correctly interpreted, but the first one has already been passed by. The answer is to enclose the inner expression in brackets, so that it will be parsed before the outer one is.