From 39b622db9bda38282a32bb45623da63efe685729 Mon Sep 17 00:00:00 2001 From: Derek Allard Date: Wed, 16 Jan 2008 21:10:09 +0000 Subject: Many new Active Record functions, and another whack of stuff --- user_guide/libraries/benchmark.html | 2 +- user_guide/libraries/calendar.html | 2 +- user_guide/libraries/config.html | 2 +- user_guide/libraries/email.html | 300 ++++++++++++++++++- user_guide/libraries/encryption.html | 2 +- user_guide/libraries/file_uploading.html | 2 +- user_guide/libraries/ftp.html | 2 +- user_guide/libraries/image_lib.html | 2 +- user_guide/libraries/input.html | 2 +- user_guide/libraries/language.html | 2 +- user_guide/libraries/loader.html | 2 +- user_guide/libraries/output.html | 2 +- user_guide/libraries/pagination.html | 2 +- user_guide/libraries/parser.html | 2 +- user_guide/libraries/table.html | 293 ++++++++++++++++++- user_guide/libraries/trackback.html | 2 +- user_guide/libraries/unit_testing.html | 2 +- user_guide/libraries/uri.html | 253 +++++++++++++++- user_guide/libraries/user_agent.html | 202 ++++++++++++- user_guide/libraries/validation.html | 2 +- user_guide/libraries/xmlrpc.html | 488 ++++++++++++++++++++++++++++++- user_guide/libraries/zip.html | 2 +- 22 files changed, 1548 insertions(+), 22 deletions(-) (limited to 'user_guide/libraries') diff --git a/user_guide/libraries/benchmark.html b/user_guide/libraries/benchmark.html index f17554e0c..af83f8e57 100644 --- a/user_guide/libraries/benchmark.html +++ b/user_guide/libraries/benchmark.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/calendar.html b/user_guide/libraries/calendar.html index ae6c571ae..698df01cf 100644 --- a/user_guide/libraries/calendar.html +++ b/user_guide/libraries/calendar.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/config.html b/user_guide/libraries/config.html index 3eec0a028..67352adca 100644 --- a/user_guide/libraries/config.html +++ b/user_guide/libraries/config.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/email.html b/user_guide/libraries/email.html index 527c34a4e..c59374ca9 100644 --- a/user_guide/libraries/email.html +++ b/user_guide/libraries/email.html @@ -1 +1,299 @@ - CodeIgniter User Guide : Email Class

CodeIgniter User Guide Version 1.5.4


Email Class

CodeIgniter's robust Email Class supports the following features:

  • Multiple Protocols: Mail, Sendmail, and SMTP
  • Multiple recipients
  • CC and BCCs
  • HTML or Plaintext email
  • Attachments
  • Word wrapping
  • Priorities
  • BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
  • Email Debugging tools

Sending Email

Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.

Here is a basic example demonstrating how you might send email. Note: This example assumes you are sending the email from one of your controllers.

$this->load->library('email');

$this->email->from('your@your-site.com', 'Your Name');
$this->email->to('someone@example.com');
$this->email->cc('another@another-example.com');
$this->email->bcc('them@their-example.com');

$this->email->subject('Email Test');
$this->email->message('Testing the email class.');

$this->email->send();

echo $this->email->print_debugger();

Setting Email Preferences

There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually as described here, or automatically via preferences stored in your config file, described below:

Preferences are set by passing an array of preference values to the email initialize function. Here is an example of how you might set some preferences:

$config['protocol'] = 'sendmail';
$config['mailpath'] = '/usr/sbin/sendmail';
$config['charset'] = 'iso-8859-1';
$config['wordwrap'] = TRUE;

$this->email->initialize($config);

Note: Most of the preferences have default values that will be used if you do not set them.

Setting Email Preferences in a Config File

If you prefer not to set preferences using the above method, you can instead put them into a config file. Simply create a new file called the email.php, add the $config array in that file. Then save the file at config/email.php and it will be used automatically. You will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

Email Preferences

The following is a list of all the preferences that can be set when sending email.

Preference Default Value Options Description
useragentCodeIgniterNoneThe "user agent".
protocolmailmail, sendmail, or smtpThe mail sending protocol.
mailpath/usr/sbin/sendmailNoneThe server path to Sendmail.
smtp_hostNo DefaultNoneSMTP Server Address.
smtp_userNo DefaultNoneSMTP Username.
smtp_passNo DefaultNoneSMTP Password.
smtp_port25NoneSMTP Port.
smtp_timeout5NoneSMTP Timeout (in seconds).
wordwrapTRUETRUE or FALSE (boolean)Enable word-wrap.
wrapchars76 Character count to wrap at.
mailtypetexttext or htmlType of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
charsetutf-8Character set (utf-8, iso-8859-1, etc.).
validateFALSETRUE or FALSE (boolean)Whether to validate the email address.
priority31, 2, 3, 4, 5Email Priority. 1 = highest. 5 = lowest. 3 = normal.
newline\n"\r\n" or "\n"Newline character. (Use "\r\n" to comply with RFC 822).
bcc_batch_modeFALSETRUE or FALSE (boolean)Enable BCC Batch Mode.
bcc_batch_size200NoneNumber of emails in each BCC batch.

Email Function Reference

$this->email->from()

Sets the email address and name of the person sending the email:

$this->email->from('you@your-site.com', 'Your Name');

$this->email->reply_to()

Sets the reply-to address. If the information is not provided the information in the "from" function is used. Example:

$this->email->reply_to('you@your-site.com', 'Your Name');

$this->email->to()

Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:

$this->email->to('someone@example.com'); $this->email->to('one@example.com, two@example.com, three@example.com'); $list = array('one@example.com', 'two@example.com', 'three@example.com');

$this->email->to($list);

$this->email->cc()

Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->bcc()

Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

$this->email->subject()

Sets the email subject:

$this->email->subject('This is my subject');

$this->email->message()

Sets the email message body:

$this->email->message('This is my message');

$this->email->set_alt_message()

Sets the alternative email message body:

$this->email->set_alt_message('This is the alternative message');

This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative message with no HTML formatting which is added to the header string for people who do not accept HTML email. If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.

$this->email->clear()

Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function in a loop, permitting the data to be reset between cycles.

foreach ($list as $name => $address)
{
    $this->email->clear();

    $this->email->to($address);
    $this->email->from('your@your-site.com');
    $this->email->subject('Here is your info '.$name);
    $this->email->message('Hi '.$name.' Here is the info you requested.');
    $this->email->send();
}

If you set the parameter to TRUE any attachments will be cleared as well:

$this->email->clear(TRUE);

$this->email->send()

The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used conditionally:

if ( ! $this->email->send())
{
    // Generate error
}

$this->email->attach()

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. For multiple attachments use the function multiple times. For example:

$this->email->attach('/path/to/photo1.jpg');
$this->email->attach('/path/to/photo2.jpg');
$this->email->attach('/path/to/photo3.jpg');

$this->email->send();

$this->email->print_debugger()

Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.

Overriding Word Wrapping

If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can get wrapped too, causing it to become un-clickable by the person receiving it. CodeIgniter lets you manually override word wrapping within part of your message like this:

The text of your email that
gets wrapped normally.

{unwrap}http://www.example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}

More text that will be
wrapped normally.

Place the item you do not want word-wrapped between: {unwrap} {/unwrap}

\ No newline at end of file + + + + +CodeIgniter User Guide : Email Class + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.6.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

Email Class

+ +

CodeIgniter's robust Email Class supports the following features:

+ + +
    +
  • Multiple Protocols: Mail, Sendmail, and SMTP
  • +
  • Multiple recipients
  • +
  • CC and BCCs
  • +
  • HTML or Plaintext email
  • +
  • Attachments
  • +
  • Word wrapping
  • +
  • Priorities
  • +
  • BCC Batch Mode, enabling large email lists to be broken into small BCC batches.
  • +
  • Email Debugging tools
  • +
+ + +

Sending Email

+ +

Sending email is not only simple, but you can configure it on the fly or set your preferences in a config file.

+ +

Here is a basic example demonstrating how you might send email. Note: This example assumes you are sending the email from one of your +controllers.

+ +$this->load->library('email');
+
+$this->email->from('your@your-site.com', 'Your Name');
+$this->email->to('someone@example.com');
+$this->email->cc('another@another-example.com');
+$this->email->bcc('them@their-example.com');
+
+$this->email->subject('Email Test');
+$this->email->message('Testing the email class.');
+
+$this->email->send();
+
+echo $this->email->print_debugger();
+ + + + +

Setting Email Preferences

+ +

There are 17 different preferences available to tailor how your email messages are sent. You can either set them manually +as described here, or automatically via preferences stored in your config file, described below:

+ +

Preferences are set by passing an array of preference values to the email initialize function. Here is an example of how you might set some preferences:

+ +$config['protocol'] = 'sendmail';
+$config['mailpath'] = '/usr/sbin/sendmail';
+$config['charset'] = 'iso-8859-1';
+$config['wordwrap'] = TRUE;
+
+$this->email->initialize($config);
+ +

Note: Most of the preferences have default values that will be used if you do not set them.

Setting Email Preferences in a Config File

+ +

If you prefer not to set preferences using the above method, you can instead put them into a config file. +Simply create a new file called the email.php, add the $config +array in that file. Then save the file at config/email.php and it will be used automatically. You +will NOT need to use the $this->email->initialize() function if you save your preferences in a config file.

+ + + + +

Email Preferences

+ +

The following is a list of all the preferences that can be set when sending email.

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
PreferenceDefault ValueOptionsDescription
useragentCodeIgniterNoneThe "user agent".
protocolmailmail, sendmail, or smtpThe mail sending protocol.
mailpath/usr/sbin/sendmailNoneThe server path to Sendmail.
smtp_hostNo DefaultNoneSMTP Server Address.
smtp_userNo DefaultNoneSMTP Username.
smtp_passNo DefaultNoneSMTP Password.
smtp_port25NoneSMTP Port.
smtp_timeout5NoneSMTP Timeout (in seconds).
wordwrapTRUETRUE or FALSE (boolean)Enable word-wrap.
wrapchars76 Character count to wrap at.
mailtypetexttext or htmlType of mail. If you send HTML email you must send it as a complete web page. Make sure you don't have any relative links or relative image paths otherwise they will not work.
charsetutf-8Character set (utf-8, iso-8859-1, etc.).
validateFALSETRUE or FALSE (boolean)Whether to validate the email address.
priority31, 2, 3, 4, 5Email Priority. 1 = highest. 5 = lowest. 3 = normal.
newline\n"\r\n" or "\n"Newline character. (Use "\r\n" to comply with RFC 822).
bcc_batch_modeFALSETRUE or FALSE (boolean)Enable BCC Batch Mode.
bcc_batch_size200NoneNumber of emails in each BCC batch.
+ + +

Email Function Reference

+ +

$this->email->from()

+

Sets the email address and name of the person sending the email:

+$this->email->from('you@your-site.com', 'Your Name'); + +

$this->email->reply_to()

+

Sets the reply-to address. If the information is not provided the information in the "from" function is used. Example:

+$this->email->reply_to('you@your-site.com', 'Your Name'); + + +

$this->email->to()

+

Sets the email address(s) of the recipient(s). Can be a single email, a comma-delimited list or an array:

+ +$this->email->to('someone@example.com'); +$this->email->to('one@example.com, two@example.com, three@example.com'); + +$list = array('one@example.com', 'two@example.com', 'three@example.com');
+
+$this->email->to($list);
+ +

$this->email->cc()

+

Sets the CC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

+ +

$this->email->bcc()

+

Sets the BCC email address(s). Just like the "to", can be a single email, a comma-delimited list or an array.

+ + +

$this->email->subject()

+

Sets the email subject:

+$this->email->subject('This is my subject'); + +

$this->email->message()

+

Sets the email message body:

+$this->email->message('This is my message'); + +

$this->email->set_alt_message()

+

Sets the alternative email message body:

+$this->email->set_alt_message('This is the alternative message'); + +

This is an optional message string which can be used if you send HTML formatted email. It lets you specify an alternative +message with no HTML formatting which is added to the header string for people who do not accept HTML email. +If you do not set your own message CodeIgniter will extract the message from your HTML email and strip the tags.

+ + + +

$this->email->clear()

+

Initializes all the email variables to an empty state. This function is intended for use if you run the email sending function +in a loop, permitting the data to be reset between cycles.

+foreach ($list as $name => $address)
+{
+    $this->email->clear();

+ +    $this->email->to($address);
+    $this->email->from('your@your-site.com');
+    $this->email->subject('Here is your info '.$name);
+    $this->email->message('Hi '.$name.' Here is the info you requested.');
+    $this->email->send();
+}
+ +

If you set the parameter to TRUE any attachments will be cleared as well:

+ +$this->email->clear(TRUE); + + +

$this->email->send()

+

The Email sending function. Returns boolean TRUE or FALSE based on success or failure, enabling it to be used +conditionally:

+ +if ( ! $this->email->send())
+{
+    // Generate error
+}
+ + +

$this->email->attach()

+

Enables you to send an attachment. Put the file path/name in the first parameter. Note: Use a file path, not a URL. +For multiple attachments use the function multiple times. For example:

+ +$this->email->attach('/path/to/photo1.jpg');
+$this->email->attach('/path/to/photo2.jpg');
+$this->email->attach('/path/to/photo3.jpg');
+
+$this->email->send();
+ + +

$this->email->print_debugger()

+

Returns a string containing any server messages, the email headers, and the email messsage. Useful for debugging.

+ + +

Overriding Word Wrapping

+ +

If you have word wrapping enabled (recommended to comply with RFC 822) and you have a very long link in your email it can +get wrapped too, causing it to become un-clickable by the person receiving it. CodeIgniter lets you manually override +word wrapping within part of your message like this:

+ +The text of your email that
+gets wrapped normally.
+
+{unwrap}http://www.example.com/a_long_link_that_should_not_be_wrapped.html{/unwrap}
+
+More text that will be
+wrapped normally.
+ +

Place the item you do not want word-wrapped between: {unwrap} {/unwrap}

+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/libraries/encryption.html b/user_guide/libraries/encryption.html index ab6195cb1..f3b3d4406 100644 --- a/user_guide/libraries/encryption.html +++ b/user_guide/libraries/encryption.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/file_uploading.html b/user_guide/libraries/file_uploading.html index 86ee0cc85..46865acad 100644 --- a/user_guide/libraries/file_uploading.html +++ b/user_guide/libraries/file_uploading.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/ftp.html b/user_guide/libraries/ftp.html index b88b640ac..585811966 100644 --- a/user_guide/libraries/ftp.html +++ b/user_guide/libraries/ftp.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/image_lib.html b/user_guide/libraries/image_lib.html index 83dc8b3c0..39c1bd27f 100644 --- a/user_guide/libraries/image_lib.html +++ b/user_guide/libraries/image_lib.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/input.html b/user_guide/libraries/input.html index de623c07b..2bca88aed 100644 --- a/user_guide/libraries/input.html +++ b/user_guide/libraries/input.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/language.html b/user_guide/libraries/language.html index 7883e2a04..3ee62008d 100644 --- a/user_guide/libraries/language.html +++ b/user_guide/libraries/language.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/loader.html b/user_guide/libraries/loader.html index 7ff597c64..6aec87c13 100644 --- a/user_guide/libraries/loader.html +++ b/user_guide/libraries/loader.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/output.html b/user_guide/libraries/output.html index 27a458a87..2d131956e 100644 --- a/user_guide/libraries/output.html +++ b/user_guide/libraries/output.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/pagination.html b/user_guide/libraries/pagination.html index d18d364f2..e05a6e887 100644 --- a/user_guide/libraries/pagination.html +++ b/user_guide/libraries/pagination.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/parser.html b/user_guide/libraries/parser.html index a5271dea9..dab6937e2 100644 --- a/user_guide/libraries/parser.html +++ b/user_guide/libraries/parser.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/table.html b/user_guide/libraries/table.html index 0e627d120..93eb38ddb 100644 --- a/user_guide/libraries/table.html +++ b/user_guide/libraries/table.html @@ -1 +1,292 @@ - CodeIgniter User Guide : HTML Table Class

CodeIgniter User Guide Version 1.5.4


HTML Table Class

The Table Class provides functions that enable you to auto-generate HTML tables from arrays or database result sets.

Initializing the Class

Like most other classes in CodeIgniter, the Table class is initialized in your controller using the $this->load->library function:

$this->load->library('table');

Once loaded, the Table library object will be available using: $this->table

Examples

Here is an example showing how you can create a table from a multi-dimensional array. Note that the first array index will become the table heading (or you can set your own headings using the set_heading() function described in the function reference below).

$this->load->library('table');

$data = array(
             array('Name', 'Color', 'Size'),
             array('Fred', 'Blue', 'Small'),
             array('Mary', 'Red', 'Large'),
             array('John', 'Green', 'Medium')
             );

echo $this->table->generate($data);

Here is an example of a table created from a database query result. The table class will automatically generate the headings based on the table names (or you can set your own headings using the set_heading() function described in the function reference below).

$this->load->library('table');

$query = $this->db->query("SELECT * FROM my_table");

echo $this->table->generate($query);

Here is an example showing how you might create a table using discreet parameters:

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');

$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');

echo $this->table->generate();

Here is the same example, except instead of individual parameters, arrays are used:

$this->load->library('table');

$this->table->set_heading(array('Name', 'Color', 'Size'));

$this->table->add_row(array('Fred', 'Blue', 'Small'));
$this->table->add_row(array('Mary', 'Red', 'Large'));
$this->table->add_row(array('John', 'Green', 'Medium'));

echo $this->table->generate();

Changing the Look of Your Table

The Table Class permits you to set a table template with which you can specify the design of your layout. Here is the template prototype:

$tmpl = array (
                    'table_open'          => '<table border="0" cellpadding="4" cellspacing="0">',

                    'heading_row_start'   => '<tr>',
                    'heading_row_end'     => '</tr>',
                    'heading_cell_start'  => '<th>',
                    'heading_cell_end'    => '</th>',

                    'row_start'           => '<tr>',
                    'row_end'             => '</tr>',
                    'cell_start'          => '<td>',
                    'cell_end'            => '</td>',

                    'row_alt_start'       => '<tr>',
                    'row_alt_end'         => '</tr>',
                    'cell_alt_start'      => '<td>',
                    'cell_alt_end'        => '</td>',

                    'table_close'         => '</table>'
              );

$this->table->set_template($tmpl);

Note:  You'll notice there are two sets of "row" blocks in the template. These permit you to create alternating row colors or design elements that alternate with each iteration of the row data.

You are NOT required to submit a complete template. If you only need to change parts of the layout you can simply submit those elements. In this example, only the table opening tag is being changed:

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

$this->table->set_template($tmpl);

Function Reference

$this->table->generate()

Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.

$this->table->set_caption()

Permits you to add a caption to the table.

$this->table->set_caption('Colors');

$this->table->set_heading()

Permits you to set the table heading. You can submit an array or discreet params:

$this->table->set_heading('Name', 'Color', 'Size'); $this->table->set_heading(array('Name', 'Color', 'Size'));

$this->table->add_row()

Permits you to add a row to your table. You can submit an array or discreet params:

$this->table->add_row('Blue', 'Red', 'Green'); $this->table->add_row(array('Blue', 'Red', 'Green'));

$this->table->make_columns()

This function takes a one-dimensional array as input and creates a multi-dimensional array with a depth equal to the number of columns desired. This allows a single array with many elements to be displayed in a table that has a fixed column count. Consider this example:

$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');

$new_list = $this->table->make_columns($list, 3);

$this->table->generate($new_list);

// Generates a table with this prototype

<table border="0" cellpadding="4" cellspacing="0">
<tr>
<td>one</td><td>two</td><td>three</td>
</tr><tr>
<td>four</td><td>five</td><td>six</td>
</tr><tr>
<td>seven</td><td>eight</td><td>nine</td>
</tr><tr>
<td>ten</td><td>eleven</td><td>twelve</td></tr>
</table>

$this->table->set_template()

Permits you to set your template. You can submit a full or partial template.

$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );

$this->table->set_template($tmpl);

$this->table->set_empty()

Let's you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:

$this->table->set_empty("&nbsp;");

$this->table->clear()

Lets you clear the table heading and row data. If you need to show multiple tables with different data you should to call this function after each table has been generated to empty the previous table information. Example:

$this->load->library('table');

$this->table->set_heading('Name', 'Color', 'Size');
$this->table->add_row('Fred', 'Blue', 'Small');
$this->table->add_row('Mary', 'Red', 'Large');
$this->table->add_row('John', 'Green', 'Medium');

echo $this->table->generate();

$this->table->clear();

$this->table->set_heading('Name', 'Day', 'Delivery');
$this->table->add_row('Fred', 'Wednesday', 'Express');
$this->table->add_row('Mary', 'Monday', 'Air');
$this->table->add_row('John', 'Saturday', 'Overnight');

echo $this->table->generate();
\ No newline at end of file + + + + +CodeIgniter User Guide : HTML Table Class + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.6.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

HTML Table Class

+ +

The Table Class provides functions that enable you to auto-generate HTML tables from arrays or database result sets.

+ +

Initializing the Class

+ +

Like most other classes in CodeIgniter, the Table class is initialized in your controller using the $this->load->library function:

+ +$this->load->library('table'); +

Once loaded, the Table library object will be available using: $this->table

+ + +

Examples

+ +

Here is an example showing how you can create a table from a multi-dimensional array. +Note that the first array index will become the table heading (or you can set your own headings using the +set_heading() function described in the function reference below).

+ + +$this->load->library('table');
+
+$data = array(
+             array('Name', 'Color', 'Size'),
+             array('Fred', 'Blue', 'Small'),
+             array('Mary', 'Red', 'Large'),
+             array('John', 'Green', 'Medium')
+             );
+
+echo $this->table->generate($data); +
+ +

Here is an example of a table created from a database query result. The table class will automatically generate the +headings based on the table names (or you can set your own headings using the set_heading() function described +in the function reference below).

+ + +$this->load->library('table');
+
+$query = $this->db->query("SELECT * FROM my_table");
+
+echo $this->table->generate($query); +
+ + +

Here is an example showing how you might create a table using discreet parameters:

+ + +$this->load->library('table');
+
+$this->table->set_heading('Name', 'Color', 'Size');
+
+$this->table->add_row('Fred', 'Blue', 'Small');
+$this->table->add_row('Mary', 'Red', 'Large');
+$this->table->add_row('John', 'Green', 'Medium');
+
+echo $this->table->generate(); +
+ +

Here is the same example, except instead of individual parameters, arrays are used:

+ + +$this->load->library('table');
+
+$this->table->set_heading(array('Name', 'Color', 'Size'));
+
+$this->table->add_row(array('Fred', 'Blue', 'Small'));
+$this->table->add_row(array('Mary', 'Red', 'Large'));
+$this->table->add_row(array('John', 'Green', 'Medium'));
+
+echo $this->table->generate(); +
+ + +

Changing the Look of Your Table

+ +

The Table Class permits you to set a table template with which you can specify the design of your layout. Here is the template +prototype:

+ + +$tmpl = array (
+                    'table_open'          => '<table border="0" cellpadding="4" cellspacing="0">',
+
+                    'heading_row_start'   => '<tr>',
+                    'heading_row_end'     => '</tr>',
+                    'heading_cell_start'  => '<th>',
+                    'heading_cell_end'    => '</th>',
+
+                    'row_start'           => '<tr>',
+                    'row_end'             => '</tr>',
+                    'cell_start'          => '<td>',
+                    'cell_end'            => '</td>',
+
+                    'row_alt_start'       => '<tr>',
+                    'row_alt_end'         => '</tr>',
+                    'cell_alt_start'      => '<td>',
+                    'cell_alt_end'        => '</td>',
+
+                    'table_close'         => '</table>'
+              );
+ +
+$this->table->set_template($tmpl); +
+ +

Note:  You'll notice there are two sets of "row" blocks in the template. These permit you to create alternating row colors or design elements that alternate with each +iteration of the row data.

+ +

You are NOT required to submit a complete template. If you only need to change parts of the layout you can simply submit those elements. +In this example, only the table opening tag is being changed:

+ + +$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
+ +
+$this->table->set_template($tmpl); +
+ +
+

Function Reference

+ +

$this->table->generate()

+

Returns a string containing the generated table. Accepts an optional parameter which can be an array or a database result object.

+ +

$this->table->set_caption()

+ +

Permits you to add a caption to the table.

+ +$this->table->set_caption('Colors'); + +

$this->table->set_heading()

+ +

Permits you to set the table heading. You can submit an array or discreet params:

+ +$this->table->set_heading('Name', 'Color', 'Size'); +$this->table->set_heading(array('Name', 'Color', 'Size')); + +

$this->table->add_row()

+ +

Permits you to add a row to your table. You can submit an array or discreet params:

+ +$this->table->add_row('Blue', 'Red', 'Green'); +$this->table->add_row(array('Blue', 'Red', 'Green')); + + +

$this->table->make_columns()

+ +

This function takes a one-dimensional array as input and creates +a multi-dimensional array with a depth equal to the number of +columns desired. This allows a single array with many elements to be +displayed in a table that has a fixed column count. Consider this example:

+ + +$list = array('one', 'two', 'three', 'four', 'five', 'six', 'seven', 'eight', 'nine', 'ten', 'eleven', 'twelve');
+
+$new_list = $this->table->make_columns($list, 3);
+
+$this->table->generate($new_list);
+
+// Generates a table with this prototype
+
+<table border="0" cellpadding="4" cellspacing="0">
+<tr>
+<td>one</td><td>two</td><td>three</td>
+</tr><tr>
+<td>four</td><td>five</td><td>six</td>
+</tr><tr>
+<td>seven</td><td>eight</td><td>nine</td>
+</tr><tr>
+<td>ten</td><td>eleven</td><td>twelve</td></tr>
+</table>
+ + + +

$this->table->set_template()

+ +

Permits you to set your template. You can submit a full or partial template.

+ + +$tmpl = array ( 'table_open'  => '<table border="1" cellpadding="2" cellspacing="1" class="mytable">' );
+ +
+$this->table->set_template($tmpl); +
+ + +

$this->table->set_empty()

+ +

Let's you set a default value for use in any table cells that are empty. You might, for example, set a non-breaking space:

+ + +$this->table->set_empty("&nbsp;"); + + +

$this->table->clear()

+ +

Lets you clear the table heading and row data. If you need to show multiple tables with different data you should +to call this function after each table has been generated to empty the previous table information. Example:

+ + +$this->load->library('table');
+
+$this->table->set_heading('Name', 'Color', 'Size');
+$this->table->add_row('Fred', 'Blue', 'Small');
+$this->table->add_row('Mary', 'Red', 'Large');
+$this->table->add_row('John', 'Green', 'Medium');
+
+echo $this->table->generate();
+
+$this->table->clear();
+
+$this->table->set_heading('Name', 'Day', 'Delivery');
+$this->table->add_row('Fred', 'Wednesday', 'Express');
+$this->table->add_row('Mary', 'Monday', 'Air');
+$this->table->add_row('John', 'Saturday', 'Overnight');
+
+echo $this->table->generate(); +
+ +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/libraries/trackback.html b/user_guide/libraries/trackback.html index a971cb0c9..096e16ac2 100644 --- a/user_guide/libraries/trackback.html +++ b/user_guide/libraries/trackback.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/unit_testing.html b/user_guide/libraries/unit_testing.html index e2f1d114b..1f37649c3 100644 --- a/user_guide/libraries/unit_testing.html +++ b/user_guide/libraries/unit_testing.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/uri.html b/user_guide/libraries/uri.html index 4b71480bb..2306d82ad 100644 --- a/user_guide/libraries/uri.html +++ b/user_guide/libraries/uri.html @@ -1 +1,252 @@ - CodeIgniter User Guide : URI Class

CodeIgniter User Guide Version 1.5.4


URI Class

The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can also retrieve information about the re-routed segments.

Note: This class is initialized automatically by the system so there is no need to do it manually.

$this->uri->segment(n)

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. Segments are numbered from left to right. For example, if your full URL is this:

http://www.your-site.com/index.php/news/local/metro/crime_is_up

The segment numbers would be this:

  1. news
  2. local
  3. metro
  4. crime_is_up

By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that permits you to set your own default value if the segment is missing. For example, this would tell the function to return the number zero in the event of failure:

$product_id = $this->uri->segment(3, 0);

It helps avoid having to write code like this:

if ($this->uri->segment(3) === FALSE)
{
    $product_id = 0;
}
else
{
    $product_id = $this->uri->segment(3);
}

$this->uri->rsegment(n)

This function is identical to the previous one, except that it lets you retrieve a specific segment from your re-routed URI in the event you are using CodeIgniter's URI Routing feature.

$this->uri->slash_segment(n)

This function is almost identical to $this->uri->segment(), except it adds a trailing and/or leading slash based on the second parameter. If the parameter is not used, a trailing slash added. Examples:

$this->uri->slash_segment(3);
$this->uri->slash_segment(3, 'leading');
$this->uri->slash_segment(3, 'both');

Returns:

  1. segment/
  2. /segment
  3. /segment/

$this->uri->slash_rsegment(n)

This function is identical to the previous one, except that it lets you add slashes a specific segment from your re-routed URI in the event you are using CodeIgniter's URI Routing feature.

$this->uri->uri_to_assoc(n)

This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:

index.php/user/search/name/joe/location/UK/gender/male

Using this function you can turn the URI into an associative array with this prototype:

[array]
(
    'name' => 'joe'
    'location' => 'UK'
    'gender' => 'male'
)

The first parameter of the function lets you set an offset. By default it is set to 3 since your URI will normally contain a controller/function in the first and second segments. Example:

$array = $this->uri->uri_to_assoc(3);

echo $array['name'];

The second parameter lets you set default key names, so that the array returned by the function will always contain expected indexes, even if missing from the URI. Example:

$default = array('name', 'gender', 'location', 'type', 'sort');

$array = $this->uri->uri_to_assoc(3, $default);

If the URI does not contain a value in your default, an array index will be set to that name, with a value of FALSE.

Lastly, if a corresponding value is not found for a given key (if there is an odd number of URI segments) the value will be set to FALSE (boolean).

$this->uri->ruri_to_assoc(n)

This function is identical to the previous one, except that it creates an associative array using the re-routed URI in the event you are using CodeIgniter's URI Routing feature.

$this->uri->assoc_to_uri()

Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:

$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');

$str = $this->uri->assoc_to_uri($array);

// Produces: product/shoes/size/large/color/red

$this->uri->uri_string()

Returns a string with the complete URI. For example, if this is your full URL:

http://www.your-site.com/index.php/news/local/345

The function would return this:

/news/local/345

$this->uri->ruri_string(n)

This function is identical to the previous one, except that it returns the re-routed URI in the event you are using CodeIgniter's URI Routing feature.

$this->uri->total_segments()

Returns the total number of segments.

$this->uri->total_rsegments(n)

This function is identical to the previous one, except that it returns the total number of segments in your re-routed URI in the event you are using CodeIgniter's URI Routing feature.

$this->uri->segment_array()

Returns an array containing the URI segments. For example:

$segs = $this->uri->segment_array();

foreach ($segs as $segment)
{
    echo $segment;
    echo '<br />';
}

$this->uri->rsegment_array(n)

This function is identical to the previous one, except that it returns the array of segments in your re-routed URI in the event you are using CodeIgniter's URI Routing feature.

\ No newline at end of file + + + + +CodeIgniter User Guide : URI Class + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.6.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

URI Class

+ +

The URI Class provides functions that help you retrieve information from your URI strings. If you use URI routing, you can +also retrieve information about the re-routed segments.

+ +

Note: This class is initialized automatically by the system so there is no need to do it manually.

+ +

$this->uri->segment(n)

+ +

Permits you to retrieve a specific segment. Where n is the segment number you wish to retrieve. +Segments are numbered from left to right. For example, if your full URL is this:

+ +http://www.your-site.com/index.php/news/local/metro/crime_is_up + +

The segment numbers would be this:

+ +
    +
  1. news
  2. +
  3. local
  4. +
  5. metro
  6. +
  7. crime_is_up
  8. +
+ +

By default the function returns FALSE (boolean) if the segment does not exist. There is an optional second parameter that +permits you to set your own default value if the segment is missing. +For example, this would tell the function to return the number zero in the event of failure:

+ +$product_id = $this->uri->segment(3, 0); + +

It helps avoid having to write code like this:

+ +if ($this->uri->segment(3) === FALSE)
+{
+    $product_id = 0;
+}
+else
+{
+    $product_id = $this->uri->segment(3);
+}
+
+ +

$this->uri->rsegment(n)

+ +

This function is identical to the previous one, except that it lets you retrieve a specific segment from your +re-routed URI in the event you are using CodeIgniter's URI Routing feature.

+ + +

$this->uri->slash_segment(n)

+ +

This function is almost identical to $this->uri->segment(), except it adds a trailing and/or leading slash based on the second +parameter. If the parameter is not used, a trailing slash added. Examples:

+ +$this->uri->slash_segment(3);
+$this->uri->slash_segment(3, 'leading');
+$this->uri->slash_segment(3, 'both');
+ +

Returns:

+ +
    +
  1. segment/
  2. +
  3. /segment
  4. +
  5. /segment/
  6. +
+ + +

$this->uri->slash_rsegment(n)

+ +

This function is identical to the previous one, except that it lets you add slashes a specific segment from your +re-routed URI in the event you are using CodeIgniter's URI Routing feature.

+ + + +

$this->uri->uri_to_assoc(n)

+ +

This function lets you turn URI segments into and associative array of key/value pairs. Consider this URI:

+ +index.php/user/search/name/joe/location/UK/gender/male + +

Using this function you can turn the URI into an associative array with this prototype:

+ +[array]
+(
+    'name' => 'joe'
+    'location' => 'UK'
+    'gender' => 'male'
+)
+ +

The first parameter of the function lets you set an offset. By default it is set to 3 since your +URI will normally contain a controller/function in the first and second segments. Example:

+ + +$array = $this->uri->uri_to_assoc(3);
+
+echo $array['name']; +
+ + +

The second parameter lets you set default key names, so that the array returned by the function will always contain expected indexes, even if missing from the URI. Example:

+ + +$default = array('name', 'gender', 'location', 'type', 'sort');
+
+$array = $this->uri->uri_to_assoc(3, $default);
+ +

If the URI does not contain a value in your default, an array index will be set to that name, with a value of FALSE.

+ +

Lastly, if a corresponding value is not found for a given key (if there is an odd number of URI segments) the value will be set to FALSE (boolean).

+ + +

$this->uri->ruri_to_assoc(n)

+ +

This function is identical to the previous one, except that it creates an associative array using the +re-routed URI in the event you are using CodeIgniter's URI Routing feature.

+ + +

$this->uri->assoc_to_uri()

+ +

Takes an associative array as input and generates a URI string from it. The array keys will be included in the string. Example:

+ +$array = array('product' => 'shoes', 'size' => 'large', 'color' => 'red');
+
+$str = $this->uri->assoc_to_uri($array);
+
+// Produces: product/shoes/size/large/color/red +
+ + +

$this->uri->uri_string()

+ +

Returns a string with the complete URI. For example, if this is your full URL:

+ +http://www.your-site.com/index.php/news/local/345 + +

The function would return this:

+ +/news/local/345 + + +

$this->uri->ruri_string(n)

+ +

This function is identical to the previous one, except that it returns the +re-routed URI in the event you are using CodeIgniter's URI Routing feature.

+ + + +

$this->uri->total_segments()

+ +

Returns the total number of segments.

+ + +

$this->uri->total_rsegments(n)

+ +

This function is identical to the previous one, except that it returns the total number of segments in your +re-routed URI in the event you are using CodeIgniter's URI Routing feature.

+ + + +

$this->uri->segment_array()

+ +

Returns an array containing the URI segments. For example:

+ + +$segs = $this->uri->segment_array();
+
+foreach ($segs as $segment)
+{
+    echo $segment;
+    echo '<br />';
+}
+ +

$this->uri->rsegment_array(n)

+ +

This function is identical to the previous one, except that it returns the array of segments in your +re-routed URI in the event you are using CodeIgniter's URI Routing feature. + + + +

+ + + + + + + \ No newline at end of file diff --git a/user_guide/libraries/user_agent.html b/user_guide/libraries/user_agent.html index 7dc747ef6..ab37935ec 100644 --- a/user_guide/libraries/user_agent.html +++ b/user_guide/libraries/user_agent.html @@ -1 +1,201 @@ - CodeIgniter User Guide : User Agent Class

CodeIgniter User Guide Version 1.5.4


User Agent Class

The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site. In addition you can get referrer information as well as language and supported character-set information.

Initializing the Class

Like most other classes in CodeIgniter, the User Agent class is initialized in your controller using the $this->load->library function:

$this->load->library('user_agent');

Once loaded, the object will be available using: $this->agent

User Agent Definitions

The user agent name definitions are located in a config file located at: application/config/user_agents.php. You may add items to the various user agent arrays if needed.

Example

When the User Agent class is initialized it will attempt to determine whether the user agent browsing your site is a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.

$this->load->library('user_agent');

if ($this->agent->is_browser())
{
    $agent = $this->agent->browser().' '.$this->agent->version();
}
elseif ($this->agent->is_robot())
{
    $agent = $this->agent->robot();
}
elseif ($this->agent->is_mobile())
{
    $agent = $this->agent->mobile();
}
else
{
    $agent = 'Unidentified User Agent';
}

echo $agent;

echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.)

Function Reference

$this->agent->is_browser()

Returns TRUE/FALSE (boolean) if the user agent is a known web browser.

$this->agent->is_mobile()

Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.

$this->agent->is_robot()

Returns TRUE/FALSE (boolean) if the user agent is a known robot.

Note:  The user agent library only contains the most common robot definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your application/config/user_agents.php file.

$this->agent->is_referral()

Returns TRUE/FALSE (boolean) if the user agent was referred from another site.

$this->agent->browser()

Returns a string containing the name of the web browser viewing your site.

$this->agent->version()

Returns a string containing the version number of the web browser viewing your site.

$this->agent->mobile()

Returns a string containing the name of the mobile device viewing your site.

$this->agent->robot()

Returns a string containing the name of the robot viewing your site.

$this->agent->platform()

Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).

$this->agent->referrer()

The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:

if ($this->agent->is_referral())
{
    echo $this->agent->referrer();
}

$this->agent->agent_string()

Returns a string containing the full user agent string. Typically it will be something like this:

Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2

$this->agent->accept_lang()

Lets you determine if the user agent accepts a particular language. Example:

if ($this->agent->accept_lang('en'))
{
    echo 'You accept English!';
}

Note: This function is not typically very reliable since some browsers do not provide language info, and even among those that do, it is not always accurate.

$this->agent->accept_charset()

Lets you determine if the user agent accepts a particular character set. Example:

if ($this->agent->accept_charset('utf-8'))
{
    echo 'You browser supports UTF-8!';
}

Note: This function is not typically very reliable since some browsers do not provide character-set info, and even among those that do, it is not always accurate.

\ No newline at end of file + + + + +CodeIgniter User Guide : User Agent Class + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.6.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

User Agent Class

+ +

The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site. +In addition you can get referrer information as well as language and supported character-set information.

+ +

Initializing the Class

+ +

Like most other classes in CodeIgniter, the User Agent class is initialized in your controller using the $this->load->library function:

+ +$this->load->library('user_agent'); +

Once loaded, the object will be available using: $this->agent

+ +

User Agent Definitions

+ +

The user agent name definitions are located in a config file located at: application/config/user_agents.php. You may add items to the +various user agent arrays if needed.

+ +

Example

+ +

When the User Agent class is initialized it will attempt to determine whether the user agent browsing your site is +a web browser, a mobile device, or a robot. It will also gather the platform information if it is available.

+ + + +$this->load->library('user_agent');
+
+if ($this->agent->is_browser())
+{
+    $agent = $this->agent->browser().' '.$this->agent->version();
+}
+elseif ($this->agent->is_robot())
+{
+    $agent = $this->agent->robot();
+}
+elseif ($this->agent->is_mobile())
+{
+    $agent = $this->agent->mobile();
+}
+else
+{
+    $agent = 'Unidentified User Agent';
+}
+
+echo $agent;
+
+echo $this->agent->platform(); // Platform info (Windows, Linux, Mac, etc.) +
+ + +

Function Reference

+ + +

$this->agent->is_browser()

+

Returns TRUE/FALSE (boolean) if the user agent is a known web browser.

+ +

$this->agent->is_mobile()

+

Returns TRUE/FALSE (boolean) if the user agent is a known mobile device.

+ +

$this->agent->is_robot()

+

Returns TRUE/FALSE (boolean) if the user agent is a known robot.

+ +

Note:  The user agent library only contains the most common robot +definitions. It is not a complete list of bots. There are hundreds of them so searching for each one would not be +very efficient. If you find that some bots that commonly visit your site are missing from the list you can add them to your +application/config/user_agents.php file.

+ +

$this->agent->is_referral()

+

Returns TRUE/FALSE (boolean) if the user agent was referred from another site.

+ + +

$this->agent->browser()

+

Returns a string containing the name of the web browser viewing your site.

+ +

$this->agent->version()

+

Returns a string containing the version number of the web browser viewing your site.

+ +

$this->agent->mobile()

+

Returns a string containing the name of the mobile device viewing your site.

+ +

$this->agent->robot()

+

Returns a string containing the name of the robot viewing your site.

+ +

$this->agent->platform()

+

Returns a string containing the platform viewing your site (Linux, Windows, OS X, etc.).

+ +

$this->agent->referrer()

+

The referrer, if the user agent was referred from another site. Typically you'll test for this as follows:

+ + if ($this->agent->is_referral())
+{
+    echo $this->agent->referrer();
+}
+ + +

$this->agent->agent_string()

+

Returns a string containing the full user agent string. Typically it will be something like this:

+ +Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en-US; rv:1.8.0.4) Gecko/20060613 Camino/1.0.2 + + +

$this->agent->accept_lang()

+

Lets you determine if the user agent accepts a particular language. Example:

+ +if ($this->agent->accept_lang('en'))
+{
+    echo 'You accept English!';
+}
+ +

Note: This function is not typically very reliable +since some browsers do not provide language info, and even among those that do, it is not always accurate.

+ + + +

$this->agent->accept_charset()

+

Lets you determine if the user agent accepts a particular character set. Example:

+ +if ($this->agent->accept_charset('utf-8'))
+{
+    echo 'You browser supports UTF-8!';
+}
+ +

Note: This function is not typically very reliable +since some browsers do not provide character-set info, and even among those that do, it is not always accurate.

+ + + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/libraries/validation.html b/user_guide/libraries/validation.html index 90ff0c127..30de0fd2b 100644 --- a/user_guide/libraries/validation.html +++ b/user_guide/libraries/validation.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

diff --git a/user_guide/libraries/xmlrpc.html b/user_guide/libraries/xmlrpc.html index cd2f9e306..039127913 100644 --- a/user_guide/libraries/xmlrpc.html +++ b/user_guide/libraries/xmlrpc.html @@ -1 +1,487 @@ - CodeIgniter User Guide : XML-RPC and XML-RPC Server Classes

CodeIgniter User Guide Version 1.5.4


XML-RPC and XML-RPC Server Classes

CodeIgniter's XML-RPC classes permit you to send requests to another server, or set up your own XML-RPC server to receive requests.

What is XML-RPC?

Quite simply it is a way for two computers to communicate over the internet using XML. One computer, which we will call the client, sends an XML-RPC request to another computer, which we will call the server. Once the server receives and processes the request it will send back a response to the client.

For example, using the MetaWeblog API, an XML-RPC Client (usually a desktop publishing tool) will send a request to an XML-RPC Server running on your site. This request might be a new weblog entry being sent for publication, or it could be a request for an existing entry for editing. When the XML-RPC Server receives this request it will examine it to determine which class/method should be called to process the request. Once processed, the server will then send back a response message.

For detailed specifications, you can visit the XML-RPC site.

Initializing the Class

Like most other classes in CodeIgniter, the XML-RPC and XML-RPCS classes are initialized in your controller using the $this->load->library function:

To load the XML-RPC class you will use:

$this->load->library('xmlrpc');

Once loaded, the xml-rpc library object will be available using: $this->xmlrpc

To load the XML-RPC Server class you will use:

$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');

Once loaded, the xml-rpcs library object will be available using: $this->xmlrpcs

Note:  When using the XML-RPC Server class you must load BOTH the XML-RPC class and the XML-RPC Server class.

Sending XML-RPC Requests

To send a request to an XML-RPC server you must specify the following information:

  • The URL of the server
  • The method on the server you wish to call
  • The request data (explained below).

Here is a basic example that sends a simple Weblogs.com ping to the Ping-o-Matic

$this->load->library('xmlrpc');

$this->xmlrpc->server('http://rpc.pingomatic.com/', 80);
$this->xmlrpc->method('weblogUpdates.ping');

$request = array('My Photoblog', 'http://www.my-site.com/photoblog/');
$this->xmlrpc->request($request);

if ( ! $this->xmlrpc->send_request())
{
    echo $this->xmlrpc->display_error();
}

Explanation

The above code initializes the XML-RPC class, sets the server URL and method to be called (weblogUpdates.ping). The request (in this case, the title and URL of your site) is placed into an array for transportation, and compiled using the request() function. Lastly, the full request is sent. If the send_request() method returns false we will display the error message sent back from the XML-RPC Server.

Anatomy of a Request

An XML-RPC request is simply the data you are sending to the XML-RPC server. Each piece of data in a request is referred to as a request parameter. The above example has two parameters: The URL and title of your site. When the XML-RPC server receives your request, it will look for parameters it requires.

Request parameters must be placed into an array for transportation, and each parameter can be one of seven data types (strings, numbers, dates, etc.). If your parameters are something other than strings you will have to include the data type in the request array.

Here is an example of a simple array with three parameters:

$request = array('John', 'Doe', 'www.some-site.com');
$this->xmlrpc->request($request);

If you use data types other than strings, or if you have several different data types, you will place each parameter into its own array, with the data type in the second position:

$request = array (
                   array('John', 'string'),
                   array('Doe', 'string'),
                   array(FALSE, 'boolean'),
                   array(12345, 'int')
                 );
$this->xmlrpc->request($request);
The Data Types section below has a full list of data types.

Creating an XML-RPC Server

An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming requests and redirecting them to the appropriate functions for processing.

To create your own XML-RPC server involves initializing the XML-RPC Server class in your controller where you expect the incoming request to appear, then setting up an array with mapping instructions so that incoming requests can be sent to the appropriate class and method for processing.

Here is an example to illustrate:

$this->load->library('xmlrpc');
$this->load->library('xmlrpcs');

$config['functions']['new_post']  = array('function' => 'My_blog.new_entry');
$config['functions']['update_post'] = array('function' => 'My_blog.update_entry');

$this->xmlrpcs->initialize($config);
$this->xmlrpcs->serve();

The above example contains an array specifying two method requests that the Server allows. The allowed methods are on the left side of the array. When either of those are received, they will be mapped to the class and method on the right.

In other words, if an XML-RPC Client sends a request for the new_post method, your server will load the My_blog class and call the new_entry function. If the request is for the update_post method, your server will load the My_blog class and call the update_entry function.

The function names in the above example are arbitrary. You'll decide what they should be called on your server, or if you are using standardized APIs, like the Blogger or MetaWeblog API, you'll use their function names.

Processing Server Requests

When the XML-RPC Server receives a request and loads the class/method for processing, it will pass an object to that method containing the data sent by the client.

Using the above example, if the new_post method is requested, the server will expect a class to exist with this prototype:

class My_blog extends Controller {

    function new_post($request)
    {

    }
}

The $request variable is an object compiled by the Server, which contains the data sent by the XML-RPC Client. Using this object you will have access to the request parameters enabling you to process the request. When you are done you will send a Response back to the Client.

Below is a real-world example, using the Blogger API. One of the methods in the Blogger API is getUserInfo(). Using this method, an XML-RPC Client can send the Server a username and password, in return the Server sends back information about that particular user (nickname, user ID, email address, etc.). Here is how the processing function might look:

class My_blog extends Controller {

    function getUserInfo($request)
    {
        $username = 'smitty';
        $password = 'secretsmittypass';

        $this->load->library('xmlrpc');
    
        $parameters = $request->output_parameters();
    
        if ($parameters['1'] != $username AND $parameters['2'] != $password)
        {
            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
        }
    
        $response = array(array('nickname'  => array('Smitty','string'),
                                'userid'    => array('99','string'),
                                'url'       => array('http://yoursite.com','string'),
                                'email'     => array('jsmith@yoursite.com','string'),
                                'lastname'  => array('Smith','string'),
                                'firstname' => array('John','string')
                                ),
                         'struct');

        return $this->xmlrpc->send_response($response);
    }
}

Notes:

The output_parameters() function retrieves an indexed array corresponding to the request parameters sent by the client. In the above example, the output parameters will be the username and password.

If the username and password sent by the client were not valid, and error message is returned using send_error_message().

If the operation was successful, the client will be sent back a response array containing the user's info.

Formatting a Response

Similar to Requests, Responses must be formatted as an array. However, unlike requests, a response is an array that contains a single item. This item can be an array with several additional arrays, but there can be only one primary array index. In other words, the basic prototype is this:

$response = array('Response data', 'array');

Responses, however, usually contain multiple pieces of information. In order to accomplish this we must put the response into its own array so that the primary array continues to contain a single piece of data. Here's an example showing how this might be accomplished:

$response = array (
                   array(
                         'first_name' => array('John', 'string'),
                         'last_name' => array('Doe', 'string'),
                         'member_id' => array(123435, 'int'),
                         'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'),
                        ),
                 'struct'
                 );

Notice that the above array is formatted as a struct. This is the most common data type for responses.

As with Requests, a response can be on of the seven data types listed in the Data Types section.

Sending an Error Response

If you need to send the client an error response you will use the following:

return $this->xmlrpc->send_error_message('123', 'Requested data not available');

The first parameter is the error number while the second parameter is the error message.

Creating Your Own Client and Server

To help you understand everything we've covered thus far, let's create a couple controllers that act as XML-RPC Client and Server. You'll use the Client to send a request to the Server and receive a response.

The Client

Using a text editor, create a controller called xmlrpc_client.php. In it, place this code and save it to your applications/controllers/ folder:

Note: In the above code we are using a "url helper". You can find more information in the Helpers Functions page.

The Server

Using a text editor, create a controller called xmlrpc_server.php. In it, place this code and save it to your applications/controllers/ folder:

Try it!

Now visit the your site using a URL similar to this:

www.your-site.com/index.php/xmlrpc_client/

You should now see the message you sent to the server, and its response back to you.

The client you created sends a message ("How's is going?") to the server, along with a reqest for the "Greetings" method. The Server receives the request and maps it to the "process" function, where a response is sent back.

 

XML-RPC Function Reference

$this->xmlrpc->server()

Sets the URL and port number of the server to which a request is to be sent:

$this->xmlrpc->server('http://www.sometimes.com/pings.php', 80);

$this->xmlrpc->timeout()

Set a time out period (in seconds) after which the request will be canceled:

$this->xmlrpc->timeout(6);

$this->xmlrpc->method()

Sets the method that will be requested from the XML-RPC server:

$this->xmlrpc->method('method');

Where method is the name of the method.

$this->xmlrpc->request()

Takes an array of data and builds request to be sent to XML-RPC server:

$request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/');
$this->xmlrpc->request($request);

$this->xmlrpc->send_request()

The request sending function. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally.

$this->xmlrpc->set_debug(TRUE);

Enables debugging, which will display a variety of information and error data helpful during development.

$this->xmlrpc->display_error()

Returns an error message as a string if your request failed for some reason.

echo $this->xmlrpc->display_error();

$this->xmlrpc->display_response()

Returns the response from the remote server once request is received. The response will typically be an associative array.

$this->xmlrpc->display_response();

$this->xmlrpc->send_error_message()

This function lets you send an error message from your server to the client. First parameter is the error number while the second parameter is the error message.

return $this->xmlrpc->send_error_message('123', 'Requested data not available');

$this->xmlrpc->send_response()

Lets you send the response from your server to the client. An array of valid data values must be sent with this method.

$response = array(
                 array(
                        'flerror' => array(FALSE, 'boolean'),
                        'message' => "Thanks for the ping!")
                     )
                 'struct');
return $this->xmlrpc->send_response($response);

Data Types

According to the XML-RPC spec there are seven types of values that you can send via XML-RPC:

  • int or i4
  • boolean
  • string
  • double
  • dateTime.iso8601
  • base64
  • struct (contains array of values)
  • array (contains array of values)
\ No newline at end of file + + + + +CodeIgniter User Guide : XML-RPC and XML-RPC Server Classes + + + + + + + + + + + + + + + + + + + + + + +
+ + + + + +

CodeIgniter User Guide Version 1.6.0

+
+ + + + + + + + + +
+ + +
+ + + +
+ + +

XML-RPC and XML-RPC Server Classes

+ + +

CodeIgniter's XML-RPC classes permit you to send requests to another server, or set up +your own XML-RPC server to receive requests.

+ + +

What is XML-RPC?

+ +

Quite simply it is a way for two computers to communicate over the internet using XML. +One computer, which we will call the client, sends an XML-RPC request to +another computer, which we will call the server. Once the server receives and processes the request it +will send back a response to the client.

+ +

For example, using the MetaWeblog API, an XML-RPC Client (usually a desktop publishing tool) will +send a request to an XML-RPC Server running on your site. This request might be a new weblog entry +being sent for publication, or it could be a request for an existing entry for editing. + +When the XML-RPC Server receives this request it will examine it to determine which class/method should be called to process the request. +Once processed, the server will then send back a response message.

+ +

For detailed specifications, you can visit the XML-RPC site.

+ +

Initializing the Class

+ +

Like most other classes in CodeIgniter, the XML-RPC and XML-RPCS classes are initialized in your controller using the $this->load->library function:

+ +

To load the XML-RPC class you will use:

+$this->load->library('xmlrpc'); +

Once loaded, the xml-rpc library object will be available using: $this->xmlrpc

+ +

To load the XML-RPC Server class you will use:

+ +$this->load->library('xmlrpc');
+$this->load->library('xmlrpcs'); +
+

Once loaded, the xml-rpcs library object will be available using: $this->xmlrpcs

+ +

Note:  When using the XML-RPC Server class you must load BOTH the XML-RPC class and the XML-RPC Server class.

+ + + +

Sending XML-RPC Requests

+ +

To send a request to an XML-RPC server you must specify the following information:

+ +
    +
  • The URL of the server
  • +
  • The method on the server you wish to call
  • +
  • The request data (explained below).
  • +
+ +

Here is a basic example that sends a simple Weblogs.com ping to the Ping-o-Matic

+ + +$this->load->library('xmlrpc');
+
+$this->xmlrpc->server('http://rpc.pingomatic.com/', 80);
+$this->xmlrpc->method('weblogUpdates.ping');
+ +
+$request = array('My Photoblog', 'http://www.my-site.com/photoblog/');
+$this->xmlrpc->request($request);
+
+if ( ! $this->xmlrpc->send_request())
+{
+    echo $this->xmlrpc->display_error();
+}
+ +

Explanation

+ +

The above code initializes the XML-RPC class, sets the server URL and method to be called (weblogUpdates.ping). The +request (in this case, the title and URL of your site) is placed into an array for transportation, and +compiled using the request() function. +Lastly, the full request is sent. If the send_request() method returns false we will display the error message +sent back from the XML-RPC Server.

+ +

Anatomy of a Request

+ +

An XML-RPC request is simply the data you are sending to the XML-RPC server. Each piece of data in a request +is referred to as a request parameter. The above example has two parameters: +The URL and title of your site. When the XML-RPC server receives your request, it will look for parameters it requires.

+ +

Request parameters must be placed into an array for transportation, and each parameter can be one +of seven data types (strings, numbers, dates, etc.). If your parameters are something other than strings +you will have to include the data type in the request array.

+ +

Here is an example of a simple array with three parameters:

+ +$request = array('John', 'Doe', 'www.some-site.com');
+$this->xmlrpc->request($request);
+ +

If you use data types other than strings, or if you have several different data types, you will place +each parameter into its own array, with the data type in the second position:

+ + +$request = array (
+                   array('John', 'string'),
+                   array('Doe', 'string'),
+                   array(FALSE, 'boolean'),
+                   array(12345, 'int')
+                 ); +
+$this->xmlrpc->request($request);
+ +The Data Types section below has a full list of data types. + + + +

Creating an XML-RPC Server

+ +

An XML-RPC Server acts as a traffic cop of sorts, waiting for incoming requests and redirecting them to the +appropriate functions for processing.

+ +

To create your own XML-RPC server involves initializing the XML-RPC Server class in your controller where you expect the incoming +request to appear, then setting up an array with mapping instructions so that incoming requests can be sent to the appropriate +class and method for processing.

+ +

Here is an example to illustrate:

+ + +$this->load->library('xmlrpc');
+$this->load->library('xmlrpcs');
+
+$config['functions']['new_post']  = array('function' => 'My_blog.new_entry');
+$config['functions']['update_post'] = array('function' => 'My_blog.update_entry');
+
+$this->xmlrpcs->initialize($config);
+$this->xmlrpcs->serve();
+ +

The above example contains an array specifying two method requests that the Server allows. +The allowed methods are on the left side of the array. When either of those are received, they will be mapped to the class and method on the right.

+ +

In other words, if an XML-RPC Client sends a request for the new_post method, your +server will load the My_blog class and call the new_entry function. +If the request is for the update_post method, your +server will load the My_blog class and call the update_entry function.

+ +

The function names in the above example are arbitrary. You'll decide what they should be called on your server, +or if you are using standardized APIs, like the Blogger or MetaWeblog API, you'll use their function names.

+ + +

Processing Server Requests

+ +

When the XML-RPC Server receives a request and loads the class/method for processing, it will pass +an object to that method containing the data sent by the client.

+ +

Using the above example, if the new_post method is requested, the server will expect a class +to exist with this prototype:

+ +class My_blog extends Controller {
+
+    function new_post($request)
+    {
+
+    }
+} +
+ +

The $request variable is an object compiled by the Server, which contains the data sent by the XML-RPC Client. +Using this object you will have access to the request parameters enabling you to process the request. When +you are done you will send a Response back to the Client.

+ +

Below is a real-world example, using the Blogger API. One of the methods in the Blogger API is getUserInfo(). +Using this method, an XML-RPC Client can send the Server a username and password, in return the Server sends +back information about that particular user (nickname, user ID, email address, etc.). Here is how the processing +function might look:

+ + +class My_blog extends Controller {
+
+    function getUserInfo($request)
+    {
+ +        $username = 'smitty';
+        $password = 'secretsmittypass';

+ +        $this->load->library('xmlrpc');
+    
+        $parameters = $request->output_parameters();
+    
+        if ($parameters['1'] != $username AND $parameters['2'] != $password)
+        {
+            return $this->xmlrpc->send_error_message('100', 'Invalid Access');
+        }
+    
+        $response = array(array('nickname'  => array('Smitty','string'),
+                                'userid'    => array('99','string'),
+                                'url'       => array('http://yoursite.com','string'),
+                                'email'     => array('jsmith@yoursite.com','string'),
+                                'lastname'  => array('Smith','string'),
+                                'firstname' => array('John','string')
+                                ),
+                         'struct');
+
+        return $this->xmlrpc->send_response($response);
+    }
+} +
+ +

Notes:

+

The output_parameters() function retrieves an indexed array corresponding to the request parameters sent by the client. +In the above example, the output parameters will be the username and password.

+ +

If the username and password sent by the client were not valid, and error message is returned using send_error_message().

+ +

If the operation was successful, the client will be sent back a response array containing the user's info.

+ + +

Formatting a Response

+ +

Similar to Requests, Responses must be formatted as an array. However, unlike requests, a response is an array +that contains a single item. This item can be an array with several additional arrays, but there +can be only one primary array index. In other words, the basic prototype is this:

+ +$response = array('Response data', 'array'); + +

Responses, however, usually contain multiple pieces of information. In order to accomplish this we must put the response into its own +array so that the primary array continues to contain a single piece of data. Here's an example showing how this might be accomplished:

+ + +$response = array (
+                   array(
+                         'first_name' => array('John', 'string'),
+                         'last_name' => array('Doe', 'string'),
+                         'member_id' => array(123435, 'int'),
+                         'todo_list' => array(array('clean house', 'call mom', 'water plants'), 'array'),
+                        ),
+                 'struct'
+                 ); +
+ +

Notice that the above array is formatted as a struct. This is the most common data type for responses.

+ +

As with Requests, a response can be on of the seven data types listed in the Data Types section.

+ + +

Sending an Error Response

+ +

If you need to send the client an error response you will use the following:

+ +return $this->xmlrpc->send_error_message('123', 'Requested data not available'); + +

The first parameter is the error number while the second parameter is the error message.

+ + + + + + +

Creating Your Own Client and Server

+ +

To help you understand everything we've covered thus far, let's create a couple controllers that act as +XML-RPC Client and Server. You'll use the Client to send a request to the Server and receive a response.

+ +

The Client

+ +

Using a text editor, create a controller called xmlrpc_client.php. +In it, place this code and save it to your applications/controllers/ folder:

+ + + +

Note: In the above code we are using a "url helper". You can find more information in the Helpers Functions page.

+ +

The Server

+ +

Using a text editor, create a controller called xmlrpc_server.php. +In it, place this code and save it to your applications/controllers/ folder:

+ + + +

Try it!

+ +

Now visit the your site using a URL similar to this:

+www.your-site.com/index.php/xmlrpc_client/ + +

You should now see the message you sent to the server, and its response back to you.

+ +

The client you created sends a message ("How's is going?") to the server, along with a reqest for the "Greetings" method. +The Server receives the request and maps it to the "process" function, where a response is sent back.

+ + + +

 

+

XML-RPC Function Reference

+ +

$this->xmlrpc->server()

+

Sets the URL and port number of the server to which a request is to be sent:

+$this->xmlrpc->server('http://www.sometimes.com/pings.php', 80); + +

$this->xmlrpc->timeout()

+

Set a time out period (in seconds) after which the request will be canceled:

+$this->xmlrpc->timeout(6); + +

$this->xmlrpc->method()

+

Sets the method that will be requested from the XML-RPC server:

+$this->xmlrpc->method('method'); + +

Where method is the name of the method.

+ +

$this->xmlrpc->request()

+

Takes an array of data and builds request to be sent to XML-RPC server:

+$request = array(array('My Photoblog', 'string'), 'http://www.yoursite.com/photoblog/');
+$this->xmlrpc->request($request);
+ +

$this->xmlrpc->send_request()

+

The request sending function. Returns boolean TRUE or FALSE based on success for failure, enabling it to be used conditionally.

+ +

$this->xmlrpc->set_debug(TRUE);

+

Enables debugging, which will display a variety of information and error data helpful during development.

+ + +

$this->xmlrpc->display_error()

+

Returns an error message as a string if your request failed for some reason.

+echo $this->xmlrpc->display_error(); + +

$this->xmlrpc->display_response()

+

Returns the response from the remote server once request is received. The response will typically be an associative array.

+$this->xmlrpc->display_response(); + +

$this->xmlrpc->send_error_message()

+

This function lets you send an error message from your server to the client. First parameter is the error number while the second parameter +is the error message.

+return $this->xmlrpc->send_error_message('123', 'Requested data not available'); + +

$this->xmlrpc->send_response()

+

Lets you send the response from your server to the client. An array of valid data values must be sent with this method.

+$response = array(
+                 array(
+                        'flerror' => array(FALSE, 'boolean'),
+                        'message' => "Thanks for the ping!")
+                     )
+                 'struct');
+return $this->xmlrpc->send_response($response);
+ + + +

Data Types

+ +

According to the XML-RPC spec there are seven types +of values that you can send via XML-RPC:

+ +
    +
  • int or i4
  • +
  • boolean
  • +
  • string
  • +
  • double
  • +
  • dateTime.iso8601
  • +
  • base64
  • +
  • struct (contains array of values)
  • +
  • array (contains array of values)
  • +
+ + +
+ + + + + + + \ No newline at end of file diff --git a/user_guide/libraries/zip.html b/user_guide/libraries/zip.html index 165d61b0c..a2ef6c42b 100644 --- a/user_guide/libraries/zip.html +++ b/user_guide/libraries/zip.html @@ -28,7 +28,7 @@
- +

CodeIgniter User Guide Version 1.5.4

CodeIgniter User Guide Version 1.6.0

-- cgit v1.2.3-24-g4f1b