Using hyperlink to call php function - Page 2
Page 2 of 2 FirstFirst 12
Results 16 to 19 of 19

Thread: Using hyperlink to call php function

  1. #16
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Wonderful!

    ...Now if I wanted to send an array, or a list of 5 variables, what would that look like?

    Right now I'm creating the links in a loop, that recurses through a multidimensional array... but I'm having trouble passing more then one variable to the function. Any advice?

  2. #17
    Pna lbh ernq guvf¿
    Join Date
    Jul 2004
    Location
    Kansas City area
    Posts
    19,399
    One way to pass an array of values would be to append multiple instances of 'paramName[]' (where 'paramName' is any name you'd like to give the query parameter, of course). In other words, this query string:
    Code:
    ?foo[]=one&foo[]=two&foo[]=three
    would then make $_GET['foo'] look like this:
    Code:
    array(3) {
      [0]=>
      string(3) "one"
      [1]=>
      string(3) "two"
      [2]=>
      string(5) "three"
    }

  3. #18
    Junior Member
    Join Date
    Nov 2011
    Posts
    4
    Thanks a lot guys! I really appreciate the help!

  4. #19
    Junior Member
    Join Date
    Jan 2013
    Posts
    1
    Hi,

    Sorry to resurrect such an old thread...

    I'm trying to run a function to alter the post_type in my wordpress query string (or to add a post type altogether). The function I'm using comes from here: http://www.addedbytes.com/blog/code/...ing-functions/

    Based on Halojoy's example, I've written this:

    Code:
    <?php //add query to URL
    function add_querystring_var($url, $key, $value) {
        $url = preg_replace('/(.*)(?|&)' . $key . '=[^&]+?(&)(.*)/i', '$1$2$4', $url . '&');
        $url = substr($url, 0, -1);
        if (strpos($url, '?') === false) {
            return ($url . '?' . $key . '=' . $value);
        } else {
            return ($url . '&' . $key . '=' . $value);
        }
    }
    ?>
    <?php 
    
    if (isset($_GET['run'])) $linkchoice=$_GET['run']; 
    else $linkchoice=''; 
    
    switch($linkchoice){ 
    
    case 'showAll' : 
        add_querystring_var($url, 'post_type', '');
    	break; 
    
    case 'showAnimation' : 
        add_querystring_var($url, 'post_type', 'animation');
        break; 
    
    case 'showPeople' : 
        add_querystring_var($url, 'post_type', 'people'); 
        break; 
    
    case 'showStudios' : 
        add_querystring_var($url, 'post_type', 'studios');
        break; 
    
    case 'showResources' : 
        add_querystring_var($url, 'post_type', 'resources'); 
        break; 
    
    case 'showWiki' :
    	add_querystring_var($url, 'post_type', 'wiki');
    	break;
    default : ;
    } 
    
    ?> 
    
    <h2>Show... </h2>
    <a href="?run=showAll">All</a>
    <a href="?run=showAnimation">Animation</a>
    <a href="?run=showPeople">People</a>
    <a href="?run=showStudios">Studios</a>
    <a href="?run=showResources">Resources</a>
    <a href="?run=showWiki">Wiki</a>
    When I click any of these links, they take me to "mywebsite.com/?run=showX" - which in turn directs me to my front page. I think this means that the function is not running, and I'm wondering if anyone has any suggestions as to why. I've tried testing without the function, and with just echoes indicating that a function has ran, instead - and these haven't done anything. I am still directed to the url indicated in the link and nothing beyond that seems to happen. Have I set the function to run incorrectly?

    Thanks in advance!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •