PHP Mysql database MVC structure advice needed - php

I am trying to figure out the best way to have a page dynamically know which data to output.
I have a index.php that I want to be able to pass some $_GET variables into it and then based on that know what to display. Aside from the content being different the type of the content is also different I have products and other types of pages like blog posts etc. The products are stored in different tables then the other pages. and also have a slightly different table structure as well.
currently I have the following tables.
Subjects, Pages and Categories.
Table = Subjects
id | menu_name | menu_number | category | menu_position | active
Table = Pages
id | subject_id | category_id | page_name
Table = Categories
id | category
These are the main tables that outline the main structure of the site. I then have secondary tables that contain the actual data for the pages.
posts, post_details, post_pypes and
products, product_details, product_types and product_specs.
What I want to active is that a variable will be send along in the query string and based on this the application will know what information to display .
My question is how can I make a table that will catalog all entries (posts and products) with a unique id and that will be the only variable needed to for my application to determine how to proceed. i.e. query the table int he database that is holding that unique id and tell the application its a "product" and the product id is X and to continue querying the needed tables for the info.
Thanks in advance.

I highly recommend looking into a secure, structured environment like CodeIgniter to accomplish whatever it is you are trying to describe above. The scope of that question is really broad which likely indicates that you would profit from a prebuilt framework of some kind.

Related

Trying to understand database schema, particular WordPress Comments

I am having trouble fully understanding the schema of the WordPress comments and commentmeta tables, and how they are linked together.
I'd like to learn by making a custom row in each table (wp_commentmeta & wp_comments).
WordPress Database Schema
Following is the example I am working with.
wp_commentmeta:
meta_id | comment_id | meta_key | meta_value
2 1352 verified 1
What does the meta_value denote in the wp_commentmeta table? Is this a rating system 0-5, or similar?
wp_comments
comment_ID | comment_post_ID | misc_cols --- | user_id
2,1352,Waldo,test#test.com,"",127.0.0.1,2014-11-15 00:18:39,2014-11-15 04:18:39,"test comment",0,1,"user_agent","",0,657
comment_type is an empty field, third from last. I'll just tried adding "comment" there, no luck.
The review does show on the backend and the product page however, the product page says "Reviews (0)." The reviews are not being counted on the product page.
Would you please explain this to me?
meta_value in the postmeta table is type-agnostic. What that data represents depends on which plugin/function stored it and what it wants it to mean. You can store integers, dates, strings, or PHP data structures, WordPress does not care, and stores them all as strings internally. In your case, I'm guessing that 1 means the user is verified and 0, NULL or no row means the user isn't verified.
comment_type is similar to post_type. If you want to add a special kind of comment (a review in your case), you'll have to figure out what the software you are using expected as comment_type. Look at existing reviews, what do they have set as comment_type?
In order for the reviews to show the count, I had to navigate to comments, edit the comment, and update the comment (with no changed fields). Perhaps the HTTP server needed to be reloaded, or WooCommerce needs to be reloaded in some way.

PHP/MySQL blog system

I'm making a blog system and I want to add 'tags' to my blogposts. These are similar to the tags you see here, they can be used to group posts with similar subjects.
I want to store the tags in the database as a comma-separated string of words (non-whitespaced strings). But I'm not quite sure how I would search for all posts containing tag A and tag B.
I don't like a simple solution that works with a small database where I retrieve all data and scan it with a PHP loop, because this won't work with a large database (hundreds if not thousands of posts). I do not intend to make this many blogposts, but I want the system to be solid and save worktime on the PHP scripts by getting right results straight from the database.
Let's say my table looks like this (it's a bit more complex actually)
blogposts:
id | title | content_html | tags
0 | "hello world" | "<em>hello world!</em>" | "hello,world,tag0"
1 | "bye world" | "<strong>bye world!</strong>" | "bye,world,tag1,tag2"
2 | "hello you" | "hello you! :>" | "hello,tag3,you"
How would I be able to select all posts that contain "hello" as well as "world" in the tags? I know about the LIKE statement, where you can search for substrings, but can you use it with multiple substrings?
You can't index a field of csv values in a meaningful way, and SQL doesn't support being able to find a unique value in a field of CSV values. Instead, you'll want to set up two more tables, and make the following alteration to your table.
blogposts:
id | title | content_html
tags:
id | tag_name
taxonomy table:
id | blogpost_id | tag_id
When you add a tag to a blog post, you will insert a new record into the taxonomy table. When you query for data, you'll join across all three tables to get the information similar to this:
SELECT `tag_name` FROM `blogposts` INNER JOIN `blogposts_taxonomy` ON
`blogposts`.`id`=`blogposts_taxonomy`.`blogpost_id` INNER JOIN `blogpost_tags` ON
`blogposts_taxonomy`.`tag_id`=`blogpost_tags`.`id` WHERE `blogposts`.`id` = someID;
//UPDATE
Setting up the N:M relationship gives you a lot of options during the build out of your application. For example, say you wanted to be able to search for blogposts that were all tagged "php." You could do that as follows:
SELECT `id`,`html_content` FROM `blogposts` INNER JOIN `blogposts_taxonomy` ON
`blogposts`.`id`=`blogposts_taxonomy`.`blogpost_id` INNER JOIN `blogposts_tags` ON
`blogposts_taxonomy`.`tag_id`=`blogposts_tags`.`id` WHERE `blogposts_tags`.`tag_name`="php";
That will return all blogposts that have been tagged with the "php" tag.
Cheers
If you really wanted to store the data like this the FIND_IN_SET mysql function would be your friend.
Have the function twice in the where clause.
But it will perform horribly - having a linked table one-to-many style as already suggested is MUCH better idea. If you have lots of the same tags a many-to-many could be used. Via a 'post2tag' table.

One MySQL Table for all resources VS multiple tables for each resource

I am developing a web application where users can create the following resources/contents:
Events | Music | Posts | Classifieds
They have alot of fields in common, such as:
created_date | title | desc | user_id
Now I am wondering if I should create separate tables for each content, or save them all in one table, with a type_id foreign key, which points to a content_type table. Ofcourse, some distinct fields will be there which will be only used by specific content types, for those not using those fields, I can just leave it blank.
Data looks more organized with separate tables for each content type, but searching for a keyword across all tables is becoming a nightmare(with joins, unions etc). If it was just a single table, searching will be very easy.
I need that the user be able to search across all content with a keyword. He would also be able to search specific contents, for that I will do a WHERE clause on the type_id field.
I am not aware of all the pros/cons of each method, but I would appreciate if people could advice me so that I don't make the wrong decision, and have to redo everything from start.
maybe think of using the "has a" relationship. For instance, an event "has a" "web item handle" attached to it, and a "web item handle" is a thing with description, created date, title, 'owner' etc...
Unless they truly have identical data, I would use separate tables. Having one table with some fields only used by specific content types is really not very good database design.
If you really want one table with the basic data, you could create one as you suggested with a content_type and the common fields, and then have 4 separate tables for each of the types with the other distinct fields, then do an inner join when you select the fields for that type. But personally I think you are better off just creating 4 tables.

Wordpress MySQL data retrieval Hotel booking reservation system

I am trying to create a Hotel reservation system using wordpress. Now, I know all the queries for data retrieval in a non-wordpress mode but I am facing some data-organization issues in dealing with wordpress. My hotels details are stored in the posts table and as custom fields for the hotel. For rates of the hotels, I created a different table according where rates vary according to months. I'd like the data to be displayed like this in my search results page:
Hotel_1 Name: (Will come from Post Name)
Hotel_1 Details: (Will come from custom fields)
Hotel_1 description: (will come from excerpt)
Room_1 Name for Hotel_1: Total Rates for selected dates
Room_2 Name for Hotel_1: Total Rates for selected dates
----------------------------------------------------------
Hotel_2 Name: (Will come from Post Name)
Hotel_2 Details: (Will come from custom fields)
Hotel_2 description: (will come from excerpt)
Room_1 Name for Hotel_2: Total Rates for selected dates
Room_2 Name for Hotel_2: Total Rates for selected dates
----------------------------------------------------------
and so on and so forth.....
----------------------------------------------------------
Pagination>>
----------------------------------------------------------
My rates table looks like this
mysql> select * from rates;
+-------------+---------+---------+-------------------+------------+------------
+-----------+--------------+-----------+------------------------------------+
| primary_key | post_id | room_id | room_type | start_date | end_date
| adultRate | extraBedRate | childRate | inclusions |
+-------------+---------+---------+-------------------+------------+------------
+-----------+--------------+-----------+------------------------------------+
where post_id = ID of the post in wp_posts table
room_id = ID given to a room
room_type = Name of the room
1 post (post_id) may have several room_types.
Any help is much appreciated. Thanks.
I just need help on
a. how to organize or create relationships among table to get desired result, &
b. how to get them displayed in wordpress. Some classes or functions need to be considered.
I am very very new to php and mysql and this is my first assignment. My skill level is such that I can make changes to a code but right now it is very difficult for me to write from scratch. However with the help around here I intend to learn.
thanks
You're approaching this problem from the wrong point of view. There's a better way to handle this kind of information on WordPress and it doesn't (necessarily) involve creating new tables. WordPress handles CRUD natively so you will use pre-made functions instead of writing it from scratch.
Also, forget about using the original posts structure to do this, it can be a major headache. Instead, read everything you can about Custom Post Types and specially this awesome tutorial from Justin Tadlock

Select and Browse Categories and Subcategories Stored in Separate Tables

I'm looking for a solution to list and browse categories and subcategories and their records (classified ads), when you store category levels in separate tables. In the past I have worked with the adjacency model but I have to stick to this database setup now, and it is new to me. I'm using php and mysql.
The site is a classified ad site structured the common way: it has the main category list on its homepage, when you click one of the category links then only its subcategories are listed and the ads that belong to this category, and so on, at every level.
I'm a bit confused in the following areas:
How do you construct
the category links when browsing
categories in order for the script to know which table it should select categories from if I consider the below
mysql structure? Do I need separate
parameters at every category level I access
like e.g: "mysite.com/?cat2=4" when
accessing category "4" in the cat2
table and "mysite.com/?cat3=9" when
accessing category "9" in cat3 table
in order to identify category
levels? If separate parameter not
needed, then how can php and mysql
tell what table you have to select
the categories from?
And most
importantly in this case, what is
the best way to construct SEO
friendly links? And how will mysql know
which table to select categories
from? I would like to use the most
simplest solution that is possible
like:
mysite.com/electronics/television/sony.
As far as I know, I have to include
at least the cat_id
somewhere in the link... where do I put it? and do I have to include the number of level as well? To
complicate it more the category
names are in foreign language with
accented characters (though I
created a function that changes
accented characters into latin ones
on the fly when generating category
links) so I think it is best to
select them by their ids.
How is a sample mysql select looks
like that selects the child
categories of a certain category?
How can I construct breadcrumb
navigation?
MYSQL STRUCTURE:
Table "cat1" (main category):
cat1_id | cat1_name
Table "cat2" (subcategory):
cat2_id | cat1_id | cat2_name
Table "cat3" (subsubcategory):
cat3_id | cat2_id | cat3_name
Table "ads":
ad_id | cat1_id | cat2_id | cat3_id | ad_title | ad_description
Thanks and sorry for the long post.
My favourite pattern for category (and tag) URLs:
http://mysite.com/articles/brains+zombies+legs+frogs
The + symbol is nice for tags, and friendly to spiders (and SEO). Using the text of the categories is important for both spiders and humans as it's meaningful.
As for the SQL, I suggest 2 tables for anything with categories or tags:
Categories (id, name, description)
CategoryRelationships (catID, thingID)
For any given thing, you join Categories to Things via CategoryRelationships. For example:
SELECT * FROM Things t
JOIN CategoryRelationships ON thingID = t.ID
JOIN Categories c ON catID = c.CatID
The result will be a list of Things and their categories, where you have only one definition of each category, and a bunch of links to the categories via the Relationship table.
As for breadcrumbs, they're a slightly different problem. Breadcrumbs either:
Provide navigation through your site hierarchy, or
Help the user retrace their steps
Depending on the type of breadcrumb you're aiming at, you take a different approach. For a simple site hierarchy set of breadcrumbs, you can simply parse the URL and foreach over the set of segments:
http://mysite.com/people/zombies/brains/brains
Parsing the URI would result in:
people, zombies, brains, brains
For which you would generate links to each segment.

Categories