Professional CodeIgniter

(singke) #1

Chapter 9: Security and Performance


281


Here ’ s how you would run the check on the headers. Notice that the incoming $dbheaders listing is
checked using in_array(). The $dbheaders listing will be a zero - indexed array like the following:

(0 = > ‘id’, 1 = > ‘name’, 2 = > ‘something_else’... )

etc., And so on, depending on the names of the fields. A simple in_array() check will quickly detect if
the $hdr variable you are processing does exist in the table. If it does exist, set $error[$k] to FALSE;
otherwise, set $error[$k] to TRUE. If you have an error, then display an asterisk and “ (error) ” next to
the header in the display.

< ?php
$error = array();
if (count($csv)){
echo form_open(‘admin/products/import’);
echo form_submit(‘cancel’,’ < < start over’);
echo form_submit(‘submit’,’finalize import > > ’);
? >
< table border=’1’ cellspacing=’0’ cellpadding=’5’ >
< tr valign=’top’ >
< ?php
$headers = array_keys($csv[0]);
foreach ($headers as $k = > $v){
$hdr = trim(str_replace(‘”’,’’,$v));
if (in_array($hdr,$dbheaders)){
$error[$hdr] = false;
}else{
$error[$hdr] = true;
}
if ($hdr != ‘’ & & !eregi(“thumbnail”,$hdr)
& & !eregi(“image”,$hdr)){
echo “ < th > ”.$hdr;
if ($error[$hdr]){
echo “* (error)\n”;
}
echo “ < /th > \n”;
}
}
? >

Now that you have a full list of which headers are in error, with the keys of the $error array matching
the header names in the CSV file, you can quickly add a proviso in the second loop that only prints out a
hidden field, if and only if, there are no errors for a given header.


< /tr >
< ?php
foreach ($csv as $key = > $line){
echo “ < tr valign=’top’ > \n”;
foreach ($line as $f = > $d){
$FIELD = trim(str_replace(‘”’,’’,$f));
$FDATA = trim(str_replace(‘”’,’’,$d));
if ($FIELD != ‘’ & & !eregi(“thumbnail”,$FDATA)
& & !eregi(“image”,$FDATA)){
echo “ < td > ”;
Free download pdf