TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 12-06-2007, 11:50 AM   #1 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default 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?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 12:23 PM   #2 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

As in?

PHP Code:
<?php posts_by_category($id); ?>
But then surely it depends what the function looks like and needs?
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-06-2007, 12:30 PM   #3 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

I can post the code for the function if that will help?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 12:31 PM   #4 (permalink)
The Addict
Upcoming Programmer Top Contributor 
 
Rendair's Avatar
 
Join Date: Nov 2007
Location: UK
Posts: 319
Thanks: 18
Rendair is on a distinguished road
Default

Yeah post the function
__________________
www.jooney.co.uk - the online portfolio
Send a message via MSN to Rendair
Rendair is offline  
Reply With Quote
Old 12-06-2007, 12:34 PM   #5 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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.
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 12:37 PM   #6 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

you need to define a variable within your function such as
PHP Code:
function posts_by_category($number
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 12:39 PM   #7 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

I dont understand. How would I implement that into my code?
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 12:44 PM   #8 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

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
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 12:59 PM   #9 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Thanks :) I'll try that out now.
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 01:03 PM   #10 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

No prob :)
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 01:03 PM   #11 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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'
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 01:05 PM   #12 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

Did it work correctly with the constant within the function?
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 01:07 PM   #13 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Yea :( lol
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 01:09 PM   #14 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

Try calling your function like this:

PHP Code:
posts_by_category("4"
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 01:14 PM   #15 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

Nope lol I tried "4" and '4' but no luck :(
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 01:22 PM   #16 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

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(); ?>
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 12-06-2007, 01:23 PM   #17 (permalink)
The Contributor
Good Samaritan 
 
d4v1d's Avatar
 
Join Date: Dec 2007
Location: Durban, South Africa
Posts: 51
Thanks: 1
d4v1d is on a distinguished road
Default

Just out of interest, how are you calling the function? the full line...
d4v1d is offline  
Reply With Quote
Old 12-06-2007, 01:25 PM   #18 (permalink)
The Contributor
Upcoming Programmer 
 
Gurnk's Avatar
 
Join Date: Oct 2007
Location: US
Posts: 66
Thanks: 19
Gurnk is on a distinguished road
Default

Quote:
Originally Posted by d4v1d View Post
Just out of interest, how are you calling the function? the full line...
Hehe, yeah I just noticed that. Check my edit Jmz.
Send a message via MSN to Gurnk
Gurnk is offline  
Reply With Quote
Old 12-06-2007, 01:29 PM   #19 (permalink)
Jmz
The Acquainted
 
Join Date: Oct 2007
Location: Newcastle, UK
Posts: 113
Thanks: 3
Jmz is on a distinguished road
Default

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(); ?>
__________________
Free CSS Tutorials
Send a message via MSN to Jmz
Jmz is offline  
Reply With Quote
Old 12-06-2007, 01:30 PM   #20 (permalink)
The Contributor
Upcoming Programmer 
 
Matt83's Avatar
 
Join Date: Oct 2007
Location: Argentina
Posts: 72
Thanks: 18
Matt83 is on a distinguished road
Default

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); 
__________________
http://www.mattvarone.com
Matt83 is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 12:16 AM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design