wire less electricity

wire less electricity


Just from few days back I was hearing about wireless charging for mobile phones, I found it interesting, but from my back of the memory I remembered that this is not a new concept, “Nikola Tesla” do you remember this name, if not let me tell you he is the person who invented AC current, along with many other achievements like Radio Waves, X-ray, Energy Weapon, he worked on wireless electricity transmission.
In 1901 he was first to introduce this concept to the world, but as JP Morgan was the main financier of Tesla, and this invention may have destroyed his business of Electricity Transmission, so he ridiculed it and destroyed the Lab which Tesla was using for this experiment.
After Tesla died in 1917 no known serious efforts were made in this direction by any scientist or engineer neither any corporate or government has taken interest in this invenstion, though this technology was pretty straight forward.

Ok that was history, but whats now?
Now there are many people who are taking interest in this technology, there is a company Witricity who has invested in this technology and design some small consumer products and taking interest in large Industrial and Military applications as well.
We use term WiFi for wireless internet, what this will be called, Tesla had already named it World Wireless System (WSS)

But whats the future?
Yes that is really interesting, whats the future, in the near future you may not see the electricity poles on the streets, no electricity cable will enter you home, you can get the wireless electricity, transmitted directly to your home. You will have to purchase the receiver and a transmitter and its done, no wiring is required when building your home and no short circuits, there will be a main transmitter in home and all your appliances will be paired with the main transmitter, like you pair Bluetooth and WiFi devices. there might be some small unprotected transmission devices too, for the unpaired unfixed devices like mobile phone, shaver etc…

In around 2004 I read an article in some magazine, that only 2% of the world wide population is computer literate, and today in Economic Times India, I read that world Six% people worldwide addicted to internet.

http://economictimes.indiatimes.com/magazines/panache/six-per-cent-people-worldwide-addicted-to-internet/articleshow/45575877.cms

Problem:
After struggling so much over the Internet for finding guided solution for my jQuery based multiple autocomplete, I found many but none of them suited me and requirement, after inverting so much time on find the solution, I decided to write my own solution.

Challenge was like, I was having a table based form for adding multiple rows into the database, and these rows has to added dynamically using the jQuery, the attached image can give you more clear idea about the requirement.

Solution:
The Below code might look messy to you, but it is very simple and straight forward.
We have simple table structure and we are adding the row in the table using the add_row() javascript function, there is a javascript function show_autocomplete() which generates the jQuery autocomplete code and initiates the Autocomplete on fly, this function is called on the onClick event of the autocomplete textbox if you see the add_row() function there in the embedded html code you can find the show_autocomplete() function called in the first input element.
I have also given the remove_row() function in the code in case you also need to remove the added row.
Apart from this you can also fetch and show the extra rows into table using the autocomplete.

HTML code:

<table style="margin: 20px;" width="95%">
<thead>
<tr>
<th width="130">Item Name</th>
<th width="425">Description</th>
<th id="huom">Unit of Measure</th>
<th><span style="color: red;">*</span>Quantity</th>
<th>Remarks</th>
<th></th>
</tr>
</thead>
<tbody id="line_body"></tbody>
</table>

<a href=”javascript:void(0);” style=”margin-left:21px;” onclick=”add_row()” id=”add_row_btn” class=”btn btn-success icon-plus-sign-alt”>&nbsp;&nbsp;Add Row</a>

Javascript code:

function show_autocomplete(id){
id_obj = '#item_id_'+id;
name_obj = '#line_item_name_'+id;
desc_obj = '#line_desc_'+id;
uom_obj = '#line_uom_'+id;

$(name_obj).autocomplete({
//minLength: 2,
source: function (request, response) {
//txt = $("#"+id).val();
txt = $(name_obj).val();
$.ajax({
type: "GET",
url: "<?php echo base_url();?>receive/item_list/"+txt,
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
response($.map(data, function (item) {
return {
value: item.item_id,
id: item.id,
desc: item.shortitem_desc,
uom: item.uom,
}
}));
}
});
},
select: function (event, ui) {
$(id_obj).val(ui.item.id);
$(name_obj).val(ui.item.value);
$(desc_obj).html(ui.item.desc);
$(uom_obj).html(ui.item.uom);
return false;
},
});
}

x=0;
function add_row(mes){
if(mes==1){
var str = "<tr id='row_"+x+"' >"
+ "<td><input class='line-input-text autocomplete' onclick='show_autocomplete("+x+")' style='border-radius: 0px;' type='text' id='line_item_name_"+x+"' name='line_data["+x+"][item_name]' size='15' autocomplete='off' /></td>"
+"<td id='line_desc_"+x+"'></td>"
+"<td id='line_uom_"+x+"'></td>"
+"<td id='line_rate_"+x+"'><input type='text' name='line_data["+x+"][rate]' id='rate_"+x+"' size='5' class='input-style line-input-text count_class' required></td>"
+"<td align='center'><input type='text' name='line_data["+x+"][quantity]' id='quantity_"+x+"' size='5' onfocus='this.select();' class='input-style line-input-text count_class' required/></td>"
+"<td><input type='text' name='line_data["+x+"][remarks]' id='remarks_"+x+"' size='20' class='input-style line-input-text' /></td>"
+"<td><a href='javascript:void(0)' onclick='remove_line("+x+")'><i title='Remove' class='icon-remove'></i></a>"
+"<input type='hidden' name='line_data["+x+"][item_id]' id='item_id_"+x+"' />"
+"</td>"
+"</tr>";
$("#line_body").append(str);
$('#huom').after("<th><span style='color: red;'>*</span>Rate</th>");
x++;
}else{
var str = "<tr id='row_"+x+"' >"
+ "<td><input class='line-input-text autocomplete' onclick='show_autocomplete("+x+")' style='border-radius: 0px;' type='text' id='line_item_name_"+x+"' name='line_data["+x+"][item_name]' size='15' autocomplete='off' /></td>"
+"<td id='line_desc_"+x+"'></td>"
+"<td id='line_uom_"+x+"'></td>"
+"<td><input type='text' name='line_data["+x+"][quantity]' id='quantity_"+x+"' size='5' onfocus='this.select();' class='input-style line-input-text count_class' required/></td>"
+"<td><input type='text' name='line_data["+x+"][remarks]' id='remarks_"+x+"' size='20' class='input-style line-input-text' /></td>"
+"<td><a href='javascript:void(0)' onclick='remove_line("+x+")'><i title='Remove' class='icon-remove'></i></a>"
+"<input type='hidden' name='line_data["+x+"][item_id]' id='item_id_"+x+"' />"
+"<input type='hidden' name='line_data["+x+"][rate]' id='item_id_"+x+"' />"
+"</td>"
+"</tr>";
$("#line_body").append(str);
x++;
}
}

function remove_line(line_no){
row_count = $(".count_class").length;
if(row_count>1){
$("#row_"+line_no).remove();
}else{
alert('All line items can not be removed.');
}
}

add_row(); // call it on page load for adding first row

Memcache is the module provides the handy procedural and object oriented interface to memcached highly effective caching demon, which was especial designed to decrease the database load in dynamic websites.
Apart from decreasing the database load it also provides the session handler(memcache).

Memcached is the open source distributed memory object caching system it helps you to speedup web applications by reducing the database interactions.

The system uses the libmemcached library to provide an API for communicating with Memcached servers, by the way Memcached uses a different port 11211, rather than using the default port i.e 80 used for the web server.

Memcached has an extra letter “d” at the end, the naming convention is followup of Memcache, in some applications the piece of work that does the background work is called “deamon”, Memcache module utilizes this daemon directly, Memcache module provides the handy procedural and object oriented interface to Memcached.

Enough for Memcached, lest come back to Memcache, now lets see how to use Memcache. Before we use it first we have install it, I will take the example of ubuntu linux.

Steps for Installation

  1. First of all update the linux.
    sudo apt-get update
  2. Now install the demon “memcached”.
    sudo apt-get install memcached
  3. Next install the php-pear, it will help to store the cache
    sudo apt-get install php-pear
  4. Install the build-essential.
    sudo apt-get install build-essential
  5. Finally install the memcache.
    PECL (PHP Extension Community Library)
    sudo pecl install memcache
    Press enter for yes, when system prompts
    “Enable memcache session handler support? [yes] :”
  6. When you are finished installation of memcache with PECL, add “memcached” to memcache.ini
    echo "extension=memcache.so" | sudo tee /etc/php5/conf.d/memcache.ini

Hurry installation is finished, now you can check that it has been installed by searching for it.
ps aux | grep memcache

How Memcache works?
Before we go for using the memcache, lets take detour for a while to see that, “how memcache works?”.

When we send request to the server, at first attempt server tries to search the data in cache, before it goes and start searching the data in database, if the data is not found in the cache it, it goes by the usual way to retrieve data from the database and saves the data in the cache for certain amount of time and replies the desired data, by caching the requested data, future requests for the same data will not have to go through all the processes to retrieve the data from the database and hence access it through cache and save the time and resources.

 How to use Memcache?
Now the real work begins, we will use a simple PHP script  in order to explain the Memcache, have a look at the below script is simple and easy, it will give you a broad idea of the working of the script, then we will do autopsy of the script in order to understand actually how Memcache is helps to save the time and resources.


<?PHP
$meminstance = new Memcache();
$meminstance->pconnect('localhost', 11211);

mysql_connect(“localhost”, “test”, “testing123”) or die(mysql_error());
mysql_select_db(“test”) or die(mysql_error());

$query = “select id from example where name = ‘new_data'”;
$querykey = “KEY” . md5($query);

$result = $meminstance->get($querykey);

if (!$result) {
$result = mysql_fetch_array(mysql_query(“select id from example where name = ‘new_data'”)) or die(‘mysql error’);
$meminstance->set($querykey, $result, 0, 600);
print “got result from mysql\n”;
return 0;
}

print “got result from memcached\n”;
return 0;
?>

In the first line only you will see that an object has been created for the memcache, if you have not installed the memcache properly or for any reason if it is not working, then this line will throw the error, so please make sure sure that memcache is installed and working properly.

Now next up to 5th line of code I don’t think that these lines need to explained, in the sixth line, we have encrypted the SQL query in order to create the key so that the matching data can be retrieved from the database or if already in the cache we can retrieve it from the there or either we can also store it into the cache, this is exactly what the above PHP script is doing.

Now lets see the next line, here we are trying to fetch the maching data from the cache using the same that we have created above, actually programmer is trying his luck, to find if matching data is already in the cache.

Now in the next line in the if condition, programmer if checking if the get function of the memcache object returns any thing, if not then the data will be retrieved from the database and saved to the cache using the set function of the memcache object.

Conclusion
Till now you might have got the pretty clear picture on how the memcache works, by seeing this simple program you might be curious to know that, just it it?, no my friends there are few more functions like “delete”, “flush”, “add” etc. and couple of  configuration settings like, max_failover_attempts, hash_function, save_handler, save_path and few more.

I will try to write more on functions and configurational settings soon, till then you can go through below URLs for more information on functions and configuration settings.

  1. http://www.php.net/manual/en/class.memcache.php
  2. http://www.php.net/manual/en/memcache.ini.php

There is hard limit of 4096 columns in a MySQL table, but this also depends on some other factors as well like, size of the every column in the table, a row in table can be maximum 65,535 bites.

There can be maximum 64 options in ENUM, as well as in SET.

The difference between is only like Radio button form and checkbox in HTML form, ENUM is like Radio button user can only select one, while SET is like the Checkbox, user are able to select more than one value from the SET.

Wild card domains can be used. Sub domains can be created by first creating a sub directory in the /htdocs folder. E.g. /htdocs/mydomain. Then, the host file needs to be modified to define the sub domain. If the sub domains are not configured explicitly, all requests should be thrown to the main domain.  

MIME – Multi-purpose Internet Mail Extensions.

MIME types represents a standard way of classifying file types over Internet. 

In the broadest definition namespaces are a way of encapsulating items. For example, in any operating system directories serve to group related files and act as a namespace for the files within them.

As the size of your PHP code library increases, the more likely you will accidentally reuse a function or class name that has been declared before. The problem is exacerbated if you attempt to add third-party components or plugins; what if two or more code sets implement a ‘Database’ class?

As a concrete example, the file foo.txt can exist in both directory /home/greg and in/home/other, but two copies of foo.txt cannot co-exist in the same directory.
In addition, to access the foo.txt file outside of the /home/greg directory, we must prepend the directory name to the file name using the directory separator to get/home/greg/foo.txt. This same principle extends to namespaces in the programming world.

In the PHP world, namespaces are designed to solve two problems that authors of libraries and applications encounter when creating re-usable code elements such as classes or functions:

  1. Name collisions between code you create, and internal PHP classes/functions/constants or third-party classes/functions/constants.
  2. Ability to alias (or shorten) Extra_Long_Names designed to alleviate the first problem, improving readability of source code.

How are Namespaces Defined?

// define this code in the MyProject namespace
namespace MyProject;

The code following this line will belong to the MyProject namespace. It is not possible to nest namespaces or define two or more namespaces for the same code block (only the last will be recognized). However, you can define different namespaced code in the same file, e.g.


namespace MyProject1;
// PHP code for the MyProject1 namespace
namespace MyProject2;
// PHP code for the MyProject2 namespace

// Alternative syntax
namespace MyProject3 {
// PHP code for the MyProject3 namespace
}

However, it si strongly advisable to defining a single namespace per file.

Sub-namespaces
PHP allows you to define a hierarchy of namespace names so libraries can be sub-divided. Sub-namespaces are separated using a backslash (\) character, e.g.

  • MyProject\SubName
  • MyProject\Database\MySQL
  • CompanyName\MyProject\Common\Widget