Tag Archives: php

How to easily create PHP image gallery

This becoming my favorite image gallery now – fotorama.io. So easy to implement with plenty of customization that we can do.

If you don’t believe how easy it is, copy following source code, paste and save as an html file. Then open it and see what will happen.

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<link href="http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.css" rel="stylesheet">
<script src="http://cdnjs.cloudflare.com/ajax/libs/fotorama/4.6.3/fotorama.js"></script>

<div class="fotorama" data-width="100%" data-ratio="800/600" data-nav="thumbs" data-loop="true" data-allowfullscreen="true">
 <img src="http://s.fotorama.io/1.jpg">
 <img src="http://s.fotorama.io/2.jpg">
</div>

Alternatives:

http://photoswipe.com/

lightbox

Getting data from CI db query

1. All result set

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   foreach ($query->result() as $row)
   {
      echo $row->title;
      echo $row->name;
      echo $row->body;
   }
}

2. Just one row

$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
   $row = $query->row(); 

   echo $row->title;
   echo $row->name;
   echo $row->body;
}

Reference: CI2, CI3

Sublime Text 3

This editor is fast and neat but requires plugins to be installed to fully utilize the power.

1. install the package control

cmd+shift+P
package control:install package

2. then install other plugins via the package control using the same step.

Some of the plugins.

  1. Sub­limeLin­ter
  2. Haven’t tried to install any other yet

Other sources on useful plugins:

http://wasil.org/sublime-text-3-perfect-php-development-set-up

http://neverstopbuilding.com/sublime-plugins-for-php

New things to learn

Getting started with some new tools. To get comfortable with uncomfortables.

Other things to checkout

CodeIgniter template library

Why use template library for CI

  • You feel like using views can be clunky, especially when “embedding” views.
  • You don’t like calling header, footer, and other global views from every Controller method.
  • You prefer having one “master template” that can be changed for any controller in order to meet unique application design needs.
  • You don’t want to drastically alter the way you interface controllers and views.
  • You like clear, thorough documentation on par with CodeIgniter’s User Guide.

source: http://jeromejaglale.com/doc/php/codeigniter_template

or alternative download

PHP Data Object (PDO)

An object in PHP to manage data

PDO

  • exec
  • prepare
  • query
  • lastInsertId

PDOStatement

  • execute
  • fetch
  • fetchAll
  • fetchColumn
  • fetchObject
  • rowCount

PDOException

  • getMessage (return string)

Some sample code:

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
$dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass); //establish connection
$dbh = null; //close connection
 
//insert by prepare statement
$stmt = $dbh->prepare("INSERT INTO REGISTRY (name, value) VALUES (?, ?)");
$stmt->bindParam(1, $name);
$stmt->bindParam(2, $value);
 
// insert one row
$name = 'one';
$value = 1;
$stmt->execute();
 
//get result
$stmt = $dbh->prepare("SELECT * FROM REGISTRY where name LIKE ?");
 
if ($stmt->execute(array("%$_GET[name]%"))) {
  while ($row = $stmt->fetch()) {
    print_r($row);
  }
}

MySQL database auto backup on windows

To backup mysql database (all databases) can use this batch file

Then you can compile the .bat file to become .exe file so that the password is hidden.

Download bat to exe converter from CNET download

Lastly, you can include the exe file to run in task scheduler

You can have option to upload the file via FTP to other server

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
:: Start FTP files to remote server
:: Create the temporary FTP script file
> script.ftp ECHO ftpusername
>>script.ftp ECHO ftppassword
>>script.ftp ECHO lcd /
>>script.ftp ECHO lcd /MySQLBackups/backupfiles
>>script.ftp ECHO binary
>>script.ftp ECHO prompt n
>>script.ftp ECHO put FullBackup.%backupdate%.zip
>>script.ftp ECHO bye
:: Use the temporary script for unattended FTP
:: Note: depending on your OS version you may have to add a '-n' switch
FTP -v -s:script.ftp ftphostcom.com
:: Overwrite the temporary file before deleting it
TYPE NUL >script.ftp
DEL script.ftp
:: DONE FTP backup file to remote server

Database config for localhost and server

To easily toggle database config for localhost and server, you can simply check whether you are accessing the local or server.

1
2
3
4
5
6
7
8
9
10
11
12
13
if (preg_match("/localhost/i", $_SERVER['SERVER_NAME'])) {
define ('DBHOST','localhost');
define ('DBNAME','localdbname'); 
define ('DBUSER','localuser');
define ('DBPASS','localpass');
define ('DEV',TRUE);
} else {
define ('DBHOST','localhost');
define ('DBNAME','serverdbname'); 
define ('DBUSER','serveruser');
define ('DBPASS','serverpass');
define ('DEV',FALSE);
}