Jump to content

Moorcam

Members
  • Posts

    189
  • Joined

  • Last visited

Posts posted by Moorcam

  1. Hi guys,

    I want to be able to dynamically change a site's color scheme using JS Colorpicker.

    I have the JS amd form etc all set and the color is updating in the database. However, I need to be able to use that database entry to change a color hex in a xss file.

    I am using Codeigniter 3

    Here is the HTML Form:

    <div class="tab-pane" id="color">
                                <?php echo form_open(base_url().'admin/setting/update',array('class' => 'form-horizontal')); ?>
                                <div class="box box-info">
                                    <div class="box-body">
                                        <div class="form-group">
                                            <label for="" class="col-sm-2 control-label">Color </label>
                                            <div class="col-sm-4">
                                                <input type="text" name="footer_bg" class="form-control jscolor" value="<?php echo $setting['footer_bg']; ?>">
                                            </div>
                                        </div>
                                        <div class="form-group">
                                            <label for="" class="col-sm-2 control-label"></label>
                                            <div class="col-sm-6">
                                                <button type="submit" class="btn btn-success pull-left" name="form_color">Update</button>
                                            </div>
                                        </div>
                                    </div>
                                </div>
                                <?php echo form_close(); ?>
                            </div>

    I have the following on the top of the style.php file:

    <?php
    header("Content-type: text/css");
    
    ?>

    This all works fine if I say have the following:

    <?php
    header("Content-type: text/css");
    $footer_bg = '#333';
    ?>
    
    background-color: <?php echo $footer_bg; ?>;

    However, if I try to call the color from settings database table it won't work.

    Can anyone help with this?

    Cheers,

    Dan

  2. Hi again folks,

    This really has me.

    I have changed to another method of importing, which includes uploading the file to the server and then reading it. Unfortunately, it isn't even uploading. Why? It baffles me as I have other uploads, such as images etc working just fine.

    Here is the controller now:

        function importcsv() {
            $data['fleet'] = $this->Model_fleet->show();
            $data['error'] = '';    //initialize image upload error array to empty
    
            $config['upload_path'] = './uploads/csv/';
            $config['allowed_types'] = 'csv';
            $config['max_size'] = '1000';
    
            $this->load->library('upload', $config);
    
    
            // If upload failed, display error
            if (!$this->upload->do_upload()) {
                $data['error'] = $this->upload->display_errors();
    
                $this->load->view('admin/fleet', $data);
            } else {
                $file_data = $this->upload->data();
                $file_path =  $file_data['./uploads/csv/'];
                
                if ($this->Csvimport->get_array($file_path)) {
                    $csv_array = $this->Csvimport->get_array($file_path);
                    foreach ($csv_array as $row) {
                        $insert_data = array(
                            'v_number'=>$row['v_number'],
                            'v_eng_number'=>$row['v_eng_number'],
    						'v_vin'=>$row['v_vin'],
    						'v_chassis'=>$row['v_chassis'],
    						'v_make'=>$row['v_make'],
    						'v_model'=>$row['v_model'],
    						'v_height'=>$row['v_height'],
    						'v_length'=>$row['v_length'],
    						'v_width'=>$row['v_width'],
                            'v_reg'=>$row['v_reg'],
    						'v_pax'=>$row['v_pax'],
    						'tacho_number'=>$row['tacho_number'],
    						'service_date'=>$row['service_date'],
    						'v_notes'=>$row['v_notes'],
    						'status'=>$row['status'],
                        );
                        $this->Model_fleet->insert_csv($insert_data);
                    }
                    $this->session->set_flashdata('success', 'Csv Data Imported Succesfully');
                    redirect(base_url().'csv');
                    echo "<pre>"; print_r($insert_data);
                } else 
                    $data['error'] = "Error occured";
                    $this->load->view('admin/fleet', $data);
                }
                
            } 
     

    Here is the Model:

        function insert_csv($data) {
            $this->db->insert('tbl_fleet', $data);
        }

    And the form (view):

    										<!-- Modal Start -->
    										<div class="modal fade" id="myModalDetail" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
    											<div class="modal-dialog" style="width:900px;">
    												<div class="modal-content">
    													<div class="modal-header">
    														<button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>
    														<h4 class="modal-title" id="myModalLabel">
    															Import Fleet
    														</h4>
    													</div>
    													<div class="modal-body">
    														<div class="form-group">
    														<h3>Upload Fleet</h3>
    														<h4>From the options below, you can simply import your fleet quickly by uploading a spreadsheet/</h4>
    <label for="exampleInputFile">Upload Excel or CSV</label>
                    <?php echo form_open_multipart(base_url().'admin/fleet',array('class' => 'form-horizontal')); ?>
                        <input type="file" name="file" ><br><br>
                        <input type="submit" name="submit" value="UPLOAD" class="btn btn-primary">
                    <?php echo form_close(); ?>
    </div>
    													</div>
    												</div>
    											</div>
    										</div>
    
    										<!-- Modal End -->

    I am also using the csvimport Library as below, which is saved in libraries/Csvimport.php

    <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); 
    
    
    /**
     * CodeIgniter CSV Import Class
     *
     * This library will help import a CSV file into
     * an associative array.
     * 
     * This library treats the first row of a CSV file
     * as a column header row.
     * 
     *
     * @package         CodeIgniter
     * @subpackage      Libraries
     * @category        Libraries
     * @author          Brad Stinson
     */
    
    class Csvimport {
    
    	private $filepath = "";
        private $handle = "";
        private $column_headers = array();
    
       /**
         * Function that parses a CSV file and returns results
         * as an array.
         *
         * @access  public
         * @param   filepath        string  Location of the CSV file
         * @param   column_headers  array   Alternate values that will be used for array keys instead of first line of CSV
         * @param   detect_line_endings  boolean  When true sets the php INI settings to allow script to detect line endings. Needed for CSV files created on Macs.
         * @return  array
         */
        public function get_array($filepath='', $column_headers='', $detect_line_endings=FALSE)
        {
            global $column_headers;
            // If true, auto detect row endings
            if($detect_line_endings){
                ini_set("auto_detect_line_endings", TRUE);
            }
    
            // If file exists, set filepath
            if(file_exists($filepath))
            {
                $this->_set_filepath($filepath);
            }
            else
            {
                return FALSE;            
            }
    
            // If column headers provided, set them
            $this->_set_column_headers($column_headers);
    
            // Open the CSV for reading
            $this->_get_handle();
            
            $row = 0;
    
            while (($data = fgetcsv($this->handle, 0, ",")) !== FALSE) 
            {   
                // If first row, parse for column_headers
                if($row == 0)
                {
                    // If column_headers already provided, use them
                    if($this->column_headers)
                    {
                        foreach ($this->column_headers as $key => $value)
                        {
                            $column_headers[$key] = trim($value);
                        }
                    }
                    else // Parse first row for column_headers to use
                    {
                        foreach ($data as $key => $value)
                        {
                           $column_headers[$key] = trim($value);
    
                        }   
    
                    }          
                }
                else
                {
                    $new_row = $row - 1; // needed so that the returned array starts at 0 instead of 1
                    foreach($column_headers as $key => $value) // assumes there are as many columns as their are title columns
                    {
                        $result[$new_row][$value] = trim($data[$key]);
                    }
                }
                $row++;
            }
     
            $this->_close_csv();
    
            return $result;
        }
    
       /**
         * Sets the filepath of a given CSV file
         *
         * @access  private
         * @param   filepath    string  Location of the CSV file
         * @return  void
         */
        private function _set_filepath($filepath)
        {
            $this->filepath = $filepath;
        }
    
       /**
         * Sets the alternate column headers that will be used when creating the array
         *
         * @access  private
         * @param   column_headers  array   Alternate column_headers that will be used instead of first line of CSV
         * @return  void
         */
        private function _set_column_headers($column_headers='')
        {
            if(is_array($column_headers) && !empty($column_headers))
            {
                $this->column_headers = $column_headers;
            }
        }
    
       /**
         * Opens the CSV file for parsing
         *
         * @access  private
         * @return  void
         */
        private function _get_handle()
        {
            $this->handle = fopen($this->filepath, "r");
        }
    
       /**
         * Closes the CSV file when complete
         *
         * @access  private
         * @return  array
         */
        private function _close_csv()
        {
            fclose($this->handle);
        }    
    }

    If anyone can please provide any idea I would love you forever.

    There are no errors etc, just nothing happening.

    Cheers,

    Dan

  3. 1 hour ago, maxxd said:

    Where? You posted this:

    echo "<pre>"; print_r($this->Model_fleet->saverecords);

    This won't actually run the saverecords() method, and even if it did it's now passing no arguments instead of 15. Instead of passing all the individual properties of saverecords() to insert(), create an array of them and pass that to the db->insert() method.

    Before you do that, though, echo every row of the csv file to the browser to make sure your problem is with storage and not input. If the script can't find the input file it certainly won't be able to store the records to the database.

    Posted a few posts back, sorry.

        function saverecords($v_number,$v_eng_number,$v_vin,$v_chassis,$v_make,$v_model,$v_height,$v_length,$v_width,$v_reg,$v_pax,$tacho_number,$service_date,$v_notes,$status) 
    	{
            $this->db->insert('tbl_fleet',$v_number,$v_eng_number,$v_vin,$v_chassis,$v_make,$v_model,$v_height,$v_length,$v_width,$v_reg,$v_pax,$tacho_number,$service_date,$v_notes,$status);
            return $this->db->insert_id();
        }

     

    Anyway, I have tried the following. It shows no result at all.

    public function import_data(){
    	if(isset($_POST['file']))
    	{
        $csv = $_FILES['file']['tmp_name'];
        $handle = fopen($csv,"r");
        while (($row = fgetcsv($handle, 10000, ",")) != FALSE)
        {
            $num = count($data);
            echo "<p> $num fields in line $row: <br /></p>\n";
            $row++;
            for ($c=0; $c < $num; $c++) {
                echo $data[$c] . "<br />\n";
            }
        }
        fclose($handle);
    }
    }

     

  4. 22 minutes ago, maxxd said:

    The other concern is that db->insert() takes two parameters - one string and one array - but you're passing 16 string parameters to it. That's one parameter that matches expected type, one parameter that doesn't match type, and 14 parameters that are ignored entirely.

    I have amended that in my last reply.

    I have tried other methods as well, uploading the file to /uploads/ and trying to read it from there. Can't even get the bloody thing to upload.

    I give up. Thanks anyway.

  5. 1 hour ago, maxxd said:

    From the documentation for db->insert:

    Have you tried catching the return value from the method and echoing it out during the loop? Or even just echoing the values on each loop so you know that you're dealing with what you think you're dealing with?

    I am running this just after the success set_flashdata:

    echo "<pre>"; print_r($this->Model_fleet->saverecords);

    Nothing

  6. 28 minutes ago, maxxd said:

    I kinda feel like you need to load the model and tell it to connect to the database before you try to do anything with it? I think the last time I worked with CI was like 3 years ago? It was slightly before the onset of covid, so I may be completely wrong.

    Don't think so. I am already updating, adding, deleting data from that same Model. I have also modified the Model code for the saverecords so it is similar to others I am using:

        function saverecords($v_number,$v_eng_number,$v_vin,$v_chassis,$v_make,$v_model,$v_height,$v_length,$v_width,$v_reg,$v_pax,$tacho_number,$service_date,$v_notes,$status) 
    	{
            $this->db->insert('tbl_fleet',$v_number,$v_eng_number,$v_vin,$v_chassis,$v_make,$v_model,$v_height,$v_length,$v_width,$v_reg,$v_pax,$tacho_number,$service_date,$v_notes,$status);
            return $this->db->insert_id();
        }

    My feeling is it is with the controller part posted below. I am just at a complete loss with it.

     

    	public function importdata()
    	{ 
    		$this->load->view('view_fleet');
    		if(isset($_POST["submit"]))
    		{
    			$file = $_FILES['file']['tmp_name'];
    			$handle = fopen($file, "r");
    			$c = 0;//
    			while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    			{
    				$v_number = $filesop[0];
    				$v_eng_number = $filesop[1];
    				$v_vin = $filesop[2];
    				$v_chassis = $filesop[3];
    				$v_make = $filesop[4];
    				$v_model = $filesop[5];
    				$v_height = $filesop[6];
    				$v_length = $filesop[7];
    				$v_width = $filesop[8];
    				$v_reg = $filesop[9];
    				$v_pax = $filesop[10];
    				$tacho_number = $$filesop[11];
    				$service_date = $filesop[12];
    				$v_notes = $filesop[13];
    				$status = $filesop[14];
    				if($c<>0){					/* SKIP THE FIRST ROW */
    					$this->Model_fleet->saverecords(
    					$v_number,
    					$v_eng_number,
    					$v_vin,
    					$$v_chassis,
    					$v_make,
    					$v_model,
    					$v_height,
    					$v_length,
    					$v_width,
    					$v_reg,
    					$v_pax,
    					$tacho_number,
    					$service_date,
    					$v_notes,
    					$status
    					);
    				}
    				$c = $c + 1;
    			}
    			echo "sucessfully import data !";
    			
    				
    		}else{
    		    	$this->session->set_flashdata('error',$error);
    		    	redirect(base_url().'admin/fleet/edit/'.$id);
    		}
    	}

     

  7. 18 minutes ago, maxxd said:

    It's been a few years since I used CodeIgniter, but it certainly looks like you're passing 15 parameters (one of which is a dynamic variable) to a method that accepts 2 parameters. If you try to dump anything other than $v_number or $v_reg from your save_records() method, what do you get?

    Same thing.

    I also sorted the 15 against the 2 variables (missed that) and also still the same.

  8. Hi guys,

    Trying to implement an import function in Codeigniter 3 but not working.

    There are no errors, just get this screen:

    An Error Was Encountered

    The action you have requested is not allowed.

    Here is the Controller part:

    	public function importdata()
    	{ 
    		$this->load->view('view_fleet');
    		if(isset($_POST["submit"]))
    		{
    			$file = $_FILES['file']['tmp_name'];
    			$handle = fopen($file, "r");
    			$c = 0;//
    			while(($filesop = fgetcsv($handle, 1000, ",")) !== false)
    			{
    				$v_number = $filesop[0];
    				$v_eng_number = $filesop[1];
    				$v_vin = $filesop[2];
    				$v_chassis = $filesop[3];
    				$v_make = $filesop[4];
    				$v_model = $filesop[5];
    				$v_height = $filesop[6];
    				$v_length = $filesop[7];
    				$v_width = $filesop[8];
    				$v_reg = $filesop[9];
    				$v_pax = $filesop[10];
    				$tacho_number = $$filesop[11];
    				$service_date = $filesop[12];
    				$v_notes = $filesop[13];
    				$status = $filesop[14];
    				if($c<>0){					/* SKIP THE FIRST ROW */
    					$this->Model_fleet->saverecords(
    					$v_number,
    					$v_eng_number,
    					$v_vin,
    					$$v_chassis,
    					$v_make,
    					$v_model,
    					$v_height,
    					$v_length,
    					$v_width,
    					$v_reg,
    					$v_pax,
    					$tacho_number,
    					$service_date,
    					$v_notes,
    					$status
    					);
    				}
    				$c = $c + 1;
    			}
    			echo "sucessfully import data !";
    				
    		}
    	}

    Now the Model part:

    	function saverecords($v_number,$v_reg)
    	{
    		$query="insert into tbl_fleet values('$v_number','$v_eng_number','$v_vin','$v_chassis','$v_make','$v_model','$v_height','$v_length','$v_width','$v_reg','$v_pax','$tacho_number','$service_date','$v_notes,$status')";
    		$this->db->query($query);
    	}

    And finally the View part:

    <label for="exampleInputFile">Upload Excel or CSV</label>
    <form enctype="multipart/form-data" method="post" role="form">
    <div class="form-group">
    <input type="file" name="file" id="file" size="150">
    <p class="help-block">Only Excel/CSV File Import.</p>
    </div>
    <button type="submit" class="btn btn-default" name="submit" value="submit">Upload</button>
    </form>
    </div>

    Please note that the View section is infact in a Modal. Not sure if that's the issue?

     

    If anyone can help with this I will send you some Guinness.

    Thanks.

  9. If anyone else wants this, here's what solved it for me...

    document.getElementById("printButton1").addEventListener("click", function() {
         var printContents = document.getElementById('invoice').innerHTML;
         var originalContents = document.body.innerHTML;
    	 var printButton = document.getElementById("printButton1");
    	 var closeButton = document.getElementById("closeButton1");
    	 
         document.body.innerHTML = printContents;
    	 
    document.getElementById('printButton1').style.visibility = 'hidden';
         window.print();
    
         document.body.innerHTML = originalContents;
    
    	 window.location.reload(true);
    });

     

  10. I have changed the above code and tried this, adding a link to the CSS file of the website...

    function printDiv() 
    {
    
      var divToPrint=document.getElementById('invoice');
      var newWin=window.open('','Print-Window');
      var cssURL='<?php echo base_url('public/css/style.css'); ?>';
    var printButton = document.getElementById("printButton1");
    var closeButton = document.getElementById("closeButton1");
    
      newWin.document.open();
    printButton.style.visibility = 'hidden';
    closeButton.style.visibility = 'hidden';
      newWin.document.write('<html><head><link href="'+cssURL+'" rel="stylesheet" type="text/css"></head><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
     printButton.style.visibility = 'visible';
     closeButton.style.visibility = 'visible';
      newWin.document.close();
    
      setTimeout(function(){newWin.close();},10);
    
    }

    Still not getting any styling in the print dialog.

  11. Hi folks,

    I am using the following JS to print the contents of a Div. However, when I click on Print, the print preview window opens but the document has no styling.

    function printDiv() 
    {
    
      var divToPrint=document.getElementById('invoice');
    
      var newWin=window.open('','Print-Window');
    
    var printButton = document.getElementById("printButton1");
    var closeButton = document.getElementById("closeButton1");
    
      newWin.document.open();
    printButton.style.visibility = 'hidden';
    closeButton.style.visibility = 'hidden';
      newWin.document.write('<html><body onload="window.print()">'+divToPrint.innerHTML+'</body></html>');
     printButton.style.visibility = 'visible';
     closeButton.style.visibility = 'visible';
      newWin.document.close();
    
      setTimeout(function(){newWin.close();},10);
    
    
    }

    Button for printing:

    <input type="button" id="printButton1" class="btn btn-success" onclick="printDiv('invoice')" value="Print Invoice" />

    Is there something wrong here or am I just missing something?

    Cheers,

    Dan

  12. 38 minutes ago, maxxd said:

    It's been a while since I've used CodeIgniter - especially an older version - but I don't see where you've loaded the URL helper. Check here for more information.

    Hey man,

    Yeah it's in the Controller:

               

    			$this->load->view('traveller/view_testimonial_add',$data);

    Yeah I'm still learning it. Went for an older version because I was told it's easier. It is but this has me stumped.

  13. Hi all,

    Hope you are all well and Happy New Year. ;)

    Now, I have pulled hair out with this since last night. When I click on Add Testimonial it should open traveller/testimonial/add, which has a file name of view_traveller_testimonial_add.php

     

    But, when I click on the button to add a new Testimonial, it doesn't work. Just refreshed the page.

    Any ideas?

     

    Here is this file:

    <section class="content-header">
    	<div class="content-header-left">
    		<h1>Add Testimonial</h1>
    	</div>
    	<div class="content-header-right">
    		<a href="<?php echo base_url(); ?>traveller/testimonial" class="btn btn-primary btn-sm">View All</a>
    	</div>
    </section>
    
    
    <section class="content">
    
    	<div class="row">
    		<div class="col-md-12">
    
    			<?php
    			if($this->session->flashdata('error')) {
    				?>
    				<div class="callout callout-danger">
    					<p><?php echo $this->session->flashdata('error'); ?></p>
    				</div>
    				<?php
    			}
    			if($this->session->flashdata('success')) {
    				?>
    				<div class="callout callout-success">
    					<p><?php echo $this->session->flashdata('success'); ?></p>
    				</div>
    				<?php
    			}
    			?>
    
    	<?php echo form_open_multipart(base_url().'traveller/testimonial/add',array('class' => 'form-horizontal')); ?>
    				<div class="box box-info">
    					<div class="box-body">
    						<div class="form-group">
    							<label for="" class="col-sm-2 control-label">Name <span>*</span></label>
    							<div class="col-sm-6">
    								<input type="text" class="form-control" name="name" value="<?php echo $d_arr['traveller_name']; ?>">
    							</div>
    						</div>
    						<div class="form-group">
    							<label for="" class="col-sm-2 control-label">Website</label>
    							<div class="col-sm-6">
    								<input type="url" autocomplete="off" class="form-control" name="url" value="">
    							</div>
    						</div>				
    						<div class="form-group">
    							<label for="" class="col-sm-2 control-label">Comment <span>*</span></label>
    							<div class="col-sm-6">
    								<textarea class="form-control" name="comment" style="height:200px;"></textarea>
    							</div>
    						</div>
    
    							<div class="col-sm-6" hidden>
    							<label for="" class="col-sm-2 control-label">Status</label>
    								<input type="text" autocomplete="off" class="form-control" name="status" value="Pending">
    							</div>
    						</div>
    						<div class="form-group">
    							<label for="" class="col-sm-2 control-label"></label>
    							<div class="col-sm-6">
    								<button type="submit" class="btn btn-success pull-left" name="form1">Submit</button>
    							</div>
    						</div>
    					</div>
    				</div>
    			<?php echo form_close(); ?>
    		</div>
    	</div>
    
    </section>

    Here is the file that has the button to open the above page:

    <div class="dashboard-area bg-area pt_50 pb_80">
    	<div class="container wow fadeIn">
    		<div class="row">
    			<div class="col-md-3 col-sm-12 wow fadeIn" data-wow-delay="0.1s">
    				<div class="option-board mt_30">
    					<ul>
    						<?php $this->view('view_traveller_sidebar'); ?>
    					</ul>
    				</div>
    			</div>
    			<div class="col-md-9 col-sm-12 wow fadeIn" data-wow-delay="0.2s">
    				<div class="box-body table-responsive">
    
    					<h1>My Testimonials</h1>
    					<table id="example1" class="table table-bordered table-striped">
    						<thead>
    							<tr>
    								<th>Serial</th>
    								<th>Name</th>
    								<th>Comment</th>
    							</tr>
    						</thead>
    						<tbody>
    							<?php
    							$i=0;
    							foreach ($my_testimonial as $row) {
    								$i++;
    
    								$CI =& get_instance();
    								$CI->load->model('Model_testimonial');
    								$CI->load->model('Model_traveller');
    								$d_arr = $CI->Model_traveller->get_traveller_name_by_id($row['traveller_id']);
    								
    
    								?>
    								<tr>
    									<td><?php echo $i; ?></td>
    									<td><?php echo $d_arr['traveller_name']; ?></td>
    									<td><?php echo $row['comment']; ?></td>
    								</tr>
    								<?php
    							}
    							?>
    							
    						</tbody>
    					</table>
    					<div class="box-body">
    					<a href="<?php echo base_url(); ?>traveller/testimonial/add" class="btn btn-primary btn-sm">Add Testimonial</a>
    					</div>
    				</div>
    			</div>
    		</div>
    	</div>
    </div>

    Here is my Controller:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Testimonial extends CI_Controller 
    {
    	function __construct() 
    	{
            parent::__construct();
            $this->load->model('traveller/Model_common');
            $this->load->model('traveller/Model_testimonial');
        }
    
    	public function index()
    	{
    		$data['setting'] = $this->Model_common->get_setting_data();
    
    		$data['testimonial'] = $this->Model_testimonial->show();
    
    		$this->load->view('traveller/view_header',$data);
    		$this->load->view('traveller/view_testimonial',$data);
    		$this->load->view('traveller/view_footer');
    	}
    
    	public function add()
    	{
    		$data['setting'] = $this->Model_common->get_setting_data();
    
    		$error = '';
    		$success = '';
    
    		if(isset($_POST['form1'])) {
    
    			$valid = 1;
    
    			$this->form_validation->set_rules('name', 'Name', 'trim|required');
    			$this->form_validation->set_rules('comment', 'Comment', 'trim|required');
    
    			if($this->form_validation->run() == FALSE) {
    				$valid = 0;
                    $error .= validation_errors();
                }
    
    
    		    if($valid == 1) 
    		    {
    				$next_id = $this->Model_testimonial->get_auto_increment_id();
    				foreach ($next_id as $row) {
    		            $ai_id = $row['Auto_increment'];
    		        }
    
    		        $form_data = array(
    					'name'        => $_POST['name'],
    					'url' => $_POST['url'],
    					'status' => $_POST['status'],
    					'comment'     => $_POST['comment']
    	            );
    	            $this->Model_testimonial->add($form_data);
    
    		        $success = 'Testimonial is added successfully!';
    		        $this->session->set_flashdata('success',$success);
    				redirect(base_url().'traveller/testimonial');
    		    } 
    		    else
    		    {
    		    	$this->session->set_flashdata('error',$error);
    				redirect(base_url().'traveller/testimonial/add');
    		    }
                
            } else {
                
                $this->load->view('traveller/view_header',$data);
    			$this->load->view('traveller/view_testimonial_add',$data);
    			$this->load->view('traveller/view_footer');
            }
    		
    	}
    
    
    	public function edit($id)
    	{
    		
        	// If there is no testimonial in this id, then redirect
        	$tot = $this->Model_testimonial->testimonial_check($id);
        	if(!$tot) {
        		redirect(base_url().'traveller/testimonial');
            	exit;
        	}
           	
           	$data['setting'] = $this->Model_common->get_setting_data();
    		$error = '';
    		$success = '';
    
    
    		if(isset($_POST['form1'])) 
    		{
    
    			$valid = 1;
    
    			$this->form_validation->set_rules('name', 'Name', 'trim|required');
    			$this->form_validation->set_rules('comment', 'Comment', 'trim|required');
    
    			if($this->form_validation->run() == FALSE) {
    				$valid = 0;
                    $error .= validation_errors();
                }
    
    		    if($valid == 1) 
    		    {
    		    	$data['testimonial'] = $this->Model_testimonial->get_testimonial($id);
    			$form_data = array(
    						'name'        => $_POST['name'],
    						'url' => $_POST['url'],
    						'status' => $_POST['status'],
    						'comment'     => $_POST['comment']
    		            );
    					$this->Model_testimonial->update($id,$form_data);
    				
    				$success = 'Testimonial is updated successfully';
    				$this->session->set_flashdata('success',$success);
    				redirect(base_url().'traveller/testimonial');
    		    }
    		    else
    		    {
    		    	$this->session->set_flashdata('error',$error);
    				redirect(base_url().'traveller/testimonial/edit'.$id);
    		    }
               
    		} else {
    			$data['testimonial'] = $this->Model_testimonial->get_testimonial($id);
    	       	$this->load->view('traveller/view_header',$data);
    			$this->load->view('traveller/view_testimonial_edit',$data);
    			$this->load->view('traveller/view_footer');
    		}
    
    	}
    
    
    	public function delete($id) 
    	{
    		// If there is no testimonial in this id, then redirect
        	$tot = $this->Model_testimonial->testimonial_check($id);
        	if(!$tot) {
        		redirect(base_url().'traveller/testimonial');
            	exit;
        	}
    
            $data['testimonial'] = $this->Model_testimonial->get_testimonial($id);
            if($data['testimonial']) {
                unlink('./public/uploads/'.$data['testimonial']['photo']);
            }
    
            $this->Model_testimonial->delete($id);
            $success = 'Testimonial is deleted successfully';
    		$this->session->set_flashdata('success',$success);
            redirect(base_url().'traveller/testimonial');
        }
    
    }

    And my Model:

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Model_testimonial extends CI_Model 
    {
    
    	function get_auto_increment_id()
        {
            $sql = "SHOW TABLE STATUS LIKE 'tbl_testimonial'";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
    	
        function show() {
            $sql = "SELECT * FROM tbl_testimonial ORDER BY id ASC";
            $query = $this->db->query($sql);
            return $query->result_array();
        }
    
        function add($data) {
            $this->db->insert('tbl_testimonial',$data);
            return $this->db->insert_id();
        }
    
        function update($id,$data) {
            $this->db->where('id',$id);
            $this->db->update('tbl_testimonial',$data);
        }
    
        function delete($id)
        {
            $this->db->where('id',$id);
            $this->db->delete('tbl_testimonial');
        }
    
        function get_testimonial($id)
        {
            $sql = 'SELECT * FROM tbl_testimonial WHERE id=?';
            $query = $this->db->query($sql,array($id));
            return $query->first_row('array');
        }
    
        function testimonial_check($id)
        {
            $sql = 'SELECT * FROM tbl_testimonial WHERE id=?';
            $query = $this->db->query($sql,array($id));
            return $query->first_row('array');
        }
        
    }

    Really appreciate the help guys. Thanks in advance.

  14. Hi mate,

     

    Sorry. Made a few changes but the above was the culprit really. Here is the full update.

    Controller:

    	public function index()
    	{
    		$data['setting'] = $this->Model_common->get_setting_data();
    		
    		$data['tour'] = $this->Model_dashboard->all_tour('p_id');
    	
    
    		$data['total_category'] = $this->Model_dashboard->show_total_category();
    		$data['total_news'] = $this->Model_dashboard->show_total_news();
    		$data['total_motel'] = $this->Model_dashboard->show_total_motel();
    		$data['total_tour'] = $this->Model_dashboard->show_total_tour();
    		$data['total_team_member'] = $this->Model_dashboard->show_total_team_member();
    		$data['total_agent'] = $this->Model_dashboard->show_total_agent();
    		$data['total_service'] = $this->Model_dashboard->show_total_service();
    		$data['total_testimonial'] = $this->Model_dashboard->show_total_testimonial();
    		$data['total_traveller'] = $this->Model_dashboard->show_total_traveller();
    		$data['amount_paid'] = $this->Model_dashboard->show_amount_paid();
    		$data['total_fleet'] = $this->Model_dashboard->show_total_fleet();
    		$data['total_driver'] = $this->Model_dashboard->show_total_driver();
    		
    
    
    		$this->load->view('admin/view_header',$data);
    		$this->load->view('admin/view_dashboard',$data);
    		$this->load->view('admin/view_footer');
    	}
    

    Model:

        public function all_tour()
        {
    		$e_id = $this->session->userdata('e_id');
            $query = $this->db->query("SELECT * FROM tbl_tour WHERE e_id = $e_id ORDER BY p_id ASC");
            return $query->result_array();
        }

    View:

    <table class="table table-striped">
        <tr>
            <th>Tour</th>
    		<th>Departure Date</th>
    		<th>Return Date</th>
    		<th>Fleet Number</th>
            
        </tr>
            <?php
                foreach($tour as $row):?>
        <tr>
            <td><?php echo $row['p_name']; ?></td>
    		<td><?php echo $row['p_start_date']; ?></td>
    		<td><?php echo $row['p_end_date']; ?></td>	
    		<td><?php echo $row['v_number']; ?></td>
        </tr>
            <?php endforeach;?>
    </table>

     

  15. So, when a user logs in, I would like to grab data from the db and display it based on their id, which is in a session.

    Here is my Controller:

    public function jobs()
        {
            if($this->session->userdata('e_id')){
                $id = $this->session->userdata('e_id');
                $data['tour'] = $this->usermodel->job($id);
                $this->load->view('tour',$data);
            }else{
                echo "No results";
    
            }
    
        }

    Here is my Model:

    public function job($id) {
        $this->db->select('*')
                 ->from('tbl_tour')
                 ->where('e_id', $id);
        return $this->db->get()->result();
    }

    And finally, View:

    <table class="table table-striped">
        <tr>
            <th>Date</th>
            <th>Tour</th>
        </tr>
            <?php
                foreach($tour as $job):?>
        <tr>
            <td><?php echo $job->start_date;?></td>
            <td><?php echo $job->name;?></td>
        </tr>
            <?php endforeach;?>
    </table>

    When executing I get Undefined variable: tour

    And also:

    Message: Invalid argument supplied for foreach()

    If anyone can help with this it would be appreciated.

  16. Fixed.

    Was not getting id from database.

    So, changed

        function update($id) {
            $this->db->where('id',$id);
            $this->db->update('tbl_team_member',$data);
        }

    to this

        function update($data) {
            $this->db->where('id',$this->session->userdata('id'));
            $this->db->update('tbl_team_member',$data);
        }

    Just in case anyone else runs into same issue.

  17. Hi guys,

    Been fighting with this and can't get it to work.

    Basically, on submit, session is being updated but data is not being sent to the database.

    Here is my view_profile.php

    <?php
    if(!$this->session->userdata('id')) {
    	redirect(base_url().'admin');
    }
    ?>
    <section class="content-header">
    	<div class="content-header-left">
    		<h1>Edit Profile</h1>
    	</div>
    </section>
    
    <section class="content" style="min-height:auto;margin-bottom: -30px;">
    	<div class="row">
    		<div class="col-md-12">
    			<?php
    			if($this->session->flashdata('error')) {
    				?>
    				<div class="callout callout-danger">
    					<p><?php echo $this->session->flashdata('error'); ?></p>
    				</div>
    				<?php
    			}
    			if($this->session->flashdata('success')) {
    				?>
    				<div class="callout callout-success">
    					<p><?php echo $this->session->flashdata('success'); ?></p>
    				</div>
    				<?php
    			}
    			?>
    		</div>
    	</div>
    </section>
    
    
    <section class="content">
    	<div class="row">
    		<div class="col-md-12">
    				
    			<div class="nav-tabs-custom">
    				<ul class="nav nav-tabs">
    					<li class="active"><a href="#tab_1" data-toggle="tab">Update Information</a></li>
    					<li><a href="#tab_2" data-toggle="tab">Update Photo</a></li>
    					<li><a href="#tab_3" data-toggle="tab">Update Password</a></li>
    				</ul>
    				<div class="tab-content">
    
          				<div class="tab-pane active" id="tab_1">						
    						<?php echo form_open(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
    						<div class="box box-info">
    							<div class="box-body">
    
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label">Display Name <span>*</span></label>
    									<div class="col-sm-4">
    										<input type="text" class="form-control" name="name" value="<?php echo $this->session->userdata('name'); ?>">
    									</div>			
    								</div>
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label">Email Address <span>*</span></label>
    									<div class="col-sm-4">
    										<input type="text" class="form-control" name="email" value="<?php echo $this->session->userdata('email'); ?>">
    									</div>			
    								</div>
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label">Phone <span>*</span></label>
    									<div class="col-sm-4">
    										<input type="text" class="form-control" name="phone" value="<?php echo $this->session->userdata('phone'); ?>">
    									</div>			
    								</div>
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label"></label>
    									<div class="col-sm-6">
    										<button type="submit" class="btn btn-success pull-left" name="form1">Update Information</button>
    									</div>
    								</div>
    							</div>
    						</div>
    						<?php echo form_close(); ?>
          				</div>
    
    
          				<div class="tab-pane" id="tab_2">
    						<?php echo form_open_multipart(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
    						<div class="box box-info">
    							<div class="box-body">
    								<div class="form-group">
    						            <label for="" class="col-sm-2 control-label">Existing Photo</label>
    						            <div class="col-sm-6" style="padding-top:6px;">
    						            	<?php if($this->session->userdata('photo') ==''): ?>
    						            		<img src="<?php echo base_url(); ?>public/img/no-photo.jpg" class="existing-photo" alt="profile photo" width="140">
    						            	<?php else: ?>
    						            		 <img src="<?php echo base_url(); ?>public/uploads/<?php echo $this->session->userdata('photo'); ?>" class="existing-photo" width="140">
    						            	<?php endif; ?>							                
    						            </div>
    						        </div>
    								<div class="form-group">
    						            <label for="" class="col-sm-2 control-label">New Photo</label>
    						            <div class="col-sm-6" style="padding-top:6px;">
    						                <input type="file" name="photo">
    						            </div>
    						        </div>
    						        <div class="form-group">
    									<label for="" class="col-sm-2 control-label"></label>
    									<div class="col-sm-6">
    										<button type="submit" class="btn btn-success pull-left" name="form2">Update Photo</button>
    									</div>
    								</div>
    							</div>
    						</div>
    						<?php echo form_close(); ?>
          				</div>
    
    
          				<div class="tab-pane" id="tab_3">
    						<?php echo form_open(base_url().'admin/profile/update',array('class' => 'form-horizontal')); ?>
    						<div class="box box-info">
    							<div class="box-body">
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label">Password </label>
    									<div class="col-sm-4">
    										<input type="password" class="form-control" name="password">
    									</div>
    								</div>
    								<div class="form-group">
    									<label for="" class="col-sm-2 control-label">Retype Password </label>
    									<div class="col-sm-4">
    										<input type="password" class="form-control" name="re_password">
    									</div>
    								</div>
    						        <div class="form-group">
    									<label for="" class="col-sm-2 control-label"></label>
    									<div class="col-sm-6">
    										<button type="submit" class="btn btn-success pull-left" name="form3">Update Password</button>
    									</div>
    								</div>
    							</div>
    						</div>
    						<?php echo form_close(); ?>
          				</div>
          			</div>
    			</div>
    		</div>
    	</div>
    </section>

    Here is my controller - Profile.php

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Profile extends CI_Controller 
    {
    	function __construct() 
    	{
            parent::__construct();
            $this->load->model('admin/Model_common');
            $this->load->model('admin/Model_profile');
        }
    	public function index()
    	{
    		$data['setting'] = $this->Model_common->get_setting_data();
    
    		$this->load->view('admin/view_header',$data);
    		$this->load->view('admin/view_profile',$data);
    		$this->load->view('admin/view_footer');
    		
    	}
    	public function update()
    	{
    		$error = '';
    		$success = '';
    
    		$data['setting'] = $this->Model_common->get_setting_data();
    
    		if(isset($_POST['form1'])) {
    
    			$valid = 1;
    
    			$this->form_validation->set_rules('email', 'Email Address', 'trim|required|valid_email');
    
    			if($this->form_validation->run() == FALSE) {
    				$valid = 0;
                    $error = validation_errors();
                }
    
                if($valid == 1) {
    	            $form_data = array(
    					'email'     => $_POST['email'],
    					'name'      => $_POST['name'],
    					'phone'     => $_POST['phone']
    	            );
    	        	$this->Model_profile->update($form_data);
    	        	$success = 'Profile Information is updated successfully!';
    	        	
    	        	$this->session->set_userdata($form_data);
    
    	        	$this->session->set_flashdata('success',$success);
    	        	redirect(base_url().'admin/profile');
                }
                else {
                	$this->session->set_flashdata('error',$error);
    	        	redirect(base_url().'admin/profile');
                }
    		}
    
    		if(isset($_POST['form2'])) {
    			$valid = 1;
    			$path = $_FILES['photo']['name'];
    		    $path_tmp = $_FILES['photo']['tmp_name'];
    		    if($path!='') {
    		        $ext = pathinfo( $path, PATHINFO_EXTENSION );
    		        $file_name = basename( $path, '.' . $ext );
    		        $ext_check = $this->Model_common->extension_check_photo($ext);
    		        if($ext_check == FALSE) {
    		            $valid = 0;
    		            $data['error'] = 'You must have to upload jpg, jpeg, gif or png file<br>';
    		        }
    		    } else {
    		    	$valid = 0;
    		        $data['error'] = 'You must have to select a photo<br>';
    		    }
    		    if($valid == 1) {
    		    	// removing the existing photo
    		    	unlink('./public/uploads/'.$this->session->userdata('photo'));
    
    		    	// updating the data
    		    	$final_name = 'user-'.'.'.$ext;
    		        move_uploaded_file( $path_tmp, './public/uploads/'.$final_name );
    		    			        
    				$form_data = array(
    					'photo' => $final_name
    	            );
    	        	$this->Model_profile->update($id,$form_data);
    	        	$success = 'Photo is updated successfully!';
    
    	        	$this->session->set_userdata($id,$form_data);
    	        	$this->session->set_flashdata('success',$success);
    	        	redirect(base_url().'admin/profile');
    		    }
    		    else {
    		    	$this->session->set_flashdata('error',$error);
    	        	redirect(base_url().'admin/profile');
    		    }
    		}
    
    		if(isset($_POST['form3'])) {
    			$valid = 1;
    
    		    $this->form_validation->set_rules('password', 'Password', 'trim|required');
    		    $this->form_validation->set_rules('re_password', 'Retype Password', 'trim|required|matches[password]');
    
    			if($this->form_validation->run() == FALSE) {
    				$valid = 0;
                    $error = validation_errors();
                }
    
    		    if($valid == 1) {
    
    		    	$form_data = array(
    					'password' => md5($_POST['password'])
    	            );
    	        	$this->Model_profile->update($id,$form_data);
    	        	$success = 'Password is updated successfully!';
    	        	
    	        	$this->session->set_userdata($id,$form_data);
    	        	$this->session->set_flashdata('success',$success);
    	        	redirect(base_url().'admin/profile');
    		    }
    		    else {
    		    	$this->session->set_flashdata('error',$error);
    	        	redirect(base_url().'admin/profile');
    		    }
    		}
    
    		$data['setting'] = $this->Model_common->get_setting_data();
    
    		$this->load->view('admin/view_header',$data);
    		$this->load->view('admin/view_profile',$id,$data);
    		$this->load->view('admin/view_footer');
    	}
    	
    }

    And finally, my Model_profile.php (only need to update)

    No errors etc.

    Any help would be appreciated.

    Cheers,

    Dan

    <?php
    defined('BASEPATH') OR exit('No direct script access allowed');
    
    class Model_profile extends CI_Model 
    {
    
        function update($id) {
            $this->db->where('id',$id);
            $this->db->update('tbl_team_member',$data);
        }
       
    }

     

  18. Just adding to this.

    I also wanted this to calculate tax and format with the correct locale, tax name (example: GST for Australia). So, after hours of clowning around, pulling hair out, Googling, and channelling the masters of Code who have long passed away, I finally have it doing what I want.

    For those of you in the same creek without a paddle, I will share my newfound expertise with you:

    <?php
    if($setting['site_currency'] == 'EUR') {
    	$locale = 'en-IE';
    }
    if($setting['site_currency'] == 'GBP') {
    	$locale = 'en-GB';
    }
    if($setting['site_currency'] == 'USD') {
    	$locale = 'en-US';
    }
    if($setting['site_currency'] == 'AUD') {
    	$locale = 'en-AU';
    }
    ?>
    <script>
        $(document).ready(function() {
    		
            $('#numberPerson').on('change',function() {
    			var format = (num) => new Intl.NumberFormat('<?php echo $locale; ?>', { style: 'currency', currency: '<?php echo $setting['site_currency']; ?>' }).format(num);
                var selectVal = $('#numberPerson').val();
                var selectPrice = <?php echo $p_price['p_price_single']; ?>;
                var taxRate = <?php echo $setting['tax_amount']; ?>;
                var salesTax = selectPrice * taxRate / 100;
                var total = selectPrice + salesTax;
                var totalPrice = selectVal * total;
                 $('#totalPrice').text(format(totalPrice));
            });
        });
    </script>

    It's probably not what the true experts would do but it works and does what I want it to do and that's what matters.

    Love you Bye ;)

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.