Integrate Codeigniter and Filterable - codeigniter

I'm trying to integrate Codeigniter and Filterable but I cant find much information about it. I have a page with many results and I want to filter the results, for example by the column "name" or "id". I have worked with Filterable before, but never had to do it with Codeigniter.
I tried to put the files on the root. Also in the folder "Application", even tried putting it on the same folder as the view. But in any of these cases I cannot link the files and get it to work.
The ways I've been trying to link the scripts are the following:
<script src="<?php echo site_url('src/filterable.js')?>"></script> This was with the scripts in application folder
<script src="./src/filterable.js"></script> Scripts on root
<script src="<?php echo site_url('admin/src/filterable.js')?>"></script> Scripts on view folder
Is there another way to achieve this? Or maybe another library I could use for it?

Here is how I like to deal with assets.
I create an helper called assets_helper in application/helpers/
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
if ( ! function_exists('js'))
{
function js($nom)
{
return '<script src="' . base_url() . 'assets/js/' . $nom . '.js" type="text/javascript" ></script>';
}
}
//This is only the part that handle js as it is what's bothering you
Here is the full helper I use : http://pastebin.com/ujETEXJ4
After that, at the same level as index.php I create those folder :
|-Application
|-System
|-index.php
|-Assets
|- css
|- sass
|- images
|- js
Put all the js file you need in your new js folder.
In my application/config/autoload.php I add my new helper
$autoload['helper'] = array('assets', ...);
Finally, in the footer of my page(s) :
<?= js('myjsfile'); ?> //I did not forgot the extension, it's how it works :)
Which will give at the end :
<script src="http://www.example.com/assets/js/myjsfile.js" type="text/javascript" ></script>

Thanks AdrienXL for your answer. I tried to follow your suggestion but in the end I couldn't make it work. For some reason Codeigniter didn't recognize the files I tried to reach.
So after a long searching I ran across a library called Datatables http://www.datatables.net/ it has the option to load the scripts locally or remotely, so just added the link to the scripts needed to the type of filter I had to implement and in the end this last option was what allowed me to make the filters work.

Related

Yii2 : Show image from Folder Common

I have a picture in folder common that i want to show in view, but it doesn't shown.
My code is :
<img src="<?= Yii::$app->request->baseUrl . '/common/web/img/ava.jpg' ?>" class=" img-responsive" >
It's generated to be <img src="/simdsm/common/web/ava.jpg" class=" img-responsive"> but the image does not appear.
How to fix it?
Since no one actually posted an example of using assets to access files saved in a backend-folder or the other way around, it's quite simple:
Let's say you are working with the yii advanced template and you have a backup/web/uploads/ folder in which you save images uploaded via your backend. Now you'd like to display those images on your frontend. Since the Frontend-App and the Backend-App are two seperate Applications, they don't know of each other.
You create a new asset in your frontend/assets/, let's call it BackendAsset.php:
<?php
namespace frontend\assets;
use yii\web\AssetBundle;
class BackendAsset extends AssetBundle {
public $sourcePath = '#backend/web/uploads';
}
where $sourcePath is the backend-folder (source) you'd like to access on the frontend. The #backend alias is predefined in the advanced template, so we'll use it.
Now in our view we can simply use the BackendAsset:
<?php
use frontend\assets\BackendAsset;
$backend = BackendAsset::register($this);
?>
and now we can easily display a file, let's say backend/web/uploads/somefile.jpg:
<img src="<?= $backend->baseUrl . '/somefile.jpg' ?>" >
NOTE: Using the asset in this way copies all the files from the backend/web/uploads to an asset folder in the frontend. To prevent that, you can tell your application not to copy the files, but to link to them (creating SymLinks) instead, using linkAssets (yii2 docu):
In your app configuration (in this case frontend/config/main.php), set the linkAssets parameter to TRUE:
'components' => [
'assetManager' => [
'linkAssets' => true,
]
]
In case of using advanced application template, images should be placed in these web accessible folders - frontend/web or backend/web.
Also usually I create alias from frontend/web/images to backend/web/images to display images in backend from frontend.
What you mentioned is not web accessible directory.
Alternative way to publish images from such directory will be creating asset bundle for that folder, that way images will be copied in frontend/web/assets for example. You can read more about asset bundles in official docs.
We can do it easily by simple step of below
In common/config/params-local.php
Yii::setAlias('#imageurl', 'http://localhost/project_name/backend/web/');
in above example i have my image under backend/web/images/product_images
In frontend, i am getting like,
<img src="<?php echo Yii ::getAlias('#imageurl'); ?>/images/product_images/<?= $product['image']?>" alt=" " class="img-responsive" />
it's simply working for me...

What's Codeigniter's absolute url? (for image src)

I have a javascript script that changes an image's src when clicked. When I had the same application in pure php without Codeigniter I simply had the images' src be something like "images/image_name" where images was the images folder, but now I'm using Codeigniter I don't know how to do it. I can't use php inside the JS script obviously so can't use the base_url() or site_url() functions or anything, so I suppose only the absolute path would work. And my images are in an images folder that's inside an assets folder which is at the root (i.e. at the same level as the Application, System and User_guide folders). So what should the images' src be? Thanks.
You can use php code inside script.
<script>
var imgSrc="<?php echo base_url('assets/images/image_name.jpg'); ?>";
</script>
Or you can also declare a global variable in header file to store base path.
<script>
basePath='http://www.yourdomain.com/';
</script>
and it can be used as
var imgSrc=basePath+"assets/images/image_name.jpg";
in addition to piyush's answer, I need to mention something that will help to use site_url() & base_url() properly.
site_url("controller/method/args") == base_url() + $config['index_page'] + /controller/method/args. You should use this to access pages.(Reference)
base_url("public/img/logo.png") == base_url() + /public/img/logo.png. You should use this to access resources such as images, stylesheets etc.(Reference)
Main difference between both functions is this part $config['index_page']. If you have already removed the $config['index_page'] from the config.php and you are using .htaccess to rewrite the URLs, then both function will result the same. But it is a best practice to use the correct function for the correct purposes.
Hope this helps.

Add js code to create layout in controller in magento

I am creating a blocks in my controller using
$this->_addContent($this->getLayout()->createBlock('mymodule/mymodule_newpage')
Is there any way how I can embed js code into the addcontent function. I dont want to add complete js but a chunk of code.
Thanks
You can add that particular js file in your static block (mymodule/mymodule_newpage) by writing it under content tab.
<script type="text/javascript" src="http://your_site.com/js/your_js_file.js"></script>
Though I am not 100% sure.Give a try.Goodluck.
You can try:
$this->_addContent($this->getLayout()->createBlock('core/text')->setText('your script here'));
Obs: I could not make sintax highlight fpr PHP work anymore...

Modifying joomla mod_banner to count banner

I'm using default joomla mod_banner.
I'd like to modify the module so can give specific css class for each banner image, so if the module displaying 3 banner images, the first banner (image) will have class="banner1", the second image will have class="banner2" the third image will have class="banner3" and so on.
How can I do that?
If you decide to modify your mod_banners module, then just follow the steps:
Open the /modules from your root directory.
Find the directory named mod_banners in modules/mod_banners
Find out the tmpl directory modules/mod_banners/tmpl
default.php is the file that display the images into the frontend of you joomla website.
just edit this file. See the img tag into and just change whatever name you want to give the class..
UPDATE : Take a look on the given Codes:
// Add variable before
$unique_number = 1;
// Provide the Provides the images URL or related information
foreach($list as $item):
now just find out the <img tag and just add a class something like
<img class='banner-<?php echo $unique_number?>'
and at the end of the file findout the endforeach; and just add the code something like
<?php
$unique_number++;
endforeach;
?>
providing you the class name like banner-1, banner-2, .... etc.
IMPORTANT: If you notice on the top most of your default.php the module provide the images into an array, If you are familiar with the Joomla modules structures then you can modify it easily.

How to use headjs with Joomla?

Trying to use HeadJS in Joomla. I added a code to my template - it grabs the javascript files in the head output, prepares a string to output them between head.js code, then removes all javascript files from joomla's head output tag.
The problem is that some scripts are loaded after the template. For example, a module will enqueue some script files after the template has loaded, so its files appear outside of my head.js code. Any ideas how I can control this?
$data = $this->getHeadData();
if( $data['scripts'] ){
foreach ($data['scripts'] as $url=>$type){
if( !strstr($url, 'ajax.googleapis.com/ajax/libs/jquery') )
$headjs[] = $url;
}
unset( $data['scripts'] );
$data['scripts'][$template . '/js/head.js'] = array(
'mime' => 'text/javascript',
'defer' => false,
'async' => false);
$this->setHeadData($data);
}
And then...
<script>
head.js(
<? foreach($headjs as $script): ?>
'<?=$script?>',
<? endforeach; ?>
function(){
}
);
</script>
Different extensions can hook up to different events that are triggered during processing the output (so even after template is rendered).
Best option I found so far is creating system plugin and moving it's order to be the last of available system plugins.
Now you have two options:
use onBeforeCompileHead event (I guess introduced Joomla 1.5.23) to move scripts to headjs.
use onAfterRender event and parse head html code so it's loaded using headjs.
This is fine if you play around with async scripts loading where you have total control over the website, but it's almost impossible to implement as universal extension for Joomla. Some extensions use inline scripts in head, some in the the html body and you'd have to preserve order of execution (simple example: Mootools have to load first).
I use to load asynchronously only those scripts which I've included myself (in my template or my extension). Any scripts that are added with Joomla core (Mootools, core.js, etc.) or other extensions I don't touch.

Resources