add an incrementing number to id with php - php

I am making a portfolio site with kirby cms.
my site is a single page website. I want to add an incrementing number after id by using foreach in the part shown in the code below..!
Is there easy way to do..?
regards,
<?php foreach ($pages->listed() as $p): ?>
<div class="slideshow">
<div class="slideshow-box">
<div class="slideshow-holder">
<?php foreach ($p->images() as $workimage): ?>
<div class="slide">
<?= $workimage ?><br><br>
</div>
<?php endforeach ?>
</div>
<div class="tooltip">
<p id="myText📍<here>"><?= $p->worktitle() ?></p>
</div>
<div class="toolbox" id="myDIV📍<here>">
<?= $p->description() ?>
</div>
</div>
<div class="actions">
<span>1/3</span>
</div>
←
→
</div>
<?php endforeach ?>
<script>
var button📍<here> = document.getElementById('myText📍<here>');
var div📍<here> = document.getElementById('myDIV📍<here>');
div📍<here>.style.display = 'none';
button📍<here>.onclick = function () {
if (div📍<here>.style.display !== 'none') {
div📍<here>.style.display = 'none';
} else {
div📍<here>.style.display = 'block';
}
};
</script>

I know I am late to the party, but here is my version.
You can actually just set a $counter Variable before the foreach() and just count it up right before the end of the foreach.
Furthermore I would you data-attributes and query them instead of an id.
Here's my Code. I tested it and it worked fine. If there are any questions let me now.
<?php $counter = 1;
foreach ($pages->listed() as $p): ?>
<div class="slideshow">
<div class="slideshow-box">
<div class="slideshow-holder">
<?php foreach ($p->images() as $workimage): ?>
<div class="slide">
<?= $workimage ?><br><br>
</div>
<?php endforeach ?>
</div>
<div class="tooltip">
<p data-tooltip="<?= $counter ?>"><?= $p ?></p>
</div>
<div class="toolbox" data-toolbox="<?= $counter ?>" style="display: none;">
<?= $p ?>
</div>
</div>
</div>
<?php $counter++; endforeach ?>
<script>
const TOOLTIPS = document.querySelectorAll('[data-tooltip]');
TOOLTIPS.forEach(TOOLTIP => {
TOOLTIP.addEventListener('click', () => {
const TOOLBOX = document.querySelector(`[data-toolbox="${TOOLTIP.dataset.tooltip}"]`);
if (TOOLBOX) {
if (TOOLBOX.style.display !== 'none') {
TOOLBOX.style.display = 'none';
} else {
TOOLBOX.style.display = 'block';
}
}
});
});
</script>

Related

Trying to sort my multi-dimensional array by price

I am very new to PhP and programming in general, I looked at similar questions and tried most of the offered solutions but couldn't find a way to apply it to my situation.
I made a filter option on a page, I am now trying to have the filtered results come out as Ascending by price. If I am to use a sort function, where should I be using it in the code for it to make sense?
<?php
require "voitures.php"; [my array][1]
if (isset($_GET["prixMin"])) {
$prixMin = $_GET["prixMin"];
$prixMax = $_GET["prixMax"];
$voitures2 = [];
foreach ($voitures as $voiture) {
if ($prixMin <= $voiture['prix'] && $prixMax >= $voiture['prix']) {
$voitures2[] = $voiture;
}
}
}
?>
<form action=" <?= $_SERVER['PHP_SELF'] ?>" method="GET">
<label for="prixMin">Prix minimal : </label>
<input type="text" name="prixMin" value="<?php if(isset($_GET['prixMin'])){ echo $_GET['prixMin']; }?>">
<label for="prixMax">Prix maximal : </label>
<input type="text" name="prixMax" value="<?php if(isset($_GET['prixMax'])){ echo $_GET['prixMax']; }?>">
<input type="submit" value="Rechercher">
</form>
<br>
<div class="g" style="grid-template-columns: repeat(4, max-content)">
<div class="t">Marques</div>
<div class="t">Modeles</div>
<div class="t">Annee</div>
<div class="t">Prix</div>
<?php if (isset($_GET['prixMin'])) { ?>
<?php foreach ($voitures2 as $voiture2) : ?>
<div class="l">
<div class="c dr"><?= $voiture2["marque"] ?></div>
<div class="c"><?= $voiture2["modele"] ?></div>
<div class="c"><?= $voiture2["annee"] ?></div>
<div class="c mi"><?= $voiture2["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } else { ?>
<?php foreach ($voitures as $voiture) : ?>
<div class="l">
<div class="c dr"><?= $voiture["marque"] ?></div>
<div class="c"><?= $voiture["modele"] ?></div>
<div class="c"><?= $voiture["annee"] ?></div>
<div class="c mi"><?= $voiture["prix"] ?></div>
</div>
<?php endforeach ?>
<?php } ?>
</div>
</main>
<footer>
</footer>
</body>
</html>
array_sort_by_column($voitures2,'prix',SORT_DESC)
function array_sort_by_column($array, $column, $direction = SORT_ASC) {
$reference_array = array();
foreach($array as $key => $row) {
$reference_array[$key] = $row[$column];
}
array_multisort($reference_array, $direction, $array);
return $array;
}

Printing image from array

first off, I'm extremely new to PHP.
I'm using Conrete5, and I have a new template to an image slider. This is what I'm using:
http://codepen.io/altitudems/pen/KdgGLG
There's a placeholder image, which I'm trying to replace by grabbing the first image set in the block itself. Here is my view file from the actual block:
<?php defined('C5_EXECUTE') or die("Access Denied.");
$navigationTypeText = ($navigationType == 0) ? 'arrows' : 'pages';
$c = Page::getCurrentPage();
if ($c->isEditMode()) { ?>
<div class="ccm-edit-mode-disabled-item" style="width: <?php echo $width; ?>; height: <?php echo $height; ?>">
<div style="padding: 40px 0px 40px 0px"><?php echo t('Image Slider disabled in edit mode.')?></div>
</div>
<?php } else { ?>
<script>
$(document).ready(function(){
$(function () {
$("#ccm-image-slider-<?php echo $bID ?>").responsiveSlides({
prevText: "", // String: Text for the "previous" button
nextText: "",
<?php if($navigationType == 0) { ?>
nav:true
<?php } else { ?>
pager: true
<?php } ?>
});
});
});
</script>
<div class="ccm-image-slider-container ccm-block-image-slider-<?php echo $navigationTypeText?>" >
<div class="ccm-image-slider">
<div class="ccm-image-slider-inner">
<?php if(count($rows) > 0) { ?>
<ul class="rslides" id="ccm-image-slider-<?php echo $bID ?>">
<?php foreach($rows as $row) { ?>
<li>
<?php if($row['linkURL']) { ?>
<?php } ?>
<?php
$f = File::getByID($row['fID'])
?>
<?php if(is_object($f)) {
$tag = Core::make('html/image', array($f, false))->getTag();
if($row['title']) {
$tag->alt($row['title']);
}else{
$tag->alt("slide");
}
print $tag; ?>
<?php } ?>
<div class="ccm-image-slider-text">
<?php if($row['title']) { ?>
<h1 class="ccm-image-slider-title"><?php echo $row['title'] ?></h1>
<?php } ?>
<?php echo $row['description'] ?>
</div>
</li>
<?php } ?>
</ul>
<?php } else { ?>
<div class="ccm-image-slider-placeholder">
<p><?php echo t('No Slides Entered.'); ?></p>
</div>
<?php } ?>
</div>
</div>
</div>
<?php } ?>
Here is my gallery view file:
<a class="gallery-launcher" href="#gallery1"> // Location of the image
<div class="overlay">
<div class="overlay-content">
<button class="btn btn-default">Open Gallery</button>
</div>
</div>
</a>
<div class="gallery inactive" id="gallery1">
<div class="gallery-item">
//Fullscreen gallery code here
</div>
</div>
Where it says // Location of the image is where I need to have the first image set. I can't figure out what I'd put there? Any help will be appreciated.
You could use the concrete5 file helper to get the file path, like this for example:
$f = File::getByID($row['fID']);
$relpath = $f->getRelativePath();
In general I suggest that you read more about the concrete5 file functions to understand more how it works:

Magento - How to show realted products block in my product page?

I have this and i think this is a code for block which displays releated products for the product in the product page here it is.
/template/catalog/product/list/releated_products.phtml :
<?php if($this->getItems()->getSize()): ?>
<div class="related-products">
<h2><?php echo $this->__('Related Products') ?></h2>
<ol class="products-grid row" id="block-related">
<?php foreach($this->getItems() as $_item): ?>
<li class="item span3">
<?php if(!$_item->isComposite() && $_item->isSaleable()): ?>
<?php endif; ?>
<div class="product"> <img src="<?php echo $this->helper('catalog/image')->init($_item, 'thumbnail')->resize(590,714) ?>" alt="<?php echo $this->htmlEscape($_item->getName()) ?>" />
<div class="product-details">
<p class="product-name"><?php echo $this->htmlEscape($_item->getName()) ?></p>
<?php echo $this->getPriceHtml($_item, true, '-related') ?>
<?php if ($this->helper('wishlist')->isAllow()) : ?>
<?php echo $this->__('Add to Wishlist') ?>
<?php endif; ?>
</div>
</div>
</li>
<?php endforeach ?>
</ol>
<script type="text/javascript">decorateList('block-related', 'none-recursive')</script>
<script type="text/javascript">
//<![CDATA[
$$('.related-checkbox').each(function(elem){
Event.observe(elem, 'click', addRelatedToProduct)
});
var relatedProductsCheckFlag = false;
function selectAllRelated(txt){
if (relatedProductsCheckFlag == false) {
$$('.related-checkbox').each(function(elem){
elem.checked = true;
});
relatedProductsCheckFlag = true;
txt.innerHTML="<?php echo $this->__('unselect all') ?>";
} else {
$$('.related-checkbox').each(function(elem){
elem.checked = false;
});
relatedProductsCheckFlag = false;
txt.innerHTML="<?php echo $this->__('select all') ?>";
}
addRelatedToProduct();
}
function addRelatedToProduct(){
var checkboxes = $$('.related-checkbox');
var values = [];
for(var i=0;i<checkboxes.length;i++){
if(checkboxes[i].checked) values.push(checkboxes[i].value);
}
if($('related-products-field')){
$('related-products-field').value = values.join(',');
}
}
//]]>
</script>
</div>
<?php endif ?>
So how i can display this block in my view.phtml which display the info for the product view page. I want to paste it bellow the product description.
Thanks in advance!
Try this https://magento.stackexchange.com/questions/3177/showing-related-products-in-product-view-page
Make sure your current product has related product.

adding a more link based on the number of entries

I have this code that prints out up to 4 service providers. once clicked, the box expands and shows the rest if there are more. What i am trying to do is if there are more than 4 entries, to print a More link so users know to click for more. I also need the More button to disappear when click and reappear when reclicked.
Can anyone help me. This is driving me crazy
Thank you for your help ahead of time.
<?php
/**
* #file
*/
?>
<head>
<script>
var remove=function(){
$('#ID_OF_BUTTON').hide(500);
}
</script>
</head>
<div class="cloud-computing-item">
<div class="container">
<div class="item-header">
<h3> <?php print $company['name'] ?> </h3>
</div>
<div class="item-subheader">
<div class="label">Services Offered:</div>
<div class="data service-offerings">
<?php
foreach($company['services_display'] as $service => $element){
print $element;
}
?>
</div>
</div>
<div class="item-body">
<div class="overview">
<div class="label">Cloud Providers:</div>
<div class="data">
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"></div>
<!--<div id="show"style="color:#000099;font-weight:bold; margin-bottom:-13px;"><a onclick="remove(); return true;">More</a></div>
<div id="hide"style="color:#000099;font-weight:bold; margin-bottom:-12px; display: none;"><a onclick="add(); return true;">Hide</a></div> -->
</div>
</div>
<div class="details">
<?php
// if entries are greater then 4, show the rest
foreach(array_slice($company['service_providers'], 4) as $provider): ?>
<div>
<?php
print $provider;
?>
</div>
<?php endforeach; ?>
<?php print theme('cloud_computing_item_details', array('company' => $company)); ?>
</div>
</div>
<div style="clear: both; height: 5px;"> </div>
</div>
</div>
So, try this modification:
<div class="label">Cloud Providers:</div>
<div class="data">
<?php $i = 0; ?>
<?php foreach($company['service_providers'] as $provider): ?>
<div<?php echo ($i > 3) ? ' class="hide"' : ''; ?>><?php print $provider; ?></div>
<?php $i++; ?>
<?php endforeach; ?>
<?php if(count($company['service_providers']) > 4):?>
<div class="show-more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">Show More</div>
<?php endif; ?>
</div>
Now let's assume You are using jQuery add this:
<script type="text/javascript">
$(document).ready(function(
$('.hide').hide(); // this will hide all the elements with the class 'hide'
$('.show-more').live('click', function(){
var parent = $(this).parent();
parent.find('.hide').show().removeClass('hide').addClass('show');
$(this).text('Show Less').removeClass('show-more').addClass('show-less');
});
$('.show-less').live('click', function(){
var parent = $(this).parent();
parent.find('.show').hide().removeClass('show').addClass('hide');
$(this).text('Show Less').removeClass('show-less').addClass('show-more');
});
){});
</script>
Use function on instead of live if You are using jQuery higher then 1.7.
Fiddle: http://jsfiddle.net/eznnC/ (though the hiding is not working but I believe it will in normal environment).
the first part to make only the 4 or more show a More link is below. the link doesn't disappear at the moment nor does it reappear.
<?php
//limit shown entries upto 4
foreach(array_slice($company['service_providers'], 0, 4) as $provider): ?>
<div>
<?php print $provider; ?>
</div>
<?php endforeach; ?>
<?php
// shows a More link for companies with more than for providers
foreach(array_slice($company['service_providers'], 4, 1) as $provider): ?>
<div>
<div id="more" style="color:#000099;font-weight:bold; margin-bottom:-13px;">
<?php print ('<a onclick="">More</a>'); ?>
</div>
</div>
<?php endforeach; ?>

Wordpress site making "holes" in output

My page http://www.allyourpods.no/ has some odd "holes" that I cant for the life of me figure out.
I would love some help to figure out how to make it automagickly fill the site in a proper fashion.
My template-file for the start-screen looks like this:
<?php
/*
Template Name: Home Template
*/
get_header(); ?>
<?php query_posts('cat='.recPodcastCategory.'&showposts=30');?>
<?php $categories = get_categories('child_of='.recPodcastCategory); ?>
<?php if($categories): $count=0;?>
<div class="recommended">
<div class="wrapper">
<div class="main_recommended_main_block">
<?php foreach($categories as $category) { if($count<30) { $count++;
if($count!=30) $class='recommend_block'; else $class='recommend_block_1'; ?>
<?php if (function_exists('get_terms_meta'))
{
$cat_image = get_terms_meta($category->term_id, 'image',true);
$add_play_podcast = get_terms_meta($category->term_id, 'play_download',true);
}?>
<div class="<?php echo $class;?>">
<div class="main">
<?php $play_podcast = get_post_meta($post->ID,'play_podcast',true);?>
<div class="view view-fifth"><?php if($cat_image):?><img src="<?php echo $cat_image;?>" alt="category image" /><?php endif;?>
<?php if($add_play_podcast):?>
<div class="mask">Play</div>
<?php endif; ?>
</div>
</div>
<?php echo substr( category_description( $category->term_id ),0,100 ) . "..."; ?>
</div>
<?php } } ?>
</div>
</div>
</div>
<?php endif; ?>
<?php if(!isset($_GET['pod_category']))$class_all = ' class="active"'; ?>
<div class="clr"></div>
<?php get_footer(); ?>
If any more information is needed please let me know.
You should add this css:
.recommend_block:nth-child(4) {
clear: left;
}
The problem is one of the first two is a little lower than the others, so it is blocking the float.
Edit: that should be
.recommend_block:nth-child(4n)

Categories