TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   General (http://www.talkphp.com/general/)
-   -   Passing a variable to a function (http://www.talkphp.com/general/1634-passing-variable-function.html)

Jmz 12-06-2007 11:50 AM

Passing a variable to a function
 
I'm trying to pass a variable to a function in my wordpress skin.

Basically the function (which is a plugin) selects the latest articles from a certain category. What I want to do is define the category number when I call the function.

To call the function I would write:
Code:

<?php echo posts_by_category(); ?>
Is there something I could add so I can define the category number when I call it?

Rendair 12-06-2007 12:23 PM

As in?

PHP Code:

<?php posts_by_category($id); ?>

But then surely it depends what the function looks like and needs?

Jmz 12-06-2007 12:30 PM

I can post the code for the function if that will help?

Rendair 12-06-2007 12:31 PM

Yeah post the function

Jmz 12-06-2007 12:34 PM

Code:

function posts_by_category() {

$number = "4";



        global $wpdb, $post;



        $tp = $wpdb->prefix;



        $pc_header = get_option('pc_header');



        $sort_code = 'ORDER BY name ASC, post_date DESC';

        $the_output = NULL;





        $last_posts = (array)$wpdb->get_results("

                SELECT {$tp}terms.name, {$tp}terms.term_id

                FROM {$tp}terms, {$tp}term_taxonomy

                WHERE {$tp}terms.term_id = {$number}

                AND {$tp}term_taxonomy.taxonomy = 'category'

                {$hide_check}

        ");



        if (empty($last_posts)) {

                return NULL;

        }



        $the_output .= stripslashes($ddle_header);



        $used_cats = array();;

        $i = 0;

        foreach ($last_posts as $posts) {

                if (in_array($posts->name, $used_cats)) {

                        unset($last_posts[$i]);

                } else {

                        $used_cats[] = $posts->name;

                }

                $i++;

        }

        $last_posts = array_values($last_posts);



        foreach ($last_posts as $posts) {

$the_output .='<div class="col">';

          $the_output .= '<h2>' . apply_filters('list_cats', $posts->name, $posts) . '</h2>';



          $where = apply_filters('getarchives_where', "WHERE post_type = 'post' AND post_status = 'publish'" , $r );

   

 $arcresults = $wpdb->get_results("SELECT *, COUNT( `ID` ) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id ={$number}) GROUP BY 1 ORDER BY post_date DESC LIMIT 0 , 5;");
                 
                //. $posts->term_id .

                        $the_output .= '<ul class="latestmenu">';

          foreach ( $arcresults as $arcresult ) {

              $the_output .= '<li><a href="' . get_permalink($arcresult->ID) . '">' . apply_filters('the_title', $arcresult->post_title) . '</a></li>';

          }

   

          $the_output .= '';

      }

      $the_output .= '</ul></div>';

      return $the_output;

}

function pc_generate($content) {

        $content = str_replace("<!-- postsbycategory -->", posts_by_category(), $content);

        return $content;

}

As you can see, I've already swapped the category number in the sql to a variable called $number but it will only work if I define $number inside the function. What I need is to be able to define it when I call the function somehow.

d4v1d 12-06-2007 12:37 PM

you need to define a variable within your function such as
PHP Code:

function posts_by_category($number


Jmz 12-06-2007 12:39 PM

I dont understand. How would I implement that into my code?

d4v1d 12-06-2007 12:44 PM

You're trying to pass a variable through to your function, but your function is not set up to receive any information. By including the variable within the function declaration like i showed above, you're assigning any input to that variable, which will be a valid variable now within the function with the value you assigned to it from outside the function.

So you would now take out your constant, thus your code would look like this:

PHP Code:

function posts_by_category($number) {



    global 
$wpdb$post;



    
$tp $wpdb->prefix;



    
$pc_header get_option('pc_header');



    
$sort_code 'ORDER BY name ASC, post_date DESC';

    
$the_output NULL;





    
$last_posts = (array)$wpdb->get_results("

        SELECT 
{$tp}terms.name, {$tp}terms.term_id

        FROM 
{$tp}terms, {$tp}term_taxonomy

        WHERE 
{$tp}terms.term_id = {$number}

                AND 
{$tp}term_taxonomy.taxonomy = 'category'

        
{$hide_check} 

    "
);



    if (empty(
$last_posts)) {

        return 
NULL;

    }



    
$the_output .= stripslashes($ddle_header); 



    
$used_cats = array();;

    
$i 0;

    foreach (
$last_posts as $posts) {

        if (
in_array($posts->name$used_cats)) {

            unset(
$last_posts[$i]);

        } else {

            
$used_cats[] = $posts->name;

        }

        
$i++;

    }

    
$last_posts array_values($last_posts);



    foreach (
$last_posts as $posts) {

$the_output .='<div class="col">';

      
$the_output .= '<h2>' apply_filters('list_cats'$posts->name$posts) . '</h2>';



          
$where apply_filters('getarchives_where'"WHERE post_type = 'post' AND post_status = 'publish'" $r );

    

 
$arcresults $wpdb->get_results("SELECT *, COUNT( `ID` ) FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'publish' AND ID IN (Select object_id FROM {$tp}term_relationships, {$tp}terms WHERE {$tp}term_relationships.term_taxonomy_id ={$number}) GROUP BY 1 ORDER BY post_date DESC LIMIT 0 , 5;");
          
        
//. $posts->term_id .

            
$the_output .= '<ul class="latestmenu">';

      foreach ( 
$arcresults as $arcresult ) {

           
$the_output .= '<li><a href="' get_permalink($arcresult->ID) . '">' apply_filters('the_title'$arcresult->post_title) . '</a></li>';

       }

    

          
$the_output .= '';

       }

       
$the_output .= '</ul></div>';

       return 
$the_output;

}

function 
pc_generate($content) {

    
$content str_replace("<!-- postsbycategory -->"posts_by_category(), $content);

    return 
$content;



and you would call this function as follows:

PHP Code:

posts_by_category(4


Jmz 12-06-2007 12:59 PM

Thanks :) I'll try that out now.

d4v1d 12-06-2007 01:03 PM

No prob :)

Jmz 12-06-2007 01:03 PM

I just get an error saying:

Code:



WordPress database error: [You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'AND wp_term_taxonomy.taxonomy = 'category'' at line 7]
SELECT wp_terms.name, wp_terms.term_id FROM wp_terms, wp_term_taxonomy WHERE wp_terms.term_id = AND wp_term_taxonomy.taxonomy = 'category'


d4v1d 12-06-2007 01:05 PM

Did it work correctly with the constant within the function?

Jmz 12-06-2007 01:07 PM

Yea :( lol

d4v1d 12-06-2007 01:09 PM

Try calling your function like this:

PHP Code:

posts_by_category("4"


Jmz 12-06-2007 01:14 PM

Nope lol I tried "4" and '4' but no luck :(

Gurnk 12-06-2007 01:22 PM

PHP Code:

WHERE wp_terms.term_id 

It looks like the $number variable isn't getting passed. However adding the ''s as david suggested seems like it would have fixed it.

And you sure it worked correctly before?

EDIT: Also, are you supposed to echo functions? I thought you just wrote it out.

PHP Code:

<?php posts_by_category(); ?>

Not

PHP Code:

<?php echo posts_by_category(); ?>


d4v1d 12-06-2007 01:23 PM

Just out of interest, how are you calling the function? the full line...

Gurnk 12-06-2007 01:25 PM

Quote:

Originally Posted by d4v1d (Post 5565)
Just out of interest, how are you calling the function? the full line...

Hehe, yeah I just noticed that. Check my edit Jmz. ^^

Jmz 12-06-2007 01:29 PM

Yea, you can see it working at: http://www.filmzone.org/ if you go to the bottom of the page, he list of trailers is what is generated by the function.

I want to display three lists exactly like that but from three different categories. Is there another way to do it?


I'm calling the function using this:
Code:

<?php echo posts_by_category(); ?>

Matt83 12-06-2007 01:30 PM

I didn't test this but seems your problem is on the query. Try this out:

PHP Code:

$last_posts = (array)$wpdb->get_results(sprintf(

        SELECT 
{$tp}terms.name, {$tp}terms.term_id 

        FROM 
{$tp}terms, {$tp}term_taxonomy 

        WHERE 
{$tp}terms.term_id = '%d' 

        AND 
{$tp}term_taxonomy.taxonomy = category 

        
{$hide_check}  

        "
,$number)); 

And call you function this way:

PHP Code:

echo posts_by_category(4); 



All times are GMT. The time now is 11:10 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0