redirect uri segment after delete - codeigniter

I created a project , I have a question about the direct url segment ...
For example:
this my URL
http://10.88.25.131/instrumentdev/instrument/instrument/detail/CT-BSC-001
when I want delete the data and to direct to the url such as the example above
this my controllers
public function detail(){
$id = $this->uri->segment(4);
$data = array(
'detail'=>$this->mcrud->get_detail($id),
'lihat' =>$this->mcrud->lihat_komentar($id),
'isi' =>'instrument/read_views');
$this->load->view('layout/wrapper', $data);
}
function deletecom() {
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect('???**this my problem**???');
}

Use session
$newdata = array(
'data' => $u
);
$this->session->set_userdata($newdata);
redirect('controller/method');
then in next method
$u = $this->session->userdata('data');

Use
$this->load->helper('url');/*if not already*/
redirect(base_url('controller/method/parametersIfAny'));

TRY this :
function deletecom() {
echo $this->uri->segment(4);die(); //what does this print ??
$u = $this->uri->segment(4);
$this->mcrud->deletecomment($u);
redirect(base_url('instrument/instrument/detail/'.$u));
}

Related

how to insert the data using codeiginter?

I am new to codeiginter,how to insert the data into table using codeiginter
my controller code;
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
It is simple form use internet to search it
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$this->db->insert('tablename',$data);
I would like to share reusable code approach by following the MVC coding style
My Controller
$table = 'table_name';
$data = array(
'subgrpname' => $this->input->post('subgrpname'),
'grpname'=> $this->input->post('grpname'),
'pltype'=> $this->input->post('pltype')
);
$record_id = $this->Common_Model->insert_into_table($table, $data);
My Common_Model
function insert_into_table($table, $data) {
// echo "<pre>";print_r($data);exit;
$insert = $this->db->insert($table, $data);
$insert_id = $this->db->insert_id();
if ($insert) {
return $insert_id;
} else {
return false;
}
}
You can use this model function as much time as you want.

Codeigniter : array to string conversion error

Here is my model:
public function count_diabetic(){
$query = $this->db->query("Select count(diabetic) as count_diabetic from member where diabetic is not NULL");
return $query->result_array();
}
public function count_hypertensive(){
$query = $this->db->query("Select count(hypertensive) as count_hypertensive from member where hypertensive is not NULL");
return $query->result();
}
here is my controller:
public function home(){
$this->load->model('Jsv_model');
$data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata($data);
$this->load->view('home');
}
Here is my view with in php tag:
echo $this->session->userdata('count_diabetic');
But Here it shows error that array to string conversion error..
please help me
Inside count_diabetic() you should change $query->result_array()
into
$query->row()->count_diabetic,
it would return number of count only not array.
Do it to count_hypertensive() too.
In PHP, you can display arrays by using the print_r function instead of echo.
For example, you have to change your code to:
print_r($this->session->userdata['count_diabetic']);
You are doing it wrong in this line.
echo $this->session->userdata('count_diabetic');
What should you do?
// To Set a Data in Session
$session_data = array(
'count_diabetic' => $this->Jsv_model->count_diabetic(),
'count_hypertensive' => $this->Jsv_model->count_hypertensive()
);
$this->session->set_userdata('user_data', $session_data);
// To get the session data
$session_data = $this->session->userdata('user_data');
$user_name = $session_data['username'];
$count_diabetic = $session_data['count_diabetic'];
$count_hypertensive = $session_data['count_hypertensive'];

joomla - router change url when getting the name of product

I have build my own component in joomla and client wants now a friendly urls f.e
website.com/someplace/{product-id}-{product-name}. So i Build my own router like this.
function componentBuildRoute(&$query)
{
$segments = [];
if (isset($query['view'])) {
$segments[] = "szkolenie";
unset($query['view']);
}
if (isset($query['product_id'])) {
$productName = JFilterOutput::stringURLSafe(strtolower(getProductName($query['product_id'])));
$newName = $query['product_id'] . '-' . $productName;
$segments[] = $newName;
unset($query['product_id']);
}
return $segments;
}
and parse route function
function componentParseRoute($segments)
{
$app = JFactory::getApplication();
$menu = $app->getMenu();
$item =& $menu->getActive();
$count = count($segments);
switch ($item->query['view']) {
case 'catalogue' : {
$view = 'training';
$id = $segments[1];
}
break;
}
$data = [
'view' => $view,
'product_id' => $id
];
return $data;
}
While on the end of buildroute function segments are ok I have exactly what I want that on the beginning of parse route I have something like
website.com/szkolenie/1-krakow <-- I dont know wtf is this krakow( I know it is city i Poland) but still where is it get from ? The getProductName function implementation is
function getProductName($productId)
{
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select('#__component_training.id as id, #__component_product' . name)
->from($db->quoteName('#__component_training'))
->where('#__s4edu_product.product_id = ' . $productId)
->leftJoin('#__component_product ON
#__component_training.product_id=#__component_product.product_id');
$training = $db->loadObject();
return trim($training->name);
}
So taking all this into consideration I think that something is happening between the buildRoute and parseRoute, something what filters the $segment[1] variable, but how to disable that and why is it happening ?
P.S
Please do not send me to https://docs.joomla.org/Joomla_Routes_%26_SEF
I already know all the tutorials on joomla website which contains anything with sef.
P.S.S
It is built on joomla 3.7.0
You do not have a product named "krakow" ?
If not you can try to remove the $productName from the build function, just to check if this "krakow" is added automaticaly or it's from the getProductName() function.
Also i noticed that you have an error i guess in the function getProductName()
->where('#__s4edu_product.product_id = ' . $productId)
It's should be
->where('#__component_product.product_id = ' . $productId)

get the google fonts list in page

i'm using laravel now i want to list all the google web fonts in my page using google font api
public function index(){
function get_google_fonts() {
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=!";
$result = json_response( $url );
$font_list = array();
foreach ( $result->items as $font ) {
echo $font;
}
return $font_list;
}
return view('website_settings');
}
i think it's not working, can anyone help me please!
because $font is an object you have to traverse.. try something like that
$url = "https://www.googleapis.com/webfonts/v1/webfonts?key=!";
$result = json_decode(file_get_contents( $url ));
$font_list = "";
foreach ( $result->items as $font )
{
$font_list[] = [
'font_name' => $font->family,
'category' => $font->category,
'variants' => implode(', ', $font->variants),
// subsets
// version
// files
];
}
return $font_list;

CodeIgniter(2.1.4) Sessions is not working

I have a problem with CI sessions.
I initialized my session library:
$autoload[‘libraries’] = array(‘database’, ‘session’) (In config/autoload.php)
This is my code:
cycle
$this->load->library(‘image_moo’);
// Upload image and return unique name
$data = array(
‘image’ => $image,
);
$this->db->insert(‘category_images’, $data);
if (!$this->session->userdata(‘uploadImages’))
{
$this->session->set_userdata(‘uploadImages’, $this->db->insert_id());
}
else
{
$session = $this->session->userdata(‘uploadImages’);
$sessionData = $session.’|’.$this->db->insert_id();
$this->session->set_userdata(“uploadImages”, $sessionData);
}
echo $this->session->userdata(‘uploadImages’); // return 256; corect result - 255|256
end of cycle
This is script for image upload with jQuery File Upload (blueimp) and i need set ids of inserted in database images to session.
Can anyone help. Thank you!
To use CI Session you also need to provide the encryption key in your application/config.php:
$config['encryption_key'] = 'xxxxxx';`
Try this
cycle
$this->load->library(‘image_moo’);
// Upload image and return unique name
$data = array(
‘image’ => $image,
);
$this->db->insert(‘category_images’, $data);
if ($this->session->userdata(‘uploadImages’)=='')// or if (!isset($this->session->userdata(‘uploadImages’)))
{
$this->session->set_userdata(‘uploadImages’, $this->db->insert_id());
}
else
{
$session = $this->session->userdata(‘uploadImages’);
$sessionData = $session.’|’.$this->db->insert_id();
$this->session->set_userdata(“uploadImages”, $sessionData);
}
echo $this->session->userdata(‘uploadImages’); // return 256; corect result - 255|256
end of cycle

Resources