Welcome to Geeklog, Anonymous Tuesday, May 21 2024 @ 12:07 am EDT

Geeklog Forums

Call a function in storytext.thtml


Status: offline

fakestar

Forum User
Chatty
Registered: 03/15/06
Posts: 37
Hi,

i created a custom function in lib-custom.php and i want to show it in every story...
bellow the title, for example.

So, what code i put in storytext.thtml to call this function?

thanks!
 Quote

Status: offline

Dirk

Site Admin
Admin
Registered: 01/12/02
Posts: 13073
Location:Stuttgart, Germany
You can't call functions from template files (well, with the exception of header.thtml ...).

What you can do, though, is define your own template variables (those things in {curly_braces}). In your lib-custom.php, you can have a function
Text Formatted Code
function CUSTOM_templateSetVars ($templatename, &$template)
{
    switch ($templatename) {
        case 'storytext':
        case 'featuredstorytext':
        case 'archivestorytext':
            $template->set_var ('hello_world', "Hello, world!");
            break;
    }
}

That would define a new variable {hello_world} that you can use in those three template files (storytext.thtml, etc.).

Note: Requires Geeklog 1.4.1 and does only work with a few template files (header, footer, and those three above, IIRC).

bye, Dirk
 Quote

Status: offline

fakestar

Forum User
Chatty
Registered: 03/15/06
Posts: 37
Thanks for your response dirk.
I don't know why, i tried several times to upgrade my (1.4.0sr1) installation to 1.41. Every time i get many errors.
I gave up for a while Frown


Well,I'm trying to implement [url =http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/]masuga ajax "star rating"[/url] to appear bellow every story title.
The standard way, the script works using an echo like this:

Text Formatted Code
 <?php echo rating_bar('{story_id}'); ?>


i'm confused now.
i know i can't echo using geeklog...that's the reason i thought to create that function and call to storytext.

the function i need to call/echo is

Text Formatted Code
<?php
/*
Page:           _drawrating.php
Created:        Aug 2006
Last Mod:       Mar 18 2007
The function that draws the rating bar.
---------------------------------------------------------
ryan masuga, masugadesign.com
ryan@masugadesign.com
--------------------------------------------------------- */
function rating_bar($id,$units='',$static='') {

require('_config-rating.php'); // get the db connection info
       
//set some variables
$ip = $_SERVER['REMOTE_ADDR'];
if (!$units) {$units = 10;}
if (!$static) {$static = FALSE;}

// get votes, values, ips for the current rating bar
$query=mysql_query("SELECT total_votes, total_value, used_ips FROM $rating_dbname.$rating_tableName WHERE id='$id' ")or die(" Error: ".mysql_error());


// insert the id in the DB if it doesn't exist already
// see: http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/#comment-121
if (mysql_num_rows($query) == 0) {
$sql = "INSERT INTO $rating_dbname.$rating_tableName (`id`,`total_votes`, `total_value`, `used_ips`) VALUES ('$id', '0', '0', '')";
$result = mysql_query($sql);
}

$numbers=mysql_fetch_assoc($query);


if ($numbers['total_votes'] < 1) {
        $count = 0;
} else {
        $count=$numbers['total_votes']; //how many votes total
}
$current_rating=$numbers['total_value']; //total number of rating added together and stored
$tense=($count==1) ? "vote" : "votes"; //plural form votes/vote

// determine whether the user has voted, so we know how to draw the ul/li
$voted=mysql_num_rows(mysql_query("SELECT used_ips FROM $rating_dbname.$rating_tableName WHERE used_ips LIKE '%".$ip."%' AND id='".$id."' "));

// now draw the rating bar
$rating_width = @number_format($current_rating/$count,2)*$rating_unitwidth;
$rating1 = @number_format($current_rating/$count,1);
$rating2 = @number_format($current_rating/$count,2);


if ($static == 'static') {

                $static_rater = array();
                $static_rater[] .= "\n".'<div class="ratingblock">';
                $static_rater[] .= '<div id="unit_long'.$id.'">';
                $static_rater[] .= '<ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
                $static_rater[] .= '<li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';
                $static_rater[] .= '</ul>';
                $static_rater[] .= '<p class="static">'.$id.'. Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast) <em>This is \'static\'.</em></p>';
                $static_rater[] .= '</div>';
                $static_rater[] .= '</div>'."\n\n";

                return join("\n", $static_rater);


} else {

      $rater ='';
      $rater.='<div class="ratingblock">';

      $rater.='<div id="unit_long'.$id.'">';
      $rater.='  <ul id="unit_ul'.$id.'" class="unit-rating" style="width:'.$rating_unitwidth*$units.'px;">';
      $rater.='     <li class="current-rating" style="width:'.$rating_width.'px;">Currently '.$rating2.'/'.$units.'</li>';

      for ($ncount = 1; $ncount <= $units; $ncount++) { // loop from 1 to the number of units
           if(!$voted) { // if the user hasn't yet voted, draw the voting stars
              $rater.='<li><a href="db.php?j='.$ncount.'&amp;q='.$id.'&amp;t='.$ip.'&amp;c='.$units.'" title="'.$ncount.' out of '.$units.'" class="r'.$ncount.'-unit rater" rel="nofollow">'.$ncount.'</a></li>';
           }
      }
      $ncount=0; // resets the count

      $rater.='  </ul>';
      $rater.='  <p';
      if($voted){ $rater.=' class="voted"'; }
      $rater.='>'.$id.' Rating: <strong> '.$rating1.'</strong>/'.$units.' ('.$count.' '.$tense.' cast)';
      $rater.='  </p>';
      $rater.='</div>';
      $rater.='</div>';
      return $rater;
 }
}
?>


Is it possible to call a php function using javascript? If yes, anyone know how can i do this?
Or any other idea on how implement this in Geeklog?

Thanks in advance.
 Quote

fakestar

Anonymous
Dirk, there is no way to make something similar using 1.4.0?
I mean, define new variable to use in storytext.thtml?

Thanks.

 Quote

Status: offline

jmucchiello

Forum User
Full Member
Registered: 08/29/05
Posts: 985
No simple way. You would need to edit STORY_renderArticle in system/lib-story.php to call your function and put its results into the variable you added to storytext.thtml

Somewhere before the end of the function you add the line

$article->set_var('rating_bar_text', rating_bar($A['sid']) );

and in the storytext.thtml you add {rating_bar_text} where ever you want the text to appear.

The annoying part is having to make this change every time you upgrade.
 Quote

fakestar

Anonymous
Thanks...

i'll upgrade to 1.41 today...it's better only edit lib-custom, right?
anyway i have some index.php modifications too... Rolling Eyes

Text Formatted Code
function CUSTOM_templateSetVars ($templatename, &$template)
{
    switch ($templatename) {
        case 'storytext':
        case 'featuredstorytext':
        case 'archivestorytext':
            $template->set_var ('hello_world', "Hello, world!");
            break;
    }
}


Well, i can replace Hello, world! with whatever php i want?

Thanks!
 Quote

Status: offline

fakestar

Forum User
Chatty
Registered: 03/15/06
Posts: 37
jmucchiello, you're the man!

It's working Smile
i don't care to do the same stuff when i update, i just can't forget Big Grin


Thanks a lot man. Mr. Green

:shakehands:
 Quote

All times are EDT. The time is now 12:07 am.

  • Normal Topic
  • Sticky Topic
  • Locked Topic
  • New Post
  • Sticky Topic W/ New Post
  • Locked Topic W/ New Post
  •  View Anonymous Posts
  •  Able to post
  •  Filtered HTML Allowed
  •  Censored Content