Do you need a PHP programmer?
Use my services as a freelance PHP programmer by hiring me to do PHP programming on your website project.

I have built many custom PHP applications like project managers, classified ad websites and content management systems. I also work with open source applications such as WordPress, online shopping cart websites like Magento and develop content management systems like Joomla.

Tuesday, May 29, 2012

Table and Model Name - Cakephp

Hi everyone,

Long time I have no post something.
Today I will write something about "Table and Model Name in Cakephp".
As you know, in Cakephp we should write the name of table in plural.
I mean for example if you want make table with name "user" you should make it like "users" and "post" should be "posts".

So how to handle table names that are written in singular with "...y"?
hehe ... just so simple, I make an example with table name "entry" and it should be "entries". :D

But for Model name in cakephp you should write the name in singular.
you already have a table "entries", than in your app\models , you should make a new file for model. The name of the file is "entry.php" (in singular 'user.php, post.php').
And below is basic code for Model Name :


class Entry extends AppModel {
 var $name = 'Entry';
}

How about if we use underscore in our table names??
for example you have table name "categories_lists", how will we make the Model name?
I show you the simple way,
in your app\models, create a new file with name "categories_list.php".
write this code inside of the file :

class CategoriesList extends AppModel {
 var $name = 'CategoriesList';
}

Ok, that's all
if you have any question, just write your comment below :D
Thanks for your visit.

warm regard from bali :)

Wednesday, April 18, 2012

Auto Request and Manage Json Data

Hello, ,
Today I have got a new knowledge from programming website.
This is about data-interchange format. Did you ever hear about JSON?

JSON (JavaScript Object Notation) is a lightweight data-interchange format. It is easy for humans to read and write. It is easy for machines to parse and generate. It is based on a subset of the JavaScript Programming Language, Standard ECMA-262 3rd Edition - December 1999. JSON is a text format that is completely language independent but uses conventions that are familiar to programmers of the C-family of languages, including C, C++, C#, Java, JavaScript, Perl, Python, and many others. These properties make JSON an ideal data-interchange language.( you can read more in www.json.org ).

This is example of json data :

{"name":"bywebs.blogspot.com","time":"12 : 20 : 56"}


And now I will make a tutorial to auto request and manage json data every x seconds. But in this tutorial I will use a jquery script (jquery-1.6.1.min.js).

Let's see for my code :

<script type="text/javascript">

    var auto_refresh = setInterval(function(){ // interval auto refresh
 
        $.getJSON("json.php", function(json) { // request method using jquery

                                               // json.php will return json data

            // parsing data

            var table = '';

            table += '<tr><td>Name</td>';

            table += '<td>'+json['name']+'</td></tr>';

            table += '<tr><td>Server Time</td>';

            table += '<td>'+json['time']+'</td></tr>';

            

            //inser data to Id bywebs

            document.getElementById('bywebs').innerHTML = table;


         });

     }, 5000); // will request json every 5 seconds, you can change it

</script>

You can develop this code by yourself.
so for the complete code you can "Download here".

Hope it useful for all, if you have any question and suggestions, just write your comment below. :)

Good Luck.

Monday, April 16, 2012

Replace space and all Illegal character on string - PHP

Hi, ,
Do you search for how to replace/remove illegal character (@#$%^&* etc) in your text string?
On this post I will share you simple code to make it.
This code will only allowed numbers (0-9) and characters (a-z).

my code :

<?php
    $string = 'by^#^we%%&bs.b_((logs+)&#@pot.()*)(~co!~@#_+m';
    $string = preg_replace('/[^0-9 a-z]+/i', '', $string);
    
    echo $string;
?>

And below is code for replace multiple space on your text string

<?php
    $string = 'by   webs.   blog  spot.  com';
    $string = str_replace(array(' '),array(''),$string);
    
    echo $string;
?>

Hope it will useful for all,
If you have questions or suggestions, please write your comment below.

Good Luck :)

Saturday, April 14, 2012

Make Confirm Dialog Javascript

Hello every body,
When the first time I learned javascript code, I was really happy when I succeed to make pop up dialog, :D
And now I will share a simple code to make a confirmation dialog before you do the actions.
I have some ways, but the simple one is:

<a href="http://bywebs.blogspot.com" onClick="return confirm('Do you realy want to visit my website?');"> My Website </a>

Demo

Beside that I also have the second way, but it will use a function.
Let's take a look

<script type="text/javascript">
    function _confirm(url){
        if(confirm('Do you realy want to show my link?')){
            document.getElementById('myDiv').innerHTML = "<a href='"+url+"'> My Website</a>";
        }
    }
</script>

<a onClick="_confirm('http://bywebs.blogspot.com');">show my link</a>
<div id="myDiv"></div>

Demo
show my link

That's all :).
Hope it will useful, if you have any question , just write your comment below.
Thanks and Good luck

Thursday, April 12, 2012

Cut a string after X characters -- PHP

Hi guys, ,
In this my post, let's we talk about string manipulation using PHP code.
There are many functions of php that you can use to manipulation your string.
But now I will share you a simple way how to cut a string after x characters.
We will use 2 manipulation functions of php, they are strlen ( for count characters of your string ) and substr ( for cut characters of your string ).

This is my code :

<?php  
    if(isset($_POST['submit'])){          
            
            $text = $_POST['text'];
            
            //check if you have value in text.  
            if(empty($text)){  
               echo "<h3>Error: Please complete the form.<a href=\"$_SERVER[PHP_SELF]\">back</a></h3>";  
               exit(); //exit the script and don't process the rest of it!  
            }  
            
            $limit = 10; //limit the char will cut
            if (strlen($text) > $limit) {
                $text = substr($text, 0, $limit) . '...'; //if $text more than $limit char will replace with (...)
            } else {
                $text = $text;
            }
                   
            //success message, redirect to main page.          
            $msg = urlencode("Result of your string : <h4>$text</h4> <a href=\"cut_string.php\">Try again?</a>");  
                header("Location: cut_string.php?msg=$msg");  
                exit();  
              
          
    }else{  
            //if there is a message, display it  
            if(isset($_GET['msg']))  
            {  
                //but decode it first!  
    
                echo "<p>".urldecode($_GET['msg'])."</p>";  
            }  
            //the upload form  
        echo "  
        <h1>Simple code to cut a string after X characters PHP - <a href=\"http://bywebs.blogspot.com\">bywebs.blogspot.com</a></h1>
        <form action=\"$_SERVER[PHP_SELF]\" method=\"post\"enctype=\"multipart/form-data\">\n  
        <p>Your text:<input type=\"text\" name=\"text\" /></p>\n  
        <p><input type=\"submit\" name=\"submit\" value=\"Submit\" /></p>";  
    }  
?>

Just a simple code :)
Let's try for your self, for the complete file you can "Download Here".
Hope it useful for all, if you have any question, you can write your comment below :)

Good Luck :)