Saturday, February 21, 2015

PDO - Full Snippet

This is fully working snippet for PDO Select with parameters

<?php
$username = "user_name";
$password = "password";
$hostname = "localhost";
$dataBase = "data_base_name";
$character_set = "utf8";
$id = 8;
$slug = "string";
try {
 $conn = new PDO("mysql:host=$hostname;dbname=$dataBase;charset=$character_set", $username, $password);
 $conn -> setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
 $stmt = $conn -> prepare("SELECT * FROM table_name WHERE id = :id AND slug=:slug");
 $stmt->bindParam(":id", $id, PDO::PARAM_INT);
 $stmt->bindParam(":slug", $slug, PDO::PARAM_STR); 
 $stmt->execute();
 echo "Query has: ".$stmt -> rowCount() . " rows <hr>";
 while ($row = $stmt -> fetch()) {
  echo $row['id'] . " - " . $row['slug'] . "\n<br>";
 }
} catch(PDOException $e) {
 echo 'ERROR: ' . $e -> getMessage();
}
?>

Monday, September 29, 2014

Change WordPress Language in Theme

If using _e("Hello","textdomain"); to translate theme you need to create an es_es.po file in text editor:

msgid ""
msgstr ""
"Plural-Forms: nplurals=3; plural=(n%10==1 && n%100!=11 ? 0 : n%10>=2 && n"
"%10<=4 && (n%100<10 || n%100>=20) ? 1 : 2);\n"
"Project-Id-Version: Theme Name\n"
"POT-Creation-Date: \n"
"PO-Revision-Date: \n"
"Last-Translator: Mike <w@w.com>\n"
"Language-Team: \n"
"MIME-Version: 1.0\n"
"Content-Type: text/plain; charset=UTF-8\n"
"Content-Transfer-Encoding: 8bit\n"
"Language: es_ES\n"
"X-Generator: Poedit 1.6.9\n"
"X-Poedit-SourceCharset: UTF-8\n"

#: ../header.php:58
msgid "Hello"
msgstr "Hola"

Compile it with POEeditor you will get file es_es.mo. Upload both files
/wp-content/themes/{yourTheme}/languages

in functions.php add

add_action('after_setup_theme', 'my_theme_setup');
function my_theme_setup(){
    load_theme_textdomain('textdomain', get_template_directory() . '/languages');
}

In WP backend change language to Spanish. And both your theme and WP backend will be in Spanish. You can use plugin to leave backend in English.

Saturday, August 9, 2014

WordPress Pagination and Search in Backend

This an advanced WordPress tutorial about plugin that creates and searches a table in database. All that is happening in WordPress backend. This is useful when you need to see what your visitor have submitted to you in contact form.


After plugin activation populate table tablePrefix_messages with some

Tuesday, August 5, 2014

jQuery Advanced Event Handling

Event handler remove, attach and event stop propagation.




<!DOCTYPE html>
<html lang="en">
 <head>
  <meta charset="utf-8">
  <title>Working with events</title>
  <script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
  <script>
   $(document).ready(function() {
    var myHandleFunc = function(e) {
     alert('Before `stopPropagation()`');
     e.stopPropagation();
     alert('After `stopPropagation()`');     
    };
    
    $('p').click(myHandleFunc);
  
    $('div.chapter').click(function() {
     alert('I\'m not executed unless you click on yellow or remove event handler for p element');
    });
    
    $('#remove').click(function() {
     $('p').off('click');
    });  
    
    $('#attach').click(function() {
     $('p').on('click', myHandleFunc);
    });              
   });
  </script>
 </head>
 <body>
  <div id="container">
   <div>
    <h2>Click on paragraph <button id="remove">Remove event</button> <button id="attach">Attach event</button></h2>
   </div>
   <div style="padding: 50px;background: yellow;"  class="chapter">
    <p style="background: white;">
     Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
    </p>
   </div>
  </div>
 </body>
</html>

Saturday, June 28, 2014

Typical SVN Operations on Command Line

1) Checkout
2) Add SVN Ignore property
3) Create branch based on trunk
4) Switch to new branch
5) Merge dry run
6) Real megring

1) svn checkout https://xp-dev.com/svn/donald456/trunk -r HEAD --depth=infinity --force

2) svn propset svn:ignore "index.php" C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk

3) svn copy -rHEAD https://xp-dev.com/svn/donald456/trunk https://xp-dev.com/svn/donald456/branches/grana001

4) svn switch https://xp-dev.com/svn/donald456/branches/grana001 C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk -r HEAD --force


5) svn merge --dry-run --depth=infinity https://xp-dev.com/svn/donald456/trunk@HEAD \ 
                                        https://xp-dev.com/svn/donald456/branches/grana002@HEAD \
                                        C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk \
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
     U C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    Dry-run merge complete.
    ==== Property Statistics: =====
    Updated: 1

6) svn merge --depth=infinity https://xp-dev.com/svn/donald456/trunk@HEAD \
                              https://xp-dev.com/svn/donald456/branches/grana002@HEAD \ 
                              C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
     U C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/donaldtrunk
    Merge complete.
    ==== Property Statistics: =====
    Updated: 1

Sunday, June 22, 2014

SVN Merge into trunk from development branch

Make sure you are in the trunk when merging. Then right click on your SVN project Team->Merge and fill form like this:

Merging to trunk as destination and branch br2 as development branch



Files from branch br2 will be transferred to trunk. After merging new files are in your working copy so you must commit newly added files to your remote repo.

merge --depth=infinity https://xp-dev.com/svn/qwef/trunk@HEAD https://xp-dev.com/svn/qwef/branches/br2@HEAD C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    --- Merging differences between repository URLs into C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    A  C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
     G C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk
    Merge complete.
    ===== File Statistics: =====
    Added: 1
    ==== Property Statistics: =====
    Merged: 1
commit -m "added num4.txt" C:/Users/yourUserName/Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
    Adding         Documents/Aptana Studio 3 Workspace/atrunk/docs/num4.txt
    Committed revision 75.

And this is the output created in console in Eclipse

Monday, May 12, 2014

PHP Design Patterns - The Observer Pattern

In this case class NoticeBoard is publisher and classes Student and Teacher are subscribers

<?php
class NoticeBoard{
  private $_observers = array();

  public function addNote( $name )  {
    foreach( $this->_observers as $obs )
      $obs->onChanged( $this, $name );
  }

  public function addObserver( $observer )  {
    $this->_observers []= $observer;
  }
}

class Student{
  public function onChanged( $sender, $args )  {
    echo("The ".__CLASS__." is informed '$args' \n" );
  }
}

class Teacher{
  public function onChanged( $sender, $args )  {
    echo( "The ".__CLASS__." is informed '$args' \n" );
  }
}

$s = new Student(); 
$t = new Teacher();

$nb = new NoticeBoard(); 
$nb -> addObserver($s); 
$nb -> addObserver($t);

$nb->addNote('Due storm exams are postponed.');
?>

Result is
The Student is informed 'Due storm exams are postponed.'
The Teacher is informed 'Due storm exams are postponed.'