Using $list['something'] Inside <p> [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
How would one use the mysql/php $list['key'] inside a php echo i.e
echo "<p> My Text . $list[''] . </p>";
Thanks In Advance

It is as simple as that:
echo "<p> My Text $list[index] </p>";
N. b.: you do not use single quotes (') for the index as you usually would, like in $a=$list['index'];, since the whole thing is already enclosed in double quotes (").
Correction:
Just found out, with indices like 'a b' you still do need the quotes! (Thanks, Jon!)
Edit: (response to comment)
That is a competely different thing! Use
list($var1,$var2) = mysql_fetch_assoc($result);
instead. The list()-construct (is it a function?!?) extracts the values out of the assigned array (in your case the result of your mysql_fetch_assoc()-function). Assuming, that your result set returns values for two columns (otherwise you will have to supply more variables in list()). And then place the variables into your text like
echo "<p> My Text $var1 and somewhere maybe also $var2 ... </p>";
Still, since you are using mysql_fetch_assoc($result) you could do
$z=mysql_fetch_assoc($result);
echo "<p> My Text $z[field1] and somewhere maybe also $z[field2] ... </p>";
with field1 and field2 being the actual column names from your MySQL table.
It is customary on this site now, to also warn you of the dangers of still using the deprecated mysql_*functions. You should change to the more secure and modern versions of mysqli_*...

In this context, usually you would just write the bare array key between the angle brackets:
echo "$array[key]";
However, if key is the empty string or if it contains any of a class of characters that have special meaning in this context then you can't do this. For example, these will not work:
echo "$array[]"; // empty string key
echo "$array[]]"; // string key: "]"
In this case you can add {} around the variable and use single quotes as if you were not inside a double quoted string context. For example:
echo "{$array['']}";
echo "{$array[']']}";
This is called complex string variable parsing; see the manual for more information.

Related

Notice: Array to String Conversion while taking form inputs using $_POST[] [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 7 years ago.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Improve this question
I have the form input like this. But it is showing
Notice: Array to string conversion in C:\xampp\htdocs\testpage\index.php on line 95
<form class='signupform' action='<?php echo htmlspecialchars('".$_SERVER."'['PHP_SELF']);?'> method='post'>
I have seen many posts around here, but could not solve it.
From your code:
<?php echo htmlspecialchars('".$_SERVER."'['PHP_SELF']);?'>
What you are doing is:
htmlspecialchars(subject)
but subject is made up of a jumble of string and variable references, so to start with the Array ($_SERVER) contains the element(s) you want to work on, which are denoted by the key in the square brackets (['PHP_SELF']). But what you have is a concatination . and a couple of quotes inbetween the two so what you are doing is
htmlspecialchars(array + quote + string )
which is clearly and obviously invalid.
So to fix it, you remove the excess quote marks and remove the concatenations between the array and it's key indicator.
htmlspecialchars('".$_SERVER['PHP_SELF'])
This is better but still not there yet, you now have to tidy up the other quotes as because your function doesn't contain any string (it's just the array variable you're working on here), you do not need any quotes in your code:
htmlspecialchars($_SERVER['PHP_SELF'])
So to wrap up a long post about a very small issue, you would correct with this replacement to your original code:
action='<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>'>
It is also worth noting that PHP_SELF can be easily abused and should not be used in this context, better to use another similar process such as $_SERVER['SCRIPT_NAME']
You must keep track of your open quotes and try and avoid mixing quotes. You should also keep track of properly closing your PHP code with ?> as your original code you forgot the > so the HTML was being interpreted as PHP by the server.
Use
<form class="signupform" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>" method="post">
or
<form class="signupform" action="" method="post">

Find all reaccurances of text inbetween two strings [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm trying to find a text between $something and $something_else and ditch it out in an array.
I would think you need preg_match to do this but I have been trying alot and still have no idea.
This should work no matter what $something and $something_else is.
You need to read the documentation of preg_match and preg_match_all.
Here's a simple example that will match whatever content inside (double quotes)..
<?php
preg_match_all('~"(.*?)"~',
'Hey there "I will be matched", because "I am inside the double quotes"',
$out, PREG_PATTERN_ORDER);
print_r(($out[0]));
OUTPUT :
Array
(
[0] => "I will be matched"
[1] => "I am inside the double quotes"
)
Correct me if i am wrong. We can use explode one string into a array. Use pre_match_all for the another string with each word of the array . In this way, it will work with any string.

Access to POST vars in php with string name [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Hi, I have a problem when try access to $_POST vars in php. I have a combo with this name "c012". Well, I send the form with this var, and I have checked this var is send ok, and when I try access with this code, where $var1, $var2 and $var3 are numbers:
$var1 = 0;
$var2 = 1;
$var3 = 2;
$pointer_combo = "c".$var1.$var2.$var3;
echo $_POST['$pointer_combo'];
Don't show anything, but if I try this:
echo $_POST['c012'];
Works, and show the value. Whats the problem with code above?
If you are using a dynamic index (index value stored in a variable), you don't need the quotes.
Try this:
echo $_POST[$pointer_combo];
PHP won't do variable substitution if the value is in single quotes. Only double quotes or no quotes. So
echo $_POST[$pointer_combo];
Would work, as would:
echo $_POST["$pointer_combo"];
(But obviously in that second example there isn't much point in the quotes being there!)
Lose the quotes:
$_POST[$pointer_combo];

How to recognize strings in php? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 8 years ago.
Improve this question
When I type:
<?php
$temp="1234";
echo "<script type='text/javascript'>document.write({$temp});</script>";
?>
I get 1234 on the screen.
But when I replace $temp="1234" with $temp="alfa" I get nothing on the screen.
What's the problem ? Where am I wrong ? "1234" and "alfa" are both strings
alert(1234) is valid javascript, and will simply pop up the integer 1234 on your screen.
alert(alfa) is attempting to pop up the contents of a variable named alfa on your screen, which doesn't exist.
If you're insert data from PHP into a Javascript context, you MUST use json_encode() to ensure that you're producing VALID javascript. e.g.
$temp = 'alfa';
$json= json_encode($temp);
echo '<script>.... {$json}...</script>';
that will produce alert('alfa') and work as expected.
You aren't enclosing the text in the double-quote character '"' which denotes a string.
document.write(1234) works because 1234 is a valid value in JavaScript (an integer), whereas alfa is a symbol name, it should be "alfa".

concatenating a number with string not parsed in PHP [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I have this simple concatenation in my PHP code that concatenates a few strings together to make an XML request. For some reason, when I concatenate the customerId with the string chain, it's not parsed. But if I pass a constant number within single quotes, it's parsed. Here is an example.
I if I concatenate with a constant number, like this:
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.'2985634';
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
Echoing $out will give me:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber>2985634</customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
But if I pass a variable instead of a constant number(which is what I passed from a from by POST method), it wont' be parsed, as shown below:
$custId = $_POST['customerId'];
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$custId;
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
The output of echo will be:
<?xml version="1.0"?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:v1="http://www.test.com/v1"><soapenv:Header/><soapenv:Body><v1:loadData><customerNumber></customerNumber><version>1</version></v1:loadData></soapenv:Body></soapenv:Envelope>
I tried different ways of concatenation and stringifying the variable, but none worked :(
Your code is working without problems , here is the result in my browser for this code:
<?php
$_POST['id']=2985634;
$out='<?xml version="1.0"?>';
$out=$out.'<soapenv:Envelope ';
$out=$out.'xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" ';
$out=$out.'xmlns:v1="http://www.test.com/v1">';
$out=$out.'<soapenv:Header/>';
$out=$out.'<soapenv:Body><v1:loadData><customerNumber>';
$out=$out.$_POST['id'];
$out=$out.'</customerNumber></v1:loadData>';
$out=$out.'</soapenv:Body>';
$out=$out.'</soapenv:Envelope>';
echo $out;
?>
After echoing XML output, use view source to check the output result instead of just viewing on the browser.
Define $custId as a string.
$custId = '2985634';
Also, instead of using $out=$out.'string';, try using $out .= 'string';

Categories