Multiple words in Variable link

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jah008
    New Member
    • Feb 2008
    • 1

    Multiple words in Variable link

    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(l ocalhost,$usern ame,$password);
    @mysql_select_d b($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_q uery($query);

    $num=mysql_numr ows($result);

    mysql_close();

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

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


    $recent_query=m ysql_result($re sult,$i,"recent _query");

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


    THE RESULT

    www.mydomain.co m/search/tim

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

    Any clues?
  • fishctr
    New Member
    • Feb 2008
    • 8

    #2
    Originally posted by jah008
    echo "<tr><td width=200><div style=padding-left:15px;><a href=/search/$recent_query target=_self>$r ecent_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....

    Comment

    • hsriat
      Recognized Expert Top Contributor
      • Jan 2008
      • 1653

      #3
      Originally posted by jah008
      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(l ocalhost,$usern ame,$password);
      @mysql_select_d b($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_q uery($query);

      $num=mysql_numr ows($result);

      mysql_close();

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

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


      $recent_query=m ysql_result($re sult,$i,"recent _query");

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


      THE RESULT



      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($rece nt_query);[/php]But if you particularly want to convert space into '+', then do like this...[php]$filtered_link = urlencode(str_r eplace(" ", "+", $recent_query)) ;[/php]

      On the other page, you can use urldecode() to decode the encoded string.
      I hope you will get it working now... :)
      Last edited by hsriat; Feb 27 '08, 05:13 AM. Reason: changed to urlencode

      Comment

      Working...