• Resolved yadigit

    (@yadigit)


    I do apologize if my question is a big confusing.
    Im building a plugin that takes data and displays it in a loop. Then I would take the ID number and get results from another table.
    eg

    global $wpdb, $bp;
    $fname =  'mike';
    $lname= 'freemond';
    $entries = $wpdb->get_results("SELECT * FROM table WHERE column1 = $fname AND column2 = $lname ORDER BY id");
    if( $entries ) {
      foreach( $entries as $entry ) {
            $contentid = $entry->id,
    	$firstname = $entry->content1,
    	$lastname = $entry->content2,
    	$city = $entry->content3,
    );
    
    echo $contentid;
    echo $firstname;
    echo $lastname;
    echo $city;
    $phonenumber = $wpdb->results("SELECT * FROM contact WHERE person = $contentid");
    echo $phonenumber;
    ... ect...
    }
    }

    (This is just an example code..)

    so the above loop will display id,first name, last name, city, and phonenumber.
    What I would like to do is have a function for the phone numbers
    eg.

    function phonenumber(){
    global $wpdb;
    $phonenumber = $wpdb->results("SELECT * FROM contact WHERE person = $contentid");
    echo $phonenumber;
    }

    Then use it in my loop.

    global $wpdb, $bp;
    $fname =  'mike';
    $lname= 'freemond';
    $entries = $wpdb->get_results("SELECT * FROM table WHERE column1 = $fname AND column2 = $lname ORDER BY id");
    if( $entries ) {
      foreach( $entries as $entry ) {
            $contentid = $entry->id,
    	$firstname = $entry->content1,
    	$lastname = $entry->content2,
    	$city = $entry->content3,
    );
    
    echo $contentid;
    echo $firstname;
    echo $lastname;
    echo phonenumber(); <-- here.
    ... ect...
    }
    }

    I looked into add_filter/apply_filters. But I couldn’t find a simple break down of an example. I was able to do the following.

    function a($one){
    $one = '1';
    return $one;
    }add_filter('addthis', 'a');
    
    function b(){
    $one = apply_filters('addthis', 'a');
    $two = '2';
    $three = $two + $one;
    echo $three
    };

    $three would echo 3.
    But it didn’t work as planned when I put it all together.
    Once again, I do apologize if my question is a bit confusing.
    Many thanks -Mike.

Viewing 2 replies - 1 through 2 (of 2 total)
  • MarkRH

    (@markrh)

    For one, need to pass $contentid to the phonenumber function:

    function phonenumber($contentid){
    global $wpdb;
    $phonenumber = $wpdb->results("SELECT * FROM contact WHERE person = $contentid");
    echo $phonenumber;
    }

    Then when calling the function:

    echo $contentid;
    echo $firstname;
    echo $lastname;
    echo phonenumber($contentid); <-- here.
    ... ect...

    That will get that bit working anyway.

    Thread Starter yadigit

    (@yadigit)

    Thank you. Worked like a charm!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘User $variables through-out plugin with out using globals.’ is closed to new replies.