Magento, PHP, MySQL, AJAX, Zend, cPanel or OpenSource…
PHP/MySQL
how to capture all fields and values of a form at once in php?
Sep 6th
Sometime we may have many fields in a form. We can get all field names and corresponding values at once in php nicely and field names which can be used as variables later.
For eg, you would like to save into database then create a table and make all database filed names same as form’s field names.
CREATE TABLE `student` ( `id` INT NOT NULL AUTO_INCREMENT , `first_name` INT NOT NULL , `last_name` INT NOT NULL , PRIMARY KEY ( `id` ) )
I have a form as given below:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> First Name : <input type="text" name="first_name"><br /> Last Name : <input type="text" name="last_name"><br /> <input type="submit" name="submit" value="Submit" > </form>
While you submit the form, capture all variables, values and create SQL query to save into database:
if(!empty($_POST)){
foreach($_POST as $variable => $key){
${$variable} = trim($key);
}
$sql = "INSERT INTO `student` (`first_name` ,`last_name`)
VALUES ( '".$first_name."', '".$last_name."');"
}
How to define dynamic variable in php?
Sep 6th
If you need to define variable dynamically in php then this can be done easily.
Say, your variable name will be ‘dynamic_variable_name’
Generally we define php variable normally as $normal_variable; but in this case we would like to create dynamically.
Then we have to write something like following way:
$normal_variable = 'dynamic_variable_name';
${$normal_variable} = 'New dynamic variable created';
echo $dynamic_variable_name;
// prints New dynamic variable created.
To explain it better I am giving an instance which will be very helpful to develop PHP sites and reduce some hard work.
I have a form as given below:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"> First Name : <input type="text" name="first_name"><br /> Last Name : <input type="text" name="last_name"><br /> <input type="submit" name="submit" value="Submit" > </form>
While you submit the form, capture all variables and values:
if(!empty($_POST)){
foreach($_POST as $variable => $key){
${$variable} = trim($key);
}
echo $first_name . ' ' . $last_name;
}
How to upgrade magento from ver.1.3.x to ver.1.4.x
Jun 30th
Today, I am going to explain how I have upgraded magento from version 1.3.0 (pretty old
) to version 1.4.1.0 using the same old database.
As we know its a pain while we upgrade magento each time from one version to another.
Well follow the steps as written below, hope your magento site will run without any errors.
- First of all, take backups, and run magento on testing server/ local machine, in my case it was version 1.3.0 and installed directory was “old_magento”
- Now unzip fresh version of magento 1.4.1.0 in a separate directory, say, “new_magento” directory
- Copy all directory/files from new_magento to old_directory, obviously replace everything with new files
- Now run this old_magento directory (http://127.0.0.1/old_magento), this will take time as it will reconfigure your database.
- In order to avoid time-out error, increase php execution time, I set it to 3600 seconds, memory 2048, I had around 1000 products with many custom attributes, categories and product images
- After successful database upgrade, site should run, but we may see some php fatal errors on both admin panel and frontend respectively given below
Fatal error: Undefined class constant 'Mage_Customer_Model_Group::ENTITY' in xxx\app\code\core\Mage\Core\Model\Config.php on line 1206 Fatal error: Call to a member function toHtml() on a non-object in xxx\app\code\core\Mage\Core\Model\Layout.php on line 529
- Now delete everything from old_magento directory except media directory, rename media to media_old
- Again copy fresh files / directories from new_magento to old_magento directory, rename directories media to media_temp and media_old to media
- Run again (http://127.0.0.1/old_magento your path may be different)
- Now you would see magento installation screen, use old database name, user and password etc properly.
- This installation should not take too much time, after installation go to frontend and admin panel
This trick worked for me smoothly with 1000 records, although you have to check your custom theme so that it becomes compatible with version 1.4.x that is another story
Best ways to Install magento in 15-30 minutes
May 20th
How easily can we install magento?
- If you have SSH access (Linux based server) then installing magento should not take more than 15 minutes.
Please check this how to install magento via SSH. - if you don’t have SSH access don’t worry upload via cPanel!
- First Download Magento zip file to your machine
- Login to your server’s cPanel and Click on File Manager
- A pop up will appear, select Home Directory, and check ‘show hidden files’
- Select public_html directory from left panel
- Click on Upload icon, you see at top menu
- A new window will open, now upload magento latest version’s zip file from local machine which you just downloaded
- After upload completion, click on Extract
- This will extract all magento files in public_html/magento directory
- Now move/copy all files from magento directory to root directory or public_html directory or rename the magento directory if you want to install in sub directory or don’t change the directory name then your magento installation will be in yourdomain.com/magento location
- Now create new database, add user and provide permission
- You are ready to install magento! visit yourdomain.com if you have moved/copied files to root directory or yourdomain.com/magento
- Follow all steps!
if you want to install sample data then upload sample data zip file too and follow as above steps, remember you need to move media files from sample directory to default media location and import sample database using phpmyadmin before installation.
- You may use Plesk too, but you should have proper permission, otherwise your installation will be a mess due to directory/file permission issues. Some Plesk control panel may not have unzip option, then you can use any php script like the attached one, but please note you may face some problem if you want to delete those files due to permission.
<?php
$zip = new ZipArchive;
$res = $zip->open('magento-1.4.0.1.zip');
if ($res === TRUE) {
$zip->extractTo('magento/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
?>
Otherwise you have to install through FTP which is enough time consuming process specially if your internet connection is slow.