Posted by: prajapatinilesh | June 25, 2009

Javascript + Array sort on Key

Hi,

Please follow following example to understand the problem.

<script>
function keySort(array, flag) {
var keys = new Array();

for(k in array) {
keys.push(k);
}

keys.sort( function (a, b) {
if (flag && flag != ‘undefined’) {
if(flag == ‘integer’) {
a = parseInt(a);
b = parseInt(b);
}
}
return (a > b) – (a < b);
} );

return keys;
}//End of function keySort

/*jsArray = new Array();
jsArray['A'] = ‘Test1′;
jsArray['Z'] = ‘Test3′;
jsArray['P'] = ‘Test4′;
jsArray['U'] = ‘Test2′;
jsArray['B'] = ‘Test5′;*/

jsArray = new Array();
jsKeyArray = new Array();

jsArray[80] = ‘nil1′;
jsArray[120] = ‘nil2′;
jsArray[160] = ‘nil3′;

for (x in jsArray) {
alert(x + ‘ –> ‘ + jsArray[x]);
}

jsKeyArray = keySort(jsArray);

/*for (x in jsKeyArray) {
alert(x + ‘ –> ‘ + jsKeyArray[x]);
}*/

//It will display wrong result….
for (var i = 0; i < jsKeyArray.length; i++) {
alert(jsKeyArray[i] + ‘ –> ‘ + jsArray[jsKeyArray[i]]);
}

jsKeyArray = keySort(jsArray, ‘integer’);

//correct result….
for (var i = 0; i < jsKeyArray.length; i++) {
alert(jsKeyArray[i] + ‘ (integer) –> ‘ + jsArray[jsKeyArray[i]]);
}

//——————————————————————–

//Value integer sorting…..

function sortASC(a, b){ return (a-b); }
function sortDESC(a, b){ return (b-a); }

integerArray = new Array(1,545,1435,3453,342,441,90);

integerArray.sort( sortASC );
document.write(‘ <br /> Ascending : ‘ + integerArray);

integerArray.sort( sortDESC );
document.write(‘ <br /> Descending : ‘ + integerArray );

</script>

Thanks,

Nilesh Prajapati

Posted by: prajapatinilesh | May 17, 2009

How to refresh the parent window on closing the child window ?

Hi,

Demo Example:

1) parent.php

<html>
<head> <title>Parent Window</title>
<script language=”javascript”>
window.name = ‘parentWindow’;
var newPopupWindow;
function popupLinkOption(url,width,height) {
newPopupWindow=window.open(url,”childWindow”,”height=”+height+”,width=”+width+”,left=100,top=100,resizable=no,scrollbars=no,toolbar=no,status=no,menubar=no,location=no”);
}
</script>
</head>
<body>
Parent File <br />
<a href=”#” onclick=”popupLinkOption(‘child.php’,200,200);”> Popup </a>
<?php
if ($_GET['rId']) {
echo “<br />Rendom Id –>”.$_GET['rId'];
}
?>
</body>
</html>

————————————————————————————————

2) child.php

<html>
<head> <title>Child Window</title>
<script language=”javascript”>
function refreshParent() {
//here window.opener is the parent window from which child window opened.
parentURL = window.opener.location.href;
if (parentURL && parentURL != ‘undefined’) {
//alert(window.opener.location.href + ‘ — ‘ + window.opener.location);
//alert(window.opener.name);
parentURLArr = parentURL.split(‘#’);

//find last characters from the string means # as we have href=’#’ in parent window link
//newStr = parentURL.substring(0, parentURL.length-1);
//alert(newStr);

parentURLArr1 = parentURLArr[0].split(‘?rId=’);
//alert(parentURLArr1[0]);

qStr = ‘?rId=’+Math.random(); //random Id added here becuase it will not take page from the browser cache.. sometimes required for some browser.

window.opener.location.href = parentURLArr1[0] + qStr;

//window.opener.location.href = window.opener.location.href; //this also used to refresh the parent window.
//window.opener.location.reload(); //this also we can add to reload the parent window.
//window.opener.close(); //parent open closing
}
}

function closeChild() {
window.close();
}
</script>
</head>
<body onunload=”refreshParent();” >
Child File <br />
<a href=”#” onclick=”closeChild();”> Close Child</a>
</body>
</html>

This is just demo with alert and other things. so, can check in your local pc.. and you can modified code as per your requirements. :)

Run parent.php file to check this exmple.

Thanks,

Nilesh Prajapati.

Posted by: prajapatinilesh | May 11, 2009

Javascript function to get particular querystring value from URL.

Hi,

function getQueryStrVal(url,name) {
name = name.replace(/[\[]/,”\\\[").replace(/[\]]/,”\\\]”);
var regexS = “[\\?&]“+name+”=([^&#]*)”;
var regex = new RegExp( regexS );
var results = null;
if (url && url != ‘undefined’) {
results = regex.exec( url );
}
else {
results = regex.exec( window.location.href );
}
if( results == null )
return “”;
else
return results[1];
}

How to use:

here url is your js variable in which your whole url is defined.

if(url.indexOf(‘prefixVal’) != -1) {

prefixVal = getQueryStrVal(url, ‘prefixVal’);

alert(prefixVal);

}

Thanks,

Nilesh prajapati.

Hi,
We are using   move_uploaded_file($_FILES['myFile']['tmp_name'], ‘/upload_files/myFile.txt’);   function in php to upload files into physical location (destination directory).
But in case if you want to process that uploaded file instead of storing it into destination directory then we can use other php function for file handling.
For example:

if (is_uploaded_file($_FILES['myFile']['tmp_name']))
$fileData = file_get_contents($_FILES['myFile']['tmp_name']);

$fileData = str_replace(“A”, “B”, $fileData);

OR
if file size is large then better to use following way: means use of diff file handling functions of php such as fopen(), feof(), fclose(), etc.

if (is_uploaded_file($_FILES['myFile']['tmp_name'])) {
$filePointer = fopen($_FILES['myFile']['tmp_name'], “rb”);

if ($filePointer!=false){
while (!feof($filePointer)){
$fileData = fread($filePointer, 4096);
// Process the contents of the uploaded file here… and also we can make insert query to store into db
}
fclose($filePointer);
}

}

Working Example:

<?php
if ($_FILES) {
echo “<pre>”;
print_r($_FILES);
echo “</pre>”;

$fileContent = file_get_contents($_FILES['upload']['tmp_name']);
echo “<pre>”;
print_r($fileContent);
echo “</pre>”;
//now you can process your content here…
die;
}
?>

<form enctype=”multipart/form-data” method=”POST”>
<input type=”file” name=”upload”>
<button type=”submit”>Enter</button>
</form>
Thanks,   Nilesh.

Posted by: prajapatinilesh | May 7, 2009

Get all dates between two dates using php code.

<?php
$fromDate = ‘01/01/2009′;
$toDate = ‘01/10/2009′;

$dateMonthYearArr = array();
$fromDateTS = strtotime($fromDate);
$toDateTS = strtotime($toDate);

for ($currentDateTS = $fromDateTS; $currentDateTS <= $toDateTS; $currentDateTS += (60 * 60 * 24)) {
// use date() and $currentDateTS to format the dates in between
$currentDateStr = date(“Y-m-d”,$currentDateTS);
$dateMonthYearArr[] = $currentDateStr;
//print $currentDateStr.”<br />”;
}

echo  “<pre>”;
print_r($dateMonthYearArr);
echo “</pre>”;
?>

Thanks,
Nilesh Prajapati.

Posted by: prajapatinilesh | May 5, 2009

Lightwindow using jquery javascript

Hi,

I found that jquery is providing good and easy way to use lighwindow (thikbox).

Following is link which explained how to use lightwindow (thikbox) using jquery.

http://jquery.com/demo/thickbox/#sectiond-2

If you will find problem for flash player during lightwindow, means lightwindow will not overlap on the flash player then try with following:

  1. To the Object tag, add the following parameter (opaque is shown as an example):
    <param name=”wmode” value=”opaque”>
  2. To the Embed tag, add the following attribute:
    wmode=”opaque”

Reference link:  http://www.communitymx.com/content/article.cfm?cid=E5141

Thanks,
Nilesh

Posted by: prajapatinilesh | April 20, 2009

Image area map html elements

The <map> tag is used to define a client-side image-map. An image-map is an image with clickable areas.
The name attribute is required in the map element. This attribute is associated with the <img>’s usemap attribute and creates a relationship between the image and the map.
The map element contains a number of area elements, that defines the clickable areas in the image map.
Example:
<html>
<body>
<p>Click on the sun or on one of the planets to watch it closer:</p>
<img src=”planets.gif” width=”145″ height=”126″ alt=”Planets” usemap=”#planetmap” />
<map name=”planetmap”>
<area shape=”rect” coords=”0,0,82,126″ alt=”Sun” href=”sun.htm” />
<area shape=”circle” coords=”90,58,3″ alt=”Mercury” href=”mercur.htm” />
<area shape=”circle” coords=”124,58,8″ alt=”Venus” href=”venus.htm” />
</map>
</body>
</html>
This is a beautiful feature to map the image on particular coordinates. so, we can use this kind of map during jpgraph generation or any use and we can put javascript tooltip to display imformation on particular point of image.

Thanks,
Nilesh

Posted by: prajapatinilesh | April 11, 2009

Overlap Div – z-index use in css html

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″ />
<title>Z-Index – Overlap DIV Example</title>
<style type=”text/css”>
<!–
body,td,th {
font-family: Georgia, Times New Roman, Times, serif;
}
div.div1 {
position: absolute;
top: 20px; left: 20px;
height: 250px; width: 250px;
color: white;
background-color: blue;
z-index: 1;}
div.div2 {
position: absolute;
top: 150px; left: 10px;
height: 100px; width: 300px;
background-color: #EB7CA7;
z-index: 20;}
div.div3 {
position: absolute;
top: 15px; left: 175px;
height: 125px; width: 125px;
background-color: yellow;
z-index: 10;}
–>
</style></head>
<body>
<div class=”div1″>Div #1 </div>
<div class=”div2″>Div #2 </div>
<div class=”div3″>Div #3 </div>
</body>
</html>

Note: If we will increase z-index value in css div then it will take that div at top of all other div.

Posted by: prajapatinilesh | April 8, 2009

hosts file setting.

Location of host file:
C:\WINDOWS.0\system32\drivers\etc\    — hosts
Host file is used to bind the site url with our ip address.
example:
<ip_address>  testurl.com  (http://testurl.com/)  www.testurl.com.
Statrt >> Run >> cmd >>
ipconfig — to check ip address of our pc.
C:/>ipconfig /help
C:/> ipconfig /displaydns
C:\>ipconfig /flushdns
C:\>ipconfig /renew
If you will find error during the flush dns then apply following steps:
C:\>ipconfig /flushdns
Windows IP Configuration
Could not flush the DNS Resolver Cache: Function failed during execution.
Solution:
1) Click Start
2) Click RUN
3) Type services.msc in the text box
4) Click OK or hit Enter
5) Scroll down to DNS Client and double click it
6) Select General tab and select Startup type: Automatic or manual
7) Click Apply
8) Finally at Service Status Click Start (if it is stopped)  //click on start

Thanks,
Nilesh Prajapati.

Posted by: prajapatinilesh | January 14, 2009

Manually Set PHP Session Timeout — PHP Session

To find out what the default (file-based-sessions) session timeout value on the server is you can view it through a ini_get command:

// Get the current Session Timeout Value
$currentTimeoutInSecs = ini_get(’session.gc_maxlifetime’);

Change the Session Timeout Value

// Change the session timeout value to 30 minutes  // 8*60*60 = 8 hours
ini_set(’session.gc_maxlifetime’, 30*60);
//————————————————————————————–

// php.ini setting required for session timeout.

ini_set(’session.gc_maxlifetime’,30);
ini_set(’session.gc_probability’,1);
ini_set(’session.gc_divisor’,1);
//————————————————————————————–
//if you want to change the  session.cookie_lifetime.
//This required in some common file because to get the session values in whole application we need to write session_start();  to each file then only will get $_SESSION global variable values.

$sessionCookieExpireTime=8*60*60;
session_set_cookie_params($sessionCookieExpireTime);
session_start();

// Reset the expiration time upon page load //session_name() is default name of session PHPSESSID

if (isset($_COOKIE[session_name()]))
setcookie(session_name(), $_COOKIE[session_name()], time() + $sessionCookieExpireTime, “/”);
//————————————————————————————–
//To get the session cookie set param values.

$CookieInfo = session_get_cookie_params();

echo “<pre>”;
echo “Session information session_get_cookie_params function :: <br />”;
print_r($CookieInfo);
echo “</pre>”;
//————————————————————————————–
Some Description of session related setting for php.ini file.

session.gc_maxlifetime integer
session.gc_maxlifetime specifies the number of seconds after which data will be seen as ‘garbage’ and cleaned up. Garbage collection occurs during session start.
session.cookie_lifetime integer
session.cookie_lifetime specifies the lifetime of the cookie in seconds which is sent to the browser. The value 0 means “until the browser is closed.” Defaults to 0. See also session_get_cookie_params() and session_set_cookie_params(). Since the cookie is returned by the browser, it is not prolonged to suffice the lifetime. It must be sent manually by setcookie().

Thanks,
Nilesh Prajapati.

Older Posts »

Categories