tags:

views:

53

answers:

4

I'm trying to turn some query results into click-able links through PHP. I'm a beginner and don't really know much. I'm working with Wordpress. Here's what I'm shooting for: http://www.celebrything.com/

The right side bar is display the count results. I'd like the celebrity names to link to search links for each name. so the first should link to http://www.celebrything.com/?s=%22Tiger+Woods%22&search=Search

Here's the PHP I'm using to display my current results:

<?php
    global $wpdb;
    $result = $wpdb->get_results('SELECT name, count FROM wp_celebcount');

    foreach($result as $row) {
        echo ''.$row->name.' - '.$row->count.' Posts <br/>';
    }
?>

The question is, how to I update this code to turn the names into search links?

A: 
echo '<a href="http://www.celebrything.com/?s='.urlencode($row-&gt;name).'&amp;search=Search"&gt;'.$row-&gt;name.' - '.$row->count.' Posts </a><br/>';
Bill Zeller
Seriously.. You guys are amazing. Thanks a TON
Mike
Now I just have to figure out how to get my wordpress search working again. EGH. I installed google search, then removed it, and now wordpress search doesn't work anymore.
Mike
A: 
<a href="<?php bloginfo('url'); ?>/?s=<?php echo urlencode($row->name); ?>">
    <?php echo "{$row->name} ({$row->count} Posts)"; ?>
</a>

Note that bloginfo is a WordPress-specific function, so if you are working on a WordPress theme, I recommend using this rather than hard-coding the domain name.

Matt Huggins
A: 

Constructing a link isn't really anything much more than what you're already doing. The important part is to make sure you properly escape relevant parts of the URL. That's what urlencode() is for.

foreach($result as $row) {
  echo '<a href="http://www.celebrything.com/?s=' .
    urlencode($row->name) . '&search=Search">' . $row->name .
    '</a> - ' . $row->count . ' Posts<br/>';
}
cletus
Mike
A: 

Try this:

<?php
    global $wpdb;
    $result = $wpdb->get_results('SELECT name, count FROM wp_celebcount');

    foreach($result as $row) {
        echo '<a href="?s='.urlencode($row->name).'&search=Search">'.$row->name.'</a> - '.$row->count.' Posts <br/>';
    }
?>
jerjer