PHP: Add html icons to a script - php

I need to add the same icon to each of the li class that are generated automatically by a PHP script that Im using in a customized widget, in a Wordpress site.
This is the script Im using in a wordpress widget to display some child pages.
<?php
$ancestor_id=24;
$descendants = get_pages(array('child_of' => $ancestor_id));
$incl = "";
foreach ($descendants as $page) {
if (($page->post_parent == $ancestor_id) ||
($page->post_parent == $post->post_parent) ||
($page->post_parent == $post->ID))
{
$incl .= $page->ID . ",";
}
}?>
<ul>
<?php wp_list_pages(array(
"child_of" => $ancestor_id,
"include" => $incl,
"link_before" => "",
"title_li" => "",
"sort_column" => "menu_order"));?>
</ul>
The code for the icon (has already a class asigned) that I need to be used is..
<img class='sideicons' src="http://accountabletest.com/wp-content/uploads/2013/12/iconSidebar1.jpg" alt="" >
How can I make each <li> generated to include the same icon?

use css, the rules you need are list-style-type and list-style-icon

Sounds like to do it in PHP would probably be more trouble than it's worth... How about javascript or jquery?
$('li').each(function(i,el){
$(this).prepend('<img src="path/to/image.ico" />');
})
If you need to change the existing source of an image, target with something like $('li img')
Change .prepend() to attr('src','your/new/image.jpg')

Why you are not using CSS? It is easier way to do. just assign a class to your ul and add a background image to li tags.
like:
<ul class="list">
<?php wp_list_pages(
array(
"child_of" => $ancestor_id,
"include" => $incl,
"link_before" => "",
"title_li" => "",
"sort_column" => "menu_order"
)
);?>
</ul>
<style>
ul.list li {
background: url(http://accountabletest.com/wp-content/uploads/2013/12/iconSidebar1.jpg) no-repeat left center;
padding-left: 10px;
}
</style>

Related

PHP specific images not aligning

I'm trying to make all images be displayed side by side with the following code:
<div id = "all-container">
<?php
$dir = "images/";
$arr = scandir($dir);
foreach ($arr as $img) {
if ($img != '.' && $img != '..') {
echo "<div class = 'img-container'><img class = 'image' src = 'images/$img'></div>";
}
}
?>
</div>
This is class 'img-container' in CSS:
.img-container {
float: left;
display: inline-block;
}
Apparently, this seems to work when I set the image source to some random picture from the internet.
But when I use my own images, they're still displayed top to bottom.
The images I'm using are the Windows 7 Sample Pictures (1024 x 768 in size, JPG).
I tried many times, and if I just change the source of the image, it seems to work.
Is there something wrong with my code?
you should nest img tags like below:
<div class = 'img-container'>
<img class = 'image' src ='http://lorempixel.com/200/200'>
<img class = 'image' src ='http://lorempixel.com/200/200'>
</div>
But your code will create div tag with img-container class each time it repeats;
you will need to print out :
<div class = 'img-container'>
before foreach and
</div> /*end of img-container div tag*/
after it.

Change CSS :before from PHP with multiple occurences

If in PHP, a program outputs a <div>, which has a :before part to it, which my code should choose the attributes for. As my code has multiple instances where the :before should have different attributes, I cannot just edit the .css file or create a <style> tag at the top.
I have tried to create a new <style> tag before each <div>, but all the attributes end up being the same last <style> (Yes, I know why). So my current thinking is to be able to edit the :before from within the <div style=""> but I can't seem to be able to get :before to be changed there.
Does anyone know how to either edit the :before part within the tag's style = "" if that is even possible or another solution to this?
My current hypothesis (which didn't work for the fact that the first letter in CSS stands for Cascading) :
$array = [["text" => "Example 1", "colour" => "#880000"],["text" => "Example 2", "colour" => "#008800"]
foreach($array as $current){
echo '<style>
div.before{
color: '.$current["colour"].';
}
</style>';
echo '<div><p>'.$current["text"].'</p></div>'
}
(Note: In the actual code it is coming from a database)
If you are using PHP to determine styling I would strongly advise creating a custom stylesheet that you conditionally control.
HTML
<link rel='stylesheet' type='text/css' href='css/style.css' />
<link rel='stylesheet' type='text/css' href='css/custom_style.php' />
CSS
<?php
header("Content-type: text/css; charset: UTF-8");
$brandColor = "#990000";
$linkColor = "#555555";
$CDNURL = "http://cdn.blahblah.net";
#header {
background: url("<?php echo $CDNURL; ?>/images/header-bg.png") no-repeat;
}
a {
color: <?php echo $linkColor; ?>;
}
ul#main-nav li a {
color: <?php echo $linkColor; ?>;
}
?>
Example Source http://css-tricks.com/css-variables-with-php/
Based on your code I would assign a class to the div depending on the color. You will need to replace the # with a letter for CSS to read it.
$array = [["text" => "Example 1", "colour" => "#880000"],["text" => "Example 2", "colour" => "#008800"]
foreach($array as $current){
$className = str_replace('#', 'C', $current["colour"]);
echo '<style>';
echo 'div.'.$className.':before {
color: '.$current["colour"].';
}';
echo '</style>';
echo '<div class="'.$className.'"><p>'.$current["text"].'</p></div>'
}

Laravel use HTML::image for background-image

how to use HTML::image() for inline styled with background-image such as this html tags:
this line is into my main.blade.php file and that is main skeletion of my template.
<div style="height: 45px;background-image:url('../public/images/header_pattern2.png');border-radius: 5px;position:relative">
in main.blade.php my page view correct template. but after redirect such as login.blade.php images do not show. but after switch to index i do not have a problem.
in public directory i have images,js,css folders and my public is this:
'public' => __DIR__.'/..',
i can remove public directory. how to convert background-image:url('../public/images/header_pattern2.png'); to blade HTML::image() tag?
You may try this:
<div style="height: 45px;background-image:url('{{ asset('images/header_pattern2.png') }}');border-radius: 5px;position:relative">
Update:
Actually, HTML::image() generates an <img /> tag and you are using a div, if you want to use this using HTML::div() then you may create a custom macro.
Update: I've created this custom macro:
/**
* Param $attr : An array of styles
* Param $html : HTML content for div
* Returns HTML DIV
*/
HTML::macro('divWithBG', function($attr = array(), $html = ''){
$style = 'style = ';
foreach ($attr as $key => $value) {
if($key == 'background-image') $style .= $key.':url(\'' . $value .'\');';
else $style .= $key . ':' . $value .';';
}
return "<div $style >$html</div>";
});
You may use it in blade view as:
{{ HTML::divWithBG(array('background-image' => asset('images/8_big.jpg'), 'height' => '45px', 'border-radius'=>'5px', 'position'=>'relative' )) }}
Output:
<div style="background-image:url('http://blog.dev/images/8_big.jpg');height:45px;border-radius:5px;position:relative;"></div>
Rendered Output:
Use the following function to solve it. This is assuming that the image exist.
public function inline($path)
{
$filetype = File::type( $path );
$response = Response( File::get($path), 200 );
$response->header('Content-Type', $filetype);
return $response;
}
I tried this and it worked
background: url('{{asset('uploads/avatars/'.$employee->avatar)}}')

FlexSlider and Fancybox

i'll try to build a slider with FlexSlider
the images in the slider should be viewed 'onclick' with Fancybox.
my code for now:
$(".slides li").fancybox();
$('#flex1').flexslider({
animation: "slide",
animationLoop: false,
itemWidth: 179,
itemMargin: 0,
minItems: 4,
maxItems: 4,
controlNav: false,
pauseOnHover: true,
slideshowSpeed: 5000,
keyboardNav: true,
slideshow: false,
});
in document ready
the HTML / PHP looks like this:
<div class="flexslider" id="flex1">
<ul class="slides home_single_item">
<?php
$handle=opendir ("pics/");
$i=0;
while ($datei = readdir ($handle)) {
$i++;
if($datei != "." && $datei != ".."){
echo '<li>';
echo '<img class="fancybox" src="pics/'.$datei.'" width="179px" height="224px" />';
echo '</li>';
}
}
closedir($handle);
?>
</ul>
</div>
If i click on a image, it where displayed as an small picture and it disappears from my gallery list.
Are there any issues known between Flexslider and Fancybox ?
Anyone has a solution ?
Thanks ;)
There is not conflict or whatsoever.
What you are doing is binding fancybox to the list element
$(".slides li").fancybox();
...so fancybox moves the content of li (the img tag) to the box. Since you have these properties in your img tag:
width="179px" height="224px"
... the image is small in fancybox.
At this point, fancybox is handling the content as inline content so the img tag is returned after closing fancybox with the css property display: none; (that is the expected behavior.)
What you have to do is to change this part of your php and add an a tag to target the image to be opened in fancybox like :
if($datei != "." && $datei != ".."){
echo '<li>';
echo '<a href="pics/'.$datei.'" class="fancybox">';
echo '<img src="pics/'.$datei.'" width="179px" height="224px" />';
echo '</a>';
echo '</li>';
}
... notice that we moved the class="fancybox" from the img tag to the a tag. Then bind fancybox to that a tag in your script like :
$(".slides a").fancybox();
or better
$(".slides a.fancybox").fancybox();
or much simpler and better
$("a.fancybox").fancybox();

Navigation with CSS and PHP - Image Based Hover Menu (jQuery)

I have a menu that is image based (one yellow and one blue, for example). I designed the buttons in Illustrator and then converted to PNG files. Right now, I'm using CSS for hovering affects.
So when I hover over the image, it changes. So this is good (because it works), but its far from perfect (that's why I'm here)... One of my buttons in CSS looks like this:
.home_menu, .about_menu, .video_menu, .demo_menu, .contact_menu {
float: left;
margin-bottom: 10px;
margin-top: 10px;
margin-left: 10px;
width: 100px;
height: 34px;
display: block;
}
.home_menu {
background: transparent url('../images/buttons/home_but.png');
}
.home_menu:hover {
background-image: url('../images/buttons/home_but_hov.png');
}
The HTML is like started out like so:
<div id="main_menu">
</div>
So basically I'm changing the CSS background image for each class.
Two questions. First, I'm trying to get each menu to be the blue version when on that page. So I wrote a PHP function to do this (in a class), just in case I want to avoid JavaScript. It looks like this:
// Display table and loop through links and images from array
public function print_navigation($image_dir, $ds, $page_nav, $page_nav_alt, $menu) {
$current_file = explode('/', $_SERVER['PHP_SELF']);
$current_page = $current_file[count($current_file) - 1];
$current_page;
//$i = 0;
foreach ($page_nav as $key => $value) {
$menu_output .= '<a href="';
$menu_output .= $key;
$menu_output .= '" id="';
$menu_output .= $menu[$key];
$menu_output .= '" style="background: transparent url(';
if ($current_page == $key) {
$menu_output .= $image_dir . $ds . $page_nav_alt[$key];
}
else {
$menu_output .= $image_dir . $ds . $page_nav[$key];
}
$menu_output .= ');"';
$menu_output .= '></a>';
$i++;
}
echo $menu_output;
}
It seems to work for the Home page ($home variable), but not for the others. I have variables like this (arrays and variables in another file, truncated for brevity):
$menu = array(
$home => 'home_menu',
...);
$page_nav_ylw = array(
$home => $home_but_ylw,
...);
$page_nav_blu = array(
$home => $home_but_blu,
...);
Then I have all the images in variables, referenced to in the arrays, eg, $home_but_ylw refers to the PNG for that button.
The PHP function is a bit odd, because I use the $key for two arrays, which I'm sure is bad. But I'm having a hard time getting it to work otherwise.
Second question is: is there any reason I can't add JavaScript (like jQuery) right on top of this to get me the hover effects so that I can remove it from the CSS? Ideally I'd like to display the buttons with a PHP loop that also handles current page and then do the hover affects with jQuery.
Sorry for the long post. Hope it makes sense.
Thanks in advance!
If you were planning on serving your pages dynamically then I think jQuery would be a much better option. However, if your links are going to separate pages then try something this:
function printNav($Page = "home"){
$HTML = "";
$HTML .= "";
$HTML .= "";
$HTML .= "";
$HTML .= "";
echo $HTML;
}
On each separate page:
<div id="main_menu">
<?php printNav("home"); ?>
</div>
CSS:
.ActiveNav {
background-image: url('../images/buttons/blue_bg.png');
}
.MenuItem {
float: left;
margin-bottom: 10px;
margin-top: 10px;
margin-left: 10px;
width: 100px;
height: 34px;
display: block;
}
.HomeMenuItem {
background: transparent url('../images/buttons/home_but.png');
}
.HomeMenuItem:hover {
background-image: url('../images/buttons/home_but_hov.png');
}
EDIT: If you wanted a different image for each button - I would suggest using a generic button background and hover and putting the text and icons on top of it.
Based on this answer, I was able to find a work around to my problem:
PHP menu navigation
Basically, I used the GET method to get the selected class. This worked nicely. I consolidated my CSS, and was able to get this thing working.
Here is what it turned out like, for one link:
<?php $class = $_GET['selected_tab']; ?>
<div id="main_menu">
<a href="index.php/?selected_tab=home" id="home_menu" title="Home"
class="<?php if(strcmp($class, 'home') == 0) {echo 'selected';} ?>"></a>
CSS like so:
#home_menu {
background: transparent url('../images/buttons/home_but.png');
}
#home_menu:hover, #home_menu.selected {
background-image: url('../images/buttons/home_but_hov.png');
}
Next step is to convert to jQuery.
Thanks Mike GB for your help but it wasn't quite what I was looking for.

Categories