Create dynamic Array in PHP

Here are the instructions to create dynamic array using php.

Dynamic Array is one where the elements can be altered during run time.

This title explain you both single dimensional and multidimensional array.

First let we declare the variables,

//Single Dimensional
$single_arr=array();
//Multidimensional
$multi_arr=array();

We can able to create dynamic new array from the database tables or existing arrays.

Now we create the dynamic array from tables

TableName: test


snodeptname
1P&ARD.Premkumar
2H,P&EN.Thangamani
3AgricultureK.Shanmugam
4RD&PRP.Usha
5H&FWM.Vijayarani
6PublicR.Kalaimani
7PublicE.Dhandapani
8P&ARA.Anbarasan
9H&FWB.Kamakshi
10Sch.Edn.M.Vijayalakshmi
11AgricultureM.R.Amamath
12L&EK.Lakshmi
13RevenueS.Karikalan
14HomeS.Sellakkannu
15AgricultureG.Ramanuja Surendranath
$sql="SELECT sno,dept,name FROM test";
$query=@mysql_query($sql);
$num_query=@mysql_num_rows($query);
if($num_query>0)	{
while($fetch=mysql_fetch_assoc($query))	{
//Single Dimension Array
$single_arr[$fetch['sno']]=$fetch['name'];
//Multiple Dimension Array
$multi_arr[$fetch['dept']][$fetch['sno']]=$fetch['name'];
}
}
//echo ' < pre > ';
//print_r( $single_arr );
//print_r( $multi_arr );

Below are the output for created dynamic single dimensional array

Array
(
    [1] => D.Premkumar
    [2] => N.Thangamani
    [3] => K.Shanmugam
    [4] => P.Usha
    [5] => M.Vijayarani
    [6] => R.Kalaimani
    [7] => E.Dhandapani
    [8] => A.Anbarasan
    [9] => B.Kamakshi
    [10] => M.Vijay
    [11] => M.R.Amamath
    [12] => K.Lakshmi
    [13] => S.Karikalan
    [14] => S.Sellakkannu
    [15] => G.Ramanuja
)

Below are the output for created dynamic multidimensional array

Array
(
    [P&AR] => Array        (
            [1] => D.Premkumar
            [8] => A.Anbarasan
        )
    [H,P&E] => Array        (
            [2] => N.Thangamani
        )
    [Agriculture] => Array        (
            [3] => K.Shanmugam
            [11] => M.R.Amamath
            [15] => G.Ramanuja
        )
    [RD&PR] => Array        (
            [4] => P.Usha
        )
    [H&FW] => Array        (
            [5] => M.Vijayarani
            [9] => B.Kamakshi
        )
    [Public] => Array        (
            [6] => R.Kalaimani
            [7] => E.Dhandapani
        )
    [Sch.Edn.] => Array        (
            [10] => M.Vijay
        )
    [L&E] => Array        (
            [12] => K.Lakshmi
        )
    [Revenue] => Array        (
            [13] => S.Karikalan
        )
    [Home] => Array        (
            [14] => S.Sellakkannu
        )
)

From above we concluded that we created dynamic php array.

Ajax Json jquery

How to convert the json obj into something
AJAX (Asynchronous JavaScript and XML).Request the data to server after page loads and get response.This is asynchronous.With AJAX we can change the particular content in a loaded page.

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is one of the best method to exchange data like XML.
jQuery is a java script library provides set of method to handle request and response.With jQuery we can request AJAX call to a server.
Server Script[server.php]

echo '{1:"PHP",2:"MYSQL",3:"AJAX",4:"JAVASCRIPT",5:"JSON"}';
Client Side Script:[test.php]
$.ajax({
   type: "POST",
   url: "server.php",
   data: "website="+website,
   success: function(resp){
	//alert(resp);
	eval("var response = " + resp);
	if(response!='')	{	
	//alert(response[1]);  // Return PHP
	//alert(response[2]);  // Return MYSQL
	for(jsonloop in response)
		alert(jsonloop+"-"+response[jsonloop]);		
	}
   },
   error: function(e){
     alert('Error: ' + e);
   }
 });

$.getJSON Method

Server Script[server.php]
Below are the php server script with json format about books

//header('Content-type: text/json');	
header('Content-type: application/json');
echo '{"title": "Learning Jquery",
"date_published": "June 2007",
"description": "Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file",
"ISBN": "978-1-847192-50-9",
"author": "Jonathan chaffer and Karl swedberg",
"tags": "Jquery Javascript design js framework"}';

Client Side Script:[test.php]
Below are the client side code

$.getJSON("http://localhost/server.php",
function(data){$.each(data,function(i){
$("#json").append("< br />"+i+":"+data[i]);
}) });

Output would be

title:Learning Jquery
date_published:June 2007
description:Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file ISBN:978-1-847192-50-9 author:Jonathan chaffer and Karl swedberg
tags:Jquery Javascript design js framework

$.ajax Method [Both GET and POST without datatype]

Client Side Script:[test.php]
Below are the client side code.When using ajax method, JSON response returned and stored in data variable. But this variable become strings. So using eval method make it to objects

$.ajax({
url: "http://localhost/testing/js/jquery/ajaxjson_server.php",
type: 'GET',
data: "sid="+Math.random(),
success: function(data) {
eval("data="+data);
$.each(data,function(i){
$("#book").append("< br/ >"+i+":"+data[i]);
})
}
});

$.ajax Method [Both GET and POST with datatype]

Client Side Script:[test.php]
Below are the client side code.When using ajax method, JSON response returned and stored in data variable.When using datatype as json, it parses into object.So no need to use eval function.

$.ajax({
url: "http://localhost/testing/js/jquery/ajaxjson_server.php",
type: 'GET',
dataType: 'json',
data: "sid="+Math.random(),
success: function(data) {
$.each(data,function(i){
$("#book").append("< br/ >"+i+":"+data[i]);
})
}
});

Output would be

title:Learning Jquery
date_published:June 2007
description:Better interation design and development with simple javascript Techniques.An easy-to-learn syntax, and robust cross-platform compatibility in a single compact file
ISBN:978-1-847192-50-9 author:Jonathan chaffer and Karl swedberg
tags:Jquery Javascript design js framework

Mysql week wise

How to get the week wise result from the table?
Below are the list of sql query to retrieve the data from mysql about current week,previous week,next week,forthcoming weeks from current week.Its very easy to handle.

To get Previous Week:

1.SELECT * FROM tablename WHERE YEARWEEK(DATE_SUB(CURDATE(),INTERVAL 7 day))=YEARWEEK(birthdate) 
To get current Week:
1.SELECT * FROM tablename WHERE YEARWEEK(CURDATE())=YEARWEEK(deliverydate) 
2.SELECT * FROM tablename WHERE YEARWEEK(DATE_ADD(CURDATE(),INTERVAL 0 day))=YEARWEEK(birthdate) 

To get Next Week:

1.SELECT * FROM tablename WHERE YEARWEEK(DATE_ADD(CURDATE(),INTERVAL 7 day))=YEARWEEK(birthdate) 

To get 2nd week from current week:

1.SELECT * FROM tablename WHERE YEARWEEK(DATE_ADD(CURDATE(),INTERVAL 14 day))=YEARWEEK(birthdate) 

To get 3rd week from current week:

1.SELECT * FROM tablename WHERE YEARWEEK(DATE_ADD(CURDATE(),INTERVAL 21 day))=YEARWEEK(birthdate)