I am developing a website in php/MySQL. Say this is a website for my students to check their course/subject results online of their respective semester. The logic is as follows.
The semester.php page lists semesters one through eight. The students clicks on the their semester link and are taken to courses.php where links to all the courses/subjects in that semester are given. When they click on any of their subject, subjectresult.php opens showing results of all the students in that subject.
I am passing semester and subject values as parameters in the URL as follows.
courses.php?semester=semestername
I am also using pagination to display only 10 results per page.
So far so good. The subjectresult.php shows only 10 results with pagination links at the bottom. Everything nice and tidy.
My problem is that when I click on page 2, everything goes away because the subject parameters is now no longer in the URL.
The two URLs as under:
course/subject URL: subjectresult.php?sub=subjectname
page # URL: subjectresult.php?page_no=pagenumber
The workaround that I have made is that I have changed the paging links logic and have added subject name as a second parameter. Now everything is working fine and the partial URL is as under:
subjectresult.php?sub=subjectname&page_no=pagenumber
Please note that I am a novice, still learning.
Now I have a few questions.
Am I doing it right? (Which I don't think so)
How to store my subject_result query result when the subjectresult.php page loads for the first time so that I don't have pass this again and again with each paging link?
Or in a Nutshell..How to do it correctly?
Thanks in advance.
Your approch is ok.
If you want to store and reuse the subject value,
In PHP use a session variable.
Eg:
session_start();
// give the subject value to the session variable.
$_SESSION['subject'] = "subject";
// get the value from the session variable if needed.
Like
session_start();
$subject = $_SESSION['subject'];
for more about sessions please check here..
Related
I'm trying to make 100+ registration pages for my site with minor differences between each one.
For example:
State, Age, Start Date, Client Goal
Page 1 characteristics: California, Age 18-25, Start Date Dec 1, Goal 1
Page 2 characteristics: New York, Age 25-30, Start Date Dec 15, Goal 3
Page 3 characteristics: New Jersey, Age 18-25, Start Date Jan 1, Goal 2
Goal:
It's important that I have separate pages and not just a form or something selected by the user on a single page. Ultimately I'd like to have many more levels of customization than what I have above and would like to control all the pages without having to go in and edit each individually.
Suggested Method:
I was thinking I could make 1 PHP template and use it on every page to pull a unique code from the href to determine what data it gets from the database to display on the page.
To get my page 1 it would be something like:
window.location.href
returns -> mysite.com/CA-18-12-1-G1
Then use that code to set PHP variables which would customize the entire page just by echoing the html with the variables.
Is there a better/smarter/safer way?
I'm fairly new to this.
Think of it this way: you want to make just one page, and you want to render it with 100+ different customizations.
Figure out the common HTML and CSS for your template page, and figure out what needs to change customization-by-customization. You'll have a set of customization parameters when you finish this design work.
Then, design some kind of data structure to hold your customization parameters. A table in MySql with one column for each parameter and one row for each of your 100 customizations might be a good way to do this.
Then write a php program to retrieve the row with the parameters for the customization you want, fill in your common HTML with the customizations, and send it to the browser for your user to fill in.
If you only need two or three custom pages, my advice would be to program them separately. But with 100 you need a template / parameter approach.
For writing a single page and then using the code of that page in multiple pages you can use PHP.
Lets say you have a page named constant.phpand you want to use the code of the page in a different PHP page named home.php. You have to use this code in the home.php where you want the code to be inserted:
<?php include "constant.php";?>
Now the code ofconstant.php code will get inserted in the home.php in the place where you wrote this code.
fairly new to PHP and webdesign.... I have a website which has a bunch of products for vehicles - all of this is stored in a database.
In order to narrow down what products are available, I want to take the user through 2-3 pages where they firstly select what categorey of product they want, then what make of car they have, then the model, then I want to run a mysql query with that information and return the answerson a page.
I already have the code to request the info from a database and then display it on the page, but The way the current website is setup, they are hard coded mysql queries (meaning the user didnt input any data to get there).
So how can I transfer variable between php pages? Page 1 (contains Categorey) - then pass to Page 2 (contains Make) then page to Page 3 (contains Model) then compile the three variavles collections, and pass them to results.php (a standard results page which Gets variables and then searches).
I know this might seem basic, but I am really stumped as to how to get it coded.
If someone can give me an a newbie explanation about how to achieve this? Thank you
You can pass PHP variables through URLs for example
Your HTML code,
<a href='yourPage.php?yourVariable=someValue'>TEST!</a>
Your PHP code,
if (isset($_GET['yourVariable'])) {
$yourVariable = $_GET['yourVariable'];
// Your code here.
}
Now using this you can create what you want with a little bit of tinkering.
Remember
You need to use $_GET because parameters are being passed using the URL and not a POST request.
Don't forget to clean all input.
Below is a picture of my database.
I am going to make the numbers on the left (81305, 81306, etc..) all hyperlinks.
When the user clicks on the hyperlink, they will be directed to a page (all links will be this page) "dpupage.php". This page will have a form with empty values. I will then use SQL queries (according to the NUM) to fill the form with the rest of the columns according to the number they picked.
The problem I have is that I will need to email users a specific link to their specific form.
But I can't set the url to, for example: http://mysite.com/dpupage.php because it will send them to a form with empty values. How can I create a link that will open up the dpupage.php and still query the data they need.
For example: Lets say I need a user to address NUM: 81309. An email is sent to them through phpmailer with a link. This link needs to take them directly to that dpupage.php and fill in all data that corresponds with NUM 81309.
Any advice is greatly appreciated!
Use PHP
Give them a link with a parameter, for example http://mysite.com/dpupage.php?num=12345
You can get the number they used via $_GET['num']. Get your values from your DB (example: "SELECT * FROM wahtever_your_table_is_called WHER NUM = $_GET['num']).
Paste the values into the field.
Beware: that's an easy example, don't do it that way! SQL-Injections will eat your dog! Google it!
My website allows visitors to search for homes (using PHP and MySql). After the search, they are presented with a list of matches which they can click on to see the detail page for that home. To see the next home, they currently have to click back, then click on the next home in the search results list they want to view.
I would like to make it so that once they have clicked on one home to view the details, they would have a "Next Home" and "Previous Home" link/button to use to navigate the results set without having to go back to the search results page.
I've seen this done, but can't get my head around how to do it. I assume you have to save the recordset from the initial search somewhere, and then recall it on the details page. And you'd have to know that you were looking at the xth home out of Y homes.
Can anyone give me a broad overview of how this would work? Do I save the initial search results in a temp MySQL table and pass that table name to the details page? Or use a session variable to hold the results set? Keep in mind that a visitor could make several different searches during their session.
Any assistance would be most appreciated,
Tom
When I've done this, I've saved the details of the current search in SESSION, and then requeried the database when I've needed to - so you can query it on the detail page to generate a Next and Previous link, and so on. I normally use a SESSION variable called 'parameters', and add the whole of the SQL WHERE statement to it.
If someone's making multiple searches, this will let them make one at a time - subsequent searches will over-write what's already in there. If your users are going to be making multiple searches at the same time using different parameters, you'll need to find a way to differentiate the parameters for each search. You can give each set a unique name by prefixing it with a call to uniqid, and you'll then need to make sure you pass the unique identifier to the detail page, so it can work out which set of parameters it needs to run.
I have completely no idea where to start so I apologise about the lack of code presented to you.
My problem is - I have a page of information, ordered by ID (gathered from the database). These ID's are referenced from another page to which the user clicks on a link and it takes them to the page with the information on, how ever, there could be potentially hundreds of ID's on that page - So I need to reference each specific ID so when the user clicks, it will take them to the exact position of the ID.
I understand its something like localhost:8888/index.html#id3 etc..
But i'm struggling to understand how to reference for a PHP Variable.
Many thanks in advance.
You would use anchors, so for example, on the page where you list your information, attach an anchor to it such as #id1, then on your links page, you would place the #id1 at the end of the url results.html?#id3