Is there a better way of doing this in PHP?

Discussion in 'PHP' started by gandalf117, Mar 22, 2013.

  1. #1
    Imagine that you have a regular page with links on it that when clicked point to the same page but partially changed. So for example if you have a page with some text and then you have a couple of links below that text that point to the same page but with different versions or extensions to that text how would you do this.

    One way I can think of doing it is with global variable.
    So the link will look like this <a href='page.php?version=1'>link1</a>
    then $_GET['version'] and then on the page a conditional statement such as
    if($_GET['version'] == 1) echo "extension1";
    else if($_GET['version'] == 2) echo "extension2";
    to get the different versions of the same page

    Another way I can think of is with $_SESSIONS but I am not sure if that would be as adequate.

    What is the best way to do this?
     
    Solved! View solution.
    gandalf117, Mar 22, 2013 IP
  2. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #2
    The way you're describing sounds fine to me. But yes, don't use sessions for this.

    Have you considered Javascript? If you're only changing some bits of your page, it may be confusing for people if you're reloading the page and only change it slightly.
     
    nico_swd, Mar 22, 2013 IP
  3. gandalf117

    gandalf117 Active Member

    Messages:
    111
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    53
    #3

    Yes, I usually use javascript and in particular AJAX and asynchronus requests for things like that but they practically work the same way. You pass a variable that determines what to display.

    In certain cases (with less data) I also use CSS for doing this. I would retrieve all the information at once and put it in different division with absolute positioning. The only thing I change with JavaScript in this case is the z-index. So they are kind of stacked over each other and the user doesn't have to rely on internet connection to view the entire page. But I have notice that when JavaScript gets involved the indexing and the SEO of the website suffers. I have several website that run entirely on JavaScript and AJAX and you can't find them in search engines.

    So this sums up all the methods I know for doing this with or without javascript. That's why I decided to inquire about some new methods or strategies.
     
    gandalf117, Mar 22, 2013 IP
  4. #4
    Well then I would suggest you use Javascript with a fallback to the method you posted above. This way, people with real browsers and Javascript will have a great user experience, and spiders will still be able to index your page properly. (And users without Javascript). Basically you would link to the new (and different page), but have Javascript to change the onclick event and return false. And at the same time you pull your new data and swap it.

    I'm not a SEO expert, but I'm not sure how good that actually is for your raking. I mean, I suppose Google would consider the page with different GET parameters different pages, and if you have almost identical content,... I don't know if it's a good thing. I kinda guess it depends on how important the keywords are that you want to 'change' on the page.
     
    nico_swd, Mar 22, 2013 IP
  5. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #5
    no need for an if statement if your passing the version you want?
    <?php
    echo "extension",$_GET['version'];
    ?>
     
    MyVodaFone, Mar 22, 2013 IP
  6. crazyblogger

    crazyblogger Active Member

    Messages:
    430
    Likes Received:
    5
    Best Answers:
    1
    Trophy Points:
    63
    #6
    I think GET method is the best for what you desire. jQuery would be great tool to achieve this effect but as everyone has stated, its better to have a PHP fallback solution.
     
    crazyblogger, Mar 22, 2013 IP