...

View Full Version : Programming within PHP



amof
11-15-2011, 04:06 PM
Okay I fear Im going to have a hard time explaining my problem but here it goes

I'm trying to create a dynamic system with the ability to be able to add custom fields to the database without creating new columns.

Heres my stucture so far.

tables | rows
-----------------------------
db_structure | fieldname
db_data | fieldname, data

* We can create a new field by adding a row to db_structure
* Any data we wish to record is recorded to db_data.

This is a very simplified description of what I am doing.

I was wondering if there is a way of giving the end user the ability to program within the webpage by ultilizing these fields, I only need basic opperator and math functions but I think its important I give the user a 'textfield' so they can write actual syntax.

Let me know your thoughts, maybe I'm trying to push PHP too far but i'm thinking I could somehow parse their input and perform operations based on that?

Fou-Lu
11-15-2011, 06:49 PM
You are not pushing it too far at all. This is the correct way SQL wise to develop unknown fields available. Such thing would be used to write something like custom profile fields for your users to write in. You may need/want to provide additional information such as the datatype allowed, validation rules such as length, and so forth too which will help when accepting data to input.

As for evaluation, that is more tricky. You can either write a parser of your own to scan in data (look and using the sscanf function for this) which is my recommendation, or if its valid PHP code you can use the eval function. Eval is spelled wrong, it should really be evil :). All kidding aside, eval is a very dangerous function to run without a great deal of control (plus eval must be valid PHP code), since it will arbitrarily execute any code provided to it. On the other hand, writing a parser is a great deal of work, especially if you want to add more complex mathematics and taking into account precedence. Both procedural and OO will prove this to be a somewhat difficult task.

amof
11-15-2011, 08:24 PM
Thank you for your response Fou-Lu, you've definately helped me alot here and sent me in the right direction.

I think my best option is to provide some basic syntax, operators and custom functions and then parse the users input to generate valid php code. I really don't want to give them the ability of excuting raw php.

Thanks once again!


You are not pushing it too far at all. This is the correct way SQL wise to develop unknown fields available. Such thing would be used to write something like custom profile fields for your users to write in. You may need/want to provide additional information such as the datatype allowed, validation rules such as length, and so forth too which will help when accepting data to input.

As for evaluation, that is more tricky. You can either write a parser of your own to scan in data (look and using the sscanf function for this) which is my recommendation, or if its valid PHP code you can use the eval function. Eval is spelled wrong, it should really be evil :). All kidding aside, eval is a very dangerous function to run without a great deal of control (plus eval must be valid PHP code), since it will arbitrarily execute any code provided to it. On the other hand, writing a parser is a great deal of work, especially if you want to add more complex mathematics and taking into account precedence. Both procedural and OO will prove this to be a somewhat difficult task.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum