{"id":39,"date":"2007-07-11T02:20:21","date_gmt":"2007-07-10T20:50:21","guid":{"rendered":"http:\/\/www.bin-co.com\/blog\/2007\/07\/web-installer-the-code\/"},"modified":"2007-07-11T02:20:21","modified_gmt":"2007-07-10T20:50:21","slug":"web-installer-the-code","status":"publish","type":"post","link":"https:\/\/www.bin-co.com\/blog\/2007\/07\/web-installer-the-code\/","title":{"rendered":"Web Installer: The Code"},"content":{"rendered":"<p class=\"intro\">The <a href=\"http:\/\/www.bin-co.com\/blog\/2007\/07\/web-application-installer\/\">last two posts<\/a> on <a href=\"http:\/\/www.bin-co.com\/blog\/2007\/07\/3-simple-steps-to-create-a-web-installer\/\">web installer<\/a> did not include any code. I wanted to dump all code into one post &#8211; this is it. Please note that this is what I did &#8211; you don&#8217;t have to copy my code as it is. Just look at the code and modify it according to your needs.<\/p>\n<h2>Getting Database Details<\/h2>\n<h3>The Form\/Frontend<\/h3>\n<pre><code class=\"html\">&lt;form action=\"\" method=\"post\"&gt;\n&lt;h1&gt;Installation : Step 1&lt;\/h1&gt;\nPlease provide the database connection details...\n&lt;fieldset&gt;\n&lt;legend&gt;Database Details&lt;\/legend&gt;\n\n&lt;label for='host'&gt;Database Host&lt;\/label&gt;&lt;input type='text' name='host' value='localhost' \/&gt;&lt;br \/&gt;\n&lt;label for='db_user'&gt;Database User&lt;\/label&gt;&lt;input type='text' name='db_user' value='root' \/&gt;&lt;br \/&gt;\n&lt;label for='password'&gt;Database Password&lt;\/label&gt;&lt;input type='text' name='password' \/&gt;&lt;br \/&gt;\n&lt;label for='database'&gt;Database&lt;\/label&gt;&lt;input type='text' name='database' value='nexty' \/&gt;&lt;br \/&gt;\n&lt;\/fieldset&gt;\n\n&lt;input type=\"hidden\" name=\"step\" value=\"2\" \/&gt;&lt;br \/&gt;\n&lt;input type=\"submit\" name=\"action\" value=\"Continue &gt;&gt;\" \/&gt;&lt;br \/&gt;\n&lt;\/form&gt;<\/code><\/pre>\n<h3>Backend<\/h3>\n<p>Make sure that the given database details are correct.<\/p>\n<pre><code class=\"php\">\/\/ The First step is Setting up Database connection\n\/\/\t\t\t\t\t   (the '2' is NOT a typo)\nif($_REQUEST['step'] == 2) {\n\t\/\/Save the data to the Session\n\tif(isset($_REQUEST['host'])) $_SESSION['host'] = $_REQUEST['host'];\n\tif(isset($_REQUEST['db_user'])) $_SESSION['db_user'] = $_REQUEST['db_user'];\n\tif(isset($_REQUEST['password'])) $_SESSION['password'] = $_REQUEST['password'];\n\tif(isset($_REQUEST['database'])) $_SESSION['database'] = $_REQUEST['database'];\n\tif(isset($_REQUEST['url'])) $_SESSION['url'] = $_REQUEST['url'];\n\n\tif(mysql_connect($_SESSION['host'],$_SESSION['db_user'],$_SESSION['password'])) { \/\/Try to connect to the DB.\n\t\t$QUERY['success'][] = 'Connection to Database server successful';\n\n\t\tif(mysql_select_db($_SESSION['database'])) {\/\/Select the provided database.\n\t\t\t$QUERY['success'][] = \"Database '$_SESSION[database]' selected\";\n\n\t\t} else {\n\t\t\t$QUERY['error'][] = 'The given database('.$_SESSION['database'].') does not exist. Please povide a valid database.';\n\t\t\t$_REQUEST['step'] = 1;\n\t\t}\n\t} else {\n\t\t$QUERY['error'][] = 'Unable to connect to the database. Make sure that the entered details are correct';\n\t\t$_REQUEST['step'] = 1;\n\t}\n}<\/code><\/pre>\n<p>Notice the over use of $_SESSION? That will come in use if I decide to add a Back button that enables the users to modify the data entered previously.<\/p>\n<h2>Database Creation<\/h2>\n<pre><code class=\"php\">\/\/Create the database only if it does not exist\n\/\/See if the database exists\n$tables_sql = mysql_query(\"SHOW TABLES\") or die(mysql_error());\n$necessary_tables = array('Context','Project','Reminder','Setting','Task','TaskContext','User');\nwhile($table = mysql_fetch_row($tables_sql)) {\n\t$necessary_tables = array_diff($necessary_tables,array($table[0])); \/\/Remove the table from the array if it exists\n}\n\n\/\/If there are no tables in the array that means that the all the necessary tables are present in the Database\n\/\/If some tables are missing, that means we have to create those tables...\nif($necessary_tables) {\n\t$quries = &lt;&lt;&lt;END\n-- Insert all the SQL to create the necessary tables here...\nEND;\n\n\t\/\/Execute all the queries\n\t$all_quires = explode(\";\",$quries);\n\t$query_count = 0;\n\tforeach($all_quires as $query) {\n\t\t$query = trim($query);\n\t\tif($query) {\n\t\t\t@mysql_query($query);\n\t\t\t$query_count++;\n\t\t}\n\t}\n\t$QUERY['success'][] = \"Database Populated.\";\n} else {\n\t$QUERY['error'][] = \"Tables already in Database - I did not overwrite it. If you want to remove the old data, please delete the tables and run the installer script agian.\";\n}\n<\/code><\/pre>\n<h2>Saving the Database connection details<\/h2>\n<pre><code class=\"php\">\n\/\/I don't know how to escape the $ charector in heredocs - so I did this...\n$config = '$config';\/\/Heh, Heh ;-)\n$system_installed = '$system_installed';\n$configuration = &lt;&lt;&lt;END\n&lt;?php\n\/\/Configuration file for Nexty\n$system_installed = true;\n$config = array(\n\t'db_host'\t\t=&gt;\t'$_SESSION[host]',\n\t'db_user'\t\t=&gt;\t'$_SESSION[db_user]',\n\t'db_password'\t=&gt;\t'$_SESSION[password]',\n\t'db_database'\t=&gt;\t'$_SESSION[database]',\n\t'url'\t\t\t=&gt;\t'$_SESSION[url]',\n\t'absolute_path'\t=&gt;\t'$abs'\n);\n\nEND;\nif(is_writable('..\/configuration.php')) {\n        \/\/ ...Write the $config text into the configuration.php file...\n\n\t$QUERY['success'][] = 'Saved the configuration file. &lt;a href=\"'.$_REQUEST['url'].'\"&gt;Go to Nexty&lt;\/a&gt;';\n} else {\n\t$QUERY['error'][] = 'Configuration file (configuration.php) is not writable. Please copy the configuration code and enter it into the \"configuration.php\" file. Then press continue.';\n}\n<\/code><\/pre>\n<p>See the <a href=\"https:\/\/nexty.svn.sourceforge.net\/svnroot\/nexty\/install\/\">install folder for Nexty<\/a> in the Subversion server.<\/p>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>The last two posts on web installer did not include any code. I wanted to dump all code into one post &#8211; this is it. <a class=\"mh-excerpt-more\" href=\"https:\/\/www.bin-co.com\/blog\/2007\/07\/web-installer-the-code\/\" title=\"Web Installer: The Code\">[&#8230;]<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,21,25,30],"tags":[80,88,144,147,149,210,245,297],"class_list":["post-39","post","type-post","status-publish","format-standard","hentry","category-xhtml","category-php","category-scripts","category-web-development","tag-code","tag-database","tag-iis","tag-install","tag-installer","tag-php","tag-script","tag-web"],"_links":{"self":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/39","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/comments?post=39"}],"version-history":[{"count":0,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/posts\/39\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/media?parent=39"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/categories?post=39"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.bin-co.com\/blog\/wp-json\/wp\/v2\/tags?post=39"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}