Display none to element in foreach if == 0 - php

I have a foreach loop, and in that I have a more information button. I want it so that when x == 0 then the display to that more information button is 'none'
This is the variable that is different in each foreach item.
$issuesFixedCount
This is the class that needs to be display none if the variable == 0.
<a href="<?='version/?version=' . $item->name; ?>" class="moreInfo">Meer
My code:
<div class="pb-2 mb-3">
<h1 class="h2">Recente Jira releases</h1>
<div class="release-items" id="release-items">
<?php
foreach ($items as $item):
$proj = new ProjectService();
$versionService = new VersionService();
$version = $proj->getVersion('', $item->name);
$res = $versionService->getRelatedIssues($version);
$uns = $versionService->getUnresolvedIssues($version);
$issuesFixedCount = $res->issuesFixedCount;
$issuesUnresolvedCount = $uns->issuesUnresolvedCount;
$roundedIssue = $issuesFixedCount - $issuesUnresolvedCount;
if ($res->issueFixedCount == 0){
echo "
<script>
const moreInfoVar = document.querySelector('.moreInfo');
const.style.display = 'none';
</script>";
}
?>
<div class="item" id="item">
<h4>Versie: <?= $item->name; ?></h4>
<div class="details">
<p>Issues in versie: <?= $res->issuesFixedCount; ?> </p>
<p>Afgeronde issues: <?= $roundedIssue;?> </p>
<p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
Meer info<br>
</div>
<small><?= $item->releaseDate; ?></small>
<a class="toggle">Toon meer</a>
</div>
<?php
endforeach;?>
<?php
$startAtValueNext = $_GET['startAt'];
$startAtNext = $startAtValueNext += 5;
$startAtValuePrevious = $_GET['startAt'];
$startAtPrevious = $startAtValuePrevious -= 5;
if ($startAtPrevious <= 0){
$startAtPrevious = 0;
}
?>
</div>
<div class="Next-Previous">
<- Vorige
Volgende ->
</div>
</div>

There's no need to inject JavaScript in this way. Instead, simply modify what is sent to the browser in the first place:
<p>Nog te verwerken issues: <?= $uns->issuesUnresolvedCount;?> </p>
<a href="<?='version/?version=' . $item->name; ?>" class="moreInfo" <?= ($res->issueFixedCount == 0 ? 'style="display:none;"' : ''); ?>>Meer info</a><br>
</div>
P.S. document.querySelector('.moreInfo') will only ever select the first element it finds which has that class. It doesn't target the specific element you're outputting on the current row, so that's one reason why that approach doesn't work.

Related

add an incrementing number to id with 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>

Use PHP foreach loop to display data in many html div tags

I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>

PHP loop rating bar error

I have a 5 point rating system I am trying to change up how it displays the rating.
So before it would be 5 dots all white if not rated, then color in the ratings blue.. I would display these dots via fontawesome glyphs. So it would output 5 of them.
But now i'm trying to change it where the 5 point rating would display a progress bar and the 5 points be used as percentages for the length of progress.
Sorry if i am vague, I can explain more if needed. It currently displays all the bars and rating values, but just once. So it's not functioning the way I want it to.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<?php
$levelpercentage = 0;
for($stars == 1; $stars <= 5; $stars++) {
if ($stars == 5):
echo $levelpercentage = 100;
elseif ($stars == 4):
echo $levelpercentage = 80;
elseif ($stars == 3):
echo $levelpercentage = 60;
elseif ($stars == 2):
echo $levelpercentage = 40;
elseif ($stars == 1):
echo $levelpercentage = 20;
endif;
?>
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $levelpercentage : '0'; ?>%;"></span>
</div>
<?php } ?>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>
It currently displays all the bars and rating values
That's because you assign and echo your bars and rating values in a for loop.
Your $stars variable is assigned every value from 1 to 5, displaying the bar at each step. Simply remove your for loop and it should work fine (provided you use an already defined $stars variable)
Bonus : Because I'm in a good mood, I have to tell you there's a simpler way to build your $levelpercentage variable than using an if...else for each value from 1 to 5, just use :
$levelpercentage = $stars * 20;
Thank you! That really helped me out. Was trying to simplify what I was trying to do. This is what I did and it seems to be working.
<?php if(count($languages) > 0) { ?>
<div class="col-md-6">
<ul class="no-bullets">
<?php foreach($languages as $index => $language) { $stars = 0; $stars <= 5; $stars++; ?>
<li>
<span class="skillset-title"><?= $language->title; ?> (<?= $language->endorsement; ?>)</span>
<span class="skillset-rating">
<div class="progress-bar blue stripes">
<span style="width: <?= ($language->level >= $stars) ? $language->level * 20 : 0; ?>%;"></span>
</div>
</span>
</li>
<?php if(ceil(count($languages) / 2) == $index + 1) { ?>
</ul>
</div>
<div class="col-md-6">
<ul class="no-bullets">
<?php } ?>
<?php } ?>
</ul>
</div>
<?php } else { ?>
<div class="alert alert-warning">
No languages were found!
</div>
<?php } ?>

Concrete5 - Custom block not editable, or recognized with the editing tool?

I'm making a site with conrete5. It's the first one I might add. I have made myself a couple of custom blocks. Named News, Teammates and References.
Now News and Teammates are not editable anymore. I will paste the News -blocks sourcecode.
----------- FORM.php ---------------------
<?php defined('C5_EXECUTE') or die(_("Access Denied.")); ?>
<?php
$al = Loader::helper('concrete/asset_library');
echo $al->file('optional', 'fID', t('Valitse kuva'), $bf, $args);
?>
<div class="form-group">
<?php echo $form->label('otsikko', t('Otsikko'));?>
<?php echo $form->text('otsikko', $otsikko);?>
</div>
<div class="form-group">
<?php echo $form->label('teksti', t('Teksti'));?>
<?php echo $form->text('teksti', $teksti); ?>
</div>
<div class="form-group">
<?php echo $form->label('korkeus', t('Korkeus'));?>
<?php echo $form->select('korkeus', array("108px"=>t("Pieni"),"299px"=>t("Iso")), $korkeus); ?>
</div>
<div class="form-group">
<?php echo $form->label('koko', t('Leveys'));?>
<?php echo $form->select('koko', array("col-md-3"=>t("Pieni"),"col-md-6"=>t("Iso")), $koko); ?>
</div>
<div class="form-group">
<?php echo $form->label('link', t('Linkki'));?>
<?php echo $form->text('link', $link); ?>
</div>
<div class="form-group">
<?php $psh = Loader::helper('form/page_selector');
echo $psh->selectPage('targetCID', $targetCID); ?>
</div>
----------- view.php ---------------------
<?php
defined('C5_EXECUTE') or die(_("Access Denied."));
$c = Page::getCurrentPage();
if($size=="col-md-3"){
$class='col-md-3';
$tag = $class;
}else{
$class="col-md-6";
$tag= $class;
}
if ($c->isEditMode()){
$class="editmode";
$editingStyle="padding: 15px; background: #ccc; color: #444; border: 1px solid #999;";
}
else {
$editingStyle = "";
}
$random = rand();
if($korkeus == "299px"){
$padding = '4px';
}else {
$padding = '5px';
}
$p = Page::getByID($targetCID);
$a = new GlobalArea('Header Navigation');
$blocks = $a->getAreaBlocksArray($c);
foreach ($blocks as $block){
if ($block->getBlockTypeHandle()=="autonav"){
$block->setCustomTemplate('cdrop.php'); // it's templates/cdrop.php -check the select option values when you set custom template manually at edit mode. I think you will need just "my_template"
$bv = new BlockView($block);
$bv->render('view');
}
}
?>
<?php $p = Page::getByID($targetCID); ?>
<a href="index.php">
<div class="pull-left <?= $koko;?>" style="padding:<?= $padding ?>;<?php echo $editingStyle;?>">
<div class="col-lg-12 alapalkki box" style="z-index:2;position:relative;">
<div class="image-big" style="background-color:transparent;text-align:center;position:relative;z-index:1;">
<!-- FiGuRe this shit out......... !-->
<?php
if($fID != 0){
$file = File::getByID($fID);
$filePath = $file->getVersion()->getRelativePath();
}
?>
<?php echo '<img src="' . $filePath . '" style="max-height:' . $korkeus . ';width:100%;"/>'; ?>
</div>
<div class="col-lg-12 " style="position:relative;z-index:255;padding:2px 0 0 15px;">
<div class="htitle">
<h4 style="color:white;"><b><?php echo $otsikko; ?></b></h4>
<p style="color:white;"><?php echo $teksti; ?></p>
</div>
</div>
</div>
</div>
</a>
Why is this not being an editable block? Why doesn't the concrete5 even recognize its existence when it is on the page? It just says at the area that it's empty.
$p = Page::getByID($targetCID);
$a = new GlobalArea('Header Navigation');
$blocks = $a->getAreaBlocksArray($c);
foreach ($blocks as $block){
if ($block->getBlockTypeHandle()=="autonav"){
$block->setCustomTemplate('cdrop.php'); // it's templates/cdrop.php -check the select option values when you set custom template manually at edit mode. I think you will need just "my_template"
$bv = new BlockView($block);
$bv->render('view');
}
}
?>
There's the problem. No idea what so ever what that is doing there..... Removed it. Worked like a charm.
-Kevin

PHP pagination content

I have a list of content that I would have a paging. Until now I was able to count correctly how content we are, then we have 3 content, and imposed a maximum of 2 applications per page, in my index shows the link to go to the next page.
Unfortunately, however, always displays the same content. For example:
http://i62.tinypic.com/156skjo.png
http://i60.tinypic.com/30xcy1l.png
However, I paste the current code, I hope you can help me out. Thank You
<?php require_once 'app/init.php'; ?>
<?php echo View::make('header')->render() ?>
<div class="row">
<!-- START MAIN GRID -->
<?php
// Create a variable imposed where the number of records
// To display on each page
$x_pag = 2;
// Retrieve the current page number.
// Usually you use a querystring
$pag = isset($_GET['pag']) ? $_GET['pag'] : 1;
// Check if $pag is valued and if numeric
// ... Otherwise I assign a value of 1
if (!$pag || !is_numeric($pag)) $pag = 1;
$quest = DB::table('questions')
->count();
// Using a simple mathematical operation define the total number of pages
$all_pages = ceil($quest / $x_pag);
// Calculation of which record start
$first = ($pag - 1) * $x_pag;
$questions = DB::table('questions')
->orderBy('id', 'desc')
->take($x_pag)
->get();
foreach ($questions as $question):
$user = User::find($question->user_id);
?>
<div class="col-md-4 col-lg-4">
<div class="main-grid">
<div class="profile-inner img-responsive" style="background-image: url('images/<?php echo $question->h_image; ?>');border-radius: 10px 10px 0px 0px;">
<img src="<?php echo $user->avatar; ?>" class="small-thumb" >
</div>
<div class="description">
<h5><strong><?php echo $question->user_name; ?></strong>
<?php if (Auth::check() && Auth::user()->id != $question->user_id): ?>
<?php $contact = Contact::find(Auth::user()->id, $question->user_id); ?>
<?php if (!empty($contact) && !empty($contact->accepted)): ?>
( <?php _e('main.remove_contact') ?> )
<?php elseif (!empty($contact)): ?>
( <a href="javascript:EasyLogin.removeContact(<?php echo $question->user_id ?>)" data-contact-id="<?php echo $question->user_id ?>" ><?php _e('main.cancel_contact') ?></a> )
<?php else: ?>
( <?php _e('main.add_contact') ?> )
<?php endif ?>
<?php endif ?>
</h5>
<h3><?php echo $question->h_title; ?>?</h3>
<hr />
Help +
</div>
</div>
</div>
<?php endforeach; ?>
<!-- MAIN GRID END -->
</div>
<?php
// If the total pages are more than 1 ...
// Mold the link to go back and forth between different pages!
if ($all_pages > 1){
if ($pag > 1){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag - 1) . "\">";
echo "Back</a> ";
}
if ($all_pages > $pag){
echo "<a href=\"" . $_SERVER['PHP_SELF'] . "?pag=" . ($pag + 1) . "\">";
echo "Next</a>";
}
}
?>
<?php echo View::make('footer')->render() ?>

Categories