RSS

Monthly Archives: October 2011

[php][html] For Beginners

People often question about the initializing task of php, following are the few tasks that help a person get started with coding in php:

Task 1: Required Installations

All you need is an editor and localhost.

Task 2: Static Welcome Message Display

Start your installed (WAMP, LAMP, XAMPP, WAMP) server (Note:If your skype is there online then you need to signout skype for starting your server because of port conflict). Open Notepad++ and create new file at wamp/www for WAMP and applications/MAMP/htdocs for MAMP.

Place the following code in the file:

<?php
echo "Hello World";
?>

OR

Hello World

In your browser goto http://localhost and you will find your file listed there, click on the file and you will see a welcome message “Hello World” there.

tags are there to enclose php code. Echo will print the content.

Task 3: Display Name Dynamically

Previously we have displayed a hardcoded message, now display your name dynamically. W3schools is there for beginners. Just explore the html section and code a basic form.

<form name=”form1″ id=”form1″ action=”" method=”POST">

Enter your name:<input type=”text” name=”uname” id=”uname” />

<input type=”submit” name=”submit” id=”submit”
value=”Save Name” />

</form>

<?php

if(isset($_POST['submit']))

echo $_POST['uname'];

?>

$_POST array have all the submitted data of the form.

Refresh the file in the browser and you will see a form, just enter your name and submit it and you will see “Your Name: your_name_here”. Just explore html section of  W3schools and play with the layout of your form.

Task 4: Save Data in Database

Now comes interesting part of learning any programming lanaguage i.e. database interaction.

In your browser goto http://localhost, click phpMyAdmin option there.

Lets `user` be the name of your database,` user_info` be the name of your table having fields `name` and `roll_number` (it is auto incremented).

  • Here is a tutorial for creating database in phpMyAdmin
  • Here is a tutorial for creating table in phpMyAdmin
<form name="form1" id="form1" action="" method="POST">

Enter your name:<input type="text" name="uname" id="uname" />

<input type="submit" name="submit" id="submit"
value="Save Name" />

</form>

<?php

$con = mysql_connect("localhost",'root','root');
//host, user,password

mysql_select_db("user",$con) or die (mysql_error);

if(isset($_POST['submit']))

{

$query = "INSERT INTO user_info (name)
VALUES ('".$_POST['name']."')";

mysql_query($query) or die (mysql_error);

}

?>

Goto your table at phpMyAdmin and refresh the table, you will see your name there.

For listing records, updating and much more functions explore W3Schools SQL section.

This tutorial is there to let you know how simple and easy going php is. Once you are done with these basic tasks you can try modifying the html and php code to learn more.

Have a nice time in the world of php 🙂

Advertisements
 
4 Comments

Posted by on October 26, 2011 in php

 

Tags: , , , , , ,

[xml][error][solution]Fix for “XML Parsing Error: not well-formed”

For resolving XML Parsing Error: not well-formed use any of following:
1) XML CDATA as the term CDATA is used about text data that should not be parsed by the XML parser.Enclose the code in between <![CDATA[ and ]]>

e.g.

 "<![CDATA[<".$key.">".$value."</".$key.">]]>"

2) htmlspecialchars

e.g.

"<".htmlspecialchars($key).">".htmlspecialchars($value).
"</".htmlspecialchars($key).">"

3) Customized special character handling function

e.g.

"<".$key.">".xmlentities($value)."</".$key.">"
function xmlentities($text)
{
$search = array('&','<','>','"','\'');
$replace = array('&amp;','&lt;','&gt;','&quot;','&apos;');
$text = str_replace($search,$replace,$text);
return $text;
}
 
Leave a comment

Posted by on October 25, 2011 in xml parsing

 

Tags: , , ,

|| cronjob || periodic importer || with the passage of time records inserted by the cron job or periodic importer get smaller and smaller.

Its a common observation while using csv files periodic import, xml parsed data insertion in database etc that in the starting few months the import was perfectly well but with the passage of time the code efficiency decreases as the number of newly added records get smaller and smaller. This is mostly observed where duplicated records are not get inserted in the database. Following are the two potential reasons,

i) Initially there are few number of records and the uniqueness factor for new records is high which go on decreasing with the insertion of more records on periodic basis. Duplicated records are one of the reason for inserting lesser records.

ii) While importing every new item it checks in the corresponding table(s) the existence of that item. When there are few records in the database, cross-checking is limited which increases exponentially as per the number of records already there in the database. Time out break will be there when every new import search through all the records, thus decreasing the number of new items imported.

Additionally, the most common issue with the cron jobs or periodic importer is the cross checking every new item has to do before insertion.

Resolution:
It is suggested to delete the old records in order to keep the size of table feasible for crosschecking while inserting every new record.

 
 

Tags: , , , , ,

|| drupal 7 || tutorial || popup with html form

In drupal 7, once i have to show a comment form in the popup. I used the greybox jQuery plugin to show a drupal comment form in popup and find that the whole page along with the interface get called in the popup whereas i just need to show the form only. I was suggested to call a html page having only the html form to be displayed in the popup window. As far as interface is concerned, its ok with the comment form in the popup but how to manage the html form action in drupal. i have to specify some redirection url in html form action=”” but what exactly?

hook_menu is there to introduce new urls as

function mymodule_menu() { //{moduleName}_menu

$items['custom_page'] = array( 
// {baseurl}/custom_page will be the url

'page callback' =>'html_form_action',
//function to be called by {baseurl}/custom_page

'access callback' => TRUE,

'type' => MENU_NORMAL_ITEM,

);

return $items;

}

function html_form_action() {

global $base_url;

//form action handling here

drupal_goto($base_url."/sites/all/modules/mymodule
/comment.php",array('query'=>array('status'=>$status))); 
// redirection to html form page to handle the 
//appropriate form action response

}

and in html

 <form action="{baseurl}/custom_page" …>

That’s it 🙂

Note: If you find that under the Navigation block there is a bullet without any title, then this is due to the menu we have created. For hiding that unnecessary bullet goto {base_url}/admin/structure/menu/manage/navigation and disable the unwanted bullet.

 
3 Comments

Posted by on October 20, 2011 in drupal

 

Tags: , , , , , , ,

|| php || tip || physical path tracking

Including file from nested directory is simple and straight forward while being at root directory, but for including files in php in nested directories beginners claim that they have did everything perfectly right but the file didn’t get called.
Following are the few cases for nested file calls,
1) ./ within the same folder e.g. currently in folder1/file1.php and want to access folder1/file2.php
2) ../ with the depth of one level e.g. currently in folder1/folder1.1/file3.php and want to access folder1/file2.php
3) ../../ with the depth of two levels e.g. currently in folder2/folder2.4/folder2.4.5/file4.php and want to access folder2/file5.php
4) ../../../ with the depth of three levels e.g. currently in folder5/folder5.3/folder5.3.1/folder5.3.1.7/file6.php and want to access folder5/file7.php

Thats it 🙂

 
Leave a comment

Posted by on October 19, 2011 in php

 

Tags: , , , ,

|| php || helping code || how to get base url in custom php?

Sometimes while coding in php, relative paths doesn’t work properly where they suppose to be, so for such cases one need an absolute url and for that need to concatenate the base url with the relative url as per requirement. Following code is there to get the base url.

$file_name = explode('/',$_SERVER['REQUEST_URI']);
$root_dir = "";
for($i = 0 ; $i< (count($file_name)-1) ;$i++) 
// -1 to exclude filename
{
$root_dir .='/'.$file_name[$i];
}

$base_url = 'http://'.$_SERVER['HTTP_HOST'].$root_dir;

$_SERVER array has many other useful info, just do print_r($_SERVER); and you can trace the SCRIPT_FILENAME of the current file,QUERY_STRING , SCRIPT_NAME, REQUEST_METHOD etc.

 
Leave a comment

Posted by on October 19, 2011 in php

 

Tags: , , ,

|| javascript || tutorial || bind events with keyboard shortcuts

For relating keyboard short cuts with checking or unchecking the checkbox, i once need to get the value of key pressed. Following is the simple code for getting the ascii of any key pressed.

<script type="text/javascript">
$(document).ready(function() {
jQuery(document).keydown(function(e) {
alert(e.which);
});
});
</script>

For binding event with multiple keys like Ctrl+a, following code is there to help you out,

<script type="text/javascript">
$(document).ready(function() {
var ctrl = false;
jQuery(document).keydown(function(e) {
if(e.which == 17) //17 = ctrl
{
ctrl = true;
}
if(e.which == 65 && ctrl) //65 = a
{
alert("You have pressed Ctrl + a");
}
});
});
</script>

Thats it 🙂

 
Leave a comment

Posted by on October 19, 2011 in javascript, php

 

Tags: , , , , ,

 
%d bloggers like this: