How to create PHP pages by following an order? - php

I am working on building a Webstie for a restaurant and because clients will need to be able to order the items from the menu I've got to store everything on a MySQL database.
Now, everything is going to be organized visually in "cards"(you'll see what i mean in the image below) and each card is made up by an image and the title. Since I'm using PHP to deal with the SQL Queries and connection, the only thing I need is that once a card has been clicked, a new page gets presented with the same style, the same js scripts that I have, in other words, everything the same to the main page but with the correct number and type of cards.
You might have noticed that there are cards that create new pages with other cards inside and there are other cards, usually the last ones at the tree(image 3) that only create the menu. I've built everything and it works but all the data was stored inside JSON files and I had to copy the div for each card and create HTML pages for every category manually.
Now, I have already put everything inside the MySQL Database, I am referring to each item's data. So, what I need to do is simply create other html pages with different cards inside or create a menu(depending on the type of card). So, I understood that I need to create a tree, I thought about doing it in PHP in this way:
$tree = array("Drinks" => array(
"Alcholic" => array("Beers", "Vodka", "Wines"),
"Non-Alcholic" => array("Juices", "Something Else...")
),
"Food" => array(
//...
)
);
But, I don't have any idea about how to go from here. I tried my best to explain my objective in a clear and precise way. If, for any reason you need more details I will edit the post without any problems. This is a serious job and I will be very greatful for any amount of help I can get. Thanks in advance and have a great day!

You could use url parameter and do it all on one page, fetching data from db and creating cards dynamically based on the fetched result. For example:
if (isset($_GET['type') {
//suppose I clicked drinks
//then fetch drink types from db and and loop over them to create cards
//for example
<a href='index.php/?type=alcoholic'><img src='alcoholic.jpg'></a>
} else {
//show home page categories (drinks, food) if no parameter in url
//for example
<a href='index.php/?type=drinks'><img src='drinks.jpg'></a>
}
Alternatively, you could create separate pages for each subcategory, but that would be just duplicating the code.

Related

How do I tidy up my url after sending lots of information?

I am a beginner programmer/coder who is currently trying to get to grips with HTML and PHP. I currently have a locally hosted searchable database that (when used) brings up a list of the first twenty entries that correspond to your search terms, with buttons to send you to the next page, last page, etc. (you know... pages...) The search outputs information into the URL ([url]?q=Alphonse&f=Elric etc). I have two problems with this at the moment.
Problem A:
My URL contains information that is unused. If I don't put anything into the search term it simply comes out with "q=&x=&f=..." etc. This makes the URL absurdly long even on the most simple searches.
Can this be cleaned up through just php?
Would this method be different if I end up hosting this online?
Problem B:
The way my paging functions is to send the user to the following link '.$_SERVER['PHP_SELF'].'?'.$_SERVER['QUERY_STRING'].'&pn='.$nextPage.'. This outputs the current link but with "pn=1" at the end (Or whatever relevant page they click).
This method itself makes the URL quite messy. If they click through multiple pages, and perhaps go back and forth, the link ends up having "pn=1&pn=2&pn=3&pn=1...." etc at the end. I assume that this will be answered by the first query, but it is slightly different in that this is information that is actually present.
How do I remove this superfluous information, and just keep the (final) relevant one?
I am thinking that I can use parse_str to turn the URL into an array, then delete each entry of the array that are empty, then create a new string out of that array and make that the link the search/next page button goes to.
Does that sound like it would work? If so, how do I delete those specific array entries, and how would that array then be stored? Would the array lose those entries and calling a deleted entry "$array['1']" for example result in an error, or does deleting entries in an array move everything up one to fill the gap?
Apologies if I'm asking too many different things in one post here!

PHP: How to view product details in a separate page without having to create a new file for each product?

So I've created a market place page that lists all products from a database. They each have buttons to view their details (price, description, etc.) but I don't know how to let the user view the details without creating a new php page for each product.
I've tried using $_GET (for example, the url would read www.websitename.com/page.php?productname=name), but as I need to pass variable for prices as well, I can't use GET or a user could modify prices within the url. I've also tried using POST, but I would like the user to be able to bookmark or refresh the page without the variables clearing. I've tried setting $_SESSION variables for things like prices to avoid this issue, but as the session variables are still inevitably defined by POST or GET, I still run into both issues listed previously.
I've also tried using SQL queries so that the prices are received on demand from a database when the page (viewproduct.php) is loaded, and it searches for the product name that matches the $_GET['name'] variable, but the products are stored by category in different tables, and I can't check against all tables because they don't have equal dimensions.
So my question is simple: what is the standard for doing something like this? How can I write php code so that a user can bookmark or refresh a page with product information, without being able to edit any of the variables, and without me having to create a file for each and every product?
Thanks in advance!
Actually, you can link and change everything corresponding to your products. For example, you can link your images this way:
echo("<img src=../imgs/" . $_GET['product_name'] . " alt=". $_GET['product_name'] . "/>");
And display in the same way every information your have in your database such as comments corresponding to your product, presentation, price, reviews, ranking, etc...

PHP pages dynamically?

I'm not asking any coding question, I'd just like to know what the 'standard' practice is. If I had PHP spit out a table or list of names/items from a database and then when you click on one of the names/items it then takes you to that page, should these pages be made dynamically or not?
For example, if it was a list of musicians, and then when you click on a musicians name it takes you to a list of their albums and then when you click on the album it has the track list, should these links/pages be done dynamically or is it best to make each musicians page normally. Basically, what I'm asking is should PHP be used to make each page or just to echo out the tables from the database.
Thank you.

Passing Variables between PHP pages then performing a MySQL search

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.

How do I display varying pieces of data through a fixed template without making multiple slightly altered 'include' files?

I have a database of different stores.
When a user clicks on a store name, I want an Ajax function to run displaying information about the store in a different div.
Information categories for all stores wll be the same: products carried, location, general information, etc.
I could easily make it so that each different store uses a different file name as an argument to the ajax function, and all files would have the same layout/format but with different data.
However i feel like this is bad form. How can i make it so that i have one fixed template and all that changes is the information specifics imputed into the template?
Please note that the store information display pages will also need to be able to have clickable links of their own (i.e. click on location and a google map pops up).
Is it something to do with XML? I dont know much about it.
Instead of returning a template, return the data.
So it says getstore.php?id=2 which returns a json string
{"name":"my store", "info" :"blah"}
Then you use java script to insert a new div, populated with that data.

Categories