Updates from April, 2012 Toggle Comment Threads | Keyboard Shortcuts

  • ibnuyahya 11:03 am on April 20, 2012 Permalink | Reply  

    php convert stdClass to Array 

    You can use this function to convert stdClass to Array

        function objectToArray($d) {
            if (is_object($d)) {
                // Gets the properties of the given object
                // with get_object_vars function
                $d = get_object_vars($d);
            }
    
            if (is_array($d)) {
                /*
                * Return array converted to object
                * Using __FUNCTION__ (Magic constant)
                * for recursive call
                */
                return array_map(__FUNCTION__, $d);
            }else {
                // Return array
                return $d;
            }
        }
     

    and this function to convert Array to stdClass
    (More …)

     
    • Elijah1475 3:43 pm on April 23, 2012 Permalink | Reply

      Hi there would you mind sharing which blog platform you’re working with? I’m planning to start my own blog soon but I’m having a tough time making a decision between BlogEngine/WordPress/B2evolution and Drupal. The reason I ask is because your design and style seems different then most blogs and I’m looking for something completely unique. P.S My apologies for getting off-topic but I had to ask!

      • ibnuyahya 3:53 pm on April 23, 2012 Permalink | Reply

        Hi

        I’m using WordPress with P2 theme. Anywhere, all blog platform you mentioned does support custom theme. You can make your own unique theme.

  • ibnuyahya 12:24 pm on April 3, 2012 Permalink | Reply
    Tags:   

    esolat jakim 

    Memandangkan banyak permintaan untuk dapatkan jadual waktu solat sepanjang tahun, maka saya sediakan class e-solat jakim yang baru yang lebih elegen. Class baru ini membolehkan korang dapatkan jadual waktu solat mengikut hari, bulan dan keseluruhan tahun. Walaubagaimanapun, kod e-solat jakim yang lama masih lagi berfungsi.

    From ibnuyahya.com

    (More …)

     
  • ibnuyahya 11:22 pm on March 13, 2012 Permalink | Reply
    Tags: , multiton   

    php design pattern : multiton 

    Sama seperti singleton, tetapi ditambah dengan map nama instance sebagai key-value. Sangat berguna jika anda ingin menyediakan fungsi multi-database connection, sebagai contoh.. anda ingin membuat connection ke mysql dan mongodb didalam satu aplikasi. Dengan multiton, anda boleh membuat instance yang berbeza bagi setiap database.

    (More …)

     
    • ibnuyahya 11:11 pm on March 13, 2012 Permalink | Reply
      Tags: , singleton   

      php design pattern : singleton 

      Singleton memastikan hanya satu instance sahaja dijalan bagi satu class.
      (More …)

       
      • ibnuyahya 11:20 am on January 27, 2012 Permalink | Reply
        Tags:   

        Codeigniter cPanel / WHM XMLAPI Library 

        In the past post, http://ibnuyahya.com/cpanel-xmlapi-with-codeigniter/ I’ve shared how to use cpanel xmlapi in codeigniter. However, since CI 2.x.. plugins have been removed. But don’t worry, you still can use helper to import external php class.

        1. Download xmlapi class from the following url https://github.com/CpanelInc/xmlapi-php
        2. Rename the file as xmlapi_helper.php and put in your helpers directory application\helpers\xmlapi_helper.php
        3. Done. you can call your xmlapi class as the following example
        $this->load->helper('xmlapi');
        $xmlapi = new xmlapi($ip);
        $xmlapi->password_auth($cpanelUser,$cpanelPwd);
        $xmlapi->set_port(2083);
        $xmlapi->set_debug(1);
        
         
        • ibnuyahya 10:42 am on April 7, 2011 Permalink | Reply
          Tags: asas, belajar php   

          asas: belajar php – 1 

          Pengenalan

          Apa itu PHP

          1. PHP pada asalnya dikenali sebagai “personal home page” dan kemudiannya di tukar kepada “Hypertext Preprocessor”
          2. Di perkenalkan oleh “Rasmus Lerdorf” pada tahun 1994 .
          3. Maklumat lanjut berkenaan sejarah PHP, boleh rujuk ke wikipedia
          4. PHP merupakan server-side scripting language

          Apa itu server-side scripting language?

          untuk lebih mudah untuk faham, bagi server-side scripting language.. skrip tu di larikan/run pada server manakala client-side scripting language pula di larikan/run pada client sebagai contoh, browser.

          Persediaan

          Tak payah merapu introduction panjang-panjang, kita terus kepada coding. Tapi sebelum tu, beberapa persediaan perlu di lakukan terlebih dahulu.

          1. WAMP Server – muat-turun wampserver dari http://www.wampserver.com/en/download.php .Wampserver ni amat mudah berbanding webserver yang lain terutamanya bagi pengguna Windows… memandangkan reader disini ramai yang menggunakan windows ;p
          2. Notepad++ – muat-turun notepad++ dariĀ  http://notepad-plus-plus.org/download . Kalau nak gunakan notepad yang ada dalam ms windows pun boleh, takda masalah. Janji jangan gunakan perisian cetak rompak terutamanya yang bergelar Muslim. Kalau ada tu.. harap kurangkan dan seteruskan gunakan yang halal. Banyak lagi pilihan ye dak?.. hehe. bagi pengguna linux dan mac osx, boleh la gunakan apa2 yang sewaktu dengannya.
          3. dah muat-turun tu, tak kan nak tengok saja… install la wampserver dan notepad++ tu

          okeh. korang buat la preparation ni dulu.. nanti sambung lagi ke asas: belajar php – 2

           
          • karim 12:05 pm on April 27, 2011 Permalink | Reply

            guna appserv pun ok gak..

            • admin 3:36 pm on April 28, 2011 Permalink | Reply

              yup. mana-mana pun boleh. :) entah bila tutorial ni nak bersambung hahaha

        • ibnuyahya 4:25 pm on February 23, 2011 Permalink | Reply
          Tags: callback, , validation   

          Codeigniter passing variables to custom validation callbacks 

          In this example, username will use custom callback function. to use callback function, you must use callback_ prefix follow by function name in the validation rule.

          If your callback function name is username_check(), then you need to write like this callback_username_check.

          BTW, if you read documentation from CodeIgniter… the sample didn’t show you how to past parameter to your function if you has more than one parameter. ( http://codeigniter.com/user_guide/libraries/form_validation.html )

          So here is the solution.

          $this->form_validation->set_rules('username', 'Username', 'callback_username_check[' . $second_param . ']');
          

          callback function

          function username_check($str, $second_param) {
                  if (($str == 'test') && ($second_param == '123')) {
                      $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
                      return FALSE;
                  } else {
                      return TRUE;
                  }
              }
          

          edited CodeIgniter sample.

          class Form extends CI_Controller {
              function index() {
                  $this->load->helper(array('form', 'url'));
                  $this->load->library('form_validation');
                  $second_param = '123';
                  $this->form_validation->set_rules('username', 'Username', 'callback_username_check[' . $second_param . ']');
                  $this->form_validation->set_rules('password', 'Password', 'required');
                  $this->form_validation->set_rules('passconf', 'Password Confirmation', 'required');
                  $this->form_validation->set_rules('email', 'Email', 'required');
          
                  if ($this->form_validation->run() == FALSE) {
                      $this->load->view('myform');
                  } else {
                      $this->load->view('formsuccess');
                  }
              }
          
              function username_check($str, $second_param) {
                  if (($str == 'test') && ($second_param == '123')) {
                      $this->form_validation->set_message('username_check', 'The %s field can not be the word "test"');
                      return FALSE;
                  } else {
                      return TRUE;
                  }
              }
          }
          
           
          • ibnuyahya 11:31 am on January 17, 2011 Permalink | Reply  

            get domain name in php 

            Lets say I would like to display domain “mail.yahoo.com” instead of displaying all url such as “http://us.mc1126.mail.yahoo.com/mc/welcome?.gx=1&.tm=1276179131&.rand=0vj0256m6pedg”

            function get_parent_domain($domain = '') {
                if($domain != ''){
                    $urls = parse_url($domain);
                    if(isset($urls['host'])){
                        $host = explode('.', $urls['host']);
                        $parent_host = array_slice($host, -3);
                        return implode('.', $parent_host);
                    }else{
                        return '';
                    }
                }else{
                    return '';
                }
            }
            
            //usage
            echo get_parent_domain("http://us.mc1126.mail.yahoo.com/mc/welcome?.gx=1&.tm=1276179131&.rand=0vj0256m6pedg");
            
             
            • ibnuyahya 9:08 pm on January 8, 2011 Permalink | Reply
              Tags: facebook crap   

              Facebook Double Squares 

              I’m frustrated with facebook hackercup. Can’t submit my file because of didn’t notice that I need to submit the answer within 6 minutes after download the input file. Btw I manage resubmit using my wife account… but there is another problem. The time timeout a few second after I download the input file…

              here is my code to get the output. Just a simple code that can give me the answer.

              $file = 'input.txt';
              $f = fopen ($file, "r");
              $u=0;
              while ($line = fgets($f, 4096)) {
                  if($u == 0)
                      $max = trim($line);
                  if( ($u > 0) && ($u <= $max) )
                      echo double_square($line) . '
              '; $u++; } fclose($f); function double_square($input) { $input = trim($input); $x = floor(sqrt($input)); $r = 0; for ($i = $x; $i >= 0; $i--) { if (fmod(sqrt(($input - pow($i, 2))), 1) == 0){ if (sqrt(($input - pow($i, 2))) <= $i){ $r += 1; }else{ break; } } } return $r; }
               
              • anao 4:20 am on January 9, 2011 Permalink | Reply

                You are getting wrong answer for 25. The correct answer should be 2 and what you are getting is 1

                • admin 8:04 am on January 9, 2011 Permalink | Reply

                  Are you sure?. It does return 2 for 25. You can verify with this input
                  input
                  5
                  10
                  25
                  3
                  0
                  1

                  output
                  1
                  2
                  0
                  1
                  1

            • ibnuyahya 9:35 am on January 7, 2011 Permalink | Reply
              Tags: collapse, expand,   

              expand / collapse table row with jquery 

              This is simple method to make table row expand and collapse to show child data.

              table expand/collapse

              
              <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
              <html>
                  <head>
                      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
                      <title>Table collapse/expand</title>
                      <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.4/jquery.min.js"></script>
                      <script type="text/javascript">
                          $().ready(function() {
                              $('.child').hide();
                              $('.action').click(function(){
                                  var id = $(this).attr('id');
                                  id = id.split('-');
                                  $("."+id[1]).toggle();
                              });
                          });
              
                      </script>
                  </head>
                  <body>
                      <table id="sample" border="1" width="50%">
                          <tr><th>Name</th><th>Address</th><th>action</th></tr>
                          <?php
                          for ($x = 1; $x < 10; $x++) {
                              echo '<tr id="' . $x . '"><td>Ayam ' . $x . '</td><td>addr ' . $x . '</td><td><a href="#" class="action" id="a-' . $x . '">Toggle</a></td></tr>';
                              for ($y = 1; $y < rand(1, 5); $y++) {
                                  echo '<tr id="'. $x . '.' . $y . '" class="child ' . $x . '"><td>---  Anak Ayam ' . $x . '-' . $y . '</td><td>addr ' . $x . '-' . $y . '</td><td>-</td></tr>';
                              }
                          }
                          ?>
                      </table>
                  </body>
              </html>
              
               
              • tc 5:54 am on January 25, 2011 Permalink | Reply

                doesn’t work..

                • admin 9:24 am on January 26, 2011 Permalink | Reply

                  should copy and paste in php file because I create random table data dynamically using php.

              • kangkam 11:00 am on February 1, 2011 Permalink | Reply

                works for me, thank you bos :)

              • Drupad 4:52 am on November 3, 2011 Permalink | Reply

                fix the jquery url to look like this!

            c
            compose new post
            j
            next post/next comment
            k
            previous post/previous comment
            r
            reply
            e
            edit
            o
            show/hide comments
            t
            go to top
            l
            go to login
            h
            show/hide help
            shift + esc
            cancel