0

I'm trying to pass the variables to a new page, I made a function in my theme to get the results. Now when I use yelplist(); on the front page of my theme it gets the results and when i click on the link it passes the image and city back to me as requested.

Questions:

  • However I am wondering what i can do to have variables pass to their own page. For example: if I click on the name, a new page comes with the results of an image and a city for that link.
  • Also is it possible to have it done in the single.php file, or will it be a completely new page/file?

Update 1

*I've made a little bit of progress using $_SESSIONS, I am now able to pass the variables into the single.php file however, when I click on the link thats suppose to grab the results from the current link it does not update the variables that are in the single.php file, instead its only getting the last parsed link's info instead of the one I am clicking on.

Also when I click on the link at the bottom and make it try to go to the single.php file I get a object not found <a href='single.php?p=$pizza_url'><img border=0 src='".$business->photo_url."'></a> what can I do to make it go to the single page and update the variables?

this is what I have in my functions file.

function yelplist(){

require_once ('/lib/OAuth.php'); 
require_once ('yelp.php'); 
require_once(ABSPATH.'/wp-admin/includes/taxonomy.php');

$yelpstring = file_get_contents('http://api.yelp.com/business_review_search?term=pizza&location=Los%20Angeles&ywsid=xxxxxxxxxxxxxxxxxxx');
$obj = json_decode($yelpstring);

        foreach($obj->businesses as $business):
            $business->photo_url;
            $pizza_name = $business->url;
            $business->name;
            $business->phone;
            $city = $business->city;

            $url = ''.$business->url.'';
            $pizza = parse_url($url, PHP_URL_PATH);
            $pizza_url = trim(''.$pizza.'','/biz');
            $lower_case_city_url = $city;
            $lower_case_city_url = strtolower($lower_case_city_url);
            $city_url = str_replace(' ','-',$lower_case_city_url);

            if(isset($_SESSION['p']) && $_SESSION['p']==''.$pizza_url.'') {

            $_SESSION['pizza_name']= $business->name; 
            $_SESSION['pizza_city']= $city = $business->city; 
            $_SESSION['pizza_phone']= $business->phone;
            $_SESSION['pizza_photo'] = $business->photo_url;

        }else{

        echo "<div><a href='?p=$pizza_url'>$business->name</a></div>";
            }
        endforeach;
        }

this is what im using in the single.php file to grab the results.

<?php session_start(); ?>
<?php echo $_SESSION['pizza_name']; ?> </br>
<?php echo $_SESSION['pizza_phone']; ?></br>
<?php echo $_SESSION['pizza_city']; ?></br>
<?php echo "<img border=0 src=".$_SESSION['pizzaphoto'].">"; ?>

Thanks

5
  • what is contained in $pizza_url? is it a page on your site or an external URL? Aug 20, 2011 at 17:25
  • external url that was stripped down see updated code for example Aug 20, 2011 at 17:54
  • One thing I see, is that you can't visit single.php directly. Trying to do that bypasses the entire WordPress bootstrap. Another thing is that "p" is a reserved query var by WP core, which expects an integer representing the ID of the post you want to display. You will have to register your own query var, and capture that during the template_redirect action to choose the template to display. There are a few answers on here showing how to implement this... I can write out example code further if you can break down exactly what problem you're trying to solve a little more clearly. Aug 22, 2011 at 23:35
  • I have some images ill try to explain what im trying to dp in the images, imgur.com/a/K0PRa in image 1 this is my front page it displays the links, now on image 2 before I had $_GET instead of $_SESSION and it would return the data in the same page, but with image 3 this is the parsed data on the single.php file as you can see it doesnt have the list of links. What im trying to do is find the right function to push the correct data in the single.php file when its clicked on, almost like a permalink, you click on the link and it returns the data for that item. hopefully im not confusing Aug 22, 2011 at 23:49
  • Can you show us the yelplist() function so we can see what kind of object you're dealing with? I also don't think the $_SESSION superglobal is what you want. Unless you want the data to be persistant between page views, there's no reason not to just use GET parameters. Aug 23, 2011 at 16:49

2 Answers 2

3
+100

Edit: OK, I get what you're trying to do a little better. This should help get you there.

  1. Cache the data that you are going to want. You'll go over your API limit quickly if you query Yelp on every page load - not to mention slowing your site down terribly.

    function yelplist() {
    
        $pizza_joints = wp_cache_get( 'pizza-joints' );
    
        if ( !$pizza_joints ) {
    
            require_once ('/lib/OAuth.php'); 
            require_once ('yelp.php'); 
            require_once(ABSPATH.'/wp-admin/includes/taxonomy.php');
    
            $yelpstring = file_get_contents('http://api.yelp.com/business_review_search?term=pizza&location=Los%20Angeles&ywsid=xxxxxxxxxxxxxxxxxxx');
    
            $obj = json_decode($yelpstring);
    
            $pizza_joints = array();
    
            foreach( $obj->businesses as $business ) {
    
                $path = trim( '' . parse_url($business->url, PHP_URL_PATH) . '', '/biz' );
                $pizza_joints[ $path ] = $business;
    
            }
    
            wp_cache_set( 'pizza-joints', $pizza_joints );
    
        }
    
        return $pizza_joints;
    
    }
    
  2. Create a page for your single pizza joint reviews and set up a custom page template for it. Keep track of the page ID for that page. I'll say for the rest of my examples that this page has an ID of 50 and the page template its using is single-pizza.php.

  3. ON your front page: when you want to link to individual pizza joint pages, link to that page with an additional variable passed reflecting the individual joint you want to show. Use the function add_query_arg to generate the url to pass that variable:

    $pizza_joints = yelplist();
    
    foreach ( $pizza_joints as $path => $pizza_joint ) {
    
        echo '<p><a href="' 
            . add_query_arg( 'pizza', $path, get_permalink( 50 ) )
            . '">' . $pizza_joint->name . '</a></p>';
    
    }
    
  4. Now when that link is clicked, the viewer will go to the page defined by the template single-pizza.php. In that template, you can access the variable passed by checking the contents of $_GET['pizza'].

    /*
    
    Template Name: Single Pizza Joint Data
    
    */
    
    $pizza_joints = yelplist();
    
    if ( isset( $_GET['pizza'] ) &&  array_key_exists( $_GET['pizza'], $pizza_joints ) ) {
    
        $this_business = $pizza_joints[ $_GET['pizza'] ];
    
        echo '<img src="' . $this_business->photo_url .'">';
        echo '<h2>' . $this_business->name .'</h2>';
        echo '<h4>' . $this_business->phone .'</h2>';
    
    } else { 
    
        // either no variable was passed, or it doesn't match a business in the list
    
        wp_redirect( home_url() );
    
            }
    
10
  • I think your on the right track, but the thing is that for each pizza place I dont want to generate a page, theres about 10-50 pizza places in each city of each state it would take a while to put all of those in lol, anyhow thats why I just wanted to do it this way when i click on lets say dominos pizza it goes to a new page with the results of dominos pizza showing the name, address, city ect... its more like pushing the data that belongs to dominos pizza to the single.php file after its clicked on Aug 23, 2011 at 17:48
  • code updated... Aug 23, 2011 at 17:49
  • Ok i think for the most part your code works I really appreciate it. However I think theres a variable missing or something to that nature, i made a page named pizza, it has the #3 and i gave it the page template single-pizza as instructed now when i put the code on the index.php and change the get_permalink to 3 file it doesnt return any names, so I took a look at the functions file to see if I was missing something, I notice that as the string for path you have $pizza_shop instead of $pizza_joints, is there a reason for this? Aug 23, 2011 at 19:46
  • also when I changed it to $pizza_joints it returned 1 name however when I click on it the items didnt appear on the singles page, so atleast now im able to access that page when I click on something. Aug 23, 2011 at 19:46
  • Yup, you were right about the inconsistent variable name... updated my answer. I also updated my yelplist() function for you - I realized I wasn't following the logic you had originally for trimming the business url to generate the pathname. Try the new yelplist() function, and try var_dumping the contents of the $pizza_joints variable and see what you get. You'll need to have a unique path (in this case I made it the array key) for each location, in order to access those items on the single page. Aug 23, 2011 at 20:03
0

Could this be as easy as resetting the variable once we've hit the page with the query string?

Say something like this at the top of single.php would reset the pizza url upon hitting the page:

<?php $_SESSION['pizza_url'] = $_GET['p']; ?>

And if you had pretty permalinks enabled and weren't able to use the $_GET string, you could grab the query variables like so:

<?php $_SESSION['pizza_url'] = get_query_var('p'); ?>

This isn't going to get you all the way there, but I think I'm just beginning to understand your question, but I hope we can arrive at a conclusion that works for you. Keep me posted.

1
  • Thanks ill play around with what you put to see if I can get a step closer. Aug 22, 2011 at 23:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.