Monday, 26 November 2012

Printing content of specific div using Javascript


Problem:

Guys you came across a situation, where you need to print a specific part of a Html or Php Page. When you try to execute this command in javascript :


"window.print();"

It will print your whole page. It gives a lot problem if you do not know how to print the specific content.
For ex - In your Html page , say there is a coupon banner which needs to printed by user. Now How to do that ?

You can do it by 2 ways :
Solution 1)
Using CSS to display only the content which is under print & hiding rest of others.

  <style type="text/css" media="print" >
           .nonPrintable{display:none;} /*class for the div or any element we do not want to print*/
</style>

==>Here , while you specify ----media="print"---- inside  <style> tag, it indicates that , this css will be applied only when you are printing some content.

Solution 2)

 The next method that can be commonly applied with most of the browser is using "Javascript".

The idea behind this is :
           While you Hit the print command, Just dynamically create a new pop up page from the existing page content(the content you need to print) & then print the page. Very simple.. Just check it out.

<script type="text/javascript"><!--
function print_specific_div_content()
{
 //alert("Hello world");
 var content = "<html>";
 content += document.getElementById("coupon_deal_id").innerHTML ;
 content += "</body>";
 content += "</html>";


 var printWin = window.open('','','left=0,top=0,width=1000,height=500,toolbar=0,scrollbars=0,status =0');

 printWin.document.write(content);
 printWin.document.close();
 printWin.focus();
 printWin.print();
 printWin.close();


}


</script>


<body>
     <div>Here comes your dummy other content </div>
     <div id= "coupon_deal_id">
                Only this content you want to print.......


     </div>
     <button type="button" onclick="print_specific_div_content()" >Print Coupon    </button> 



</body>

------Just check out the flow-----------------
1) When you click on Print coupon button , the control will go into  print_specific_div_content() function.
Then in content variable you are writing entire new html page, there you can add any content or items that you want to display with printed item. For ex - You can add the current Date inside that.

2)  After creating a content, just you are creating an instance of window using
    var printWin = window.open(.......).
 
==>Then you are setting some required parameters of window such as Height & width.
==>Then you write the content to that window using printWin.document.write(content);
==>then execute the print command printWin.print();
==>& finally , close the window.

Simple...

Just Rock on .. Use this function where ever you want the function to print the part of the page.





Wednesday, 4 July 2012

Retrieving Records in Php MySQL (Concept on Code Reusability)


USING MULTIDIMENSIONAL ARRAY TO FETCH MYSQL RECORDS IN  PHP:  

You may be pretty big fan of Php-Mysql, & if you are the application development geek or at all the new learner, you will  be frequently  involved in  retrieving the data from database. As following a general pattern, if you have 10 places to retrieve/display the  record , you will write the same fetch query-giving field names of the table for 10 time as I used to do the same. This is some what quite tedious task to write the same query again & again  Like in this way :


  $query = msyql_query($yourSql);
  while($row = mysql_fetch_array($query))
  {
$field1_value = $row['field1'];
$field2_value = $row['field2'];
  }

The limitation of this type of pattern is:
1) You can not display the field values or records execpt inside this while loop.
2) If you have 10  places like this to show records, you will struggle writing the same query for 10 times, specifying field names , variables names & all.

Infact this type of pattern was quite irritating to me also. If you are familar with joomla JDatabase features, it returns you all of the table records in multidimensional array with one single function $db->loadAssocList();

I was inspired with this type of joomla feature & thought why not to implement the same feature, using core php only?

So , here goes the function to  retrieve MySQL records in multidimensional array format so that you can use it any where. All you need to do is just pass the the table name & your where condition for selecting the records from table.

Check it out.




function getAllDataDetails($table, $where)
{
 if($where != '')
 {
  $sql = "SELECT * FROM $table WHERE $where";
 }
 else
 {
  $sql = "SELECT * FROM $table" ;
 }

 $resultArray = array();
 $row_num = 0 ;

 $query = mysql_query($sql);

 //count the number of fields in table
 $num_fields = mysql_num_fields( $query);
  //(resource,index) //returns filed name at particular index
 //Get the field names

 while($row = mysql_fetch_array($query))
 {
 
  for($field_index = 0 ; $field_index < $num_fields ; $field_index++)
  {
  $field_name =  mysql_field_name($query, $field_index) ; //firstly it will hold first field ,then 2nd etc
$resultArray[$row_num][$field_name] = $row[$field_name];
  }

  $row_num++;
 }

 return $resultArray;

}


Now for displaying the records  just call the above function:

$table_name = "myTable";
$where_condition = "where id > 10 ORDER BY id";
$tableRecordDetails = getAllDataDetails($table_name,$where_condition);

// tableRecordDetails  will be the multidimensional array

Now all you need to do is loop through an array , i am the pretty big fan of using foreach() loop pattern.

foreach($tableRecordDetails as $tableData )
{

//here goes your table records for ex-
        $id =  $tableData[fieldName1];  //this will be the field name of your table
$name = $tableData [fieldname2];    

}


No one would be happier than me , if you code,copy, paste or customize the code & implement it in your  web application. If you hava any queries, donot forget to post comment or you can directly reach to me at prem.singh.nepal@gmail.com

Thank you
Have a Browsing moment ahead







 


Thursday, 19 April 2012

A glance at Android Development

Developer Site| Android Development|Opportunities

Grow with Android..........


Develop For Development


Almost 40-60% of market share has been taken up by Android Market. So, it is wisefull to grasp up with android development.

Android is really easy to learn,  if you have a good concept on any one of object oriented language C++, php or in fact java. 
Android is nothing but implementation of java classes.
My focus to learn android is why because :

Android is An Open Platform for Mobile Development.

A Linux operating system kernel that provides the low-level interface with the hardware, memory, management, and process control, all optimized for mobile devices.

Has powerful APIs, excellent documentation, a thriving developer community, and no development or distribution costs. As mobile devices continue to increase in popularity, this is an exciting opportunity to create innovative mobile phone applications no matter what your development background.

Alternative options are Iphone (Apple ) & Windows(Mango) Currently rising, but these are fully proprietary hardware and software platform. while Android is an open source software stack produced and supported by the Open Handset Alliance(OHA) and designed to operate on any handset that meets the requirements.

The Open Handset Alliance (OHA) is a collection of more than 30 technology companies including hardware manufacturers, mobile carriers, and software developers.

Android provides a lightweight relational database for each application using SQLite. It can easily communicate with Php & MySQL at server side. So you can really beautify your application using Client - server Architecture & Store- Forward Architectures too.

No liscensing fee... , Its really easy & Awesome...

Get Start your Android Journey today... (http://developer.android.com/)
Wish you all the Best......
Let us join Hands together....