Friday, August 31, 2012

Index PHP Array to use in Javascript / Javascript array

So here is what I wanted, I wanted to the text of check box when clicked on check box like
<input type='checkbox' value='1' name='chkbox'>First chk box</input>

So I wanted 'First chk box' when clicked on it, in here you might be able to simply get it using innerHTML but my case was bit different so I got a php array that had id=>value (1=>'First chk box') array now the issue is you can't use it in javascript

So step 2 is converting php array to javascript array, following are the things you can do

1. Simply make a javascript array and put php values in it
var jsArray = new Array('<?=$phpArr[0]?>',<?=$phpArr[1]?>',....);

That doesn't work in my case because my indexes were different

2. You can have insertion in JS Array using the splice function to insert in mid, you can google that, but the issue with that is you can't have a javascript array like

arr[1] = index1
arr[2] = index2
arr[5] = index5

means you need EACH index defined which is not my case, I could have skipping index so here is solution for me, or you if you have similar issue... or any of above might work for you...

3. var jsArray = <?php echo json_encode($phpArray) ?>;
Now if could do is simply using
jsArray[chkbox.value] 
and it would give me 'First chk box'

Friday, August 24, 2012

Generate Random Password Joomla and PHP

The following few lines creates a random password and puts it into and md5 encrypted format to be saved in joomla database. If you are using non-encrypted password you can just use the first part, or if you are using simple md5 encryption and not looking for key:salt as joomla then you can simply take the $key from first part and encrypt it using the md5 function and store that. :)

        $key = "";
        srand((double)microtime() * rand(1000, 9999));
        $charset = "abcdefghijkmnopqrstuvwxyzABCDEFGHIJKLMNPQRSTUVWXYZ0123456789";
        for($len=0; $len<8; $len++)
            $key .= $charset[rand(0, strlen($charset)-1)];
        //End of part 1, you will have a simple random password here in $key
        $salt = JUserHelper::genRandomPassword(32);
        $crypt = JUserHelper::getCryptedPassword($key, $salt);
        $password = $crypt . ':' . $salt;

$password will have the value that you can save into database in users table.

Wednesday, August 8, 2012

Joomla 2.5 - Redirect to login page with return URL

If you have a private page that is supposed to be viewed only by registered users and if URL is accessed directly or is accessed on session expire you want to redirect user to login page and on successful login return him to the requested page then simply make a function named something like 'validateUser' and call it for all such views. Here is the brief and comprehensive function:


function validateUser()
{
        $user = JFactory::getUser();
        $userId = $user->get('id');
        if(!$userId)
        {
            $mainframe = &JFactory::getApplication();
            $return = JFactory::getURI()->toString();
            $url  = 'index.php?option=com_users&view=login';
            $url .= '&return='.base64_encode($return);
            $mainframe->redirect($url, JText::_('You must login first') );
            return false;
        }
        return true;
}

Note: If you are trying to do something similar for Joomla 1.5 note you would have to use 'option=com_user' instead of 'users'.

Wednesday, August 1, 2012

Remove parameter from URL PHP

If you are looking to remove a specific parameter from the URL here is how to do it:

Assume you have URL
http://www.b4blinky.com/index.php?option=com_content&view=article&id=1
And you want to remove the id


$currURL = $_SERVER[QUERY_STRING];
//this will get you option=com_content&view=article&id=1
parse_str($currURL,$params); //Puts the url parameters in key=>value pair
//This will give you option=>com_content, view=>article, id=>1
unset($params[$fieldName]);  //Removes the parameter on which 'x' is clicked
//This will remove the $fieldName from the array e.g. 'id'
$newURL = http_build_query($params); //Rebuilds the query with the remaining parameters
//This will rebuild array excluding id
$newURL = 'index.php?'.$newURL;
//and finally your newURL like
index.php?option=com_content&view=article

That you can modify if relative doesn't work for you.

Hope it helps. Happy coding :)