I am simply trying to echo or print out specific data from a DB into a string (i hope thats the right name), which should be a very simple process as I've done it before. The point is everytime a user inserts information into the database this string echo's or prints out the inserted data.
But for some very odd reason this time around when i try to echo out the data, I literally get this.
Very frustrating. As you can see from the image above i have tried using 2 different ways to do this a variable and a session, but the echo literally just prints it out. I have done this before so i am aware that it is possible. I am just a little lost into how i am meant to achieve this or even better where i went wrong. I know how to do this using a different style of coding, but i am trying to keep everything uniformed (newbie).
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo '
<p>$addon . " " . $_SESSION["Add_On_Price"]; </p>
';
My session is started and the php file is connected to the DB.
I also have error handling which has not given out any error messages.
You must do:
echo "<p>$addon ".$_SESSION["Add_On_Price"]."; </p>";
A string encapsulated into ' is rendered just as it is.
Use " to render a string that contains variables. Example:
$a = 3;
$a++;
echo "the result is $a";
will result in the result is 4.
On the other hand,
echo 'the result is $a';
gives the result is $a.
As the documentation points out:
Single quoted ¶ The simplest way to specify a string is to enclose it
in single quotes (the character ').
Doued ¶
If the string is enclosed in double-quotes ("), PHP will interpret
more escape sequences for special characters
Try not mix it..
And if within double quotes you have an associative array you may concat.
echo "string $variable". $array["index"];
or
echo "string $variable {$array["index"]}";
Then your code should look like
$addon_name = $_SESSION['Add_On_OpName'];
mysqli_report(MYSQLI_REPORT_INDEX); //overrid a common php nonsense error
$prod_sel = $dbc->query("SELECT * FROM Add_On WHERE Add_On_OpName = '$addon_name'");
$prod_sel->data_seek(0);
while ($output = $prod_sel->fetch_assoc()) {
$prod_run .= $output['Add_On_OpName'] . $output['Prod_Name'] . $output['Add_On_Price'] . $output['Add_On_Select'] . '<br>';
$addon = $output['Add_On_OpName']; //echo out product name
$_SESSION['Prod_Name'] = $output['Prod_Name']; //echo out product desc
$_SESSION['Add_On_Price'] = $output['Add_On_Price']; //echo out price
echo "<p>$addon {$_SESSION["Add_On_Price"]}; </p>'";
}
Long time ago
I never use double quotes due to it require parse the whole string for special notations. However it.
Try not mix single quotes with double quotes. pick up a standard for you code you will not notice any difference than is easy to code and read without surprises
You're mixing single quotes and double quotes. Single quotes do not perform interpolation of variables so when you write this:
echo '... whatever including " char and $ sign';
PHP will just literally print everything inside.
You forget some ' or " !
echo '<p>' . $addon . ' ' . $_SESSION["Add_On_Price"] . '</p>';
Use double quotes
echo "<p>$addon $_SESSION['Add_On_Price']; </p>";
Related
I need to be able to store and echo regular expressions. In my case the user enters the regex into a form and that exact sequence of characters needs to be echo-ed to the screen sometime later. The problem is that the echo changes the characters.
So for instance I have tried this
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
but when I echo it..
echo $regex;
I get...
((((amount\">\$)(.*?)(?=)
If I do this
$regex = htmlentities($regex);
I get this which helped with the missing part of the regex but not the multiple ((((
((((amount\">\$)(.*?)(?=</strong>
htmlspecialchars did not help either.
How do I get it to echo the variable exactly as it is written? And what would I need to do to store them in MySQL and retrieve them exactly as written?
EDIT - in response to some observations below, I add a bit more detail. This new example was done on a PHP 7.1 server in the cloud, Centos 7 rendered using Chrome.
$regex = '(?<=amount\">\$)(.*?)(?=</strong>)';
$page_elements_regex[1][0] = $regex;
$page_elements_regex[1][1] = addslashes($regex);
$page_elements_regex[1][2] = htmlspecialchars($regex);
$page_elements_regex[1][3] = htmlentities($regex);
echo "regex " . $page_elements_regex[1][0] . "<BR>";
echo "addslashes " . $page_elements_regex[1][1] . "<BR>";
echo "htmlspecialcharacters " . $page_elements_regex[1][2] . "<BR>";
echo "htmlentities " . $page_elements_regex[1][3] . "<BR>";
Results
regex ((((amount\">\$)(.*?)(?=)
addslashes ((((amount\\\">\\$)(.*?)(?=)
htmlspecialcharacters ((((amount\">\$)(.*?)(?=</strong>)
htmlentities ((((amount\">\$)(.*?)(?=</strong>)
It is also a big clue that if you take off the first ( like this
$regex = '?<=amount\">\$)(.*?)(?=</strong>)';
The result removes the first a of amount!! Is it interpreting the regex instead of echoing it?
?(((mount\">\$)(.*?)(?=)
I have solved it, and I feel a bit foolish about the answer. My bad.
Somewhere else in my code I had this
$regex[1] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[2] = '(?<=amount\">\$)(.*?)(?=</strong>)';
$regex[3] = '(?<=amount\">\$)(.*?)(?=</strong>)';
I have no idea why this gave the result it did, rather than a straight up error, but once removed it all is fine. The bottom line is that both htmlspecialcharacters and htmlentities give the right answer, Lesson learnt. Check all the code, my mistake was in the use of arrays, defining $regex as an array and a variable, not as I first thought here.
I'm trying to pass GET variables inside the URL with a bit of html inside of my PHP but can't figure out the quotes situation. I need to embed two variables inside the URL. I have one in but don't know how to embed the other. Here is the string:
echo "<a href='?id=".($id-1)."' class='button'>PREVIOUS</a>";
and here is what I need to go inside
&City=$City
Thanks for the help
Its pretty simple,
echo "<a href='?id=".($id-1)."&city=" . $City . "' class='button'>PREVIOUS</a>";
In php double quotes "" can eval variables inside them.
$test = "123;"
echo "0$test456"; // prints 0123456
In your case you better use single quote ''.
echo '<a href=\'?id=' . ($id-1) . '&City=' . $City . '\' class=\'button\'>PREVIOUS</a>';
or better
echo 'PREVIOUS';
Use something like this:
echo "<a href='?id=".$id."&City=".$city."'>";
You do need (well, it's good practice anyway) to use & for your ampersand. Otherwise it's fairly straight forward;
echo "<a href='?id=".($id-1)."&City=$City' class='button'>PREVIOUS</a>";
This is because you are using double quotes, which means you can put variables directly into the string (there are some exceptions which you might need to put in curly brackets {}).
I suggest you get a text editor with syntax highlighting, such as jEdit (other editors are available).
Hope this helps.
Maybe is it better to use the sprintf function
$id = 100;
$previousId = $id - 1;
$City = 'Amsterdam';
$link = 'PREVIOUS';
echo sprintf($link, $id, $City);
I need to get this output.
the result is"random"safdsaf
I am using this piece of code
<?php
$x = "random";
echo 'the result is' .$x. 'safdsaf';
?>
But i am getting this
the result israndomsafdsaf
I have to define random before printing it.
i.e. I do not want to change this piece of code
<?php
$x = "random";
What change should i make inside echo to get the desired output?
If you are using the same type of quotes delimit the quotes in your string like this:
echo "The result is\"" .$x. "\"safdsaf";
or simply use two sets of different quotes:
echo 'the result is"' .$x. '"safdsaf';
Output of either line of code:
The result is"random"safdsaf
Try this
Added the little bit space befor ' and added ", it will give the some out put as you want
echo 'the result is "' .$x. '" safdsaf';
the result will be
The result is "random" safdsaf
If you want to print out double quotes you can include them in single quotes
Something like this would do the trick.
$x = '"random"';
If for whatever reason you don't want to use single quotes you can also escape them like :
$x = "\"random\"";
As you want to keep the string, I suggest you change the original line where you put it in :
echo 'the result is"' .$x. '"safdsaf';
the principle stays the same
Here's some reading material : http://php.net/manual/en/language.types.string.php
You can use simply :-
echo 'the result is "' .$x. '" safdsaf';
OR you can use .
echo "the result is \" $x\" safdsaf";
Using the \ before the quote like this: the result is \"random\" safdsaf.
if you have alot of quotes and such in a string, i would suggest using the addslashes(). This method will do the work for you for you.
For more info, take a look here - http://www.w3schools.com/php/func_string_addslashes.asp
I'm building a table (tho not using <table>) and populating it with data from mysql. I have the following code to build the "table":
$NYC = // data from mysql, working fine
$NYC_min = 300;
switch($cou_mun){
case "New York":
$NYC++;
break;
//etc
}
function cityMinMet($city){
$city_min= "$city" . "_min";
if($city>$city_min){return "yes";}
else{return "no";};
}
echo "<h3>Table 6: Recruitment by target area</h3>";
echo "<ul>\n<li><span>Target Area</span><span>Number Recruited</span><span>Amount under minimum</span><span>Minimum</span><span>Minimum met?</span></li>";
echo "<li><span>12 Jurisdictions with Highest AIDS Prevalance</span><span></span><span></span><span></span><span></span></li>";
echo "<li><span>New York</span><span>$NYC</span><span>" . $NYC_min-$NYC . "</span><span>$NYC_min</span><span>"; cityMinMet('$NYC'); echo "</span></li>";
I encounter a problem with " . $NYC_min-$NYC . ": it breaks the row (the row gets interpreted as ending just before " . $NYC_min-$NYC . ". However, if I have <span>$NYC_min-$NYC</span> (not as an additional component of the echo), the value of the cell is printed as 300-500 instead of 200.
Also I'm not sure I've setup my function properly (but this is not breaking the table/row). From cityMinMet('$NYC') I want the literal string $NYC (and not its value) sent to the function. Inside the function I need to append _min to $NYC and then call $NYC_min and it return with its value.
EDIT: I changed the order of $NYC and $NYC_min in the equation.
Wrap it in parenthesis:
" . ($NYC - $NYC_min) . "
For the " . $NYC-$NYC_min . ", change to " . ($NYC-$NYC_min) . "
To pass the literal string $NYC use "\$NYC". Note, you need to use double quotes " instead of single quotes ' here, otherwise it will pass the string literal \$NYC
As #webbiedave points out in his comment, passing it as '$NYC' should already be sending the string literal $NYC
Your first problem: $NYC - $NYC_min is being interpreted as a string (subtly cast as such) because you are concatenating it with a string. You should perform the maths before the echo and store it in a variable which you write in there (clean) or you can put it in brackets so its interpreted before the php parser considers the string concatenation context.
From cityMinMet('$NYC') I want the literal string $NYC (and not its
value) sent to the function.
The way you're calling the function (with single quotes) will pass the literal string.
However, if you need the value of $NYC_min in function cityMinMet then just pass the value to the function:
function cityMinMet($city, $city_min)
{
if ($city > $city_min) {
return "yes";
} else {
return "no";
}
}
In your calling code do:
echo cityMinMet($NYC, $NYC_min);
Or you can forgo the function all together and just do:
echo ($city > $city_min) ? 'yes' : 'no';
Might be easiest to make a quick function to do what you need to do:
function calculate($nyc, $nyc_min) {
return $nyc - $nyc_min;
}
then replace what you have with:
echo "<li><span>New York</span><span>$NYC</span><span>" . calculate($NYC,$NYC_min) . "</span>...";
I'm using the following code but it gives me an error about an unexpeted " in the statement at this line, but I'm not sure how the syntax should go:
list($width,$height,$type,$attr) = getimagesize("' . $SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo'] . '");
echo "<p>This logo is ".$width; x $.height; echo "pixels in size.</p>";
PHP says the error is on that first line.
Try this
list($width,$height,$type,$attr) = getimagesize($SESSION_['html_folder'] . '/uploadedfiles/' . $row['logo']);
The error in your code was that you opened the string "twice", one with " and one with '. If you use a variable as parameter where a string is expected, you do not need to set the variable in quotes.
You should make sure that in $SESSION_['html_folder'] there's no malicious code, e.g. with
if(!is_dir($SESSION_['html_folder']))
die("ERROR");
youre adding all kinds of unnecessary quotes
list($width,$height,$type,$attr) = getimagesize($SESSION_["html_folder"] . '/uploadedfiles/' . $row['logo']);
You want:
list($width,$height,$type,$attr) = getimagesize($_SESSION["html_folder"] . '/uploadedfiles/' . $row['logo']);
The main issue was that you were using double quotes to open a string, but you didn't close the string with double quotes. The above is a better formated string(and fixed your $_SESSION variable).