How to align dynamically generated divs or paragraphs - php

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');
});

Related

Looping to make outputs centered

I want to display all my categories in one page, 4 items per row.
If I have 4,8 or 12 items then I have no problem, however if I have 5 items or any "number of items" that is not divisible by 4 without any remainder appears on the next row (as I have set each item 25% of the screen with css) with subsequent gap space which looks really odd and empty. This is a problem and I call those remainder items indeBox as in independent boxes.
My end goal is to align those independent boxes on the centre.
I am trying to create an algorithm for this purpose.
Here is what I have done so far
The following function takes two parameters.
numberRestriction is the number of items allowed per row (4)
childrens is an object with all categories.
I am counting how many categories are present within the childrens object
and finding out if there are remainders.
function calculateDivision($numberRestriction, $childrens)
{
$arrayobj = new ArrayObject($childrens);
$itemLenght = $arrayobj->count();
$itemBasket = $itemLenght;
$remaining = $itemBasket % $numberRestriction;
return $remaining;
}
On the template
<?php
//$term = get_queried_object();
$childrens = get_categories(array('parent' => 27,'hide_empty' => false));
$rowRestriction =4;
$indeBox = calculateDivision($rowRestriction, $childrens);
$arrayobj = new ArrayObject($childrens);
$itemLenght = $arrayobj->count();
$i = 0;
if($indeBox == 0)
{
foreach($childrens as $index => $children)
{
$cat_image = z_taxonomy_image_url($children->term_id);
$cat_id = $children->term_id;
$desc = #explode('|', $term->description);
?>
<a href="<?php echo get_category_link($cat_id) ?>" >
<div class="food_list_single menus_list" style="background: url(<?php echo $cat_image ?>) no-repeat scroll right center / cover ">
<div class="food_hover menus_hover ">
<p style="width: 100%; overflow: hidden; padding: 10px; text-align: center;" class="food_hover_title_small"><?php echo $desc[2] ?></p>
<p class="food_hover_title"><?php echo $children->name ?></p>
<button class="btn btn-default food_button"><i class="fa fa-eye"></i>View Details</button>
</div>
</div>
</a>
<?php
} //foreach ends
}else
{
//loop all items
//set a counter
//everytime the counter reaches maximum item allowed in a row, counter should reset and add class to row even
//once it finds odd number in a row it should call the row uneven
$counter1 = 0;
foreach($childrens as $index => $children)
{
$counter1++;
$cat_image = z_taxonomy_image_url($children->term_id);
$cat_id = $children->term_id;
$desc = #explode('|', $term->description);
//out put div
?>
<a href="<?php echo get_category_link($cat_id) ?>" >
<div class="food_list_single menus_list" style="background: url(<?php echo $cat_image ?>) no-repeat scroll right center / cover ">
<div class="food_hover menus_hover ">
<p style="width: 100%; overflow: hidden; padding: 10px; text-align: center;" class="food_hover_title_small"><?php echo $desc[2] ?></p>
<p class="food_hover_title"><?php echo $children->name ?></p>
<button class="btn btn-default food_button"><i class="fa fa-eye"></i>View Details</button>
</div>
</div>
</a>
<?php
//also track the last items index
$last_items_index = $index;
?>
<?php
}
echo "Inde items :".$indeBox; //finds how many indeboxes
echo " Last items index " . $last_items_index ;
}
?>
Image
Solution: Not beautiful or efficient, but it works
Find out how many items in the array attach to a variable.
Find out how many boxes fit in a row and attach to a variable.
Calculate independent boxes using following function:
.
function calculateDivision($numberRestriction, $childrens)
{
$arrayobj = new ArrayObject($childrens);
$itemLenght = $arrayobj->count();
$itemBasket = $itemLenght;
$remaining = $itemBasket % $numberRestriction;
return $remaining;
}
Since I already know 4 boxes fit in a row, so there can only be either 1,2 or 3 independent boxes. and if there are 4 boxes then there are no independent boxes. I can run conditions
if($indeBox == 1)
{
//if there are one independent box remaining
//find the last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}
else if($indeBox == 2)
{
//if there are two independent boxes remaining
//find the second last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}
else if($indeBox == 3)
{
//if there are three independent box remaining
//find the third last item of the array and put a bootstrap class col-md-offset-3 to
//if not last item, no need for adding additional class
}

Generating random images to the height of another div. PHP

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>

How to put jssor slider count in caption

I have a jssor slider working, including displaying captions. Each caption shows a title in the first line and a description in the lines below the title. I'm looking to add the slider count to the caption, so to the right of the title I would show, for example, "2 of 47".
Based on another post I found, I added the below code to the js:
function DisplayIndex() {
$("#displayIndex").text(jssor_slider2.$CurrentIndex() + 1 + " of " + jssor_slider2.$SlidesCount());
}
DisplayIndex();
jssor_slider2.$On($JssorSlider$.$EVT_PARK, DisplayIndex);
I also added the "<div id='displayIndex'" line to the php code, subordinate to caption div, as below:
echo "<div class='caption2_text'>";
echo "<h1>$title</h1>";
echo "<p>$desc</p>";
echo "<div id='displayIndex' u='any' style='position: absolute; top: 10px; left: 790px; width: 100px; height: 26px;'></div>";
echo "</div>";
The count appears, but is in the inner container rather than in the caption, so any full width picture overlays the count and the count is not visible.
How can I put the count into the caption itself?
Any assistance is much appreciated.
I found that you placed the displayIndex element inside a caption. You know, a caption should be a child element of a slide. It always moves along with the slide.
The more reasonable way is to make the displayIndex to be a static/fixed element.
A static/fixed element is an element parallel with every slide. It should be treated as a slide, but with u="any" attribute specified, it's no longer a slide, it will always stay where it should be.
<div u="slides">
<div><!-- slide 1 --></div>
<div><!-- slide 2 --></div>
<div id='displayIndex' u='any' style='position: absolute; top: 10px; left: 790px; width: 100px; height: 26px;'>
<!-- this is a static element -->
</div>
</div>
Reference: http://www.jssor.com/development/slider-with-fixed-static-element.html

Add a border to each number in a div

I'm not sure if this is possible but I would like to add a border to each number in a div.
I know how to add a border to a div but I would like that each number in a div to have a border.
Example:
<div id="borders">12345</div>
The output should look like this:
(1) (2) (3) (4) (5)
Where "()" is the border.
PS: I don't want to use a separate div for each number because this number will be a php code, like this:
<div id="borders"><?php $number; ?></div>
Is it possible?
Not possible without adding extra markup:
Live example: http://codepen.io/anon/pen/gDHlA
Markup
<div id="borders">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
Css
span:before, span:after {
display: inline-block;
}
span:before {
content: "(";
}
span:after {
content: ")";
}
I've inserted parens, of course feel free to change them with a pipe (|) or use borders applied to span elements (in this case, give them also a display, a width and a height)
About your latest requirement, your code can easily adapted like so:
$number = 12345;
$span_number = "";
foreach (str_split($number) as $key => $digit) {
$span_number .= "<span>$digit</span>";
}
echo $span_number;
//output: <span>1</span><span>2</span>...<span>5</span>
I think it is impossible. You can wrap each number in a <span> and make border for spans
You can use span elements like this, but you have to build all tags according to the numbers, using php string functions to split each number. There is no way to do that without tags.
<div id="borders">
<span>1</span>
<span>2</span>
<span>3</span>
<span>4</span>
<span>5</span>
</div>
Your CSS:
#borders span {
display:inline-block;
border:1px solid red;
padding:2px;
margin:2px;/*if you want space between numbers*/
}

How to combine two images into one so I can vertically align across all browsers?

I'm creating an e-commerce site and I'm having trouble vertically centering all my thumbnails. The problem is all my images are different sizes and getting each one to vertical align across all browsers is turning out to be a pain. I've looked into the different CSS options, display-table, line-height, and others. They worked in modern browsers, but not well in IE (of course). My thought is the large big time sites are resizing the image (which I can do with no problem) and then overlaying the image on top of a background the exact size they need. Does anyone know if this is how it's done? IF so can you direct me to some documentation of how to do this in PHP?
Or if someone thinks I can do this without all the extra work of overlaying images please let me know. In-case you want to see what I'm working with here ya go:
HTML
<a href="#">
<div id="product">
<div id="product-image">
<img src="" border="0" />
</div>
<div id="product-name"></div>
<div id="product-price"></div>
</div>
</a>
OPTION 1 : JQUERY (this seemed to be my best hope, but couldn't get it to work right)
var h = $('#product-image').height();
$.map($('#product-image img'), function(e)
{
var top =( h- $(e).height())/2;
$(e).css("margin-top",top);
});
OPTION 2 : CSS
#product
{
float:left;
margin:5px;
width:200px;
height:200px;
border:1px solid #999;
}
#product-image
{
margin:2px auto;
width:194px;
height:145px;
text-align:center;
}
#product-image img
{
max-height: 100%;
max-width: 100%;
vertical-align:middle;
}
EDIT
I found the working code, thanks Explosion Pills. For anyone trying to get this work I would suggest using this jQuery method and Fiddle link http://jsfiddle.net/9VfUS/1/:
WORKING JQUERY
var h = $('div#product-image').height();
$('div#product-image img').each(function ()
{
var top = (h - $(this).height()) / 2;
$(this).css("margin-top",top);
});​
If you can use JavaScript, I would do it that way as it's surefire to get things to work the way you want. You are using .map for the wrong purpose. You want .each:
$('#product-image img').each(function () {
var top = (h - $(this).height()) / 2;
$(this).css("margin-top",top);
});
I assume that h was already calculated correctly as the tallest image or the height of the container or what have you. If it's not, then you have to do that.
Try this, if you know in advance the sizes of your images...
HTML:
<a href="#">
<div class="product">
<div class="product-image" data-image-loc="path_to_your_image"> </div>
<div class="product-name"></div>
<div class="product-price"></div>
</div>
</a>
CSS:
div.product-image {
background-position:center center;
background-repeat:no-repeat;
background-color: transparent;
background-attachment: scroll;
}
div.product-image-width-x-height {
width:{width}px;
height:{height}px;
}
JS:
$(document).ready(function() {
$('div.product-image').each(function() {
$(this).css({backgroundImage:url($(this).attr('data-image-loc'))});
});
});
If you don't know your sizes, then a resize script that serves all your images to a new size would fix that, and you would simply move the width/height css properties to the div#product-image CSS declaration.

Categories