Welcome Guest, Not a member yet? Register   Sign In
A PHP Error was encountered Severity: Notice Message: Undefined index:
#1

(This post was last modified: 03-06-2019, 07:25 AM by ciadmin. Edit Reason: Added code tags )

Hi I have a site which was previously built by someone we no longer deal with and whilst I have been able to sort many issues out one keeps getting my all twisted,

I have an area on my dashboard which allows me to add a file to upload a pdf

and when on the front end it gives me a clickable link.

I now need to add an extra PDF in the same format, so I have copied the code in the admin modules added it below the other PDF and changes the details as required  and have been able to make it appear in the dashboard it all works adds the file to the DB and puts it in the correct folder.

then I copied the relevant code for the front end change the details and the link appears but i get this error message.



A PHP Error was encountered
Severity: Notice
Message: Undefined index: acccesories_fitting_pdf_file
Filename: views/index.php
Line Number: 104

the code for this is

Code:
<?php
                }
                
                if(!empty($accessories[0]['accessories_fitting_pdf_file']))
                {
                    ?>
                    <div class="bullet-box-content">
                        <a href="<?php echo base_url(); ?>images/accessories_images/pdf/<?php echo $accessories[0]['acccesories_fitting_pdf_file']; ?>" target="_blank">Fitting Instructions</a>
                    </div>   

Which was a copy of this
<?php
                if(!empty($accessories[0]['accessories_pdf_file']))
                {
                ?>
                    <div class="bullet-box-content">
                        <a href="<?php echo base_url(); ?>images/accessories_images/pdf/<?php echo $accessories[0]['accessories_pdf_file']; ?>" target="_blank">View Drawing</a>
                    </div>


the complete index.php code is as follows

<div class="container-fluid">

<?php
if(!empty($accessories) && !empty($accessories[0]['accessories_banner_image']))
{
?>
    <div class="row">
        <div class="col-sm-12 pl0 pr0">
          <div class="product-banner-bg">
              <img src="<?php echo base_url(); ?>images/accessories_images/<?php echo $accessories[0]['accessories_banner_image']; ?>" alt="" title="" class="img-responsive"/>
            </div>        
        </div>
    </div>
<?php
}
?>

<div class="clearfix"></div>

<div class="container">
    <div class="row">
        <div class="col-sm-12">
            <div class="product-hd">Accessories</div>
            <div class="medium-hd"><?php echo $accessories[0]['accessories_name']; ?></div>
        </div>
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="content">
                <?php echo html_entity_decode($accessories[0]['accessories_description']);

                /*<!--<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum ac libero vitae ligula fringilla sollicitudin non in lacus.
                Aliquam non nulla at nisi maximus pharetra id at sapien. Pellentesque ullamcorper imperdiet ex. Praesent eu finibus nisl, id viverra
                 mauris. Sed iaculis erat a dolor mattis eleifend. Integer dolor augue, fermentum sed mauris a, pretium ornare ligula. Fusce ut
                 orci sem. Curabitur sit amet neque sit amet erat scelerisque iaculis. </p>-->*/ ?>
            </div>
        </div>
    </div>
    
    <?php
    if(!empty($accessory_images) && count($accessory_images) > 0)
    {
    ?>
    <div class="row">
        <div class="col-sm-12">
            <div class="medium-hd"></div>
            <div class="content">
                <div id="links-install" class="links">
                    <?php
                    $accessory_img_cnt = 0;
                    
                    foreach($accessory_images as $acc_img)
                    {
                        $accessory_img_cnt = $accessory_img_cnt + 1;
                    ?>
                        <div class="col-sm-3 mt15 mb15 product-border">
                             <a href="<?php echo base_url(); ?>images/accessories_images/<?php echo $acc_img['accessories_image']; ?>" title="Accessory image <?php echo $accessory_img_cnt; ?>">
                            <img src="<?php echo base_url(); ?>images/accessories_images/<?php echo $acc_img['accessories_image']; ?>" alt="Accessory image <?php echo $accessory_img_cnt; ?>" class="img-responsive">
                            </a>
                        </div>
                    <?php
                    }
                    ?>
                </div>
            </div>
        </div>
    </div>
    <?php
    }
    ?>
    
    <?php
    if(!empty($accessories[0]['accessories_pdf_file']))
    {
    ?>
    
    <div class="row">
        <div class="col-sm-12">
            <div class="medium-hd"></div>
            <div class="content">
                <?php
                if(!empty($accessories[0]['accessories_pdf_file']))
                {
                ?>
                    <div class="bullet-box-content">
                        <a href="<?php echo base_url(); ?>images/accessories_images/pdf/<?php echo $accessories[0]['accessories_pdf_file']; ?>" target="_blank">View Drawing</a>
                    </div>
                <?php
                }
                
                if(!empty($accessories[0]['accessories_zip_file']))
                {
                    ?>
                    <div class="bullet-box-content">
                        <a href="<?php echo base_url(); ?>images/accessories_images/zip/<?php echo $accessories[0]['accessories_zip_file']; ?>" target="_blank">2D Drawings</a>
                    </div>
                <?php
                }
                
                if(!empty($accessories[0]['accessories_fitting_pdf_file']))
                {
                    ?>
                    <div class="bullet-box-content">
                        <a href="<?php echo base_url(); ?>images/accessories_images/pdf/<?php echo $accessories[0]['acccesories_fitting_pdf_file']; ?>" target="_blank">Fitting Instructions</a>
                    </div>    
                    <?php
                }
                ?>
                
            </div>
        </div>
    </div>
    
    <?php
    }
    ?>

</div>
<script>
    document.getElementById('links-install').onclick = function (event)
    {
        event = event || window.event;
        var target = event.target || event.srcElement,
            link = target.src ? target.parentNode : target,
            options = {index: link, event: event},
            links = this.getElementsByTagName('a');
            blueimp.Gallery(links, options);
    };
</script>


I have looked everywhere for a refernce but I am obviously missing something basic. please could someone help?
Reply
#2

The error means the variable $accessories[0] does not have an index called "acccesories_fitting_pdf_file". In other words the array item $accessories[0]['accessories_fitting_pdf_file'] does not exist.

You should look in the controller that loads the view file for the array key 'accessories', i.e. $data['accessories']. (It might not be named $data. It could have any valid variable name. That variable will be passed to the $this->load->view(...); method.)

Based on the view's code there should be code in the controller that sets the value of $data['accessories'] to an array. One of the keys of that array should be named 'accessories_fitting_pdf_file'. Make sure some such code exists and that actual values are properly assigned.
Reply
#3

Hi Dave,

thank you for your response, I've search through the controller file and the information in my opinion is correct I can't see where I have missed changing a name, I simply coppied it from the relevant code and pasted it back in and then changed from accessories_pdf_file to accessories_fitting_pdf_file,
I've been staring at the same bits of code for a day and a half now,

This is the code for the controller

and this is the page http://www.ohm.co.uk/accessories/lwb-pt
were the fault is occurring.

I appreciate any help thank you.


PHP Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
session_start(); //we need to call PHP's session object to access it through CI
class Accessories extends CI_Controller {

    function 
__construct()
    {
        
parent::__construct();
        
$this->load->model('accessoriesmodel','',TRUE);
    }
 
    function 
index()
    {
        if(!empty(
$_SESSION["webadmin"]))
        {
            
$this->load->view('template/header.php');
            
$this->load->view('accessories/accessoriesList');
            
$this->load->view('template/footer.php');
        }
        else
        {
            
//If no session, redirect to login page
            
redirect('login''refresh');
        }
    }

    function 
fetch()
    {    
        
$get_result $this->accessoriesmodel->getRecords($_GET);
        
        
$result = array();
        
$result["sEcho"]= $_GET['sEcho'];
        
        
$result["iTotalRecords"] = isset($get_result['totalRecords']) ? $get_result['totalRecords'] : 0;                                    
        
$result["iTotalDisplayRecords"]= isset($get_result['totalRecords']) ? $get_result['totalRecords'] : 0;                            
        
        
$items = array();
        
        for(
$i=0;$i<sizeof($get_result['query_result']);$i++){
            
$temp = array();
            
$actionCol "";
            
$status "";
            
array_push($temp$get_result['query_result'][$i]->accessories_name);
             
            
$status strtolower($get_result['query_result'][$i]->status) == strtolower('active') ? '<font color="green">Active</font>''<font color="red">Inactive</font>';
            
array_push($temp$status);
            
            
$actionCol .='<a href="accessories/addEdit?text='.rtrim(strtr(base64_encode("id=".$get_result['query_result'][$i]->accessories_id), '+/''-_'), '=').'" title="Edit"><i class="icon-picture icon-edit"></i></a>';
            
$actionCol .='&nbsp;&nbsp;<a href="javascript:void(0);" onclick="deleteData(\''.$get_result['query_result'][$i]->accessories_id.'\');" title="Delete"><i class="icon-remove-sign"></i></a>';
            
array_push($temp$actionCol);
            
            
array_push($items$temp);
        }
        
        
$result["aaData"] = $items;
        echo 
json_encode($result);
        exit;
    }
 
    function 
addEdit()
    {
        if(!empty(
$_SESSION["webadmin"]))
        {
            
$accessories_id "";
            if(!empty(
$_GET['text']) && isset($_GET['text'])){
                
$varr=base64_decode(strtr($_GET['text'], '-_''+/'));    
                
parse_str($varr,$url_prams);
                
$accessories_id $url_prams['id'];
            }
            
            
$result "";
            
            if(!empty(
$accessories_id))
            {
                
$result['recorddata'] = $this->accessoriesmodel->getFormdata($accessories_id);
                
$condition "accessories_id = '".$accessories_id."' ";
                
$result['accessories_images'] = $this->accessoriesmodel->getData("tbl_accessories_images",$accessories_id);
            }
            
            
//echo "<pre>";
            //print_r($result);
            //exit;
            
$this->load->view('template/header.php');
            
$this->load->view('accessories/index',$result);
            
$this->load->view('template/footer.php');
        }
        else
        {
            
redirect('login''refresh');
        }
     }
             
     function 
submitForm()
     {
         
//echo "<pre>";
         //print_r($_POST);
        // print_r($_FILES);
        // exit;
            
if(!empty($_SERVER['HTTP_X_REQUESTED_WITH']) && strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) == 'xmlhttprequest')
            {
                if(empty(
$_POST['accessories_name']))
                {
                    echo 
json_encode(array("success"=>false,"msg"=>"Please enter accessories name.."));
                    exit;
                }
                
                
$chk_condition "accessories_name = '".trim($_POST['accessories_name'])."'";
                if(!empty(
$_POST['accessories_id']))
                {
                    
$chk_condition .= " AND accessories_id != ".$_POST['accessories_id']." ";
                }
                
                
$chk_result $this->accessoriesmodel->getData("tbl_accessories_master",$chk_condition);
                
//print_r($chk_result)."gfhfgshgf";
                //echo "</br>".$chk_condition;
                //exit;
                
if(!empty($chk_result[0]['accessories_name']))
                {
                    echo 
json_encode(array("success"=>false,"msg"=>"Record Already Exist.."));
                    exit;
                }
                
                
$data = array();
                
$data['accessories_name'] = $_POST['accessories_name'];
                
$accessories_link = (str_replace(' ''-'strtolower(trim($_POST['accessories_name']))));
                
$data['link'] = $accessories_link;
                
//$data['accessories_image'] = $thumnail_value;
                
$data['accessories_description'   htmlentities($_POST['accessories_description']);
                
                
//banner image
                
if(isset($_FILES) && isset($_FILES["accessories_banner_image"]["name"]))
                {
                    if(!empty(
$_POST['accessories_id']))
                    {
                        
$prev_banner $this->accessoriesmodel->getFormdata($_POST['accessories_id']);
                        if(!empty(
$prev_banner[0]->accessories_banner_image))
                        {
                            
$prev_banner_path DOC_ROOT_FRONT."/images/accessories_images/".$prev_banner[0]->accessories_banner_image;
                            if(
file_exists($prev_banner_path))
                            {
                                
unlink($prev_banner_path);
                            }
                        }
                    }
                    
                    
$this->load->library('upload');
                    
                    
$config = array();
                    
$config['upload_path'] = DOC_ROOT_FRONT."/images/accessories_images/";
                    
$config['max_size'      '1000000';
                    
$config['allowed_types'] = 'gif|jpg|png';
                    
//$config['file_name']     = md5(uniqid("100_ID", true));
                    
                    
$this->upload->initialize($config);
                    
                    if (!
$this->upload->do_upload("accessories_banner_image"))
                    {
                        
$image_error = array('error' => $this->upload->display_errors());
                        echo 
json_encode(array("success"=>false,"msg"=>$image_error['error']));
                        exit;
                    }
                    else
                    {
                        
$image_data = array('upload_data' => $this->upload->data());
                        
$data['accessories_banner_image'] = $image_data['upload_data']['file_name'];
                    }
                }
                else
                {
                    
$data['accessories_banner_image'] = $_POST['input_accessories_banner_image'];
                }
                
                
                
//pdf file
                
if(isset($_FILES) && isset($_FILES["accessories_pdf_file"]["name"]))
                {
                    
                    if(!empty(
$_POST['accessories_id']))
                    {
                        
$prev_pdf $this->accessoriesmodel->getFormdata($_POST['accessories_id']);
                        if(!empty(
$prev_pdf[0]->accessories_pdf_file))
                        {
                            
$prev_pdf_path DOC_ROOT_FRONT."/images/accessories_images/pdf/".$prev_pdf[0]->accessories_pdf_file;
                            if(
file_exists($prev_pdf_path))
                            {
                                
unlink($prev_pdf_path);
                            }
                        }
                    }
                    
                    
$this->load->library('upload');
                    
                    
$config = array();
                    
$config['upload_path'] = DOC_ROOT_FRONT."/images/accessories_images/pdf/";
                    
$config['max_size'      '1000000';
                    
$config['allowed_types'] = 'pdf';
                    
//$config['file_name']     = md5(uniqid("100_ID", true));
                
                    
$this->upload->initialize($config);
                    
                    if (!
$this->upload->do_upload("accessories_pdf_file"))
                    {
                        
$image_error = array('error' => $this->upload->display_errors());
                        echo 
json_encode(array("success"=>false,"msg"=>$image_error['error']));
                        exit;
                    }
                    else
                    {
                        
$image_data = array('upload_data' => $this->upload->data());
                        
$data['accessories_pdf_file'] = $image_data['upload_data']['file_name'];
                    }
                }
                else
                {
                    
$data['accessories_pdf_file'] = $_POST['input_accessories_pdf_file'];
                }
                
                
//pdf file
                
if(isset($_FILES) && isset($_FILES["accessories_zip_file"]["name"]))
                {
                    
                    if(!empty(
$_POST['accessories_id']))
                    {
                        
$prev_zip $this->accessoriesmodel->getFormdata($_POST['accessories_id']);
                        if(!empty(
$prev_zip[0]->accessories_zip_file))
                        {
                            
$prev_zip_path DOC_ROOT_FRONT."/images/accessories_images/zip/".$prev_zip[0]->accessories_zip_file;
                            if(
file_exists($prev_zip_path))
                            {
                                
unlink($prev_zip_path);
                            }
                        }
                    }
                    
                    
$this->load->library('upload');
                    
                    
$config = array();
                    
$config['upload_path'] = DOC_ROOT_FRONT."/images/accessories_images/zip/";
                    
$config['max_size'      '1000000';
                    
$config['allowed_types'] = 'zip';
                    
//$config['file_name']     = md5(uniqid("100_ID", true));
                
                    
$this->upload->initialize($config);
                    
                    if (!
$this->upload->do_upload("accessories_zip_file"))
                    {
                        
$image_error = array('error' => $this->upload->display_errors());
                        echo 
json_encode(array("success"=>false,"msg"=>$image_error['error']));
                        exit;
                    }
                    else
                    {
                        
$image_data = array('upload_data' => $this->upload->data());
                        
$data['accessories_zip_file'] = $image_data['upload_data']['file_name'];
                    }
                }
                else
                {
                    
$data['accessories_zip_file'] = $_POST['input_accessories_zip_file'];
                }
                                
//pdf file
                
if(isset($_FILES) && isset($_FILES["accessories_fitting_pdf_file"]["name"]))
                {
                    
                    if(!empty(
$_POST['accessories_id']))
                    {
                        
$prev_pdf $this->accessoriesmodel->getFormdata($_POST['accessories_id']);
                        if(!empty(
$prev_pdf[0]->accessories_fitting_pdf_file))
                        {
                            
$prev_pdf_path DOC_ROOT_FRONT."/images/accessories_images/pdf/".$prev_pdf[0]->accessories_fitting_pdf_file;
                            if(
file_exists($prev_pdf_path))
                            {
                                
unlink($prev_pdf_path);
                            }
                        }
                    }
                    
                    
$this->load->library('upload');
                    
                    
$config = array();
                    
$config['upload_path'] = DOC_ROOT_FRONT."/images/accessories_images/pdf/";
                    
$config['max_size'      '1000000';
                    
$config['allowed_types'] = 'pdf';
                    
//$config['file_name']     = md5(uniqid("100_ID", true));
                
                    
$this->upload->initialize($config);
                    
                    if (!
$this->upload->do_upload("accessories_fitting_pdf_file"))
                    {
                        
$image_error = array('error' => $this->upload->display_errors());
                        echo 
json_encode(array("success"=>false,"msg"=>$image_error['error']));
                        exit;
                    }
                    else
                    {
                        
$image_data = array('upload_data' => $this->upload->data());
                        
$data['accessories_fitting_pdf_file'] = $image_data['upload_data']['file_name'];
                    }
                }
                else
                {
                    
$data['accessories_fitting_pdf_file'] = $_POST['input_accessories_fitting_pdf_file'];
                }
                
                
                
$error "";
                
                
$accessories_id "";
                if(!empty(
$_POST['accessories_id']))
                {
                    
$accessories_id $_POST['accessories_id'];
                    
                    
$data['created_by'] = $_SESSION["webadmin"][0]->user_id;
                    
$data['created_on'] =  date("Y-m-d H:i:s");
                    
$result $this->accessoriesmodel->updateData($_POST['accessories_id'],$data);
                    if(!empty(
$result))
                    {
                        
$error false;
                        
//echo json_encode(array('success'=>'1','msg'=>'Record Updated Successfully'));
                        //exit;
                    
}
                    else
                    {
                        
$error true;
                        
//echo json_encode(array('success'=>'0','msg'=>'Problem in data update.'));
                        //exit;
                    
}
                }
                else
                {
                    
$data['updated_by'] = $_SESSION["webadmin"][0]->user_id;
                    
$data['updated_on'] =  date("Y-m-d H:i:s");
                    
$result $this->accessoriesmodel->insertData('tbl_accessories_master',$data,'1');
                    if(!empty(
$result))
                    {
                        
$accessories_id $result;
                        
$error false;
                        
//echo json_encode(array("success"=>"1",'msg'=>'Record inserted Successfully.'));
                        //exit;
                    
}
                    else
                    {
                        
$error true;
                        
//echo json_encode(array("success"=>"0",'msg'=>'Problem in data insert.'));
                        //exit;
                    
}
                }
                
                if(!
$error)
                {
                    if(!empty(
$accessories_id))
                    {

                        
//accessories image
                        
if(isset($_FILES) && !empty($_POST["input_accessories_image"]) && !empty($_FILES["accessories_image"]))
                        {
                            
$this->load->library('upload');
                            foreach(
$_POST['input_accessories_image'] as $key => $val)
                            {
                                
$accessories_image               = array();
                                
$accessories_image['accessories_id'] = $accessories_id;
                            
                                
$config = array();
                                
$config['upload_path'] = DOC_ROOT_FRONT."/images/accessories_images/";
                                
$config['max_size'      '1000000';
                                
$config['allowed_types'] = 'gif|jpg|png';
                                
//$config['file_name']     = md5(uniqid("100_ID", true));
                            
                                
$_FILES['userfile']['name'    $_FILES['accessories_image']['name'][$key];
                                
$_FILES['userfile']['type'    $_FILES['accessories_image']['type'][$key];
                                
$_FILES['userfile']['tmp_name'] = $_FILES['accessories_image']['tmp_name'][$key];
                                
$_FILES['userfile']['error'   $_FILES['accessories_image']['error'][$key];
                                
$_FILES['userfile']['size'    $_FILES['accessories_image']['size'][$key];
                                
                                
$this->upload->initialize($config);
                            
                                if (!
$this->upload->do_upload("userfile"))
                                {
                                    
$image_error = array('error' => $this->upload->display_errors());
                                    echo 
json_encode(array("success"=>false,"msg"=>$image_error['error']));
                                    exit;
                                }
                                else
                                {
                                    
$image_data = array('upload_data' => $this->upload->data());
                                    
$accessories_image['accessories_image'] = $image_data['upload_data']['file_name'];
                                    
                                    
                                    
$accessories_image_result $this->accessoriesmodel->insertData('tbl_accessories_images',$accessories_image,'1');
                                }
                            }
                        }
                    }
                    echo 
json_encode(array("success"=>true,'msg'=>'Record add/update Successfully.'));
                    exit;
                }
                else
                {
                    echo 
json_encode(array("success"=>false,'msg'=>'Problem while add/update record..'));
                    exit;
                }
                
            }
     }
 
 
 function 
delRecord($id)
 {
 
   $data = array();
    
$data['status'] = "In-Active";
    
$appdResult $this->accessoriesmodel->delrecord("tbl_accessories_master","accessories_id",$id,$data);
     
        if(
$appdResult)
        {
            echo 
"1";
        }
        else
        {
            echo 
"2";    
        }    
 }
 
    function 
deleteAccImage()
    {
        
$accessories_image_id $_POST['accessories_image_id'];
        
$accessories_id $_POST['accessories_id'];
        if(!empty(
$accessories_image_id) && !empty($accessories_id))
        {
            
$condition "accessories_id = '".$accessories_id."' AND accessories_image_id = '".$accessories_image_id."' ";
            
$acc_image $this->accessoriesmodel->getData("tbl_accessories_images",$condition);
            
// echo "<pre>";
            // print_r($prod_drawing_image);
            // exit;
            
if(!empty($acc_image))
            {
                
$acc_image_path DOC_ROOT_FRONT."/images/accessories_images/".$acc_image[0]['accessories_image'];
                if(
file_exists($acc_image_path))
                {
                    if(!
unlink($acc_image_path))
                    {
                        echo 
json_encode(array("success"=>false'msg'=>"Error while deleting image.."));
                        exit;
                    }
                    else
                    {
                        
/* Delete record */
                        
$appdResult $this->accessoriesmodel->delrecords("tbl_accessories_images""accessories_image_id"$accessories_image_id); 
                        if(
$appdResult)
                        {
                            echo 
json_encode(array("success"=>true'msg'=>"Image Deleted Successfully"));
                            exit;
                        }
                        else
                        {
                            echo 
json_encode(array('success'=>false'msg'=>'Problem While Deleting Image..'));
                            exit;
                        }
                    }
                }
                else
                {
                    echo 
json_encode(array("success"=>false'msg'=>"Something Went Wrong!!"));
                    exit;
                }
            }
            else
            {
                echo 
json_encode(array("success"=>false'msg'=>"Something Went Wrong!!"));
                exit;
            }
        }
        else
        {
            echo 
json_encode(array("success"=>false'msg'=>"Something Went Wrong!!"));
            exit;
        }
    }    
    
}

?>
Reply




Theme © iAndrew 2016 - Forum software by © MyBB