Adding array to a model file in Codeigniter - php

I have the following array in my view file (which works):
<?php
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
foreach (array_combine($images, $captions) as $image => $caption ) {
echo "<li>", $image, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
?>
However I wish to use the same foreach in a slideshow file across multiple pages with different images displaying so i thought the simplest way would be to create a model page for each page that required different data and having the images and captions in an array as above. I have tried searching how to do this but all the solutions I have found relate to being in a database which is unfortunately not an option.
Also I would like to code it so that if there is not image caption the caption code does not run ie:
"<div class=\"orbit-caption\">",$caption,"</div>
The options I have tried either I get a blank semi transparent box ie it puts down:
<div class="orbit-caption"></div>
Or I getting nothing if I have a caption or not.
I understand a small amount of php but am more of a designer and am fairly new to MVC so will not be offended if anyone has a solution in an idiots guide :) just trying to learn. If anyone has a better solution then please share

Create a helper in application/helpers folder and name it slideshow_helper.php, Now, in slideshow_helper.php write :
<?php
function get_image_array(){
$images = array(img("slideshow/demo1.png", "one"),img("slideshow/demo2.png", "two"),img("slideshow/demo4.png", "three"));
$captions = array("first image", "second image", "third image");
return array_combine($images, $captions);
}
?>
In your controller function from where you load the view for slideshow page, write:
<?php
$data = array();
$this->load->helper('slideshow');
$data['slideshow'] = get_image_array(); #will return the image array from the helper
$this->load->view('view', $data); #load the image array in the view
?>
In your View:
<?php
foreach ($image as $imageSrc => $caption ) {
if($caption != ""){
echo "<li>", $imageSrc, "<div class=\"orbit-caption\">",$caption,"</div></li>";
}
}
?>

Related

How to get the intro image of a joomla article

I am trying to customize the the category list of my website. So I have a menu option that shows the list of all article is a certain category.
In this list I would like to show the into image of the article too. But unfortunately I cannot find the path of this intro image of each article to put it in the '' tag.
This code takes the first image in the article. Which is not what I want.
<?php
$text = $article->introtext;
if(preg_match('/<\s*img[^>]+>/Ui', $text, $matches)){
$image=$matches[0];
$text = str_replace($image,'',$text);
}
if ($matches) {
$image = str_replace('img src="','img width="150px" src="/demo/persianlondon/',$image);
$image = str_replace("caption","", $image);
echo '' . $image . '';
}
else {echo '<img width="150px" src="http://goo.gl/k47rNN" class="defaultArticleImage" />';}
?>
So I need to get the intro image url and set it for $image.
Any idea to do it?
If you are talkin about blog view of a category, this code will write out the intro image filename with path, if you put it in file:
jroot/component/com_content/views/category/tmpl/blog.php or in the template overrided blog.php file.
$article_items = $this->get('lead_items');
foreach ($article_items as $item) {
$imageObj = json_decode($item->images);
echo $imageObj->image_intro;
}
other variables of $imageObj:
stdClass Object
(
[image_intro] =>
[float_intro] =>
[image_intro_alt] =>
[image_intro_caption] =>
[image_fulltext] =>
[float_fulltext] =>
[image_fulltext_alt] =>
[image_fulltext_caption] =>
)

Wordpress NextGen Gallery - Only show if gallery has images

I have created 4 empty galleries(using Nextgen) and I have one list of static image links. The empty galleries have been created so that if someone wants to add images in them later, that gallery will show up in place of the static image links.
The static image links should up if no images are in the Nextgen gallery, but if there are images in the NextGen gallery - use those in place of the static image links.
This is the code I am using for the Nextgen gallery that I'm calling in the page template:
<?php global $nggdb;
$gallery = $nggdb->get_gallery(1, 'sortorder', 'ASC', true, 0, 0);
foreach($gallery as $image) {
echo '<img src="';
echo $image->imageURL;
echo '" />';
}
?>
I know I need to check to see if the specified gallery has images, and then if not, do an else statement, but I'm not exactly sure how to do this.
Any help is greatly appreciated. Thank you.
You could try something like this:
$gallery = $nggdb->get_gallery(1, 'sortorder', 'ASC', true, 0, 0);
if ( !empty($gallery) && is_array($gallery) && count(gallery) > 0 ) {
foreach($gallery as $image) { ... }
} else {
//show static image links
}

Fail to using if/else to hover image by dynamic url

I'm using the basic way to doing the hover image as the CSS method doesn't work for me. Current I'm using the if/else statement to do so. If the contain the URL like abc.com it will hover the image.
But now I only can hover the group url but if there is sub categories in groups I won't able to hover, how can I do it all the activity inside the group, the image will hover?
How to doing if the URL contain the words or path. For example abc.com/groups/* it will hover the groups. Similar like we doing searching in MySQL the words/variable as using "%".
<?php
$request_url = apache_getenv("HTTP_HOST") . apache_getenv("REQUEST_URI");
$e = 'abc.com/dev/';
$f = 'abc.com/dev/groups/';
$g = 'abc.com/dev/user/';
?>
<div class="submenu">
<?php
if ($request_url == $e) {
echo '<div class="icon-home active"></div>';
} else {
echo '<div class = "icon-home"></div>';
}
?>
<?php
if ($request_url == $f) {
echo '<div class="icon-groups active"></div>';
} else {
echo '<div class = "icon-groups"></div>';
}
?>
</div>
I propose a javascript way to do so, with jQuery
$("a[href*='THE_URL_PATTERN_YOU_WANT_TO_MATCH']").children(".icon-home").addClass("active");
BTW, it is NOT a good idea to wrap a div into a a tag.

Get last section from echo and store in variable PHP

I've a function that will echo the URL of the image from the content of Wordpress.
I got the function work now no problem
// Get Image Attachments
function sa_get_image($postid=0, $size='thumbnail') { //it can be thumbnail or full
if ($postid<1)
$postid = get_the_ID();
$thumb = get_post_meta($postid, "thumb", TRUE); // Declare the custom field for the image
if ($thumb != null or $thumb != '') {
echo $thumb;
}
elseif ($images = get_children(array( //If you upload an image function gets first image
'post_parent' => $postid,
'post_type' => 'attachment',
'numberposts' => '5',
'post_mime_type' => 'image', )))
foreach($images as $image) {
$thumbnail=wp_get_attachment_image_src($image->ID, $size);
?>
<?php echo $thumbnail[0]; ?>
<?php }
else { //If you don't upload or declare as thumb custom field func. gets custom (default) image
echo get_bloginfo ( 'template_directory' ); //same as wp-content/themes/your-theme/
echo '/images/image-pending.gif'; // Put this image into your themes images folder and set the path here
}
}
The only problem now is that the <?php echo $thumbnail[0]; ?> if there are more than one image it will echo all of them something like this
<img src=" http://applesiam.com/wp-content/uploads/2555-05-02_19h14_34-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h14_11-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_43-150x123.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_20-150x150.png http://applesiam.com/wp-content/uploads/2555-05-02_19h13_17-150x150.png ">
As you can see it just separated by some spaces.
Now I just want to have the last image if there is more than one image in the $thumbnail
I'm not really expert with PHP as my semester for PHP course will start next week.
Thanks in advance for any suggestion going to be.
Try:
$imgSrc = end((explode(' ', trim($imgSrc)));
Where $imgSrc is the value you put into <img src="!!!==>>here<<==!!!">.
quickly typed, w/o any warranty. Should leave single URLs intact, and if multiple ones separated by space(s) will take the last.
Try this instead.
echo trim($thumbnail[sizeof($thumbnail)-1]);

Getting Broken link on frontend for image uploaded via Wysiwyg editor in a custom module !

M getting this :
<img alt="" }}="" es_3.jpg="" wysiwyg="" src="{{media url=">
In my form i added this code
$wysiwygConfig = Mage::getSingleton('cms/wysiwyg_config')->getConfig(
array('tab_id' => 'form_section')
);
$wysiwygConfig["files_browser_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg_images/index');
$wysiwygConfig["directives_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["directives_url_quoted"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/cms_wysiwyg/directive');
$wysiwygConfig["widget_window_url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/widget/index');
$wysiwygConfig["files_browser_window_width"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_width');
$wysiwygConfig["files_browser_window_height"] = (int) Mage::getConfig()->getNode('adminhtml/cms/browser/window_height');
$plugins = $wysiwygConfig->getData("plugins");
$plugins[0]["options"]["url"] = Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin');
$plugins[0]["options"]["onclick"]["subject"] = "MagentovariablePlugin.loadChooser('".Mage::getSingleton('adminhtml/url')->getUrl('adminhtml/system_variable/wysiwygPlugin')."', '{{html_id}}');";
$plugins = $wysiwygConfig->setData("plugins",$plugins);
$fieldset->addField('longdescription', 'editor', array(
'name' => 'longdescription',
'label' => Mage::helper('press')->__('Description'),
'title' => Mage::helper('press')->__('Description'),
'style' => 'width:500px; height:300px;',
'config' => $wysiwygConfig,
));
I m still not clear about the above code but i copied it from somewhere,But i do know that it enables to browse for images files instead of writing custom url.
After that i just call it in frontend like this:
<?php echo $item["longdescription"]; ?>
I am getting the text , but not the image and for image i am getting the broken link mentioned at top.
Am i missing something ?? if yes then what ?
add this code :
<?php $_cmsHelper = Mage::helper('cms');?>
<?php $_process = $_cmsHelper->getBlockTemplateProcessor();?>
<?php echo $_process->filter($item["longdescription"]); ?>
It looks like you might be breaking on quotes somewhere, how are you passing in the image/url of the image? If you look right here: alt="" }}="" es_3.jpg="" you can see that you are passing in closing brackets somewhere, and are skipping the SRC entirely. Try VAR_EXPORTing the $item and show me what you have.
I had the same problem today. It turns out that Mage_Adminhtml_Cms_WysiwygController::directiveAction() tries to create an image according to a url. This function calls Varien_Image_Adapter_Gd2::open() which in turn tries to open the file. This is where it goes wrong:
The image adaptor tries to get information about the image, like image size and mime type. But... when your site is on a localhost or a vagrant box or something like that, the sever tries to getimagesize('http://www.domain.com/image.jpg') instead of getimagesize('/Users/john/sites/domain.com/image.jpg') (for example).
The fix for this is to override the directiveAction() in your own module to add another catch before throwing an exception:
public function directiveAction()
{
$directive = $this->getRequest()->getParam('___directive');
$directive = Mage::helper('core')->urlDecode($directive);
$url = Mage::getModel('core/email_template_filter')->filter($directive);
try {
$image = Varien_Image_Adapter::factory('GD2');
$image->open($url);
$image->display();
} catch (Exception $e) {
// Try to get an absolute path:
$path = Mage::getBaseDir().'/'.preg_replace('/http:\/\/(.*)\//Ui', '', $url);
$image = Varien_Image_Adapter::factory('GD2');
$image->open($path);
$image->display();
} catch (Exception $e) {
$image = Varien_Image_Adapter::factory('GD2');
$image->open(Mage::getSingleton('cms/wysiwyg_config')->getSkinImagePlaceholderUrl());
$image->display();
}
}
A nice little bonus: the broken links in your admin are now gone too! ;-)

Categories