display several tables in pagination - php

I am using cake php and mysql.
I have several tables that all link up to a table called relates. The relates table holds the primary ids for all the other tables. example I have a table called clients its primary key is called id in the relates table its called client_id.
heres my deal i created a crud application in cakephp it works great, but it only displays information via pagination for one table. I would like to use pagination to display several tables at once so i can display my clients table and another tables like clientsphone table ect. I tried a few things out but i am completely lost. In my relates table i created pagination for it and it shows the primary ids when you click the primary id is goes to the proper record and displays the information from that table, but display just numbers wont help anyone if you dont know what it is.
how can i display several tables at once for the relates table ? any advice would be great

This really depends on what you're looking for.
If you'd like to have all the data at hand but not necessarily on screen at once, a great solution would be to use jQuery's UI Tabs. You could throw each table in a separate div and easily toggle them with the tabs.
You could display multiple at once a la a stock trading application. But from a UI standpoint that can get ugly in a hurry.
How about dynamically building one gigantic table via HTML or JSON string then using jQuery's datatables to paginate, sort, filter, and show/hide columns? That probably would give you the best of all worlds while still making it readable to your users. You could also use Datatables and tabs together to get more than one at a time. Datatables is the easiest way I can think of to make a standard table more user friendly quickly.

Related

How to add new columns if needed

I am new to MySQL databases and I'm trying to create a web stock and production database, using PHP.
In this inventory software, I am trying to create a table in which products are created and also their components inserted. But if a product has a different number of components, I wanted to know if there is any way to add more components columns from the management page of the website.
Another thing is that these components are taken from another table, and as long as a new product order is created, the quantity of those used components should be subtracted from the components table. (but this is a major issue, solving the first issue should be enough for now).
Yes, you can add, or remove, columns from a database table at any time.
However, I would not do this. You have to try and design a database that can handle products with a varying number of component. Normally this would be done this way:
Create a table for your products.
Create a table for your components.
Create a linking table product_components, to indicate what components a product consists off.
See: Using linking tables for many to many relationships in MySQL
You need to know about relationships in mysql. There are basically 3 types(some may say 4 though) of relationships available in a relational database like mysql.
Here according to your description you can use
1 to many
many to many
relationship in your database. This may help you-
https://afteracademy.com/blog/what-are-the-different-types-of-relationships-in-dbms

Best way to map a mysql table to new table with different field names

I'm facing a migration problem on the job. Right now we have a single database which is used by our website and by a backoffice tool.
As part of trying to improve our code in the website, we've desgined a new schema for several tables in the database.
As an example let's take the products table. It's fairly standard with fields like description, name, product code, price, creation date etc. We now have a new table, let's call it better_products. The problem is, we can change the website code all we want, but we can't touch the backoffice tool's code which relies heavily on the old products table.
We're going to end up in a situation in production where the backoffice tool is writing to the old products table, and the website is reading from the new better_products table. The question is, how do we keep both of them in sync? I've been googling around for some time now, and by far the most common solution is to use triggers, and map the incoming data to the new table. I've written the AFTER INSERT trigger for the products, but when I went to write the UPDATE trigger it turned out there's no way to iterate over the fields that changed inside the trigger and map them over. This means writing out the fields by hand a la 'IF NEW.fieldName <> OLD.fieldName THEN' which is ugly and requires listing the fields out by hand.
Is there a better way? a semi-better way? anything except writing this out field by field?
Is there a better practice than using triggers?
Please don't suggest changing the backoffice tool as this is not a realistic option right now. It's planned, but not soon enough for us to be able to wait for it.
Create a view in the mysql database called better_products that is a select statement on the old product but with aliases for the column names that have changed.
Eventually, you can update the code in the backoffice app, to use this view. Once both systems are using the new view, that view can be replaced by an actual table called better_products that has all the data from the old table copied over.

CMS database schema

I'm building a custom CMS system usign PHP and MySQL. My CMS will have pages. Each page is of a type. Each page type has assigned some properties of different types (number, text, etc.). I'm currently creating the database for storing these things - page types, properties, pages and property values. The first solution is to have 4 different tables:
=pages=
page_id
page_type_id
page_name
=page_types=
page_type_id
page_type_name
=page_properties=
property_id
property_name
=page_property_values=
page_id
property_id
property_value
The only problem is, that I would have to save all kind of properties to the same data type filed (for example varchar). What I really want is to save boolean data to boolean field.
My second solution is to create a separate table for each page type. For example:
=page_type_text_page=
page_id
title [varchar]
content [text]
active [boolean]
Could somebody help me, how to modify this structure in order to get the right solution?
A third option might be something in the lines of
property_types
property_type_id
property_name
property_datatype
page_property_values
page_id
property_id
property_stringvalue
property_boolvalue
property_intvalue
property_datetimevalue
...
..where you'd use just one of the value columns for a certain page property and leave the rest empty. This way you would be able to get all the properties for a page in a single call with a couple of joins and then programatically fetch the value from the apropriate column by looking at the property data type.
I would like to help you with some of my experiences:
Do not build your own CMS - it may not be a good idea. It is very complicated to do it right. Use/modify some open-source CMS (Wordpress, Joomla, etc) to get what you want.
If I would be creating a new CMS do not use PHP and MySQL combination, because it's already done, and it's done pretty good with great community. Maybe you can try something thats not done yet - Node.js CMS, Single page application CMS and use some mongoDB etc.
If you really want build your own CMS based on PHP MySQL and don't know right database structure, inspire yourself with open-source CMS, like I said Wordpress, Joomla, Drupal etc. Its more complicated than what you describe, but you can use only what you want.

Template Design / Architecture

I am looking into creating something similar to a form template system on my web site.
As an example, say I want the users to be able to create form templates (similar to Wufoo, they can define any number of inputs, etc). Then from these created templates, anybody would be able to use these templates, fill them out, and therefore create a number of form instances (this would be possible for each user-defined template). Also, there would be no limit to the number of templates and instances created.
Purely from a server/persistence perspective, what is the best way of creating a system like this? Would I need to create a new database table for each created template and then insert the form instances as records into the table? How well would this scale?
This topic is very broad. As to the scalability, Wufoo (and other form creation websites) currently work. So a brief answer is, it's already being done, so scaleability shouldn't be an issue. However, random generation of multiple tables will get out of control very quickly.
If you are not at a point where you are having this problem, I would first build the system with single tables and UUIDs for the Primary Keys. This makes them moveable later on. Then, if table sizes become an issue, you can split out the tables anyway you see fit. For example, you can have all of the customes whos last name start with A in the a_forms table.
As for the structure of the tables, you would build these as a ONE-TO-MANY. One form can have many elements. The elements can all be predefined (i.e. text, text area, radio button, check box, submit button, etc.) When someone builds a form, you can serialize() the form elements and save them in the table. When the form needs to be rendered, you unserialize(), parse the elements and build the form.

MySQL Status Model -- Best Implementation?

So I'm working on a framework-esque system with one of my co-workers. Our current challenge is how to best implement statuses. Oftentimes, a status will carry with it unique data (a color for a table row, or text to be displayed to a user. etc). Currently, we have a statuses table which contains all this data. Contained in that table is a column: "css_class", which, whenever a record has that status, the specified CSS class is attached to the element (in this case a tr). Also, in order to assign another record a specific status, a foreign key is specified in that database table (in this case, a user has a specific status. So in the users table, there is a statuses_id foreign key). This implementation works alright, but there are a few problems. First, what if I need to perform a specific action in PHP if a record is in a specific status? The way we do it now is something like this:
if($user->status==0)
{
//execute some code
}
This really doesn't work well if statuses can change. Change one status, and the associated code either breaks or behaves differently than intended.
The other issue, and the main reason for posting a question is that the table contains the column "css_class". This is very versatile and allows us change the style of a specific status very quickly. But we really dislike the idea of putting code inside a database. Perhaps having CSS classes in a database isn't necessarily a bad thing, but I really don't know what the common practice is. Any ideas?
EDIT:
What I've gathered from the first few answers is that I should keep all my view stuff out of my model stuff in order to maintain an MVC framework. My argument is that if I keep the css_class name out of the database, then I'm checking the status id in the view in order to decide which class to assign it. So if I put the class in the database, I'm putting View information in the Model. If I don't put CSS classes in the database then I'm putting Model information in the View (checking which ID it belongs to). So by not muddying up the Model, I muddy up the view instead.......
The most elegant way I've seen this solved so far (and I've worked with a few MVC implementations now) is to store only the relevant data in the database. E.g. you'd store status="red" in the database, and leave it up to the view to know what to do with a red status, in terms of CSS. The problem is then solved by designing a sufficiently advanced View layer that creates reusable structures -- that way you don't need to always be updating things on a page-by-page basis when the css changes.
Passing this information up to the Model somewhat defeats the point of the content/presentation separation, because now your code needs to know to pull presentation information off the database and forward it along to the View level or, shudder, you'll be pulling that stuff from the database right in your View layer code, which makes maintenance a nightmare, as you've now lost control over the information flow.
If you want to continue your paradigm of storing this in the DB, you could make another table that maps VARCHAR names of the statuses to their corresponding INTEGER IDs.
However, if this was my framework. I would not be storing view information like this in the database. This would be handled by the V of my MVC setup.
From a data modelling point of view:
Have a different table for each "kind" of status; keep user statuses separate from page statuses (for example) - group the like entities together.
Don't put the CSS classes into the database, but use some form of status indicator - this could be an ENUM column, if you know the set of possible statuses up front. Transform this into the appropriate CSS class in the view layer. You don't want to end up in a situation where your CSS can't be changed because some data in the database prevents it.

Categories