cannot print url data in a required format via php - php

I need to get url data printed in the following format below
https://mysite/data/api?q="'xxxxx' in contents"
but each time i run the code below am getting something like this below which is not what I want
https://mysite/data/api?q=xxxxx in contents
here is what I have tried
$my_id = 'xxxxx';
$myurl= "https://mysite/data/api?q={$my_id}";
echo $myurl . " in contents";

If you want to print the quotes to the query parameter, you need to
Include your quote to the string, and
Encode the query in proper url encoding.
<?php
$query = http_build_query([
'q' => "\"'xxxxxxx' in contents\"",
]);
$myurl= "https://mysite/data/api?{$query}";
echo $myurl;
Which would output this:
https://mysite/data/api?q=%22%27xxxxxxx%27+in+contents%22
And is effectively this when copied and paste to your browser's location bar:
https://mysite/data/api?q="'xxxxxxx' in contents"

Try like this HTML code in your variable declaration:
<?php
$my_id = '"'xxxxx'';
$myurl= "https://mysite/data/api?q={$my_id}";
echo $myurl . " in contents" ";
?>
You will get the output like this:
https://mysite/data/api?q="'xxxxx' in contents"

Related

Php and Html code within echo

I'm writing an if statement in which a button needs to show if the cart is empty.
For this button I need to get the form key of the product for the data-url
So something like this:
Order
As mentioned above I need to wrap this button in an if statement, so something like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()){
echo 'Order';
}
else{
'<p>hello</p>';
}
?>
But obviously I can't have php echo within echo. Can anybody point me in the right direction of how to do this?
You don't put PHP inside HTML inside PHP. Since you're already in the context of PHP code, just concatenate the values you want to the output:
echo 'Order';
The resulting output is always just a string. You can simply build that string with whatever values you have.
You can just use string concatenation:
echo '<a href="#" data-url=".../' . Mage::getSingleton(...) . '"' ...
Simply don't open PHP up again. You can terminate the HTML interpretation inside an echo.
Your code should look like this:
<?php
$_helper = Mage::helper('checkout/cart');
if (1 > $_helper->getItemsCount()) {
echo 'Order';
}
else {
'<p>hello</p>';
}
?>

Run PHP code while echoing PHP

I'm using the echo command in PHP, but I want to enter PHP code like <?php echo $variable [id_login]?>, while echoing something else out, but this does not work.
Is this possible to do, and if so, how would I do it?
echo "<script>location='member.php?&id=<?php echo $taruh[id_login] ?></script>";
You cannot use echo or open/cloce php twice like you did, you might want to try something like the line below,
echo '<script>location=member.php?id=' . $taruh[id_login] . '</script>';
after echo you can write enything you'd like, even if it's a php variable, just use single quote and dot where you need it (like I do here), as you can see, echo is only used once..
For example:
<?php
$your_variable = 'some text';
$other_variable = 'some PHP code';
echo 'I wrote: ' . $your_variable . ' and ' . $other_variable . '!';
?>
Output will be:
I wrote: some text and some PHP code!
I hope this will bring you into the right direction..
EDIT
Also important: if you use query string in URLs, the first 1 can be a ? every other part after should be a & for example see the url below
http://www.example.com/index.php?id=12345&coder=yes&country=usa
before id I used a quest sign, for all others I didn't use the quest sign...

cannot get html special chars from url

I'm very new to php and can't find answer to my issue.
I try to echo a string with special characters (ie. à,é) from the URL.
URL string:
preview.php?content=<p>blà é bla</p>
Expected echoed result: blà é bla
So I do this:
$cont = $_GET['content'];
echo $cont;
Result:
bl
So, even if my page has a <meta charset="UTF-8">, I tried:
$cont = $_GET['content'];
$cont = html_entity_decode($cont, ENT_COMPAT, 'UTF-8');
echo $cont;
Same result:
bl
I tried with a header (header('Content-Type: text/html; charset=UTF-8');) at top of the page with same result.
What's weird is that is I try this:
echo html_entity_decode("<p>blà é bla</p>");
or even this:
echo "<p>blà é bla</p>";
I get the expected result:
blà é bla
So, I don't think it's a charset issue but can't understand why it works with the literal string but not with the get variable, can someone help me?
Encode your URL with: urlencode and htmlentities for example:
$url = 'content=' . urlencode('content=<p>blà é bla</p>');
echo '<a href="preview.php?' . htmlentities($url) . '">';
#JustOnUnderMillions, is correct.
E.g:
If you try to access data from URL like
http://localhost/index.php?content=%3Cp%3Eblà%20é%20bla%3C/p%3E
echo $_GET['content'];
OUTPUT: bl
But when you pass data by encoding data like
http://localhost/index.php?content=%3Cp%3Ebl%26agrave%3B%20%26eacute%3B%20bla%3C%2Fp%3E
echo $_GET['content'];
OUTPUT: blà é bla
It is better to use urlecode function for encode data in URL.
Thanks all.
This works perfectly !
Actualy, as my URL is generated with javascript, I did a encodeURIComponent on my link's href variable and it works as well.
If you want to pass a URL with parameters as a value in a URL and through a javascript function, such as.
Pass the URL value through the PHP urlencode() function twice, like this
<?php
$url = "index.php?id=4&pg=2";
$url = urlencode(urlencode($url));
echo "<a href=\"javascript:openWin('page.php?url=$url');\">";
?>
On the page being opened by the javascript function (page.php), you only need to urldecode() once, because when javascript 'touches' the URL that passes through it, it decodes the URL once itself. So, just decode it once more in your PHP script to fully undo the double-encoding.
<?php
$url = urldecode($_GET['url']);
?>
If you don't do this, you'll find that the result URL value in the target script is missing all the var=values following ? question mark.
index.php?id=4
kindly check this online tool also : URL Encode , URL Decode

$id is not going with the link

I am creating a hyperlink in foreach loop. It is working fine. When I am passing $id in URL parameter then it is not working. my link is showing http://****/test/index.php/test/view?id=**. i don't what i am doing wrong here.
foreach($list as $item)
{
$rs[]=$item['uname'];
$id=$item['uid'];
//var_dump($id); here it's printing $id value...
echo '<b> '.$item['uname'].'<br/>';
}
I want to pass $id value with hyperlink. Please suggest me.
It's of course getting printed -- your browser is just not displaying it to you since it's not being correctly parsed as HTML due to the extra " around the $id variable.
Set your header as follows:
header('Content-Type: text/plain');
and you'll see that it returns something like:
<b> FOOBAR<br/>
^ ? ^
As you can see, the issue is the extra double-quote before 55.
Change your code to:
echo '<b> <a href="/test/index.php/test/view?id=' . $id .'">'.
$item['uname'] . '</a><br/>';
Alternatively, you could also use double-quotes and enclose your variables inside {}, like so:
echo "<b> <a href=\"/test/index.php/test/view?id=$id\">{$item['uname']}
</a><br/>";
I'd use sprintf as it's cleaner.
echo sprintf('<b> %s<br/>', $id, $item['uname']);
You have another ".
Change this:
echo '<b> '.$item['uname'].'<br/>';
To this:
echo '<b> '.$item['uname'].'<br/>';
Try this:
echo "<b><a href='/test/index.php/test/view?id=$id'>$item</a></b><br/>";
This works!
And is the easiest and cleanest option. Inside of the double escaping, the simple is used for the html and through the double escaping all variables are written inside :) . Very simple.

Add PHP variable inside echo statement as href link address?

I'm trying to use a PHP variable to add a href value for a link in an echo statement.
Here's a simplified version of the code I want to use. I know that I can't just add the variable into the echo statement, but I can't seem to find an example anywhere that works.
$link_address = '#';
echo 'Link';
Try like
HTML in PHP :
echo "<a href='".$link_address."'>Link</a>";
Or even you can try like
echo "<a href='$link_address'>Link</a>";
Or you can use PHP in HTML like
PHP in HTML :
Link
you can either use
echo 'Link';
or
echo "Link';
if you use double quotes you can insert the variable into the string and it will be parsed.
Basically like this,
<?php
$link = ""; // Link goes here!
print "Link";
?>
as simple as that: echo 'Link';
You can use one and more echo statement inside href
Link
link : "/profile.php?usr=firstname&email=email"
This worked much better in my case.
HTML in PHP: Link
The safest way to generate links in PHP is to use the built-in function http_build_query(). This function is very easy to use and takes an array as an argument.
To create a dynamic link simply echo out the result of http_build_query() like so:
$data = [
'id' => $id,
'name' => $name
];
echo 'Link';
If you want to print in the tabular form with, then you can use this:
echo "<tr> <td><h3> ".$cat['id']."</h3></td><td><h3> ".$cat['title']."<h3></</td><td> <h3>".$cat['desc']."</h3></td><td><h3> ".$cat['process']."%"."<a href='taskUpdate.php' >Update</a>"."</h3></td></tr>" ;

Categories