Javascript: Converting a variable string name to a readable form

I build this one on a project where-in i have a lot of input types, and i’m a bit lazy to manually type it’s readable form, so i created a function wherein it will automatically converts a variable name or string to its readable form. This javascript function has some rules in mind:

  • naming convesion: the first 3 letters of the string specifies its element type (e.g “txt” for a “text” type, “ddl” for a dropdown list). regardless of types, the starting 3 letters is ignored.
  • Camel casing – the string should be in a form of camel casing since it relies that the first capital letter or number it found means a new word.
  • numeric is considered a new “word” (e.g txtNameOfSibling1, will result in “Name Of Sibling 1”

Sample usage:

ToProperWord("txtNameOfSibling1");
Result: ” Name Of Sibling 1″

so here’s the code snippet. let me know your thoughts.

	function ToReadableForm(ElementName)
	{
		var toConvert = ElementName;
		if(toConvert.length > 3)
		{
			var converted = toConvert.substring(3, toConvert.lenght);
			var buildWord = "";
			var finalWord = "";
			var ucaseCtr = 0;
			for(var ctr = 0; ctr < converted.length; ctr++)
			{
				var newWord = false;				
				var curChar = converted.charAt(ctr);
				if(curChar == curChar.toUpperCase() || !isNaN(curChar)) { newWord = true; }								
				if(buildWord != "" || newWord == true)
				{
					if(newWord == true)
					{
						finalWord = finalWord + " " + buildWord;
						buildWord = "";
					}
					buildWord = buildWord + curChar;				
				}
			}
			finalWord = finalWord + " " + buildWord;
		}		
		return finalWord;
	}
Advertisements

Angular.js with PHP RESTful JSON API

This previous days I’ve been working with Angular.js “JavaScript MVC Framework create by Google” to enhance HTML with new attributes, expression, etc…

Below are the steps I made to access RestAPI with angular.

1. Declaring element of HTML as a root  using “ng-app” directive.

angular - directives

2. Including angular library.

angular - include angular

3. Calling our API using our controller.

angular - controller

4. Bind our data to HTML.

angular - bindings
5. Our post.php API.

angular - post

6. API Result.

angular - api Result
7. Result.


angular - result

Below is the full source:

<!DOCTYPE html>
<html ng-app>
<head>
<script src=”http://localhost:8081/webapi/cnd/js/angular.js”></script&gt;
<script type=”text/javascript”>
function simplebindcontroller($scope,$http){
$http.get(‘api/post.php’).
success(function(data) {
$scope.user = data;
});
}
</script>

</head>
<body>
<div ng-controller=”simplebindcontroller”>
<ul>
<li ng-repeat=”name in user”>
{{name.id}} | {{name.name}}
</li>
</ul>
</div>
</body>
</html>

Calling MYSQL Store Procedure using PHP PDO

Calling MYSQL Stored Procedure with PHP PDO.

1. Create your Store Procedure

PDO - SQL

 

2. Call your Store Procedure using PDO

PDO - use


 Source code below:

1. STORE PROCEDURE

CREATE DEFINER=`root`@`localhost` PROCEDURE `spu_user_create`(in Firstname varchar(50),
in Lastname varchar(50),
in Email varchar(50),
in Password varchar(50))
BEGIN
INSERT INTO user
(
user.Firstname,
user.Lastname,
user.Email,
user.Password)
VALUES
(Firstname,
Lastname,
Email,
Password);
END


2. PDO

public function Create($firstname,$lastname,$email,$password){

try{

$db=pdoconnection::connect();

$stmt = $db->prepare(“CALL spu_user_create(?,?,?,?)”);
$stmt ->bindValue(1, $firstname, PDO::PARAM_STR);
$stmt ->bindValue(2, $lastname, PDO::PARAM_STR);
$stmt ->bindValue(3, $email, PDO::PARAM_STR);
$stmt ->bindValue(4, $password, PDO::PARAM_STR);

$stmt -> execute();
$db=null;

return true;

}catch(PDOException $ex){
$db->rollBack();
echo “Error encountered in user registration.”.$ex->getMessage();
}
}

hope this helps.

Connect PHP to MYSQL using PDO

Simple way to connect PHP to MYSQL (stored procedure) using PDO.

We will be using 2 classes config.php and connection.php.

config.php

- this is where we put our server, database and authentication.
PDO - config

connection.php

- this is how we connect the server using PHO PDO.
PDO - pdoconnection

sample.php

- this is sample how we use the classes.
PDO - use


below are the source code:

config.php

<?php
class config{
public static $host=’localhost’;
public static $database=’newco’;
public static $username=’root’;
public static $password=”;
}
?>
<?php


connection.php

require_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/config.php’;
class pdoconnection{
public static function connect(){
$driver =’mysql:host=’;
$db =’;dbname=’;
$charset =’;charset=utf8′;
$con = new PDO($driver . config::$host . $db . config::$database . $charset, config::$username, config::$password);
$con ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$con ->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$con ->setAttribute(PDO::ATTR_PERSISTENT , true);
return $con;
}
}
?>


sample.php

<?php
require_once $_SERVER[‘DOCUMENT_ROOT’] . ‘/connection.php’;
class User{
public function Create($firstname,$lastname,$email,$password){
try{
$db=pdoconnection::connect();
$stmt = $db->prepare(“CALL spu_user_create(?,?,?,?)”);
$stmt ->bindValue(1, $firstname, PDO::PARAM_STR);
$stmt ->bindValue(2, $lastname, PDO::PARAM_STR);
$stmt ->bindValue(3, $email, PDO::PARAM_STR);
$stmt ->bindValue(4, $password, PDO::PARAM_STR);
$stmt -> execute();
$db=null;
return true;
}catch(PDOException $ex){
$db->rollBack();
echo “Error encountered in user registration.”.$ex->getMessage();
}
}
?>

hope this helps you.