Controllers placed in Sub-Folders have no content - php

I am using a PHP Framework for the first time to get away from writing spaghetti code and so far it is wonderful. I am using Code Igniter and have a base Controller named "Dashboard.php" that loads fine and looks perfect.
When attempting to create some organization, I placed my "Devices.php" into a "Controllers/Devices" sub-folder, then accessed it at "http://domain.com/devices/devices" which loads the text, but the images/css/etc. are missing.
The content of the "Devices" controller is exactly the same as my "Dashboard" so I am not understanding why only the text loads. If I move the "devices.php" file into the root Controllers directory, there are no problems.
class Devices extends CI_Controller
{
public function index()
{
$data['page_title'] = 'Device Portal';
$this->load->view('meta');
$this->load->view('sidebar');
$this->load->view('userbar');
$this->load->view('header');
$this->load->view('devices/content');
$this->load->view('footer');
}
}
Here is the "Sidebar.php" View...
<!-- Left side content -->
<div id="leftSide">
<div class="logo"><img src="assets/images/logo.png" alt="" /></div>
<div class="sidebarSep mt0"></div>
<!-- Search widget -->
<form action="" class="sidebarSearch">
<input type="text" name="search" placeholder="search..." id="ac" />
<input type="submit" value="" />
</form>
<div class="sidebarSep"></div>
<!-- General balance widget -->
<div class="genBalance">
<a href="#" title="" class="amount">
<span>General balance:</span>
<span class="balanceAmount">$10,900.36</span>
</a>
<a href="#" title="" class="amChanges">
<strong class="sPositive">+0.6%</strong>
</a>
</div>

You need to avoid using relative paths when linking to images/css/etc in your views.
As an example, when you use assets/images/logo.png (relative path) from the page http://domain.com/devices/devices, the browser looks for http://domain.com/devices/assets/images/logo.png
Using the same image path with the page http://domain.com/devices the browser will look for http://domain.com/assets/images/logo.png instead.
When specifying urls to pages, images, etc within your domain, use the URL helper functions to automatically format your urls.
You want to avoid writing urls like this:
<script type="text/javascript" src="./assets/js/jquery.js"></script>
<img src="assets/images/logo.png" alt="" />
And instead generate your urls like this:
<script type="text/javascript" src="<?php echo base_url('assets/jquery.js'); ?>"></script>
<img src="<?php echo base_url('assets/images/logo.png'); ?>" alt="" />
Which will output:
<script type="text/javascript" src="http://domain.com/assets/js/jquery.js"></script>
<img src="http://domain.com/assets/images/logo.png" alt="" />

In Codeigniter use base_url() function to print assets.
<img src="<?php echo base_url('assets/images/logo.png');?>" alt="" />
This function automatically generate domain before the url.

Related

How to Fix My Image Didn't Showing In My Page?

My image didn't call, with the current path. How to fix it?
The folder data in out of folder application.
I'm using a framework Codeigniter with Bootstrap.
<center>
<a href="../../data/images/buying.png" data-fancybox="group" data-caption="How to Buying in Store">
<img class="img-flu rounded mb-4" src="../../data/images/buying.png" alt="" >
</center>
First, you need to set up this on application->config->config.php before using the base_url().
$config['base_url'] = 'http://localhost:8000/sitename';
In view
<center>
<a href="<?php echo base_url() ?>data/images/buying.png" data-fancybox="group" data-caption="How to Buying in Store">
<img class="img-flu rounded mb-4" src="<?php echo base_url() ?>data/images/buying.png" alt="" >
</center>
For more info about the proper setup or uses of base_url()
How to set proper codeigniter base url?

xampp php not importing css, images, but imports php files

I am trying to copy a website to my local machine. I have downloaded all the code, and things like page redirection, and importing work fine. But displaying images, css, and using javascript doesn't seem to work.
<?php
$root = $_SERVER['DOCUMENT_ROOT'];
include ($root . '/resources/config.php');
echo "$imagepath/user_suit.png";
?>
<div class="sidebar1">
<ul class="nav">
<li>
<div id="loginLink" class="menu" onmousedown="return false;" >
<img src="
<?php
echo $imagepath
?>/user_suit.png" width="15" height="14" alt="Member Login"> Member Login
</div>
</li>
</ul>
Welcome Guest!
</div>
<script type="text/javascript">
$("#loginLink").click(function(){
// load contact form onclick
$("#contentPane").hide();
$("#loadingPageGif").show();
$("#contentPane").load("
<?php
echo $server
?>/code/login/Studentlogin.php",
function() {
$("#loadingPageGif").hide();
$("#contentPane").fadeIn();
});
});
</script>
In this code, the embedded image does not load, and clicking on the link does nothing.
I can right click on the unloaded image, go to view image, and it correctly displays.
I can put echo lines in $root . '/resources/config.php' and they show up correctly.
The site I downloaded it from does not have these issues, so I am guessing it has something to do with xampp.
The browser adds http://, you should make that
<div id="loginLink" class="menu" onmousedown="return false;" > <img src="http://localhost:82/public_html/images/user_suit.png" width="15" height="14" alt="Member Login"> Member Login </div>

How to get URL from specific <img src=> and put into <a href=> for use with Lightbox

I have built a site for someone else who will be updating it via a CMS (CushyCMS). They wanted a lightbox gallery included. I want to be able to allow them to upload a new image and for that image url to be copied into the a tag so lightbox works.
So far I am able to do that, but the client has to ensure the file names and format are exactly the same as what I have set them to in the php code. Is there a way to get the img url from the img tag (possibly identifying it using an id attribute) and put it directly into the tag?
Heres what I have so far:
<?php
$src1 = "images/test1.jpg";
$src2 = "images/test2.jpg";
?>
<a href="<?php echo $src1?>" data-lightbox="group-1">
<img class="cushycms profile" name="image1" id="slideshow_image" src="images/test1.jpg"/></a>
<a href="<?php echo $src2?>" data-lightbox="group-1">
<img class="cushycms profile" id="slideshow_image" src="images/test2.jpg"/></a>
Many thanks!
You can do it with jQuery:
<a data-lightbox="group-1">
<img name="image1" src="images/test1.jpg" />
</a>
<script>
var img = $('img[name="image1"]');
var imgSrc = img.attr('src');
img.parent().attr('href', imgSrc);
</script>
If you have multiple images and want to automate this you can use the map function:
<a data-lightbox="group-1">
<img name="image1" src="images/test1.jpg" class="slideshow" />
</a>
<a data-lightbox="group-1">
<img name="image2" src="images/test2.jpg" class="slideshow" />
</a>
<a data-lightbox="group-1">
<img name="image3" src="images/test3.jpg" class="slideshow" />
</a>
<script>
$(".slideshow").map(function() {
$(this).parent().attr('href', this.src);
});
</script>
Assuming you use jQuery somewhere there, you could/should use a lightbox plugin, like this one here:
http://www.jacklmoore.com/colorbox/
Using the xamples there, you would then use:
(http://www.jacklmoore.com/colorbox/example1/)
$('a.gallery').colorbox({rel:'group-1'});
which would result in a lighbox gallery showing on "click" on any anchor with class "gallery" while the gallery would show all the elements pointed by "href" from all anchors of that have class="group-1" (so each anchor would be
although you have to include some files for jquery and the lightbox plugin, I think it will make your life much easier in the end.
Also not sure what your php level is, but the example code you have there asks for:
<?php
$images = array(
'images/test1.jpg',
'images/test2.jpg'
);
?>
<?php foreach($images as $i => $url): ?>
<a href="<?php echo $url?>" data-lightbox="group-1">
<img class="cushycms profile" name="image<?php echo $i+1 ?>" id="slideshow_image" src="<?php echo $url ?>"/>
</a>
<?php endforeach; ?>
Just in case you got more images there.

How to load Images in Codeigniter?

Hey i am totally new in Codeigniter. In the Controllers folder I created a file named caller.php and I created a file home1.php in \Views. In root directory i created an image folder named \Images and also created a css folder named \css. In images Folder there are 6 picture. In css folder style.css file exist.
In caller.php i write
<?
class caller extends CI_Controller
{
function index()
{
$this->load->view('home1');
// what i have to write here lo load images....
}
}
In home1.php i write
<html>
<head>
<link rel="stylesheet" type="text/css" href="<?php echo base_url();?>css/style.css">
// what i have to write here to load images
</head>
<body>
<div id="outer">
<div id="container">
<div id="images">
<img src="new.jpg" width="960" height="400"/>
<img id="image1" src="1.jpg" />
<img id="image2" src="2.jpg" />
<img id="image3" src="3.jpg" />
<img id="image4" src="4.jpg" />
<img id="image5" src="5.jpg" />
</div>
<div id="slider">
1
2
3
4
5
</div>
...................................................
....................................................
</div>
</div>
</body>
</html>
Now for the above code how i load the images .Please experts help me.
if additional config's are needed please mention that.
As you're already using the url helper I suggest wrapping your image src attributes with base_url, such as:
<img src="<?php echo base_url('images/1.jpg'); ?>" />
And as mentioned in the other answer it's best (mandatory?) to capitalize the class name in your controller
class Caller extends CI_Controller { ...
First of all, you need to capitalize your class name.
class Caller extends CI_Controller {
public function index()
{
// method code goes here
}
}
Then, you need to link to those images with absolute links or with the CI URL helper: "/images/1.jpg" and so on.
How to use with the CI URL helper is detailed here : Helper
EDIT
Load the URL helper with this in your constructor method:
$this->load->helper('url');
You can create a URL like this:
echo base_url("blog/post/123");
That will make:
http://example.com/index.php/news/local/123
Or
http://example.com/news/local/123
If you've taken out the index.php in your config file.
Here's a class with the constructor that calls the URL helper:
class Caller extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->helper('url');
}
public function index()
{
// method code goes here
}
}
Make folder in root directory and code easily in views html file.
if directory name is directoryName and image name imageName.jpg
you can call like this.
<img src="directoryName/imageName.jpg">
You can do the same for as you did to Load the CSS files.
Create a folder in Root Directory. Create a sub folder(if you have Multisite)
then point your base_url in config file to your base path.
and then do the same
<img id="image1" src="<?PHP echo base_url(); ?>images/[sub-folder]/1.jpg" />
Only simple way
<img id="image1" src="<?php echo base_url('1.jpg'); ?>" />
That sit!!!

What would be the best way to make a widget that will link to 4 different websites?

I'm trying to limit maintnance headaches by avoiding having to copy and paste code and having to update it on several different sites. Should I use an iframe? So far I just used inline CSS to style it and plan on copying a pasting to 3 or 4 other sites. Kind of like:
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;">
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;">
</a>
</div>
If you don't mind having a slight delay, you could write a light JavaScript that loaded the content into the page via AJAX, much like facebook / many other widgets do.
The benefit that offsets the fact that your links are not part of the page's initial HTML is the fact that you can update the content of all widgets from one place, with no chance that you'll forget one int he future.
Have a look into how Facebook / Google + / Twitter / Everyone else does this.
Edit
Your question got me thinking about how one might do this, so I did it. I've made a working JSFiddle example.
Basically, you paste an empty div and a script tag into your target pages. The script references a file stored on your central server. It creates another script tag in the document, which itself contains a call to a function defined in the first script, which inserts your widget into the specified div on the page.
Pasted into your pages
<div id="placeholder-div"></div>
<script type="text/javascript" src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/script.js"></script>
First script content
(function loadContent() {
(function xss_ajax(url) {
var script_id = null;
var script = document.createElement('script');
script.setAttribute('type', 'text/javascript');
script.setAttribute('src', 'http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/content.php');
script.setAttribute('id', 'script_id');
script_id = document.getElementById('script_id');
if(script_id){
document.getElementsByTagName('head')[0].removeChild(script_id);
}
// Insert <script> into DOM
document.getElementsByTagName('head')[0].appendChild(script);
})();
})();
function callback(data) {
document.getElementById('placeholder-div').innerHTML = data;
}
Inserted script content:
<?php ob_start(); ?>
<div style="width:165px; height:40px; background: url('http://site1.com/images/DH-sharebar.gif') repeat-x top #333;float:right;margin-top:15px;margin-right:20px;border-radius:5px;border:1px #565656 solid;">
<a href="http://site1.com/" target="_blank" title="site 1">
<img src="http://davidhairabedian.com/davidhairabedian/images/DH-sharebar-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site2.org/" target="_blank" title="site 2">
<img src="http://site1.com/images/DH-sharebar-HPM-icon.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
<a href="http://site3.org/" target="_blank" title="site 3">
<img src="http://site1.com/images/DH-sharbar-EHF.png" style="border-radius:0;margin-top:5px;margin-left:3px;"/>
</a>
<a href="http://site4.org/" target="_blank" title="site 4">
<img src="http://site1.com/images/DH-sharebar-EHC.png" style="border-radius:0;margin-top:5px;margin-left:10px;"/>
</a>
</div>
<?php $content = json_encode(ob_get_clean());
echo "callback($content);";
And after all this, it occurred to me that you could just use an iframe:
<iframe src="http://pagesofinterest.net/stack-examples/what-would-be-the-best-way-to-make-a-widget-that-will-link-to-4-different-websit/iframe.html"></iframe>
Personally, I would use the JavaScript method, as this would allow me to modify the style of the widget whenever I wanted, without requiring my users to update their pages.

Categories