1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

2 PHP Questions

Discussion in 'PHP' started by trenttdogg, Feb 23, 2013.

  1. #1
    Hello everyone,
    I have 2 issues I'm trying to figure out with PHP.
    1) I have a sidenav that I would like have change automatically, based on the page. Ex: pages is ...worksheets.php?subject=math. I need the side navigation to list the categories for the subject of math. When the user goes to page ...worksheets.php?subject=reading, the sidenav populates with the reading categories and so on. I was trying to do this with if/else statements but couldnt get it to work.

    2) Is it possible to paginate a div only? I want to have a div that shows results of data and has pagination at the bottom for the additional pages of results. When I click next or 2 it does return those results, but it actually refreshes the entire page rather than the div only.

    Any ideas are appreciated.
     
    trenttdogg, Feb 23, 2013 IP
  2. kulik

    kulik Member

    Messages:
    162
    Likes Received:
    18
    Best Answers:
    1
    Trophy Points:
    45
    #2
    I'm guessing something like this you mean...
    $subject = isset($_GET['subject']) ? $_GET['subject'] : '';
     
        if ( $subject == "math" )
        {
        do your math stuff here
        }
        elseif ( $subject == "reading" )
        {
        do your reading stuff here
        }
        else
        {
        do what you want here when no subject is selected
        }
    PHP:
    And part two sounds like you want Ajax if desire is not to have page reload.
     
    kulik, Feb 23, 2013 IP
  3. crivion

    crivion Notable Member

    Messages:
    1,669
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    210
    Digital Goods:
    3
    #3
    part two can also be achieved via an iframe.
     
    crivion, Feb 23, 2013 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    I would alter my database with a related-to column, assuming you have main categories... as they load to the page, an sql statement could gather related topics etc.... based on category names or id's pending on how you set it up.

    Its worth thinking about doing something like that now.. rather then when you have hundreds of pages.
     
    MyVodaFone, Feb 23, 2013 IP
  5. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #5
    Thanks for all your help guys.
    Would you suggest a separate table for each subject? Right now, I have a main table and the subjects are defined in a column. It also has columns for category as well as subCategory. So each record in the table is assigned a subject, a category and a subCategory.
    @kulik-thanks for the snippet, I will give it a try. Where it says "do" is that where I would put the include_once "mathNav.php"
     
    trenttdogg, Feb 23, 2013 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    If you already have a cats with subscats then its just a matter of creating the right sql statement.... what type of cms system are you using?
     
    Last edited: Feb 23, 2013
    MyVodaFone, Feb 23, 2013 IP
  7. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #7
    It's not built using any CMS framework. Probably would have been easier to go that route (but not as fun, lol)
     
    trenttdogg, Feb 23, 2013 IP
  8. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #8
    Im getting closer. Here's the code:
    <?php
        $subject = isset($_GET['subject']) ? $_GET['subject'] : '';
       
            if ( $subject == "math" )
            {
            echo '
            include_once "mathNav.php"';
            }
            elseif ( $subject == "reading" )
            {
            echo '
            include_once "readNav.php"';
            }
            else
            {
            echo '
            "sideNav.php"';
            }
    ?>
    Code (markup):
    What I'm getting is "include_once "mathNav.php" INSTEAD of the actual contents of mathNav.php.
     
    trenttdogg, Feb 23, 2013 IP
  9. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #9
    Wrong code, you are just printing on screen (using echo), try this:


    
    $navLoad = array(
        "math"        => "mathNav.php",
        "reading"    => "readNav.php",
    );
     
    if (isset($_GET['subject'])) {
       
        $subject = $_GET['subject'];
     
        $nav = $navLoad[$subject];
       
        if ($nav != "") include $nav;
                else    include "sideNav.php";
     
    } 
    PHP:
     
    YoGem, Feb 23, 2013 IP
  10. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #10
    Part two can be done with Ajax and PHP. Never using iFrames.
     
    YoGem, Feb 23, 2013 IP
  11. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #11
    Thanks YoGem. That works exactly as I need:D
     
    trenttdogg, Feb 23, 2013 IP
    YoGem likes this.
  12. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #12
    You are very welcome :)
     
    YoGem, Feb 23, 2013 IP
  13. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #13
    I have no idea how to write the code for the "part 2" so I was thinking an alternative might be to have the results "shuffle" instead of paginate. Is that possible. It's for a block of testimonials.
     
    trenttdogg, Feb 23, 2013 IP
  14. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #14
    Ok.. so I imagine you are creating a website and you want to show in a div two different blocks of testimonials.

    Does this example works good for you? This is basically the easiest way to have more content on a page but show it only on demand without refreshing the page.

    <div id="testimonials">
        <div id="part1" style="display:block">
              <p>Testimonial 1</p>
              <p>Testimonial 2</p>
              <p>Testimonial 3</p>
              <p>Testimonial 4</p>
              <p>Testimonial 5</p>
        </div>
        <div id="part2" style="display:none">
              <p>Testimonial 6</p>
              <p>Testimonial 7</p>
              <p>Testimonial 8</p>
              <p>Testimonial 9</p>
              <p>Testimonial 10</p>
        </div>
    </div>
    <div id="easynav" align="center">
        <p>
                  <span onclick="document.getElementById('part2').style.display = 'none'; document.getElementById('part1').style.display = 'block;" style="cursor:pointer;">Part 1</span>
                  &nbsp;
                  <span onclick="document.getElementById('part2').style.display = 'block'; document.getElementById('part1').style.display = 'none;" style="cursor:pointer;">Part 2</span>
        </p>
    </div>
    HTML:
     
    YoGem, Feb 23, 2013 IP
  15. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #15
    I currently have the testimonial div tied to a db and the records are fetched and the first 5 display on the page automatically. I just dont like the way the entire page "refreshes" when you select page 2 of testimonial pagination. Will the method you posted above automatically fetch the records from the db?
     
    trenttdogg, Feb 23, 2013 IP
  16. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #16
    No, my method is just an example.

    Follow this:

    Create a testmonial.php file that will access the database, as input for this file use page:

    ie: testimonial.php?page=1

    then using Ajax you can load dynamically the content of that div:
    
     
    <div id="testimonials">
        <p>Please wait...</p>
    </div>
    <script>
     
        $("#testimonials").load("testimonial.php?page=1");
     
    </script>
    
    HTML:
    But I assume you know a bit of Ajax/jQuery...

    Edit:
    Alright, I cannot assume. Before trying this code you need to include in your <head></head> section something like this:

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.1/jquery.min.js"></script>

    Then you will be able to use Ajax.
     
    Last edited: Feb 23, 2013
    YoGem, Feb 23, 2013 IP
  17. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #17
    YoGem, code all of our websites for free!
     
    ezprint2008, Feb 24, 2013 IP
  18. YoGem

    YoGem Active Member

    Messages:
    676
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    90
    #18
    :confused:

    Yeah ezprin2008 you are right..
     
    YoGem, Feb 25, 2013 IP
  19. trenttdogg

    trenttdogg Greenhorn

    Messages:
    62
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    8
    #19
    lol. Thanks again Yogem
     
    trenttdogg, Feb 25, 2013 IP
  20. HolyRoller

    HolyRoller Well-Known Member

    Messages:
    552
    Likes Received:
    27
    Best Answers:
    1
    Trophy Points:
    150
    #20
    Just a quick point never use anything directly in your code that comes from $_GET, $_POST or other user inputs. Assume everything is dirty and filter all data from external sources.
     
    HolyRoller, Mar 7, 2013 IP