Jump to content

Need help with cleaning up urls in a tag cloud


maineyak

Recommended Posts

I accidentally posted this in the wrong forum, I guess this is where I should have posted it.

-----------------------------------------

 

Below is a code for a tag cloud, I would like to add a + to replace the spaces in the urls created, but cannot figure out how to do it. I'm no coder, but I can usually figure things out by googling and other methods, but this one has had me stumped for 2 days!

 

So blue widget would have an url like http://www.domain.com/blue+widget rather than http://www.domain.com/blue widget

 

I will be forever indebted if you can help me lol.

 

<?php

$CFG['db_host'] = 'localhost';

$CFG['db_user'] = 'USER';

$CFG['db_pass'] = 'PASSWORD';

$CFG['db_name'] = 'DB';

 

mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error());

mysql_select_db($CFG['db_name']);

 

function tag_info() {

  $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40");

  while($row = mysql_fetch_array($result)) {

    $arr[$row['tag_name']] = $row['count'];

  }

  //ksort($arr);

  return $arr;

}

 

function tag_cloud() {

    $min_size = 14;

    $max_size = 22;

 

    $tags = tag_info();

 

    $minimum_count = min(array_values($tags));

    $maximum_count = max(array_values($tags));

    $spread = $maximum_count - $minimum_count;

 

    if($spread == 0) {

        $spread = 1;

    }

 

    $cloud_html = '';

    $cloud_tags = array();

 

$step = ($max_size - $min_size)/($spread);

 

 

    foreach ($tags as $tag => $count) {

        $size = $min_size + ($count - $minimum_count)

            * $step;

 

 

//  $size = ($max_size + $min_size)/$spread;

 

        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'

            . '" class="tag_cloud" href="/search-' . $tag .'-blended'

            . '" title="' . $count . ' searches for \'' . $tag  . '\'">'

            . htmlspecialchars(stripslashes($tag)) . '</a>';

    }

 

 

 

    $cloud_html = join("\n", $cloud_tags) . "\n";

return $cloud_html;

}

 

?>

 

<?php

 

if (!eregi('[.@\"\'!/:%]', $_GET['k']))

if (mysql_affected_rows()){

        mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\"");

  }

      if (!mysql_affected_rows()){

        mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()");

      }

 

?>

Link to comment
Share on other sites

well you can simply replace the space in your url with a + sign

 

using str_replace

 

 

<?php

$url = "http://www.domain.com/blue widget";

$url = str_replace(" ","+",$url);

echo $url; // http://www.domain.com/blue+widget

?>

Link to comment
Share on other sites

He's right, str_replace() will work, but seeing as you're "not a coder," I'm guessing you wouldn't know how to intergrate that.

 

This *should* work.  ;)

 

<?php
$CFG['db_host'] = 'localhost';
$CFG['db_user'] = 'USER';
$CFG['db_pass'] = 'PASSWORD';
$CFG['db_name'] = 'DB';

mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error());
mysql_select_db($CFG['db_name']);

function tag_info() { 
  $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40"); 
  while($row = mysql_fetch_array($result)) { 
    $arr[$row['tag_name']] = $row['count'];
  } 
  //ksort($arr); 
  return $arr; 
}

function tag_cloud() {
    $min_size = 14;
    $max_size = 22;

    $tags = tag_info();

    $minimum_count = min(array_values($tags));
    $maximum_count = max(array_values($tags));
    $spread = $maximum_count - $minimum_count;

    if($spread == 0) {
        $spread = 1;
    }

    $cloud_html = '';
    $cloud_tags = array();

   $step = ($max_size - $min_size)/($spread);


    foreach ($tags as $tag => $count) {
        $size = $min_size + ($count - $minimum_count) 
            * $step;


//  $size = ($max_size + $min_size)/$spread;

$theurl = htmlspecialchars(stripslashes($tag));

$theurl = str_replace(" ", "+", $theurl);

        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
            . '" class="tag_cloud" href="/search-' . $tag .'-blended'
            . '" title="' . $count . ' searches for \'' . $tag  . '\'">' 
            . $theurl . '</a>';
    }
   


    $cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
}

?>

<?php

if (!eregi('[.@\"\'!/:%]', $_GET['k'])) 
if (mysql_affected_rows()){
        mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\"");
  }
      if (!mysql_affected_rows()){
        mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()");
      }

?>

Link to comment
Share on other sites

This returns a blank page, what would be causing that?

 

He's right, str_replace() will work, but seeing as you're "not a coder," I'm guessing you wouldn't know how to intergrate that.

 

This *should* work.  ;)

 

<?php
$CFG['db_host'] = 'localhost';
$CFG['db_user'] = 'USER';
$CFG['db_pass'] = 'PASSWORD';
$CFG['db_name'] = 'DB';

mysql_connect($CFG['db_host'], $CFG['db_user'], $CFG['db_pass']) or die(mysql_error());
mysql_select_db($CFG['db_name']);

function tag_info() { 
  $result = mysql_query("SELECT * FROM tags GROUP BY tag_name ORDER BY RAND() DESC LIMIT 40"); 
  while($row = mysql_fetch_array($result)) { 
    $arr[$row['tag_name']] = $row['count'];
  } 
  //ksort($arr); 
  return $arr; 
}

function tag_cloud() {
    $min_size = 14;
    $max_size = 22;

    $tags = tag_info();

    $minimum_count = min(array_values($tags));
    $maximum_count = max(array_values($tags));
    $spread = $maximum_count - $minimum_count;

    if($spread == 0) {
        $spread = 1;
    }

    $cloud_html = '';
    $cloud_tags = array();

   $step = ($max_size - $min_size)/($spread);


    foreach ($tags as $tag => $count) {
        $size = $min_size + ($count - $minimum_count) 
            * $step;


//  $size = ($max_size + $min_size)/$spread;

$theurl = htmlspecialchars(stripslashes($tag));

$theurl = str_replace(" ", "+", $theurl);

        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
            . '" class="tag_cloud" href="/search-' . $tag .'-blended'
            . '" title="' . $count . ' searches for \'' . $tag  . '\'">' 
            . $theurl . '</a>';
    }
   


    $cloud_html = join("\n", $cloud_tags) . "\n";
return $cloud_html;
}

?>

<?php

if (!eregi('[.@\"\'!/:%]', $_GET['k'])) 
if (mysql_affected_rows()){
        mysql_query("UPDATE tags SET count=count+1 WHERE tag_name=\"".$_GET["k"]."\"");
  }
      if (!mysql_affected_rows()){
        mysql_query("INSERT INTO tags SET tag_name=\"".$_GET["k"]."\", count=1, search_date=NOW()");
      }

?>

Link to comment
Share on other sites

I just played around with a bit more, and this is what worked:

 

$tag = htmlspecialchars(stripslashes($tag));
$tags = htmlspecialchars(stripslashes($tag));


$tag = str_replace(" ", "+", $tag);
        $cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
            . '" class="tag_cloud" href="/search-' . rawurlencode($tag) .'-blended'
            . '" title="' . $count . ' searches for \'' . $tag  . '\'">' 
            . $tags . '</a>';
    }

 

Thanks for the help!

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.