By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,304 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,304 IT Pros & Developers. It's quick & easy.

Multiple words in Variable link

P: 1
I have used a mysql query to receive a variable and placed it in a link. The problem is when the variable contains two words it only displays the first in the link.

eg. if the variable contained the words 'tim tam' after the query this is what would happen

THE CODE

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT recent_query, COUNT(*) as total from recent GROUP BY recent_query ORDER BY total DESC LIMIT 5";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<table width=200 border=1><tr><td><b>Most Searched</b></td></tr>";

$i=0;
while ($i < $num) {


$recent_query=mysql_result($result,$i,"recent_quer y");

echo "<tr><td width=200><div style=padding-left:15px;><a href=/search/$recent_query target=_self>$recent_query</a></div></td>
</tr>";


THE RESULT

www.mydomain.com/search/tim

if i can get the resulting link to say www.mydomain.com/search/tim+tam things would work.

Any clues?
Feb 27 '08 #1
Share this Question
Share on Google+
2 Replies


P: 8
echo "<tr><td width=200><div style=padding-left:15px;><a href=/search/$recent_query target=_self>$recent_query</a></div></td>
</tr>";
I don't know too much about this, but if i were you, i would try putting curly braces around your variable...

.../search/{$recent_query} target...

that technique has helped me out while inserting into databases, but i'm not sure that it works for url's....
Feb 27 '08 #2

hsriat
Expert 100+
P: 1,654
I have used a mysql query to receive a variable and placed it in a link. The problem is when the variable contains two words it only displays the first in the link.

eg. if the variable contained the words 'tim tam' after the query this is what would happen

THE CODE

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT recent_query, COUNT(*) as total from recent GROUP BY recent_query ORDER BY total DESC LIMIT 5";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<table width=200 border=1><tr><td><b>Most Searched</b></td></tr>";

$i=0;
while ($i < $num) {


$recent_query=mysql_result($result,$i,"recent_quer y");

echo "<tr><td width=200><div style=padding-left:15px;><a href=/search/$recent_query target=_self>$recent_query</a></div></td>
</tr>";


THE RESULT

www.mydomain.com/search/tim

if i can get the resulting link to say www.mydomain.com/search/tim+tam things would work.

Any clues?
Space is actually an illegal character for URLs, so everything after space is being ignored in your case.

You can filter the string and make it url friendly by this...[php]$filtered_link = urlencode($recent_query);[/php]But if you particularly want to convert space into '+', then do like this...[php]$filtered_link = urlencode(str_replace(" ", "+", $recent_query));[/php]

On the other page, you can use urldecode() to decode the encoded string.
I hope you will get it working now... :)
Feb 27 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.