Xml template file, pinpoint data location on xml file - php

A simple three step task
Get data from MySQL
Create XML-file based on data
Send XML via Ajax and process responses
but...
In my project there would be a multiple different xml-content and that is not good if they are hardcoded in cms. It would also make lots of additional work if those files content is needed to change and it is very hard to pinpoint exact location for data in different XML-templates.
I considered to get first used column names from database as an array.
Then xml-template content is parsed to html form and select-list of columns (witch contains all needed db table column names) are positioned next current element or property (there is needs for lots of additional fields but i keep explanation simple).
Value of single element or property can be now easily select and when the form is saved, html-form content is combined back to XML but select-list values are coded inside xml-elements like {#name}, {#job}, {#description}.
On third step data is pulled from db as associative array. Then every coded position is replaced with corresponding array key content.
Does this make any sense or can it be done other way?

Related

Dynamic generation of <option> list without separate php file

I need to create a specific <option> list on a form, depending on which option is selected by the user on the previuos <select> list. In this case, as part of a school ERP, when user selects course the system should load on the next field only the subjects related to this course.
This can be done by ajax, using the 'change' method, posting on real-time course ID, and giving back as the result of the query, the list of related subjects wich is loaded on its <select> list.
Is making this by having a separate file instead of trying another options the best practice to solve this cases? I mean, if the best way to do this is by having an specific file for every list that must be dinamically generated, then I'll make it this way, but I feel that having a php file for every single dynamic list that should be generated on real-time maybe is not the most efficient way to do this (got many form fields that should behave this way).
One PHP file will do.
Use this tutorial to learn about URL parameters:
http://html.net/tutorials/php/lesson10.php
Combine with this tutorial on how to retrieve data from a database:
http://html.net/tutorials/php/lesson20.php
The idea here is, using AJAX, instead of fetching your list like this:
http://example.com/list/thing1.php, http://example.com/list/thing2.php
You use a single PHP file, like this:
http://example.com/list.php?foo=thing1, http://example.com/list.php?foo=thing2
So instead of having a php file for every single dynamic list you can have one file that loads and format the specific data you need dynamically.

Storing an array in a MySQL table

I have a 5 level multidimensional array. The number of keys in the array fluctuates but I need to store it in a database so I can access it with PHP later on. Are there any easy ways to do this?
My idea was to convert the array into a single string using several different delimiters like #* and %* and then using a series of explode() to convert the data back into an array when I need it.
I haven't written any code at this point because I'm hoping there will be a better way to do this. But I do have a potential solution which I tried to outline below:
here's an overview of my array:
n=button number
i=item number
btn[n][0] = button name
btn[n][1] = button desc
btn[n][2] = success or not (Y or N)
btn[n][3] = array containing item info
btn[n][3][i][0] = item intput type (Default/Preset/UserTxt/UserDD)
btn[n][3][i][1] = array containing item value - if more than one index then display as drop down
Here's a run-down of the delimiters I was going to use:
#*Button Title //button title
&*val1=*usr1234 //items and values
&*val2=*FROM_USER(_TEXT_$*name:) //if an items value contains "FROM_USER" then extract the data between the perenthesis
&*val3=*FROM_USER(_TEXT_$*Time:) //if the datatype contains _TEXT_ then explode AGAIN by $* and just display a textfield with the title
&*val4=*FROM_USER($*name1#*value1$*name2#*value2) //else explode AGAIN by $* for a list of name value pairs which represent a drop box - name2#*value2
//sample string - a single button
#*Button Title%*val1=*usr1234&*val2=*FROM_USER(_TEXT_$*name:)&*val3=*FROM_USER(_TEXT_$*date:)&*val4=*FROM_USER($*name1#*value1$*name2#*value2)
In summary, I am seeking some ideas of how to store a multidimensional array in a single database table.
What you want is a data serialization method. Don't invent your own, there are plenty already out there. The most obvious candidates are JSON (json_encode) or the PHP specific serialize. XML is also an option, especially if your database may support it natively to some degree.
Have a look at serialize or json_encode
The best decision for you is json_encode.
It has some advantages for json_encode beside serialize for storing in db.
taking smaller size
if you
must modify data manually in db there will be some problems with serialize, because this format stores size of values that has been serialized and modifying this values you must count and modify this params.
SQL (whether mySQL or any other variant) does not support array data types.
The way you are supposed to deal with this kind of data in SQL is to store it across multiple tables.
So in this example, you'd have one table that contains buttonID, buttonName, buttonSuccess, etc fields, and another table that contains buttonInputType and buttonInputValue fields, as well as buttonID to link back to the parent table.
That would be the recommended "relational" way of doing things. The point of doing it this way is that it makes it easier to query the data back out of the DB when the time comes.
There are other options though.
One option would be to use mySQL's enum feature. Since you've got a fixed set of values available for the input type, you could use an enum field for it, which could save you from needing to have an extra table for that.
Another option, of course, is what everyone else has suggested, and simply serialise the data using json_encode() or similar, and store it all in a big text field.
If the data is going to be used as a simple block of data, without any need to ever run a query to examine parts of it, then this can sometimes be the simplest solution. It's not something a database expert would want to see, but from a pragmatic angle, if it does the job then feel free to use it.
However, it's important to be aware of the limitations. By using a serialised solution, you're basically saying "this data doesn't need to be managed in any way at all, so I can't be bothered to do proper database design for it.". And that's fine, as long as you don't need to manage it or search for values within it. If you do, you need to think harder about your DB design, and be wary of taking the 'easy' option.

How to generate FORM that reflects MySQL table?

I want to generate HTML form based on MySQL table field. The generated input should reflect the type of the MySQL column, e.g. VARCHAR = text, TEXT = textarea, ENUM = checkbox, SET = radio, etc.
I think this kind of Lib is not available
But ya you can do it by making one table in your database that conatains details about form and retrive it and make HTML code dynamically
This is a two step problem.
First you need to analyse the database table. You can get column names, type, etc. using SHOW COLUMNS.
You need to generate the form based on the retrieved information.
Generating form can be as simple as iterating the array received from SHOW COLUMNS query and building HTML along the iteration, or using existing form generation libraries, e.g. Zend_Form or Dora.
Depending on your needs, there is existing software for reflecting and manipulating the whole database, e.g. phpMyAdmin. Furthermore, some frameworks provide means of reflecting the whole model (i.e. database table) in a form for CRUD operations, e.g. Symfony Forms.

Get values from html table using codeigniter

How can I get the values from a html table and pass that to a controller in codeigniter?
I'm passing an array to a view. I walk that array and display its content in a table where the user can alter the table. For example, adding a row, or deleting one, but changing values is a possibility too. But then the user saves the data by pressing the button 'save'.
How do i get the data from the table and pass that to php?
When the "save" button is pressed you trigger a javascript function that uses the DOM to get at the values you want. You "select" a table cell and then use innerhtml to get the string inside it. Using unique html id's on your cells will make this easy. Collect all this data in an array and "send" it to your PHP via an Ajax POST request.
That's one way of going about it. Another way is to use Simplehtmldom where you use PHP instead of JS to get your values. This may be easier / more difficult depending on how good your JS is, but the methods are the same. Simplehtmldom uses a syntax that's quite similar to jquery's and in this case you put the load on the server instead of the client.

Sorting a list in PHP

Alright, so I've just (almost) finished my first little php script. Basically all it does is handling forms. It writes whatever the user put in to the fields of the form to a text file, then I include that text file in the index of the little page I have set up.
So, currently it writes to the beginning of the text file (but doesn't overwrite previous data). But, several users wants this list to be alphabetically sorted instead. So what I want to do is make whatever they submit fall into the list in alphabetical order.
The thing here is also that all I use in the text file are divs. The list is basically 'divided' into 3 parts. 'Title', 'Link', and 'Poster'. All I have done is positioned them with css.
So, can I sort this list (the titles, in this case) alphabetically and still have the 'link' and 'poster' information assigned the way they already are, but just with the titles sorted?
It don't use databases at all on my site, so there is no database handling at all used in this script (mainly because I'm not experienced at all in this).
I would also suggest storing the data in the file as either XML or JSON. PHP can sort the records easily and the sorting will be preserved in the file when the data is read back in.
For example
file_put_contents("/tmp/datafile",json_encode($recordset));
and when reading the file back in
$recordset = json_decode(file_get_contents("/tmp/datafile"));
edit
but seriously if you have customers and are charging them for your experience and time, use a database, there are many out there (a few mentioned already)
MySQL
sqlite
PostgreSQL
Oracle
MSSQL
If you really don't want to use a database, even a one-file database (such as sqlite), you can group the three fields using a separator such as _ and sort this single-field list
If this is a small project for some friends or something and the file shouldn't ever have more than maybe a few hundred lines, the functions you're looking for are "file" and "usort".
If you make sure each row is on its' own line, you can load the lines into an array with "file". You can sort the array using a function to compare items with the "usort" function.

Categories