I am writing an application that will look at a single record, obtain values from about 12 flags (0 or 1), look up those flags against a status table (in MySQL) and return a variable called $status_message which is in that table.
In this table I need to have hyperlinks (working fine) but also echo some variables, i.e.
You have no bids for {{$row->_item_name}}
or
View this item now by clicking here
Now I need item name and the other example to be translated into <?php echo $row->_item_name; ?>
I have tried a preg_replace with the following:
<?php
$find = array('/{{/', '/}}/');
$replace = array('<?php echo ', ' ?>');
echo preg_replace($find, $replace, $status_message);
?>
but this is not working.
Can anyone advise how I can get the desired result and 'echo' the variable in the MySQL field?
Had a brainwave. Much simpler,
instead of $row->_item_name I just put {{itemname}} in the string. I then use the following code:
<?php
$message_buyer = str_replace('{{itemname}}', $row->_item_name , $message_buyer);
echo $message_buyer;
?>
so no need to have <?php calls in the string at all.
Related
I select a list of names from mysqli database then display row details in display.php with if (isset($_GET['name']));
The link is
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=$str'>$str</a></td></tr>";
This executes correctly unless name contains '(apostrophe).
For instance $str (as input/click) shows as L'ECLIPSE but the <a> link only L'
The result in display.php is 'No data found for your request'
I have found exact same queries on this site but none of the answers have resolved my problem. Perhaps I am not implementing correctly.
I assume this is about escaping. But I know little about it.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?name=".urlencode($str)."'>$str</a></td></tr>";
urlencode() the string first. So you don't get this kind of problems.
Try this code.
<?php
$str = strtoupper($str);
echo "<tr><td><a href='php/display.php?
name=".htmlspecialchars($str)."'>$str</a></td></tr>";
?>
Your Single quote becomes ' ;
I hope it will help
database has php value saved like that as string
<?php $s = 'my name is'; ?>
and what am trying to do is calling the value as php code and want when type echo $s; to print my name is but now it's given my empty value see the code below to get what i mean
<?php
$b = '<?php $s = 'my name is';?>';
echo $b; //empty value
?>
Sounds like you're talking about eval() - but I'd be wary of using it. If you do, be extremely careful.
"If eval() is the answer, you're almost certainly asking the wrong question." -Rasmus Lerdorf
You'd probably need to strip the <?php and ?> tags, and watch for double quotes surrounding variables you don't want to replace:
$s=0;
eval('$s = "my name is";');
echo $s;
PHP is not recursively embeddable:
$b = '<?php $s = 'my name is';?>';
That's not PHP code in there. it's some text with the letters <, ?, p, etc...
And you ARE getting output. But you're viewing it in a browser, so the <?php ... ?> gets rendered as an unknown/illegal html tag and simply not displayed. If you'd bothered doing even the most basic of debugging, e.g. "view source", you'd have seen your PHP "code" there.
Hi I need a simple way with inline PHP to search/replace the URL string, then output the result into the HTML
Our checkout cart redirects people to our post-purchase page, and something like the following ends up in the URL bar:
http://example.com/postpurchasepage.php?amount=$9.99&firstname=Billy&lastname=Bob
More often, based on the browser, they end up with this in their URL bar:
http://example.com?amount=%249.99&firstname=Billy&lastname=Bob
(converts the $ into %24 )
Yes, the cart sends the $ symbol along inside the parameter value. No I don't want it, nor the %24. So, the challenge is to remove both the $ symbol or the %24 (whichever happens),
Right now I am trying to pull the amount value into the page using <?php echo #$_GET['amount']; ?>
For example:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
However that outputs:
<p>Hey Billy !</p>
<p>Thanks for donating %249.99</p>
I want it to output:
<p>Hey Billy !</p>
<p>Thanks for donating 9.99</p>
(or whatever the amount value is that was passed from the cart, without the $ symbol or the %24)
Like I said, basically I am looking for a way to search the URL bar for whatever I define (multiple 'or' type searching), replace with whatever I define, and output the result into the html
Any help appreciated!
You might be looking for urldecode()
echo urldecode("%24"); // $
Then you can strip off the leading $ (if any):
$string = (strpos($string)===0) ? substr($string, 1) : $string;
Manual: http://php.net/manual/en/function.urlencode.php
There are two ways (or more - depending on your creativity & scenario) to get this done;
If you have access to the value of amount before the redirecting occurs, you can strip off the "$" sign and just send in the numeric value and use it there.
Otherwise, you can get the value on the page, and then as willoller mentioned, urldecode it and you can then strip the any leading character that is not numeric. (Not sure if performance would be an issue but that's what worked for me several times before)
Here's a sample for your code:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$Get_Amount = urldecode($_GET['amount']);
// WHILE LOOP = TO VALIDATE IF VALUE IS NUMERIC
while (is_numeric($Get_Amount)==false) {
if (is_numeric(substr($Get_Amount, 0,1))){
// DO NOTHING = First Character Numeric
// Might be an issue if you have characters in between the number
// THIS WILL CAUSE RE-LOOP PROBLEM (Dependent on your scenario)
}else{
// TRIM OFF THE FIRST CHARACTER
$Get_Amount = substr($Get_Amount, 1);
}
}
?>
<p>Thanks for donating <?php echo $Get_Amount; ?></p>
Hope this helps! Good Luck!
OR -----------------------
You can use the preg_replace function like this:
<p>Hey <?php echo #$_GET['firstname']; ?> !</p>
<p>Thanks for donating <?php echo #$_GET['amount']; ?></p>
<?php
$String_Get_Amount = urldecode($_GET['amount']);
// Here you can add the characters that you wanna delete
// You should not delete anything that is not numeric cause then it would even replace the decimal point
$String_Search_For_Pattern = '/[$]/';
// Replace it with nothing
$String_To_Replace = '';
$String_Get_Amount = preg_replace($String_Search_For_Pattern, $String_To_Replace, $String_Get_Amount);
?>
<p>Thanks for donating <?php echo $String_Get_Amount; ?></p>
Not sure of a better title for this question but basically heres my code:
<?php
$v = file_get_contents('http://examplecom/index.php?=');
$popularnames = array('Gravity Falls','The Amazing World of Gumball','South Park','American Dad');
?>
Basically what im trying to do is reference the $popularnames array in different areas so like in my href for example this is what i need:
<?= $popularnames[0]; ?>
Basically what it should be doing is adding popularnames[0] (Gravity Falls) url encoded to the file_get_contents url of $v but for some reason all it does is output Gravity Falls
But my result should have gotten me http://example.com/index.php?=Gravity+Falls and then have do file_put_contents to it and its result should then have been Hello World! or whatever the page was displaying.
I cant just add $popularname[0] to the $v as im using the $v in multiple href's so doing that it will make all the hrefs one url but I want them different like:
<?= $popularnames[0]; ?>
<?= $popularnames[1]; ?>
<?= $popularnames[2]; ?>
Using this works fine doe so theres no issue with what the variable contain:
<?php $v = file_get_contents('http://example.com/index.php?q='.urlencode($popularnames[0])); echo $v; ?>
Any ideas?
From an answer below how can I make it more compact so it would preferably make me able to just do <?= $v.$v2.$popularnames[0]; ?>:
<?php
$v = 'http://example.com/index.php?q=';
$v2 = file_get_contents($v.urlencode($popularnames[0])); echo $v2;
?>
Do note that the echoed $v2 will be echoed inside a src=" "
But my result should have gotten me http://example.com/index.php?=Gravity+Falls
Then just store the URL fragment in $v:
$v = 'http://examplecom/index.php?=';
Currently you're getting the file contents of that URL fragment and storing that in $v:
$v = file_get_contents('http://examplecom/index.php?=');
If the contents of that incomplete URL are empty, then naturally $v would be empty. And concatenating an empty string with another string would result in just that other string.
(Side note: I'm not sure what you plan to do at index.php since the query string value you're sending doesn't have a key associated with it. Maybe you forgot to include the key in the URL fragment?)
So I'm basically calling and returning an entire row from a mysql table using a while loop (which is working), but I'm trying to use the data that I call inside an html link, but I can't seem to get it to work.
Ideally, eventually it will just be a list of links with each person's individual name. I can return the list fine, but I can't seem to return the list with a link.
Here is my code that I feel should be working :(
<?php
require 'db/connect.php';
$result = $con->query('SELECT distinct name FROM mytable');
while($rows = $result->fetch_assoc())
{
echo ''$rows['name']'' , "</br>";
}
?>
Any help would be greatly appreciated!
Issue might be with your string concatenation. Try following code block
echo ''.$rows['name'].'';
echo ''. $rows['name']. '' , "</br>";
You just need to use . to concatenate strings together.
try this
echo ''.$rows['name'].'' , "</br>";
Should work just fine. Basically it's '.$row['name'].'
when concatenating strings with variables you have to use dot(.) like echo "string".$var; it will be invalid to write echo "string"$var; in your example you have ignored this point.