Get variable with space? - php

I want to pass name in get variable. That name contain spaces. So is not working in php.
$facid=$row['Facultyname'];
echo "<a href=tutorials.php?$facid>" . $row['Facultyname']. "</a><p>";
Please help me if Faculty name =A.K. Sharma then it only send A.K.

Encode the URL:
echo '<a href="tutorials.php?variable_name='.urlencode($row['Facultyname']).'">'
. $row['Facultyname']. '</a>';
EDIT:
Set a variable name for the GET.

Looks like you're not actually setting a variable after the ?, you're just pasting the value. Try:
$facid = $row['Facultyname'];
echo "<a href='tutorials.php?facid=". urlencode($facid) ."'>". $facid ."</a>";

Related

How to print a variable in href with php?

I'm trying to pass an id in a link href and I need it to be printed in the URL, my code is:
echo " $ville_nom ";
But the id is not print in the url, could you help me with the syntax ?
You have mistake in your usage of echo and your concat was wrong.
The corrected code :
echo "<a href='ville.php?id='".$ville_id."'> $ville_nom ";
When you used the double quote it's not necessary to concat variables to show their content. Be careful: it can be dangerous sometimes.
echo "<a href='ville.php?id=$ville_id'> $ville_nom ";
Try to use this:
echo "<a href='ville.php?id=" . $ville_id . "'>"

How can i json_encode long html?

If I have a large html markup that gets populated with values from the database and gets echoed containig lots of divs that have classes:
echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";
. . .
// large markup incoming
How can I save this in a variable so I can send it back as json ? is it possible to do that ?
This is what I am trying to do:
$html = "echo "<div>";
echo"<div class='className'> {$_results['value']} </div>";
echo"</div>";"
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']);
I just don't know how to save all the html in a variable so I can send it back in an array along with other values that need to be used separately.
Using echo means that you output strings. So, if you don't need to output all strings, then concatenate them into one and assign this final string to a variable, e.g.:
$html = "<div>"
. "<div class='className'>" . $_results['value'] . "</div>"
. "</div>";
echo json_encode(array('html'=> $html, 'otherValue' => $_results['otehr']));
A simple fiddle.
I will give you what I think is a great advice.
Use a template system for this, I will recommend you mustacheJS
It will be a little difficult the first time, but you will gain a better and clear code.

Incorporating php variable into url with href attribute

I am trying to create the necessary url from this code however it is working and I am struggling to find out why.
$linkere = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linkere); ?>">'
Currently this code is producing the url: me.php?message= . But, I would like it to create the url: me.php?message=hello for example.
Thanks for helping!
You are passing $linkere to rawurlencode(). The variable is actually named $linker.
$linker = $row['message'];
echo '<a href="me.php?message=<?php echo rawurlencode($linker); ?>">'
You have alot of syntax problems here.
first, you need to use Concatenation message='.rawurlencode($linker).'"
second your variable do not exist, it should be $linker.
Second close the tag and insert the text, in this case i used Test.
$linker = $row['message'];
echo 'Test';
Can you try this,
$linker = $row['message'];
echo 'YOUR LINK TEXT HERE';
You don't need the <? ?> and echo in your echo, it should just be:
$linkere = $row['message'];
echo 'Test';
Otherwise you are turning php on and off again to echo something within an already open instance of php in which you are already echoing.

php code inside a href tag not working

I would like to redirect to other pages but the = sign isn't working, the page name, the title, and the id are coming from database. I tried putting . before the = but that doesn't work. I think this should be simple but couldn't figure it out. can someone help me out?
echo "".click here."";
Try like
echo "<a href='".$row['page']."=".$row['topic']."&id=".$row['id']."'>click here</a>";
Like this ?
echo 'click here';
You were just missing a few quotes and dots. This will work.
echo "<a href='".$row['page']."=".$row['topic']."&id=".$row['id']."'>click here</a>";

$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.

Categories