This code at the moment picks 6 random images from my images directory and places them in a sidebar for display. The problem is that I don't want the sidebar to display just 6 images, I want the sidebar to generate images to match the height of my main content div.
for($i = 0 ; $i < $imagesCount ; $i++) {
$randomIndex = array_rand($images);
$file_title = $randomImage = $images[$randomIndex];
unset($images[$randomIndex]);
?>
<a href="<?php echo $randomImage ?>"><img src="<?php echo $randomImage ?>"
width="100%" height="150%"; alt="" /> <br /><br /></a>
<?php
}
?>
Right now my idea was using jquery to find the height of the div and generating images until the value of the height is reached using something like this pseudo code:
for($i = 0 ; $i < $("#content").height(); ; $i++) {
random images stuff here;
}
The problem here (besides the fact that I am mixing JQuery and PHP which I would fix later) is that .height() is going to return me a value in pixels thus randomly generating 800-1200 random images. Does anyone have any ideas on how to approach this problem? Here is a link to the page so you can see what I mean if there is any confusion. Link
There's really a lot of ways you can approach this but...
Use php to create your dom elements like so and pass them off to jQuery as a variable:
<?php
$images = howeverYourGettingYourSixImages();
for($i=0; $i<count($images); $i++){
$image_array[]='<img class="images" src="'.$images[$i].'"/>';
}
echo('<script type="text/javascript">var images='.json_encode($image_array).'</script>');
?>
somewhere in your body declare your container
<div id="content"></div>
and before the body close execute the jQuery
<script type="text/javascript">
var container = jQuery('#content');
var container_height = container.height();
var image_height = container_height / images.length;
jQuery.each(images,function(k,v){
container.append(v);
});
jQuery(".images").height(image_height);;
</script>
and to ensure your content container has a height and that the elements stack add some css to your head
<style type="text/css">
#content{
height: 600px;
}
.images{
float:left;
clear:both;
}
</style>
Related
I want to display a product page with every item aligned with each other. These products are dynamically generated using PHP code. The problem is, they won't align horizontally when one product has a longer name than the other.
To further understand my problem, here is a screenshot:
You can see that the product prices are not aligned with each other because they are influenced by the length of the product names.
Here is my PHP and HTML code:
<!--PHP CODE FOR EXTRACTING DATA FROM TABLE-->
<div class="span_container">
<div class="col-md-4 bottom-cd simpleCart_shelfItem" style="display: inline-block;">
<div class="product-at ">
<a href="single.php"><img class="img-responsive" style="width:300px; height:auto;" src="/EDGE/storeadmin/product_images/<?php echo $row['i_image']; ?>" alt="">
<div class="pro-grid">
<span class="buy-in">View Details</span>
</div>
</a>
</div>
<p class="tun"><?php echo $row['i_name'];?></p>
<p class="number item_price"><i> </i>₱<?php echo $row['i_price']; ?></p>
</div>
</div>
And this is the CSS code of my paragraph (tun):
p.tun {
font-size:1em;
color: #949494;
text-align: center;
line-height: 1.3em;
padding: 1em 0;
}
This is simple. Set position:relative; for the containing .simpleCart class, and a height like height:300px - replace 300 with whatever your box height needs to be so every containing box has the same height.
Then use position:absolute; to position the elements within .simpleCart according to the heights that we know already.
We know and can deduce:
the height of the image: therefore we use, as an example: position:absolute;top:5%; - change 5% according to your needs.
the height of the product prices and that those will be at the bottom no matter how high the .simpleCart box will be. So we use position:absolute;bottom:5% - change 5% according to your needs, again. Now every product price is at the same level. Provided that every .simpleCart box is of the same height.
Finally, we place the paragraph text with the .tun class in between with position:absolute;top:150px; - where you change 150 according to your needs so it fits between the image and the product pricing.
You can use the following js function to match the module heights to the tallest.
equalheight = function(container){
var currentTallest = 0,
currentRowStart = 0,
rowDivs = new Array(),
$el,
topPosition = 0;
$(container).each(function() {
$el = $(this);
$($el).height('auto')
topPostion = $el.position().top;
if (currentRowStart != topPostion) {
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
rowDivs.length = 0; // empty the array
currentRowStart = topPostion;
currentTallest = $el.height();
rowDivs.push($el);
} else {
rowDivs.push($el);
currentTallest = (currentTallest < $el.height()) ? ($el.height()) : (currentTallest);
}
for (currentDiv = 0 ; currentDiv < rowDivs.length ; currentDiv++) {
rowDivs[currentDiv].height(currentTallest);
}
});
}
$(window).load(function() {
equalheight('.equalise-height');
});
This is semi related to my last question but i have set up a filemaker foreach loop to output a group of images to accompany there names and ids, along with a checkbox.
Once checked the relating images go to another page to print, No matter how much i try i cant get the elements to fit to one page ?
I have used inline styling, a Print css stylesheet, all possible combinations with chromes inspector.
I can make it fit, once the image name is taken away, but i need this included.
include('head.php');
if (isset($_POST['img'])) {
$img = $_POST['img'];
} else {
$img = '';
echo 'error';
}
?>
<div class="container">
<div class="row" >
<?
foreach ($img as $image){
//echo '<div class="col-md-5">';
echo '<img class="" style="margin:20px 10px 10px 0px; width:45%;" src="Invoices/Photos/RC_Data_FMS/Invoices_db/Photos/'.$image.'">';
echo '<p class="centered" style="width:45%;">'.$image.'</p>';
//echo '</div>';
}
?>
</div>
</div>
Basically i'm trying to achieve an A4 portrait, with a grid of 6 images, with a margin between and the label underneath.
I tried pushing everything into a col-md-6 div, taking the <p> tags away but this didn't help.
I also tried using px opposed to %, just cant figure this one out.
The code from the previous page;
echo '<input type="checkbox" class="form-control check" id="img" name="img[]" value="'.$pic.'">';
With the $pic variable being the image name.
I think you can achieve what you want by tweaking the img height in #media print, like so:
#media print{
img{max-height:280px} //tweak this until you're happy
}
Also, don't forget the img-responsive bootstrap class - it works wonders:)
<div class="col-xs-6"> <!-- this will limit img width if img-responsive used too-->
<img class="img-responsive" src="image.jpg">
<p>Filename 1</p>
</div>
You can probably also forgo most of your inline styling and stick with bootstrap's defaults at first, then tweak later if you really need to.
http://www.bootply.com/YZkHOq0C1i
We are making a website for a school project and we help with some (simple) PHP.
We got a website with 4 pages, we are using includes for the header/navigation. But we want different background colors for our pages, but if we change the color, the color is going to be changed on all the sites because we use includes.
This is an example of a page:
<?php
$pagetitle = "Forside";
require_once("includes/header.inc.php");
?>
###SOME CONTENT FOR THIS PAGE###
<?php
require_once("includes/footer.inc.php");
?>
We want to change the color of the < body > tag which is inside in "includes/header.inc.php"
But as I said if we change that color, the color is changed on all the pages where we use the header include.
Is it possible to change this with some PHP?
Our navigation is pretty simple it can bee seen here:
<nav id="menu">
<img class="navigation" src="./img/forside-billede.png" />
<img class="navigation" src="./img/gaestebog-billede.png" />
<img class="navigation" src="./img/citater-billede.png" />
<img class="navigation" src="./img/koncept-billede.png" />
</nav>
Please write if you need any more information in order to help us or if you dont understand our problem. Thanks in advance!
You can set the color on each PHP file before you include the header. Then you can use that PHP variable in css in the header.inc.php
<?php
$pagetitle = "Forside";
$bodyColor = "#ff0000";
require_once("includes/header.inc.php");
?>
###SOME CONTENT FOR THIS PAGE###
<?php
require_once("includes/footer.inc.php");
?>
If the css is between your header tag you can do something like this in your css:
....
....
body {
background:<?=$bodyColor;?>;
}
....
....
If it isn't you could use the style attribute, e.g.:
....
....
<body style="background:<?=$bodyColor;?>;">
....
....
<?php
$path_parts = pathinfo(__FILE__);
$page = $path_parts['filename'];// Get the page name
$page = ! empty($page) ? $page : 'index'; // if no page name, set it to `index`
$backgrounds = array(); // define array of background colors.
$backgrounds['index'] = 'BACKGROUND COLOR FOR THIS PAGE'; //assign colors for pages.
$backgrounds['gaestebog'] = 'BACKGROUND COLOR FOR THIS PAGE';
$backgrounds['citater'] = 'BACKGROUND COLOR FOR THIS PAGE';
$backgrounds['koncept'] = 'BACKGROUND COLOR FOR THIS PAGE';
$background = ! empty($backgrounds[$page]) ? $backgrounds[$page] : ''; // get respective color for the page.
?>
header.inc.php
<body style="background-color:<?php echo $background?>">
you can change the background color in the HTML code of each of your pages using tags:
<style type="text/css">
body{ background-color: #ccc;}
</style>
I am creating a social website.I want to show all the details of registered users from the database in a list which includes their image and name.I want to display an image if the user has not provided the image.If the image has been provided by the user,sometimes it may get lost from database.At that time also i need to display a particular image.I have found that by using javascript method onerror(),we can check whether the image is loaded or not.I am unaware of its implementation.
My code sample is like this:
while($row_data = mysql_fetch_array($result_personal))
{
$row_photo = $row_data['acnt_profile_picture'];
<div class="feedstory" id="feedstory">
<img src="<?php if($row_photo!=NULL){echo $row_photo;}
else{?>images/no_image.png<?php }?>" id="image1" width="34" height="34" align="left" class="feedthumb" alt="photo"
onerror="imageError()"/>
</div>
}
My javscript method is like this:
function imageError()
{
document.getElementById("image1").src="images/no_image.png";
}
I need to display no_image.png if $row_photo is null or $row_photo is not loaded
You've said (in the comments)
if the variable is null,"no_image.png" is displaying.Bit if the image is not loaded,"no_image.png" is not been displayed
So it's not a path issue, there's something wrong with the imageError call.
The call would be right if you had an img element with the id "image1". But your img element doesn't have that id, and you haven't shown a different one that does.
If your goal is to show no_image.png in the img element that had the error, then:
Change your onerror attribute from:
onerror="imageError()"
to
onerror="imageError(this)"
And then change imageError to:
function imageError(img)
{
img.src="images/no_image.png";
}
Make sure you enclose your php code in php tags.
jQuery solution:
<?php
while($row_data = mysql_fetch_array($result_personal)) {
$row_photo = $row_data['acnt_profile_picture'];
?>
<div class="feedstory" id="feedstory">
<img src="<?php echo $row_photo ?>" width="34" height="34" align="left" class="feedthumb" alt="photo" />
</div>
<?php } ?>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
$('img').error(function() {
$(this).attr({
src: '/images/missing-image.jpg',
alt: 'Sorry! This image is not available!',
style:'border: 1px solid #f00;'
});
});
});
</script>
Depending on whether or not your database structure is the same as mine, you can use this function, I put it inside my User class.
public static function ProfileImage($user_ID, $image_ID, $size)
{
global $http_r;
$image_URL;
if($image_ID != null)
$image_URL = $http_r ."/userfiles/images/". $user_ID ."/thumbnails/". $image_ID .".jpeg";
else
$image_URL = $http_r ."/template/images/noprofilepic.jpg";
?>
<img src="<?php echo $image_URL; ?>" class="profile-<?php echo $size; ?>">
<?php
}
Which is then used like the following: User::ProfileImage($user_ID, $image_ID, "small");
But, I have a seperate table for my images and I do not store image paths directly.
I have an HTML page with 5 images (48px × 48px each) on it. I want to appear the images at random locations on the webpage everytime the page is loaded.
I don't know how to implement this, I just know I'm going to need CSS and JavaScript (for randomization) for this. Any ideas or suggesions?
Here is sample HTML code:
<img alt="Twitter" src="/images/twitter-48.png" />
<a href="http://facebook.com/><img alt="Facebook" src="/images/facebook-48.png" /></a>
<img alt="Google Plus" src="/images/plus-48.png" />
<img alt="GitHub" src="/images/github-48.png" />
Here's a rather simple implementation: http://jsfiddle.net/Eu8wT/
$('div.randomizeme').css({
top: Math.random() * 100+'%',
left: Math.random() * 100+'%'
});
To apply this over several elements:
$('div.randomizeme').each(function(){
$(this).css({
top: Math.random() * 100+'%',
left: Math.random() * 100+'%'
});
});
Here's the same thing without jQuery:
var elements = document.querySelectorAll('.randomizeme');
for (var i in elements) {
elements[i].style.top = Math.random() * 100 + '%';
elements[i].style.left = Math.random() * 100 + '%';
}
A solution that uses pure JavaScript (no library)
var imgs = document.querySelectorAll('.rand'),
len = imgs.length,
/* subtract the width/ height of images */
w = document.body.clientWidth - 48,
h = document.body.clientHeight - 48;
for(var i = 0; i < len; i++) {
imgs[i].style.top = Math.floor(Math.random() * h) + 'px';
imgs[i].style.left = Math.floor(Math.random() * w) + 'px';
}
working demo
Lets assume that you already have the images hardcoded on to your html document. What you would need to do (if you want to accomplish this with PHP) is add a style attribute to each element. In your case, your images are held within <a> tags, so you'll want to position the <a> tag randomly and not the images...
function generateRandomPositions(){
// define maximum and minimum 'top' values
$maxTop = 1000;
$minTop = 0;
// define maximum and minimum 'left' values
$maxLeft = 1000;
$minLeft = 0;
$styleProperties = array(
'position:absolute',
'top:'.rand($minTop,$maxTop) . 'px',
'left:'.rand($minLeft,$maxLeft) . 'px'
);
return ' style="'.implode(';',$styleProperties).'" ';
}
The function will return a string similar to this -
style="position:absolute;top:10px; left:45px;"
Now all you have to do is call this function for each image on your page -
<a <?php echo generateRandomPositions();?> ><img src="...
<a <?php echo generateRandomPositions();?> ><img src="...
<a <?php echo generateRandomPositions();?> ><img src="...
<a <?php echo generateRandomPositions();?> ><img src="...
Your <a> tags will now contain random CSS positioning parameters and their images will move with them.
As I'm sure you can see, this method of using PHP to generate inline CSS properties is kind of a backwards way to do this. JavaScript would probably be the best way to get what you are looking for and there are other answers that cover it. You could initially hide your elements with CSS and then display them with the JavaScript after you have set the random positioning.
References -
rand()
implode()
<?php
$images= array(
'twitter-48.png',
'facebook-48.png',
'plus-48.png',
'github-48.png'
);
$keys= range(0, count($images)- 1);
shuffle($keys);
foreach($keys as $key) {
print "<div>{$images[$key]}</div>";
}
EDIT:
<?php
$images= array(
array('href'=> 'http://twitter.com/', 'alt'=> 'Twitter', 'src'=> '/images/twitter-48.png'),
array('href'=> 'http://facebook.com/', 'alt'=> 'Facebook', 'src'=> '/images/facebook-48.png'),
array('href'=> 'https://plus.google.com/', 'alt'=> 'Google Plus', 'src'=> '/images/plus-48.png'),
array('href'=> 'https://github.com/', 'alt'=> 'GitHub', 'src'=> '/images/github-48.png')
);
$keys= range(0, count($images)- 1);
shuffle($keys);
?>
<div class='location1'>
<a href='<?php print $images[$keys[0]]['href']; ?>'><img alt='<?php print $images[$keys[0]]['alt']; ?>' src='<?php print $images[$keys[0]]['src']; ?>'></img></a>
</div>
<div class='location2'>
<a href='<?php print $images[$keys[1]]['href']; ?>'><img alt='<?php print $images[$keys[1]]['alt']; ?>' src='<?php print $images[$keys[1]]['src']; ?>'></img></a>
</div>
<div class='location3'>
<a href='<?php print $images[$keys[2]]['href']; ?>'><img alt='<?php print $images[$keys[2]]['alt']; ?>' src='<?php print $images[$keys[2]]['src']; ?>'></img></a>
</div>
<div class='location4'>
<a href='<?php print $images[$keys[3]]['href']; ?>'><img alt='<?php print $images[$keys[3]]['alt']; ?>' src='<?php print $images[$keys[3]]['src']; ?>'></img></a>
</div>
Try this:
$images = array('1.gif', '2.gif', '3.gif');
$images = array_shaffle($images);
foreach ($images as $image) {
echo "<img src='{$image}' />;
}