24 June 2017

CodeIgniter 3 Sessions

CI sessions are an alternative to php sessions. It is advised to autoload the sessions so they are always available. CI sessions simplify using native php sessions, ie the $_SESSION superglobal. For example, there is no need to include the session_start() function (to start a new or resume an existing session). Session data is simply an array, associated with a particular session ID (cookie).

As well as read, set and unset values, CI also provides 2 special types of session data, flashdata and tempdata.

To read a session variable use either $_SESSION['item_key_name'] (or the magic getter) $this->session->item_key_name

To retrieve all of the existing userdata, you can simply omit the item key $_SESSION for example print_r($_SESSION) (the magic getter only works for properties).

You can assign (set) data to the $_SESSION array, as with any other variable. Or as a property of $this->session. For example $_SESSION['something'] = 'hello' or $this->session->something = 'hello'

Check if a session variable exists: isset($_SESSION['some_name'])

To remove session variables, use for example, unset($_SESSION['some_name']) or multiple values: unset($_SESSION['some_name'], $_SESSION['another_name'])

Session Flashdata

Flashdata is data that will only be available for the next request, and is then automatically cleared. This can be very useful, especially for one-time informational, error or status messages. To create a session variable as flashdata:

$_SESSION['item'] = 'value';
$this->session->mark_as_flash('item');

Or more concisely:

$this->session->set_flashdata('item', 'value');

Read flashdata variables the same way as regular session data $_SESSION['item']

However, if you want to be sure that you’re reading “flashdata” (and not any other kind), you can also use the flashdata() method:

$this->session->flashdata('item');

Or to get an array with all flashdata, simply omit the key parameter:

$this->session->flashdata();

If you find that you need to preserve a flashdata variable through an additional request, you can do so using the keep_flashdata() method.

$this->session->keep_flashdata('item');

Tempdata

Tempdata is session data with a specific expiration time. To mark an existing item as tempdata, simply pass its key and expiry time (in seconds!) to the mark_as_temp() method:

$_SESSION['item'] = 'value';
$this->session->mark_as_temp('item', 300); // erased after 300 seconds

Or concisely:

$this->session->set_tempdata('item', 'value', 300);

Read tempdata variables the same way as regular session data $_SESSION['item']

Destroying a Session

To clear the current session both of these will work in exactly the same way session_destroy() or $this->session->sess_destroy()

Conclusion

You can still use the native php session functions with the CI session library and for simplicity, I think this is best practice. You then only need to learn the extra CI session methods (ie. flashdata, tempdata). The added bonus to the CI's session library is that you don't have to add session_start() to every file that uses sessions.

30 May 2017

CodeIgniter's last_query()

I had an issue running codeigniter's $this->db->last_query().

It returns the last query that was run (the query string, not the result). But, remember to run the query, ie call the get() method before the last_query() method.

Example

$this->db->select('a_column');
$this->db->from('your_table');
// note, you have to call get()
$query = $this->db->get();
echo $this->db->last_query();

It's a great function to call when you are having issues converting your SQL statements to the query builder class.

25 April 2017

Running Vagrant on Windows

Recently Packt Publishing kindly gave away Learning PHP 7 in a short 24 hour promotion. I've been going through it and had a problem setting up Vagrant 1.9.3 on Windows. Specifically running vagrant up returned this error

C:/Program Files (x86)/HashiCorp/Vagrant/embedded/gems/gems/vagrant-1.9.3/lib/vagrant/util/is_port_open.rb:21:in `initialize': The requested address is not valid in its context. - connect(2) for "0.0.0.0" port 8080 (Errno::EADDRNOTAVAIL) 

Here is the reported error and fix Vagrant 1.9.3 up fails.

None of this is the book's falt of course. Technology changes fast and it's tough to keep up to date with it.

Next problem I had was the book assumes you have an ssh client installed on windows, which I didn't, so the command vagrant ssh returned another error. Although this time, the error simply instructed me to install an ssh client and even suggested some. I chose Git, but found there was one more step to do after installing it. You need to add C:\Program Files\Git\bin and C:\Program Files\Git\usr\bin to your Windows environment path. This latter path contains ssh.exe along with a bunch of other tools.

Now you can easily run vagrant up and vagrant ssh from your project folder path.

The chapter index in this book looks fantastic! There are lots of new things I need to learn there, and some other things I should master. Good luck.

24 April 2017

QPython Hello World over SSH

QPython is a script engine that allows you to run python programs on Android. It comes with SL4A (Scripting Layer for Android) built in. SL4A has access to many of the APIs available to full-fledged Android applications, but has a greatly simplified interface that makes it easy to get things done by wrapping it in Python modules. It supports my really old Gingerbread 2.3.5 OS and can be installed directly from the PlayStore. It really is a great piece of standalone software that will get you coding in minutes, but I decided to run this over SSH with SSHDroid. Take your time to properly setup and protect SSHDroid, get things like the IP address, then try this hello world application.

On your local PC, create a file called test.py and add

import androidhelper
droid=androidhelper.Android()
droid.makeToast("hello world!")

Copy it to the QPython scripts folder on the SD card (note, your ssh port number and IP Address will be different)

scp -P 2017 test.py root@192.168.0.109:/mnt/sdcard/qpython/scripts

To test the script, you can do it via the QPython GUI interface. You can also run it by calling QPython's python interpreter from a logged in SSH session, and pass it your script to execute. It's a bit like calling python test.py

ssh -p 2017 root@192.168.0.109
/data/data/org.qpython.qpy/files/bin/qpython.sh /mnt/sdcard/qpython/scripts/test.py

If all looks good, you might want to now call it over SSH. Log out of your SSH session. Wrap your whole command in an alias to issue a quick command from your Linux terminal

alias phone-helloworld = 'ssh -p 2017 root@192.168.0.109 "/data/data/org.qpython.qpy/files/bin/qpython.sh /mnt/sdcard/qpython/scripts/test.py"'

26 March 2017

Tweaking the vertical position of an image

Positioning images/icons alongside text or other elements can be tricky. The following CSS will allow you to slide the image along the vertical axis.

.something img {
    position:relative;
    top:50%;
    transform:translateY(-35%); /* play with this */
}

23 February 2017

Running CodeIgniter in Heroku

Note: This does not include a database setup.

Grab a copy of the latest codeigniter and navigate to the root folder

cd diylingo/CodeIgniter-3.1.3/

Login to heroku

heroku login

Add git

git init

Identify heroku as the remote repository for git to use

heroku create diylingo

In codeigniter, open config.php and add the heroku address outputted from the create command to

$config['base_url'] = 'https://diylingo.herokuapp.com/';

Because this will be detected as a php app and there is a composer.lock file which demands you satisfy all dependencies, you'll need to run

composer update

Now prepare the files for upload

git add .

git commit -m "initial commit"

Push your code to the remote heroku git repository

git push heroku master

Check the result

open heroku

You should have the CodeIgniter welcome page opened for you in the browser.

16 February 2017

Getting started with pagination in CodeIgniter

Intro

Sometimes your database can return a lot of data. Like hundreds (or even thousands) of rows that may need to be outputted to the user. In these kinds of situations, it’s good to use pagination, which codeigniter has an built-in library for.

Pagination is the process of dividing this data into chunks, like pages in a book. On your webpage, this often looks like a series of clickable number links, but can also be formatted to just previous and next links. Behind the scenes though, pagination works with SQL’s LIMIT and OFFSET clauses. These two clauses in essence specify a range of results (database rows) to output.

In CodeIgniter, the pagination library takes care of figuring out how many pagination links are required, and inserts the OFFSET clause value as a method argument to the controller method you are calling it from. Here is an example of just one pagination link

<a href="/language/read/20" data-ci-pagination-page="2">2</a>

How to do it

Here is how I attempted to use pagination.

Controller

public function read($pagination_offset = 0)
{
  $this->load->library('pagination');
  $pagination_config['base_url'] = base_url('language/read'); // the controller you are calling
  $pagination_config['per_page'] = 20; // sql query LIMIT number

  $this->data['languages'] = $this->language_model->get_languages($pagination_config['per_page'], $pagination_offset); // your database call with LIMIT and OFFSET arguments
  $total_query_rows = $this->db->query('SELECT id FROM languages')->num_rows(); // what the database would have yielded without the LIMIT and OFFSET clauses (*see Notes)

  $pagination_config['total_rows'] = $total_query_rows;
  $this->pagination->initialize($pagination_config); // prepares the pagination based on your configurations

  $this->data['pagination'] = $this->pagination->create_links(); // outputs the pagination as a string (*see Notes)
  $this->load->view('language', $data);
}

Model

public function get_languages($limit, $pagination_offset)
{
  $query = $this->db->query("
  SELECT
    languages.id AS id,
    languages.title AS language,
    country_code,
    COUNT(courses.id) AS number_of_courses
  FROM languages
  LEFT JOIN courses
  ON languages.id = courses.language_id
  GROUP BY languages.title
  LIMIT $limit OFFSET $pagination_offset
  ");
  return $query->result();
}

Notes

The three most important things here are

  • $pagination_config['base_url'] the controller method to call.
  • $pagination_config['per_page'] the SQL LIMIT or how many results per page you want to see.
  • $pagination_config['total_query_rows'] all the rows in the results set, so the pagination library can figure out how many links to create.

In $total_query_rows instead of writing the whole query without LIMIT and OFFSET, I wrote a much simpler one because I knew it would return the same number of rows every time.

If there is no pagination to show, create_links() will output an empty string.

In the model, you can of course use the query class library to pass LIMIT and OFFSET clauses.

Changing the view output

Above is the minimum you need to get pagination working, but it’s likely you will want to style or customise your output. To do this you can edit the default preferences which are passed to the initialize() function. I prefer to do this by creating a pagination.php file in the config folder. Here is an example using the W3.CSS front-end CSS framework

pagination.php

<?php
defined('BASEPATH') OR exit('No direct script access allowed');

$config['full_tag_open'] = '<ul class="w3-pagination w3-border w3-light-grey">';
$config['full_tag_close'] = '</ul>';

$config['first_link'] = 'First'; // might require more than 5 pages by default to show up
$config['first_tag_open'] = '<li>';
$config['first_tag_close'] = '</li>';
// $config['first_url'] = ''; // An alternative URL to use for the “first page” link.

$config['last_link'] = 'Last';
$config['last_tag_open'] = '<li>';
$config['last_tag_close'] = '</li>';

$config['next_link'] = '&raquo;';
$config['next_tag_open'] = '<li>';
$config['next_tag_close'] = '</li>';

$config['prev_link'] = '&laquo;';
$config['prev_tag_open'] = '<li>';
$config['prev_tag_close'] = '</li>';

$config['cur_tag_open'] = '<li><b><a class="w3-green">';
$config['cur_tag_close'] = '</a></b></li>';

$config['num_tag_open'] = '<li>';
$config['num_tag_close'] = '</li>';

View

<?php if (!empty($pagination)): ?>
  <div class="w3-container">
    <div class="w3-card-2 w3-padding w3-light-grey">
      <?php echo $pagination; ?>
    </div>
  </div>
<?php endif; ?>

22 December 2016

Creating backups of database rows

I came across a situation where I needed to make a backup of every row either newley insterted, or updated in my database. I didn’t want to share the latest backed up data on the same table, but then needed a way to reference the pk id of the original row when the data from that row was created for the first time.

MySQL has a handy function called LAST_INSERT_ID() which get’s the last inserted row id just executed. CodeIgniter also has a very easy way to get this value $this->db->insert_id();, which can be assigned to a variable and used with an insert statement, to create a reference to the backup of the new row in the backup table. You'll probably also want to do this under a table lock to prevent concurrent operations.

I don’t know whether this is the best way to do backups. This is just something I came up with. I should probably lookup how others do it at some point :P

15 December 2016

My essential web development tools, processes, libraries

This post will be continuously updated/revised.

Visualisation

Charts and graphs: https://gionkunz.github.io/chartist-js/

Charts and graphs: http://www.chartjs.org/

Sortable tables: http://tablesorter.com/docs/

Vertical slides: http://alvarotrigo.com/pagePiling/#page1

Responsive design

Emmet Re:View is a small Google Chrome extension that finds responsive design breakpoints on your page and creates view for each breakpoint.

HTML customisation

tablecloth.js is a jQuery plugin that helps you easily style HTML tables along with some simple customizations.

List.js adds search, sort, filters and flexibility to plain HTML lists, tables, or anything.

HTML5 Mobile

Ionic is a powerful HTML5 native app development framework that helps you build native-feeling mobile apps all with web technologies like HTML, CSS, and Javascript.

Dynamic pages

scrollMonitor allows you to receive events when elements enter or exit a viewport.

ScrollReveal - Easy scroll animations for web and mobile browsers.

Resources

Learn Layout teaches the CSS fundamentals that are used in any website’s layout.

Language libraries

The Voca library offers helpful functions to make string manipulations comfortable

Front-end libraries

W3.CSS is a modern CSS framework with built-in responsiveness. It's based on Google's material design, and is extremely light and easy to work with.

Resolving issues using Google's smtp email with Codeigniter

So this took me days to figure out, so hence a blog-post.

I’ve been looking for an easy way to test apps that use email locally. Using an online smtp service like gmail is in my opinion, much easier than setting up a local mail server. The natural choice for me was gmail since I have already created some test accounts for development there (It’s best not to mix personal and dev accounts for reasons which will be apparent later on). However, I did run into a couple of issues trying this with CodeIgniter.

Issue 1.

With your dev account open, go here and turn on access for less secure apps. Thanks to http://www.wpsitecare.com/gmail-smtp-settings/ for that.

Issue 2.

After loading CodeIgniter’s email library, the following line is essential to send email

$this->email->set_newline("\r\n"); // in the controller

or add the equivalent to the email config file (see example below)

$config['newline'] = "\r\n"; // essential to use double quotes here!!!

(Update: I’ve found more info on that issue here).

After checking off these two points, you can now proceed to send smtp email locally.

Here is a quick example

In application/config, create a file called email.php (it will be automatically recognised by CodeIgniter). Add the following

<?php if(!defined('BASEPATH')) exit('No direct script access allowed');

// SMTP config
$config['mailtype'] = 'html'; // default is text
$config['protocol'] = 'smtp';
$config['charset'] = 'utf-8';
//$config['newline'] = "\r\n"; // essential to use double quotes here!!!
$config['smtp_host'] = 'ssl://smtp.gmail.com';
$config['smtp_port'] = 465;
$config['smtp_user'] = 'your_dev_email@gmail.com';
$config['smtp_pass'] = 'Your_Password';

In your controller, add

$this->load->library('email');
$this->email->set_newline("\r\n"); // will fail without this line!!!

$this->email->from('your_dev_email@gmail.com', 'Your_Name');
$this->email->to('another_dev_email@gmail.com');
$this->email->subject('Email Test');
$this->email->message('Test_mail controller.');

if($this->email->send()){
  $data['message'] = "Mail sent...";
}
else {
    $data['message'] = "Sorry Unable to send email...";
}

$this->load->view('your_view', $data);

Then check the output in your_view by echoing the $message variable, and of course check your email ;)