Jump to content

Someone please help me with fetching this row!


benji87

Recommended Posts

I cant for the life of me how to register the row link from a table as a variable in my script! This sounds really stupid but i can register it obviously be doing

$link = $row['link']

but this will only work if i put it just under the sql query. but if i try and echo it where i want it which is in the clouds tag array as a variable being passed through the url it wont register!!

 

Here is my code

<?php
function get_tag_data() { 
  include "db.php";
  $result = mysql_query("SELECT tag, link, count FROM tags"); 
  while($row = mysql_fetch_array($result)) { 
$arr[$row['tag']] = $row['count'];
$link = $row['link'];
//
// this is where i can echo links echo =$link //
//
  } 
  ksort($arr); 
  return $arr; 
}


function get_tag_cloud() {

// Default font sizes
$min_font_size = 12;
$max_font_size = 30;

// Pull in tag data
$tags = get_tag_data();

$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(); // create an array to hold tag code
foreach ($tags as $tag => $count) {
$size = $min_font_size + ($count - $minimum_count) 
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px' 
// this is where it wont register!
	. '" class="tag_cloud" href="tagprocess.php?tag=' . $link . '" title="\'' . $tag  . '\' returned a count of ' . $count . '">' 
	. htmlspecialchars(stripslashes($tag)) . '</a>';

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

?>
<h3>Sample Tag Cloud results</h3>
<div id="wrapper"
<!-- BEGIN Tag Cloud -->
<?php print get_tag_cloud(); ?>
<!-- END Tag Cloud -->
</div>

 

I know there must be a simple solution i just dont know how to solve it! Ive spent hours playing around with this. Cheers

You have to declare the variable a global to use it outside the function.

 

<?php
function get_tag_data() {
global $link;
//rest of the code
?>

 

But you're not putting the $link into the array. If you do (you have to, if you want to save a link for each tag), you won't need to declare it as a global, since you're returning the array.

I already have the link id saved in the database, that is what im trying to fetch. But it doesnt work inside the $cloud_tags[] although it does work if i try and echo it just after the sql query. Therefore something is preventing it from working inside the $cloud_tags[] or before but i dont know what. How would i put the link into the array and would that make it work?

Don't think $link will be correct anyway. The way your loop goes you overwrite $link every time the query loops so $link will end up being the last row that is fetched. You can alway store everything into a multi dimensional array.

 

function get_tag_data() { 
  include "db.php";
  $result = mysql_query("SELECT tag, link, count FROM tags"); 
  while($row = mysql_fetch_array($result)) { 
$arr[$row['tag']][$row['count']] = $row['link'];
//
// this is where i can echo links echo =$link //
//
  } 
  ksort($arr); 
  return $arr; 
}

 

Now assign your data

foreach ($tags as $tag => $countarray) {
  foreach($countarray as $count => $link){
$size = $min_font_size + ($count - $minimum_count)
	* ($max_font_size - $min_font_size) / $spread;
$cloud_tags[] = '<a style="font-size: '. floor($size) . 'px'
// this is where it wont register!
	. '" class="tag_cloud" href="tagprocess.php?tag=' . $link . '" title="\'' . $tag  . '\' returned a count of ' . $count . '">'
	. htmlspecialchars(stripslashes($tag)) . '</a>';
  }

}

 

I have not tested but should work

 

Ray

 

Didn't you read my post? "You have to declare the variable a global to use it outside the function."

 

But to solve your problem, you will need a two-dimensional array, as you're storing more than 2 values. But that will require you to edit your later code a bit, since you're doing some calculations using the array. I haven't got time to do this for you, but I hope you get the idea.

 

---

Seems craygo had the time :)

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.