I like to preface this question by apologizing for being noob.
For codeigniter URL, format goes:
example.com/class/function/ID
If I were to have the urls below
website.com/books/chapter/1
website.com/books/chapter/2
I know how to create class named "books" under which I would create public function "chapter", so...
public funtion chapter() {
$this->load->view("content");
}
How would I add the ID's 1 and 2, assuming the only difference between the two website is I would have:
1.png within my "content" for website.com/books/chapter/1
and
2.png within my "content" for website.com/books/chapter/2, with 2.png replacing where 1.png was supposed to be.
Thanks!
Since you are using this URL: website.com/books/chapter/2, you are already passing the id after the last slash. Therefore your controller method should receive that ID as a parameter like this:
Controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
Controller:
public function chapter() {
$data["image_id"] = $this->uri->segment(3); //since your ID(number) is in third segment so 3
$this->load->view("content", $data);
}
View:
<img src="<?= $image_id ?>.png">
for more info on URI class
https://ellislab.com/codeigniter/user-guide/libraries/uri.html
The ids are just the method parameters. You can get it by passing the parameter in your method.
In the controller:
public function chapter($id) {
$data["image_id"] = $id;
$this->load->view("your_view", $data);
}
In your view:
<img src="<?php echo $image_id;?>.png"/>
Related
I have function in controller with one argument and now I want to pass value for it from view through url.
courses.php
public function index($id){
print_r();
exit;
}
coursesview.php
<a class="jscroll-next" href="<?php echo base_url(); ?>/courses/index?page=<?php echo $nextPage; ?>&id=<?php echo $parentId ?>">next page</a>
How it possible?
From the user guide,
Simply put the parameter in an array and pass it to the view by using the second parameter of load->view().
Afterwards, in the view, the variable will be available as the key.
In this example: $id or $anothervar.
courses.php
public function index($id){
$data = array('id' => $id, 'anothervar' => 'yeahi');
$this->load->view('coursesview', $data);
// exit;
}
coursesview.php
echo $id; // the ID
echo $anothervar; // "yeahi"
You are going to use minimum arguments in URL, here is my suggestions
1.<a class="jscroll-next" href="<?php echo base_url('courses/$nextPage/$parentId/'); ?>">next page</a> //controller followed by arguments - if you call index method
(OR)
2.<a class="jscroll-next" href="<?php echo base_url('courses/methodname/$nextPage/$parentId/'); ?>">next page</a> //controller,method name and followed by arguments
How to get those arguments in controller
Example:
$nextPage = $this->uri->segment(3);
$parentId = $this->uri->segment(4);
How do I display image from the php function? [updated]
default.htm
<img id="avatar-image" alt="Jason's Image" src="{{ getAvatarImage() }}" />
Component.php
public function getAvatarImage()
{
$var = \System\Models\File::select('disk_name')->where('attachment_id', $avatar_id)->first();
if (count($var) == 0) return "";
return $var->path;
}
How do I get the image source and display the image?
First of all, are you sure that this function returns the correct file path? I'll consider that yes.
So, organizing things, what you're trying to do is use a custom function inside Twig environment.
To do so you need to register your function in the CMS extending twig.
STEP 1
Create a registerMarkupTags() method that returns an array of custom functions assigned by the "function name" in the plugin registration class.
YourPlugin/Plugin.php
public function registerMarkupTags() {
return [
'functions' => [
'getAvatarImage' => [ $this, 'getAvatarImageInternal' ]
]
];
}
STEP 2
We are referring the custom function to $this, so add the method in the same class.
YourPlugin/Plugin.php
public function registerMarkupTags() {
return [
'functions' => [
'getAvatarImage' => [ $this, 'getAvatarImageInternal' ]
]
];
}
/**
* Example of registering a Twig function.
*
* #return string
*/
public function getAvatarImageInternal() {
// Your function body goes here.
$path = 'http://app.localhost/storage/image.jpg';
return $path;
}
STEP 3
Now the custom function is registered and you can use inside Twig environment.
<img src="{{ getAvatarImage() }}" />
First, getAvatarImage is not a static Method. This means you need an instance of it to be able to call it. Second; to display the Image, you need to echo it back to get the return value (not just call it). Your code should have read something like this:
<?php
$avatar_id = 10; // JUST A SIMPLE SIMULATION. YOU SHOULD HAVE A WAY TO GET THE ID
$obj = new ClassContainingGetAvatarImageMethod();
$imgURL = $obj->getAvatarImage($avatar_id);
?>
<img id="avatar-image" alt="Jason's Image" src="<?php echo $imgURL;?>" />
OR
<img id="avatar-image" alt="Jason's Image" src="<?php
avatar_id = 10; // WHATEVER THE AVATAR_ID MAY BE
$obj = new ClassContainingGetAvatarImageMethod();
echo $obj->getAvatarImage($avatar_id);?>" />
It's not a good way to select the image directly from the file system. Instead you can use attachments system as I explained in this question
How to change the front page & component image from the backend? how to get HTML tag attributes in php?
please help me, this is simple thing but I don't know why this still keep error for an hour,
on my view I've got :
<a href="admin/editProduct?idd=$id">
on my controller that directed from above :
public function editProduct(){
$data["id"] = $_GET['idd'];
$data["produk"] = $this->model_get->get_data_list(2);
//this below doesn't work either, i just want to past the parameter to my model
//$data["produk"] = $this->model_get->get_data_list($_GET['idd']);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I can not use the array id. It keeps telling me undefined variable idd.
I don't know what to do anymore, please anyone help!
I am using Codeigniter framework
Change your view as:
<a href="admin/editProduct/<?php echo $id;?>">
And in the controller, either get the id as parameter,
public function editProduct($id) {
}
or as uri segment
public function editProduct() {
$id = $this->uri->segment(3);
}
Change your link (in view) with this
<a href="admin/editProduct/$id">
And change your controller as
public function editProduct($id) {
}
Then user $id inside your controller
Make the href link as follows:
...
And edit the controller like this:
public function editProduct($id){
...
$this->model_get->get_data_list($id);
}
Where $id will be the passed $id.
make
try this this works fine
public function editProduct()
{
$id=$this->uri->segment(3);
$data["produk"] = $this->model_get->get_data_list($id);
$this->adminHeader();
$this->load->view("adminPages/editProduct", $data);
$this->adminFooter();
}
I am having difficulty getting the correct URL when I call a method to load a view.
Heres my controller:
public function post() {
$title = $this->input->post('title');
$data = $this->p_Model->post($title);
$this->qs($data);
}
public function qs($id){
$title = $this->s_Model->getTitle($id);
$result = $title->result();
$this->load->view('q_View', array('results' => $result));
}
Heres my view:(note this view is not the view which gets loaded from the qs function, but one which calls the qs function)
<html>
<body>
<table>
<?php
if (isset($qs)) {
foreach ($qs as $row) {
$id = $row->qID;
echo '<a href="'.site_url('myController/qs/'.$id).'">';
echo $row->title;
echo "<br>";
}
}
?>
</table>
</body>
</html>
So in my controller I have two functions, the qs function works separately by itself so can be called in the view and give the following url myController/qs/1 however when I use the post function I get a url like this myController/post so my question is how can I get my url to be like the first example?
Instead of using the line:
$this->qs($data);
You can use a redirect:
redirect('/mainController/qs/'.$data);
That should work in the same way that you have used in your view
Try base_url and also you can use current_url() returns the full URL (including segments) of the page being currently viewed.
echo '<a href="'.base_url('myController/qs/'.$id).'">';
I want to pass a language id for each link i click from my view to controller.
My view code is
<?php foreach ($languages as $lang) { ?>
<li>
</li>
<?php } ?>
My controller is
public function box($box_id=null, $language_name=null, $language_id=null) {
/// my function code
echo $box_id;
echo $language_name;
echo $language_id;
$data['languages'] = $this->Home_model->getLanguages($box_id);
}
The languages array contain the language id and language name
i want the name to be in url but not the id
The url looks like this
http://localhost/mediabox/home/box/12/en
if i send the language id in url it is then visible otherwise it is not visible in the controller.
How can I get language id for each link in controller without sending it in url
Thanks
pass the language name in the url without the ID, compare to languag_name column in table.
Lets assume you have url: http://localhost/mediabox/home/box/en
controller
<?php
# I wont write a controller but you should know how to do that, im also writing code as if you are just focusing on getting language.
public function box( /**pass in your other uri params as needed **/ $lang_name = 'en'){
#you could load this in the constructor so you dont have to load it each time, or in autoload.php if your using it site wide.
$this->load->model('lang_model', 'langModel');
#this example shows loading the library and running the function
$this->load->library('lang_library');
$this->lang_library->_getLang($lang);
#this example shows putting the getLang function inside the controller itsself.
self::_getLang($lang);
}
library/private function
<?php
private functon _getLang($lang = 'en'){
#run the query to retrieve the lang based on the lang_name, returns object of lang incl id
$lang = $this->langModel->getLang($lang_name);
if (!$lang){
die('language not found');
}else{
return $lang;
}
lang model
<?php
public function getLang($lang_name = 'en'){
$this->db->where('lang_name', $lang_name);
$this->db->limit(1);
$q = $this->db->get('languages');
if ($q->mysql_num_rows > 0){
return $q->result();
}else{
return false;
}
}
you will then have a variable with object associated to it then you can simply call $lang->lang_name; or $lang->lang_id;
Session storage
<?php
#you could call this in the beginning after using an ajax `$.post();` to retrieve the ID.. the easiest route though is whats above. I use this in my REST APIs
$this->session->set_userdata('lang', $lang);
Your confusion is in 'passing back' to the controller. Don't think of it as from controller => View (passing it say $data['something'] variables).
Its basically a <form>, so take a look at form helper and then at form validation.
That will give you an idea of how to create a form using codeigniter syntax.
In your controller, you would do a validation, and if it matches the language (or whatever you submit), then you can utilize sessions to save it for every page (so you don't need it in the URL).
Sessions are very simple and saving an item is as easy as:
$this->session->set_userdata('varname', 'value');
Later, on every other controller, you can check the variable
$language = $this->session->userdata('varname');
// load language etc;
Make a table with language ID and Language name in the database, just pass the language name to the controller and get the language ID by doing a db call.
You could do it using jQuery. Something like;
In View;
<ul id="language_selector">
<?php foreach ($languages as $lang) { ?>
<li>
<a href="javascript:;" class="change-language" data-language="<?php echo $lang['language_name']?>" data-box="<?php echo $template_data['box_id']?>">
<img src="<?php echo base_url(); ?>public/default/version01/images/country_<?php echo $lang['language_name'] ?>.png" width="27" height="18" border="0" />
</a>
</li>
<?php } ?>
</ul>
The JS;
$(function() {
$('.change-language').live('click', function() {
var language_name = $(this).data('language');
var box_id = $(this).data('box');
$.ajax({
url: '/home/box/'+language_name,
type: 'post',
data: 'box_id='+box_id,
success: function( data ) {
self.parent.location.reload();
},
error: function( data ) {
alert('oops, try again');
}
});
});
});
The controller:
public function box($language) {
$box_id = $this->input->post('box_id');
// do a llokup on the language as suggested by #vivek
}
You are telling CodeIgniter that you will be receiving both the language name and id in your action.
public function box($box_id=null, $language_name=null, $language_id=null) {
}
Change that to just
public function box($box_id=null, $language_name=null) {
}
For your example URL you should then get $box_id == 12 and $language_name == 'en'.
Then lookup the language id by using it's name either in a helper or as part of a Language model as Mike suggests.