• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Display Active Template First

#1
[eluser]riwakawd[/eluser]
Hi, I have been searching around for how to fix my project.

What now I am trying to do is make it so on my select form it shows the current theme/template from the database.

Currently when go back and check select form all ways displays the non active one.

It submits input fine to database just have to work that issue out?

What do I need to do to make the form select show current active theme / first from database.

Controller

Code:
<?php

class Settings extends MX_Controller {

   public function index() {
  
      $data['action'] = site_url('admin/settings');

      $this->load->model('admin/setting/model_setting_store');

      if ($this->model_setting_store->config_template()) {
         $data['config_template'] = $this->input->post('config_template');
      }

      $data['templates'] = array();

      $directories = glob(APPPATH . 'modules/catalog/views/theme/*', GLOB_ONLYDIR);

      foreach ($directories as $directory) {
         $data['templates'][] = basename($directory);
      }

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

      $this->form_validation->set_rules('config_meta_title', 'Website Title');
      $this->form_validation->set_rules('config_meta_description', 'Description');
      $this->form_validation->set_rules('config_meta_keyword', 'Keywords');
      $this->form_validation->set_rules('config_template', 'Template');

      if ($this->form_validation->run() == FALSE) {

         return $this->load->view('setting/settings', $data);

      } else {

         redirect('admin/dashboard');

      }
  
   }

}

Model
Code:
<?php

class Model_setting_store extends CI_Model {

   function config_template() {
      $data = array(
         'setting_id' => "10",
         'website_id' => "0",
         'group' => "config",
         'key' => "config_template",
         'value' => $this->input->post('config_template')
      );

      $this->db->update('setting', $data);
   }

}

View

Note: form action removed will not display on here.

Code:
<form  method="post" enctype="multipart/form-data" id="form-setting" class="form-horizontal">
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template">&lt;?php echo $entry_template; ?&gt;</label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
&lt;?php foreach ($templates as $template) { ?&gt;
&lt;?php if ($template == $config_template) { ?&gt;
<option value="&lt;?php echo $template; ?&gt;" selected="selected">&lt;?php echo $template; ?&gt;</option>
&lt;?php } else { ?&gt;
<option value="&lt;?php echo $template; ?&gt;">&lt;?php echo $template; ?&gt;</option>
&lt;?php } ?&gt;
&lt;?php } ?&gt;
</select>
&lt;/form&gt;

#2
[eluser]Tim Brownlaw[/eluser]
Where are you reading the value back from the database?

#3
[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1409632520"]Where are you reading the value back from the database?[/quote]

I tried doing the single value from my library file item below, gave it ago but still would not show active template from database. I had auto loaded the library.

Code:
&lt;?php

class Item {

function __construct() {
  $this->CI =& get_instance();
}

public function getTemplate($value) {
  $this->CI->db->select($value);
  $this->CI->db->where("group","config");
  $this->CI->db->where("key","config_template");
  $query = $this->CI->db->get('setting');
  return $query->row()->$value;
}
}

I not sure why did not work before so just removed it in mean time.

#4
[eluser]Tim Brownlaw[/eluser]
It seems you are telling it what value it's meant to be reading without actually reading the value from the DB!!!!

Now I know you are using HMVC from your last post...So WHY do you insist on using libraries and not use Models within your module... This settings stuff could go into a Settings module... to keep it all nice and tidy.
Or a Template Module... But it's definitely a candidate for using in a Model.

So this answer is based upon you having it in a model somewhere...

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $query->row();  // I've just broken this out from what you had
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}

I am assuming that your setting table has the fields value,group and key

#5
[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1409637624"]It seems you are telling it what value it's meant to be reading without actually reading the value from the DB!!!!

Now I know you are using HMVC from your last post...So WHY do you insist on using libraries and not use Models within your module... This settings stuff could go into a Settings module... to keep it all nice and tidy.
Or a Template Module... But it's definitely a candidate for using in a Model.

So this answer is based upon you having it in a model somewhere...

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $query->row();  // I've just broken this out from what you had
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}

I am assuming that your setting table has the fields value,group and key
[/quote]

I re changed part on the controller to what below but now get error from model.

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: setting/model_setting_store.php

Line Number: 66

Fatal error: Call to a member function row() on a non-object in C:\Xampp\htdocs\codeigniter\project-2\application\modules\admin\models\setting\model_setting_store.php on line 66

Controller

Code:
$this->load->model('admin/setting/model_setting_store');

if($this->model_setting_store->config_template()) {
    $data['config_template'] = $this->input->post('config_template');
} else {
   // Should display current theme/template
   $data['config_template'] = $this->model_setting_store->getTemplate();
}

Model

Code:
function config_template() {
  $data = array(
   'setting_id' => "10",
   'website_id' => "0",
   'group' => "config",
   'key' => "config_template",
   'value' => $this->input->post('config_template')
  );

  $this->db->update('setting', $data);
}

public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0) {
   $row = $query->row();  // I've just broken this out from what you had
   return $row->value;
  }else{
   return 'Entry Not Found';
  }
}

#6
[eluser]Tim Brownlaw[/eluser]
Seriously!!!!!!!!!!!!!!
Yes there is a mistake... and you got the Error message that kind of shouts out what it is

Severity: Notice
Message: Undefined variable: query
Filename: setting/model_setting_store.php
Line Number: 66

A Quick glance of the ole eyeballs over it would see...

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $query->row();  // <<<<<< THIS IS BROKEN - IT SHOULD BE $result - Flashing Lights And Loud Sirens Please!!
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}

I mean if you can't read through the code and pick that out - I really really really don't know what to say!!!!!!!!!!

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $result->row();  // Ahhh that's better!
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}
AND I've not tested this code - It's a suggestion and it's your responsibility to test it and fix it and let me know I made a mistake out of niceness!


#7
[eluser]riwakawd[/eluser]
[quote author="Tim Brownlaw" date="1409645002"]Seriously!!!!!!!!!!!!!!
Yes there is a mistake... and you got the Error message that kind of shouts out what it is

Severity: Notice
Message: Undefined variable: query
Filename: setting/model_setting_store.php
Line Number: 66

A Quick glance of the ole eyeballs over it would see...

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $query->row();  // <<<<<< THIS IS BROKEN - IT SHOULD BE $result - Flashing Lights And Loud Sirens Please!!
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}

I mean if you can't read through the code and pick that out - I really really really don't know what to say!!!!!!!!!!

Code:
/**
* Read back the Current Template Name
*
* @return string template name or an error message
*/
public function getTemplate() {
  $this->db->select('value');
  $this->db->where('group','config');
  $this->db->where('key','config_template');
  $result = $this->db->get('setting');
  if($result !== FALSE AND $result->num_rows() > 0)
  {
    $row = $result->row();  // Ahhh that's better!
    return $row->value;
  }
  else
  {
    return 'Entry Not Found';
  }
}
AND I've not tested this code - It's a suggestion and it's your responsibility to test it and fix it and let me know I made a mistake out of niceness!

[/quote]

Nothing happens but now each time I go back to check if it works on form it also then removes value from database very strange.

Below shows list but not current theme

Code:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template">&lt;?php echo $entry_template; ?&gt;</label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
&lt;?php foreach ($templates as $template) { ?&gt;
&lt;?php if ($template == $config_template) { ?&gt;
<option value="&lt;?php echo $template; ?&gt;" selected="selected">&lt;?php echo $template; ?&gt;</option>
&lt;?php } else { ?&gt;
<option value="&lt;?php echo $template; ?&gt;">&lt;?php echo $template; ?&gt;</option>
&lt;?php } ?&gt;
&lt;?php } ?&gt;
</select>
<br />
<img src="" alt="" id="template" class="img-thumbnail" /></div>
</div>

Shows Current theme but with out theme list

Code:
<div class="form-group">
<label class="col-sm-2 control-label" for="input-template">&lt;?php echo $entry_template; ?&gt;</label>
<div class="col-sm-10">
<select name="config_template" id="input-template" class="form-control">
<option value="&lt;?php echo $template; ?&gt;" selected="selected">&lt;?php echo $template; ?&gt;</option>
</select>
<br />
<img src="" alt="" id="template" class="img-thumbnail" /></div>
</div>

Both not working to geather as shown on first code

#8
[eluser]Tim Brownlaw[/eluser]
Well for starters...

Code:
&lt;?php if ($template == $config_template)

Do you know what $template is - do a var_dump of that... and also what $config_template is... do a var_dump of that as well...

You just need to display these values... see what they are and why you are not getting a match.
Then work your way back from there...


#9
[eluser]InsiteFX[/eluser]
LOL!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.