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.
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.
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.
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"
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?
It's not built using any CMS framework. Probably would have been easier to go that route (but not as fun, lol)
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.
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:
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.
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> <span onclick="document.getElementById('part2').style.display = 'block'; document.getElementById('part1').style.display = 'none;" style="cursor:pointer;">Part 2</span> </p> </div> HTML:
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?
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.
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.