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()");

      }

 

?>

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()");
      }

?>

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()");
      }

?>

Use urlencode() OR rawurlencode() instead of str_replace()

 

http://www.php.net/manual/en/function.urlencode.php

http://www.php.net/manual/en/function.rawurlencode.php

 

And use urldecode() or rawurldecode() on the others end.

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!

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.