Carlos Developer Blog Just another developer weblog

18Jul/090

CodeIgniter vCard Library to Export Vcard .VCF files with Zip Support

Posted by Carlos

The Vcard.php Library Code (applications/library/Vcard.php) :

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php  
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/*
/*
 * ---------------------------------------------------------------------------------------------------------------------------
 *  vCard library for Code Igniter applications extended from class_vcard from Troy Wolf [troy@troywolf.com]
 * ---------------------------------------------------------------------------------------------------------------------------
 */

 
/**
* Codeigniter vCard  library
*/

class Vcard
{
    /**
     * vcard variables
     **/

    protected $ci;

    protected $log;
    protected $data;            //array of this vcard's contact data
    private $filename;          //filename for download file naming
    private $class;             //PUBLIC, PRIVATE, CONFIDENTIAL
    private $revision_date;     //vCard Date
    private $card;              //vCard String

    /**
     * __construct
     **/

    public function __construct()
    {
        $this->ci =& get_instance();
        //$ci->load->library('session');
        $this->ci->load->library('zip');
    }

    /**
     *The Vcards class constructor. You can set some defaults here if desired.
     *
     */
   
  function Vcard($data = false) {
    $this->log = "New vcard() called<br />";

    $this->data = array(
      "display_name"=>null
      ,"first_name"=>null
      ,"last_name"=>null
      ,"additional_name"=>null
      ,"name_prefix"=>null
      ,"name_suffix"=>null
      ,"nickname"=>null
      ,"title"=>null
      ,"role"=>null
      ,"department"=>null
      ,"company"=>null
      ,"work_po_box"=>null
      ,"work_extended_address"=>null
      ,"work_address"=>null
      ,"work_city"=>null
      ,"work_state"=>null
      ,"work_postal_code"=>null
      ,"work_country"=>null
      ,"home_po_box"=>null
      ,"home_extended_address"=>null
      ,"home_address"=>null
      ,"home_city"=>null
      ,"home_state"=>null
      ,"home_postal_code"=>null
      ,"home_country"=>null
      ,"office_tel"=>null
      ,"home_tel"=>null
      ,"cell_tel"=>null
      ,"fax_tel"=>null
      ,"pager_tel"=>null
      ,"email1"=>null
      ,"email2"=>null
      ,"url"=>null
      ,"photo"=>null
      ,"birthday"=>null
      ,"timezone"=>null
      ,"sort_string"=>null
      ,"note"=>null
      );
    if(is_array($data))
    {
        foreach($data as $item => $value)
        {
            $this->data[$item] = $value;
        }
    }
    return true;
  }
/**
*The Reload method. This metod update the DATA array content.
*
*/
   
  function reload($data = false) {
    $this->log = "reload() called<br />";

    $this->data = array(
      "display_name"=>null
      ,"first_name"=>null
      ,"last_name"=>null
      ,"additional_name"=>null
      ,"name_prefix"=>null
      ,"name_suffix"=>null
      ,"nickname"=>null
      ,"title"=>null
      ,"role"=>null
      ,"department"=>null
      ,"company"=>null
      ,"work_po_box"=>null
      ,"work_extended_address"=>null
      ,"work_address"=>null
      ,"work_city"=>null
      ,"work_state"=>null
      ,"work_postal_code"=>null
      ,"work_country"=>null
      ,"home_po_box"=>null
      ,"home_extended_address"=>null
      ,"home_address"=>null
      ,"home_city"=>null
      ,"home_state"=>null
      ,"home_postal_code"=>null
      ,"home_country"=>null
      ,"office_tel"=>null
      ,"home_tel"=>null
      ,"cell_tel"=>null
      ,"fax_tel"=>null
      ,"pager_tel"=>null
      ,"email1"=>null
      ,"email2"=>null
      ,"url"=>null
      ,"photo"=>null
      ,"birthday"=>null
      ,"timezone"=>null
      ,"sort_string"=>null
      ,"note"=>null
      );
    if(is_array($data))
    {
        foreach($data as $item => $value)
        {
            $this->data[$item] = $value;
        }
    }
    $this->build();
   
    return $this->card;
  }
/**
 * Build  method checks all the values, builds appropriate defaults for
 * missing values, generates the vcard data string
 *
 * @param
 * @return VCF file
 */
 
  function build() {
    $this->log .= "vcard build() called<br />";
/*
For many of the values, if they are not passed in, we set defaults or
build them based on other values.
*/

    if (!$this->class) { $this->class = "PUBLIC"; }
    if (!$this->data['display_name']) {
      $this->data['display_name'] = trim($this->data['first_name']." ".$this->data['last_name']);
    }
    if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['last_name']; }
    if (!$this->data['sort_string']) { $this->data['sort_string'] = $this->data['company']; }
    if (!$this->data['timezone']) { $this->data['timezone'] = date("O"); }
    if (!$this->revision_date) { $this->revision_date = date('Y-m-d H:i:s'); }
   
$this->card = "BEGIN:VCARD\r\n";
$this->card .= "VERSION:3.0\r\n";
$this->card .= "CLASS:".$this->class."\r\n";
$this->card .= "PRODID:-//Vcard Extended Class from carlos.alcala@upandrunningsoftware.com//NONSGML Version 1//EN\r\n";
$this->card .= "REV:".$this->revision_date."\r\n";
$this->card .= "FN:".$this->data['display_name']."\r\n";
$this->card .= "N:"
.$this->data['last_name'].";"
.$this->data['first_name'].";"
.$this->data['additional_name'].";"
.$this->data['name_prefix'].";"
.$this->data['name_suffix']."\r\n";
if ($this->data['nickname']) { $this->card .= "NICKNAME:".$this->data['nickname']."\r\n"; }
if ($this->data['title']) { $this->card .= "TITLE:".$this->data['title']."\r\n"; }
if ($this->data['company']) { $this->card .= "ORG:".$this->data['company']; }
if ($this->data['department']) { $this->card .= ";".$this->data['department']; }
$this->card .= "\r\n";
   
if ($this->data['work_po_box']
    || $this->data['work_extended_address']
    || $this->data['work_address']
    || $this->data['work_city']
    || $this->data['work_state']
    || $this->data['work_postal_code']
    || $this->data['work_country']) {
$this->card .= "ADR;TYPE=work:"
.$this->data['work_po_box'].";"
.$this->data['work_extended_address'].";"
        .$this->data['work_address'].";"
        .$this->data['work_city'].";"
        .$this->data['work_state'].";"
        .$this->data['work_postal_code'].";"
        .$this->data['work_country']."\r\n";
    }
    if ($this->data['home_po_box']
    || $this->data['home_extended_address']
    || $this->data['home_address']
    || $this->data['home_city']
    || $this->data['home_state']
    || $this->data['home_postal_code']
    || $this->data['home_country']) {
      $this->card .= "ADR;TYPE=home:"
        .$this->data['home_po_box'].";"
        .$this->data['home_extended_address'].";"
        .$this->data['home_address'].";"
        .$this->data['home_city'].";"
        .$this->data['home_state'].";"
        .$this->data['home_postal_code'].";"
        .$this->data['home_country']."\r\n";
    }
    if ($this->data['email1']) { $this->card .= "EMAIL;TYPE=internet,pref:".$this->data['email1']."\r\n"; }
    if ($this->data['email2']) { $this->card .= "EMAIL;TYPE=internet:".$this->data['email2']."\r\n"; }
    if ($this->data['office_tel']) { $this->card .= "TEL;TYPE=work,voice:".$this->data['office_tel']."\r\n"; }
    if ($this->data['home_tel']) { $this->card .= "TEL;TYPE=home,voice:".$this->data['home_tel']."\r\n"; }
    if ($this->data['cell_tel']) { $this->card .= "TEL;TYPE=cell,voice:".$this->data['cell_tel']."\r\n"; }
    if ($this->data['fax_tel']) { $this->card .= "TEL;TYPE=work,fax:".$this->data['fax_tel']."\r\n"; }
    if ($this->data['pager_tel']) { $this->card .= "TEL;TYPE=work,pager:".$this->data['pager_tel']."\r\n"; }
    if ($this->data['url']) { $this->card .= "URL;TYPE=work:".$this->data['url']."\r\n"; }
    if ($this->data['birthday']) { $this->card .= "BDAY:".$this->data['birthday']."\r\n"; }
    if ($this->data['role']) { $this->card .= "ROLE:".$this->data['role']."\r\n"; }
    if ($this->data['note']) { $this->card .= "NOTE:".$this->data['note']."\r\n"; }
    $this->card .= "TZ:".$this->data['timezone']."\r\n";
    $this->card .= "END:VCARD\r\n";
  }

    /**
     * Download method streams the vcard to the browser client.
     *
     * @param
     * @return VCF file
     */
 
    function download() {
        /*$this->log .= "vcard download() called<br />";
        if (!$this->card) { $this->build(); }
        if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
        $this->filename = str_replace(" ", "_", $this->filename);
        header("Content-type: text/directory");
        header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
        header("Pragma: public");
        echo $this->card;
        */

        echo $this->getvcard();
        return true;
    }

    /**
     * Zipdownload method. Streams the vcard zipped to the browser client.
     *
     * @param
     * @return VCF file ZIPPED
     */
 
    function zipdownload() {
       
        $this->log .= "vcard download() called<br />";
        if (!$this->card) { $this->build(); }
        if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
        $this->filename = str_replace(" ", "_", $this->filename);
        /*
        IM NOT SURE OF THIS COMMENT
        header("Content-type: text/directory");
        header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
        header("Pragma: public");
        */

        //echo $this->card;
        $datavcard = $this->getvcard();

        $name = $this->filename.".vcf";//'mydata1.txt';
        //$datavcard = 'A Data String!';

        $this->ci->zip->add_data($name, $datavcard);

        // Write the zip file to a folder on your server. Name it "my_backup.zip"
        $this->ci->zip->archive(base_url().'vcards/'.$this->filename.'.zip');

        // Download the file to your desktop. Name it "my_backup.zip"
        $this->ci->zip->download($this->filename.'.zip');
        return true;
    }
    /**
     * Get Vcard for Download.
     *
     * @param
     * @return VCF file
     */
 
    function getvcard() {
        $this->log .= "vcard download() called<br />";
        if (!$this->card) { $this->build(); }
        if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
        $this->filename = str_replace(" ", "_", $this->filename);
        header("Content-type: text/directory");
        header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
        header("Pragma: public");
        return $this->card;
        //return true;
    }
    /**
     * Get Vcard for Download.
     *
     * @param
     * @return VCF file
     */
 
    function returnvcard() {
        $this->log .= "vcard download() called<br />";
        if (!$this->card) { $this->build(); }
        if (!$this->filename) { $this->filename = trim($this->data['display_name']); }
        $this->filename = str_replace(" ", "_", $this->filename);
        //header("Content-type: text/directory");
        //header("Content-Disposition: attachment; filename=".$this->filename.".vcf");
        //header("Pragma: public");
        return $this->card;
        //return true;
    }

    /**
     * Zip Download method. Streams the vcard ARRAY zipped to the browser client.
     *
     * @param
     * @return VCF file ZIPPED
     */
 
    function zipdownloads($filename = false, $vcards = false) {
       
       
        foreach($vcards as $item => $value)
        {
            foreach($value as $key => $val)
            {
                $this->ci->zip->add_data($key, $val);
            }          
        }
        // Write the zip file to a folder on your server.
        $this->ci->zip->archive(base_url().'vcards/'.$filename.'.zip');
        // Download the file to your desktop.
        $this->ci->zip->download($filename.'.zip');
        return true;
    }      
   
}
?>

Then we can use it on a controller function like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    function linkvcard($id = false)
    {
        $this->load->library('vcard');
        $datavcard = $this->getvcard($id);
        if (is_array($datavcard))
        {  
            $this->vcard->vcard($datavcard);
        }
        else
        {
            $this->vcard->vcard();
           
        }
        $this->vcard->zipdownload();
    }

The vcard function on the controller calls the library Vcard.php and then use the constructor, for that you have to arrange an array of COMMON VALUES for, we can use the Troy example for this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
function getvcard(){
/*
filename is the name of the .vcf file that will be sent to the user if you
call the download() method. If you leave this blank, the class will
automatically build a filename using the contact's data.
*/

#$datavcardfilename = "";

/*
If you leave this blank, the current timestamp will be used.
*/

#$datavcardrevision_date = "";

/*
Possible values are PUBLIC, PRIVATE, and CONFIDENTIAL. If you leave class
blank, it will default to PUBLIC.
*/

#$datavcardclass = "PUBLIC";

/*
Contact's name data.
If you leave display_name blank, it will be built using the first and last name.
*/

#$datavcarddata['display_name'] = "";
$datavcarddata['first_name'] = "Troy";
$datavcarddata['last_name'] = "Wolf";
#$datavcarddata['additional_name'] = ""; //Middle name
#$datavcarddata['name_prefix'] = "";  //Mr. Mrs. Dr.
#$datavcarddata['name_suffix'] = ""; //DDS, MD, III, other designations.
$datavcarddata['nickname'] = "TJ";

/*
Contact's company, department, title, profession
*/

$datavcarddata['company'] = "TroyWolf.com";
#$datavcarddata['department'] = "";
$datavcarddata['title'] = "Web Developer";
#$datavcarddata['role'] = "";

/*
Contact's work address
*/

#$datavcarddata['work_po_box'] = "";
#$datavcarddata['work_extended_address'] = "";
$datavcarddata['work_address'] = "7027 N. Hickory";
$datavcarddata['work_city'] = "Kansas City";
$datavcarddata['work_state'] = "MO";
$datavcarddata['work_postal_code'] = "64118";
#$datavcarddata['work_country'] = "United States of America";

/*
Contact's home address
*/

#$datavcarddata['home_po_box'] = "";
#$datavcarddata['home_extended_address'] = "";
$datavcarddata['home_address'] = "7027 N. Hickory";
$datavcarddata['home_city'] = "Kansas City";
$datavcarddata['home_state'] = "MO";
$datavcarddata['home_postal_code'] = "64118";
#$datavcarddata['home_country'] = "United States of America";

/*
Contact's telephone numbers.
*/

$datavcarddata['office_tel'] = "";
#$datavcarddata['home_tel'] = "";
$datavcarddata['cell_tel'] = "(816) 739-9653";
$datavcarddata['fax_tel'] = "";
#$datavcarddata['pager_tel'] = "";

/*
Contact's email addresses
*/

$datavcarddata['email1'] = "troy@troywolf.com";
#$datavcarddata['email2'] = "";

/*
Contact's website
*/

$datavcarddata['url'] = "http://www.troywolf.com";

/*
Some other contact data.
*/

#$datavcarddata['photo'] = "";  //Enter a URL.
$datavcarddata['birthday'] = "1971-08-13";
$datavcarddata['timezone'] = "-06:00";

/*
If you leave this blank, the class will default to using last_name or company.
*/

#$datavcarddata['sort_string'] = "";

/*
Notes about this contact.
*/

#$datavcarddata['note'] = "Troy is an amazing guy!";
}

You can customize the $datavcard array to suit your contact information and then passit to the constructor like the function before.

I hope this would be quite enough for this to get work on export vCards.

Thanks for your comments,

Carlos

PS. The Codeigniter Wiki for this library is here: vCard Library

Filed under: codeigniter No Comments
15Jul/093

CodeIgniter + Email + SMTP Custom Config to Gmail Working

Posted by Carlos

I was trying to make use of the email library on codeigniter, and found some issues on the Gmail config and authentication errors, then after making my own I have made a custom configuration that works fine.

1. Create this CONSTANTS on config/constanst.php

define('MAIL_PROT', 'smtp');
define('SITE_ADMIN', 'Webmaster');
define('SITE_EMAIL', 'gmail.user@gmail.com');
define('SMTP_HOST', 'ssl://smtp.googlemail.com');
define('SMTP_USER', 'gmail.user@gmail.com');
define('SMTP_PASS', 'somepassword');
define('SMTP_PORT', 465);
define('MAIL_TYPE', 'html');
define('MAIL_CHARSET', 'utf-8');

2. Then create a smtp config function on a email_model for example:

	function smtpconfig()
	{
			$config = array();
			//$config = $this->config->item('email');
			$config = Array(
			    'protocol' => MAIL_PROT,
			    'smtp_host' => SMTP_HOST,
			    'smtp_port' => SMTP_PORT,
			    'smtp_user' => SMTP_USER,
			    'smtp_pass' => SMTP_PASS,
			    'smtp_timeout' => 30,
			    'mailtype'  => MAIL_TYPE
			);
			//var_dump($config);

			$this->load->library('email', $config);
			$this->email->clear();
			$this->email->set_newline("\r\n");
	}

3. Finally we can use BOTH in a test function like:

function emailtest()
{
		$this->smtpconfig();
                $this->email->from(SITE_EMAIL, SITE_ADMIN);
		$this->email->to('receiver.user@gmail.com');

		$this->email->subject('CodeIgniter & Gmail Rules !! ');
		$this->email->message('Hello Gmail World');

		if (!$this->email->send())
		    show_error($this->email->print_debugger());
		else
			echo 'Your e-mail has been sent!';
}

I hope this would be useful.

Thanks for your feedback,

Carlos

Filed under: Useful, codeigniter 3 Comments
14Jul/091

30 HTML Best Practices for Beginners

Posted by Carlos

Thanks to Jeffrey Way for this great article, I just want to share to the world (and thanks Peter Hanson for this also)...

30 HTML Best Practices

30 HTML Best Practices

30 HTML Best Practices for Beginners

I hope it would be useful for all the Web  developers.

Carlos

Filed under: Useful 1 Comment
8Jul/09Off

Codeigniter SimpleXML data parser with Table View

Posted by Carlos

I read about the SimpleXML library from this blog SimpleXML Library, but this time, I mafe some changes to make the MVC concepts on the table view and xmltest controller best fit.

First we load the Simplexml library to applications/library on codeigniter:

result="";
	$this->evalCode="";
	$values="";
	$encoding = 'UTF-8';
	$parser = xml_parser_create($encoding);
	xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
	xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
	$ok = xml_parse_into_struct($parser, $data, $values);
	if (!$ok) {
		$errmsg = sprintf("XML parse error %d '%s' at line %d, column %d (byte index %d)",
		xml_get_error_code($parser),
		xml_error_string(xml_get_error_code($parser)),
		xml_get_current_line_number($parser),
		xml_get_current_column_number($parser),
		xml_get_current_byte_index($parser));
	}

	xml_parser_free($parser);
	return $this->xml_reorganize($values);
	}

	function xml_reorganize($array)
	{

		$count = count($array);
		$repeat = $this->xml_tags($array);
		$repeatedone = false;
		$tags = array();
		$k = 0;
		for ($i = 0; $i < $count; $i++) {
			switch ($array[$i]['type']) {
				case 'open':
					array_push($tags, $array[$i]['tag']);
					if ($i > 0 && ($array[$i]['tag'] == $array[$i-1]['tag']) && ($array[$i-1]['type'] == 'close'))
					$k++;
					if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
						array_push($tags, '@content');
						$this->array_insert(count($tags), $tags, $array[$i]['value'], "open");
						array_pop($tags);
					}

					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
						if (($repeatedone == $array[$i]['tag'] . $array[$i]['level']) && ($repeatedone)) {
							array_push($tags, strval($k++));
						} else {
							$repeatedone = $array[$i]['tag'] . $array[$i]['level'];
							array_push($tags, strval($k));
						}
					}

					if (isset($array[$i]['attributes']) && $array[$i]['attributes'] && $array[$i]['level'] != $this->ignore_level) {
						array_push($tags, '@attributes');
						foreach ($array[$i]['attributes'] as $attrkey => $attr) {
							array_push($tags, $attrkey);
							$this->array_insert(count($tags), $tags, $attr, "open");
							array_pop($tags);
						}
						array_pop($tags);
					}
					break;

				case 'close':
					array_pop($tags);
					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
						if ($repeatedone == $array[$i]['tag'] . $array[$i]['level']) {
							array_pop($tags);
						} else {
							$repeatedone = $array[$i + 1]['tag'] . $array[$i + 1]['level'];
							array_pop($tags);
						}
					}
					break;

				case 'complete':
					array_push($tags, $array[$i]['tag']);
					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
						if ($repeatedone == $array[$i]['tag'] . $array[$i]['level'] && $repeatedone) {
							array_push($tags, strval($k));
						} else {
							$repeatedone = $array[$i]['tag'] . $array[$i]['level'];
							array_push($tags, strval($k));
						}
					}

					if (isset($array[$i]['value']) && ($array[$i]['value'] || !$this->skip_empty_values)) {
						if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
							array_push($tags, '@content');
							$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
							array_pop($tags);
						} else {
							$this->array_insert(count($tags), $tags, $array[$i]['value'], "complete");
						}
					}

					if (isset($array[$i]['attributes']) && $array[$i]['attributes']) {
						array_push($tags, '@attributes');
						foreach ($array[$i]['attributes'] as $attrkey => $attr) {
							array_push($tags, $attrkey);
							$this->array_insert(count($tags), $tags, $attr, "complete");
							array_pop($tags);
						}
						array_pop($tags);
					}

					if (in_array($array[$i]['tag'] . $array[$i]['level'], $repeat)) {
						array_pop($tags);
						$k++;
					}

					array_pop($tags);
					break;
			}
		}
		eval($this->evalCode);
		$last = $this->array_reindex($this->result);
		return $last;
	}	

	function array_insert($level, $tags, $value, $type)
	{
		$temp = '';
		for ($c = $this->ignore_level + 1; $c < $level + 1; $c++) {
			if (isset($tags[$c]) && (is_numeric(trim($tags[$c])) || trim($tags[$c]))) {
				if (is_numeric($tags[$c])) {
					$temp .= '[' . $tags[$c] . ']';
				} else {
					$temp .= '["' . $tags[$c] . '"]';
				}
			}
		}
		$this->evalCode .= '$this->result' . $temp . "=\"" . addslashes($value) . "\";//(" . $type . ")\n";
		#echo $code. "\n";
	}

	/**
	 * Define the repeated tags in XML file so we can set an index
	 *
	 * @param array $array
	 * @return array
	 */
	function xml_tags($array)
	{	$repeats_temp = array();
	$repeats_count = array();
	$repeats = array();

	if (is_array($array)) {
		$n = count($array) - 1;
		for ($i = 0; $i < $n; $i++) {
			$idn = $array[$i]['tag'].$array[$i]['level'];
			if(in_array($idn,$repeats_temp)){
				$repeats_count[array_search($idn,$repeats_temp)]+=1;
			}else{
				array_push($repeats_temp,$idn);
				$repeats_count[array_search($idn,$repeats_temp)]=1;
			}
		}
	}
	$n = count($repeats_count);
	for($i=0;$i<$n;$i++){
		if($repeats_count[$i]>1){
			array_push($repeats,$repeats_temp[$i]);
		}
	}
	unset($repeats_temp);
	unset($repeats_count);
	return array_unique($repeats);
	}

	/**
	 * Converts Array Variable to Object Variable
	 *
	 * @param array $arg_array
	 * @return $tmp
	 */
	function array2object ($arg_array)
	{

		if (is_array($arg_array)) {
			$keys = array_keys($arg_array);
			if(!is_numeric($keys[0])) $tmp = new Xml;
			foreach ($keys as $key) {
				if (is_numeric($key)) $has_number = true;
				if (is_string($key)) $has_string = true;
			}
			if (isset($has_number) and !isset($has_string)) {
				foreach ($arg_array as $key => $value) {
					$tmp[] = $this->array2object($value);
				}
			} elseif (isset($has_string)) {
				foreach ($arg_array as $key => $value) {
					if (is_string($key))
					$tmp->$key = $this->array2object($value);
				}
			}
		} elseif (is_object($arg_array)) {
			foreach ($arg_array as $key => $value) {
				if (is_array($value) or is_object($value))
				$tmp->$key = $this->array2object($value);
				else
				$tmp->$key = $value;
			}
		} else {
			$tmp = $arg_array;
		}
		return $tmp; //return the object
	}

	/**
	 * Reindexes the whole array with ascending numbers
	 *
	 * @param array $array
	 * @return array
	 */
	function array_reindex($array)
	{
		if (is_array($array)) {
			if(count($array) == 1 && isset($array[0])) {
			//if(count($array) == 1 && $array[0]){
				return $this->array_reindex($array[0]);
			}else{
				foreach($array as $keys => $items) {
					if (is_array($items)) {
						if (is_numeric($keys)) {
							$array[$keys] = $this->array_reindex($items);
						} else {
							$array[$keys] = $this->array_reindex(array_merge(array(), $items));
						}
					}
				}
			}
		}
		return $array;
	}
}
?>

The XML file:




	
		1
		iPhone
300
	

	
		2
		iMac
2500
	

	
		3
		MacBook Pro
2000
	

	
		4
		iTouch
150
	

	
		5
		Wii
1250
	

	
		6
		Time Capsule
1000
	

	
		7
		Apple TV
800
	

The xmltest.php controller for codeigniter (applications/controller/xmltest.php):

load->library('parser');
		$this->load->library('simplexml');

		$filename = 'myxml.xml';
		$xmlfile="./xml/".$filename;
		$xmlRaw = file_get_contents($xmlfile);
		$data['xmlData'] = $this->simplexml->xml_parse($xmlRaw);
		//$this->parser->parse('xml/table_view', $data);
		echo $this->load->view('xml/table_view', $data, TRUE);
	}
}

/* End of file xmltest.php */
/* Location: ./system/application/controllers/xmltest.php */

At last but not least the table view on codeigniter (applications/views/xml/table_view) with foreach on xml row items:

';
		foreach($row as $item => $value)
		{
			//could be $value instead of $row[$item], but this show the array manipulation in the foreach
			$result .= '


';
		}
		$result .= '

';
	}
	echo $result;
?>
	
PRODUCT ID PRODUCT NAME CATEGORY PRICE
'.$row[$item].'

And the Jquery AddClass for even class on rows:

Thanks for your comments, the next wave would be the MySQL insert function on the xmltest controller.

Filed under: codeigniter Comments Off
5Jul/090

Web Developer Tools

Posted by Carlos

With so many browser out there in the wild, working as a Web Developer certainly is not an easy job these days. Thankfully, over the last years, decent tools have been developed by bright people that make developing and debugging of code easier for the developer. But sometimes, it is not easy to keep track which tools are at a Developer’s disposal (and which are actually usable). The following article seeks to list the best tools available and quickly describing how to activate/install/use them.

Thanks to KlausKomenda.com

http://www.klauskomenda.com/archives/2008/02/16/collection-of-web-developer-tools-per-browser/

Filed under: Useful No Comments
4Jul/090

send AJAX post parameters data with Jquery UI Dialog

Posted by Carlos

After SEVERAL HOURS of try/catch I finally came with this working example, its working on AJAX POST with new rows appends to the TABLE on the fly (that was my real problem):

The magic came with link this:

remove
remove
remove

This is the final working with AJAX POST and Jquery Dialog:

var $k = jQuery.noConflict();  //this is for NO-CONFLICT with scriptaculous
     function removecompany(link){
        companyid = link.id.replace('remove_', '');
    $k("#removedialog").dialog({
                        bgiframe: true,
                        resizable: false,
                        height:140,
                        autoOpen:false,
                        modal: true,
                        overlay: {
                                backgroundColor: '#000',
                                opacity: 0.5
                        },
                        buttons: {
                                'Are you sure ?': function() {
                                        $k(this).dialog('close');
                                        alert(companyid);
                                        $k.ajax({
                                              type: "post",
                                          url: "../ra/removecompany.php",
                                          dataType: "json",
                                          data: {
                                                        'companyid' : companyid
                                                },
                                          success: function(data) {
                                                //alert(data);
                                                if(data.success)
                                                {
                                                        //alert('success');
                                                        $k('#companynew'+companyid).remove();
                                                }
                                      }
                                        }); // End ajax method
                                },
                                Cancel: function() {
                                        $k(this).dialog('close');
                                }
                        }
                });
                $k("#removedialog").dialog('open');
                //return false;
     }

Finally the dialog DIV:

This company will be permanently deleted and cannot be recovered. Are you sure?
Filed under: jquery No Comments
4Jul/09Off

Just another Blog site

Posted by Carlos

Welcome to my Site !

My personal Blog Site www.carlos-alcala.com is borning this day, July 4th as an "Blog Independence Day", but ONLY THIS TIME it's just for me,  and to remind every one else my comming soon Birthday on July 6th.

This site porpouse is for my blogging system and "share to the world " over software development tips&tricks for PHP, AJAX, Jquery, JSON, CodeIgniter, Drupal and so on.

Thanks for your feedback to make this site better every day!

Sincerely

Carlos Alcala

10Jun/091

Hello world!

Posted by Carlos

Hello Blog World, finally I get my own blog site, enjoy it  !!!

Filed under: welcome 1 Comment