PHP Trick to generate variables from FORM
I hate to write code, really! hehehe … I often hand-code my code, even in Visual Studio! Heck, I don’t know why I love this style of coding. The one that I don’t code is when approching Database, I often use tools to help me generate code, just say PHPMyAdmin.
Sometime, I hate to write code especially when dealing with something called “Form Gathering” with any method like GET/POST. If I handle only one to six data, I will write it happlily ever after. I was create a webform consisting 27 inputs, yeah, 27 inputs. I assume you understand about HTML Form.
PHP is very handy programming engine and I love to make a shortcut to deal with those 27 inputs. I create a code which gathering all inputs and my PHP code create another code.
This is the code :
<?php
foreach($_POST as $key => $value) {
echo $key.' = $_POST["'.$key.'"]; <br />';
}
?>
I don’t like escaping too much so I use echo and concation operator (the dot). So what the code doing?
- $_POST is an array and contain collections of element in form. So, we could create loop to access the individual item. If you use GET method, please change the $_POST into $_GET.
- You may be wondering why I create $value variable which I never be use. The reason is very simple, I just want to know the “name” of form elements (input) not the value.
- Echoing the the line to create a new code that I could copy paste into another PHP File.
Creating little tool like this often help developer increase his/her productivity. Why? because you can reuse it for next projects.
Happy Programming with PHP!.
ASP.NET Typed Dataset Common Problem
If you’re new to ASP.NET you will introduced to Typed Dataset, what the heck is that? It’s feature which available in Visual Studio. I assume you already understand how to create Typed Dataset.
Most of programmer tell me that Typed Dataset is very fragile to any modification. Yes, indeed it is. Why I have such conclusion? It started when I starting to use Typed Dataset. The famous “Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.” error will make you desperate and force you to write a class to accesing your table. If you think it’s simpler to write your own code than automatically generated by ASP.net, please do so. It will save your time a lot.
Well, some people will find the answer why it happen to their Typed Dataset. Typed Dataset saves our time to write a Data Access Layer (DAL) class, really!. I just run a wizard the voila you could use it in any database component in your Visual Studio toolbox. Okay, I admit it’s not easy when you found an error in dataset. Okay, let’s start!
First, I found that we must consistent in any SELECT command to query (TableAdapter). Let’s take simple case, you start with this SELECT command :
SELECT username, realname FROM myUsers
When you want to filter using WHERE clause for the user by userID then your query must like this :
SELECT username, realname FROM myUsers WHERE userID = @userID
Did you see what the same? yes! the field must exact like the first query when we create new TableAdapter. Why? it will save you from unnecessary error. I don’t know the technical reason, because this tool is really complex.
Secondly, stay away from Table Changes. When developing process often we add or change the column in a table, because many reasons. Often people meet the famous error that I mention above after changing the table and the corresponding DataTable/Table Adapter in Dataset. By trial and error I found several solution to this :
- You could always change the MaxLength on VarChar / NVarchar to restrict user when inputing data to database.
- If you want varchar data type to ntext / text, please user varchar(MAX) / NVarChar(MAX). Then set the MaxLength with -1 value to allow release the length restriction. You will void the famouse error by stay using NVARCHAR(MAX) rather than NTEXT.
- Plan your database and the queries to heart before starting to build Typed DataSet … hehehe. What else better?
Thridly, DataType Conversion Error. The error often show like “System.InvalidCastException: Unable to cast object of type ‘System.DBNull’ to type ‘System.String’.“. How you get rid of this? don’t worry, it’s ASP.NET fault not the DataSet. You could this :
- Create a variable to store the data if exists, fill the variable with null.
- Check your field value
Example in C# :
string _realName = null;
If(!myUsers.IsRealNameNull()) _realName = myUsers.RealName;
labelRealName.Text = _realName;
myUsers is the TableAdapter, and IsRealNameNull() was automatically generated by ASP.NET.
Hopes this journal will help you out, I found many problem like this. Microsoft must fixing this Typed Dataset complexity into simpler form that human and developer understood.
Happy Programming, Folks!.
My Favorite Web Editor
I love hand code but I need powerfull and flexible IDE (Integrated Development Environment). I search on google and I found good IDE for my self. The software is PSPad editor. PSPad editor is free and you could it for your commercial project.
PSPad have powerfull scripting engine which allow us to create our own menu and functions for our dialy job. The scripting engine use VBScript and JScript. You could the most comfortable scripting language for your own to expand the PSPad functionality. Integrated FTP client help me to edit my files on the web.
This site template created on PSPad. Why I choose PSPad? the reason is I don’t want extra huge mega size application and memory eater. It’s light and fast. PSPad still have limitation in threading and you could fell it when trying accessing to FTP server using the internal FTP client. I wish PSPad team will provide the source.
Want to try it? go to http://www.pspad.com/ and install in your computer. It’s awesome editor, many PHP developer use this tool.
Happy Programming