Active button error - php

how are you? I'm having a problem with a program I'm modifying.
is a series site I almost managed to solve everything else I arrived at a point that is difficult to identify the problem.
In the page of the series to be displayed the seasons appear and also the episodes appear.
The buttons of the first episode of each season appear highlighted as if they were being watched, and when I click on an episode everyone is unmarked and what I clicked becomes active.
I want the only thing to stay active is episode 1 of the first season which is what is being displayed on screen and the other first episodes are turned off.
the problem is in the following line:
<?php
if($episode['stream_key']==$current_key){
echo 'btn-success';
} else {
if($current_key=='000000' && $i=='1'){
echo 'btn-success';
}else{
echo 'btn-default';
}
}
?>
the line below is to understand the context:
<div class="row">
<div class="col-md-12 m-t-10 m-b-10">
<?php
$this->db->order_by('seasons_id', "ASC");
$seasons = $this->db->get_where('seasons',array('videos_id'=>$watch_videos->videos_id))->result_array();
foreach ($seasons as $season):
?>
<?php if($this->common_model->get_num_episodes_by_seasons_id($season['seasons_id']) > 0): ?>
<div class="season">
<p class="btn btn-inline text-uppercase" style="font-size: 12px;color: #055b86">TEMP: <?php echo $season['seasons_name']; ?> | EP: </p>
<?php
$this->db->group_by('seasons_id');
$this->db->order_by('seasons_id', "ASC");
$this->db->group_by('episodes_id');
$this->db->order_by('episodes_id', "ASC");
$episodes = $this->db->get_where('episodes',array('videos_id'=>$watch_videos->videos_id,'seasons_id'=> $season['seasons_id']))->result_array();
$i=0;
$current_key = '000000';
if(isset($_GET['key']))
$current_key = $_GET['key'];
foreach($episodes as $episode):
$i++;
?>
<?php echo $episode['episodes_name']; ?>
<?php endforeach; ?>
</div>
<?php endif; ?>
<?php endforeach; ?>
</div>

Related

Combining php loop and count - Wordpress ACF

I wondered if anyone could help me combine two blocks of code I have. I have one block looping though items and the start of another block showing a count and hopefully enabling me to display the items looping through in rows by adding a div around them every two items... Heres the first bit of code, the loop:
<?php if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
I'm using Advance Custom Fields for Wordpress and this is pulling through repeater fields... this displays them just one after the other.
Here's the code I have found to hopefully display them in rows.
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
// Other Code
if($num %2) {
echo '</div>';
}
$num++
}
?>
I would like to display them in rows of two...
ONE TWO
THREE FOUR
FIVE SIX
Etc...
So, Im guessing I need to combine the code somehow... I currently have this: but it doesn't seem to work:
<?php
$num = 1;
foreach ( $terms as $term ) {
if($num%2) {
echo '<div class="area-row">';
}
if(get_field('areas')): ?>
<?php while(has_sub_field('areas')): ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php endwhile; ?>
<?php endif; ?>
if($num %2) {
echo '</div>';
}
$num++
}
?>
Okay so it looks like there was a couple of simple issues with this, you had closed the php tag after your if statment and then continued to write php without reopening the php tags. Also there is a slight logic error with the if($num%2) statements as one of these needs to be if, the other needs to be if not, so that the alternate.
Give this code a try and let me know how you get on:
<?php
if(get_field('areas')):
$num = 1;
?>
<?php while(has_sub_field('areas')):
if($num%2) {
echo '<div class="area-row">';
} ?>
<div class="single-area-item six columns">
<p> <img src="<?php the_sub_field('area_icon'); ?>" style="width:100%;"> <p>
<h4> <?php the_sub_field('area_title'); ?> </h4>
<p> <?php the_sub_field('area_info'); ?> <p>
</div>
<?php
if(!$num%2) {
echo '</div>';
}
$num++
endwhile; ?>
<?php endif; ?>
The main reason behind this question was for me to be able to target the third item of the looped through items, as it had padding on it that was breaking the rows that I needed to remove.
I have since found another solution that seems to be working.
By using the nth-child element, I have been able to target and remove the padding on every third item that's looped through - fixing the issue.
.single-area-item:nth-child(3n+3) {
margin-left: 0;
}
This is for rows or 2 items, if it were rows of 3 or 4 then it would need to target every 4th or 5th item respectively.

How to echo CSS class only once in first iteration foreach in PHP?

I'm facing a little problem, I have a css class which is called "negative margin" which I just want to apply once in the first div in a series of divs as it's part of my design. The code is as follows:
<?php foreach($records as $rec){ ?>
<div class="grid grid-lightgrey grid-pad negative-margin">
<div class="col-1-1">
<div class="module module-grey">
<em>Project title: <?php echo $rec->project_title; ?></em><br>
<em>Category: </em><?php echo $rec->project_category; ?><br>
<em>Description: </em><?php echo $rec->project_description; ?>
<br>
<em>This project was started in: <?php echo $rec->project_year; ?></em><br>
<em>This project ID is: </em>
<?php echo $rec->project_id; ?>
</div>
</div>
</div>
<?php } ?>
The class negative-margin is applied to all the divs making them stack on top of each other and it's not what I wan't but I need this negative margin to be applied in the first div as I said for design purposes but I really don't know how this could be achieved. Any ideas would be greatly appreciated.
Use the first-child selector to define a CSS style that only applies to the first div.
div.grid:first-child {
margin-left: -10px;
}
You need a variable to track the first element because there is no other way to know the index in foreach loop. You can do:
<?php $i = 0; foreach($records as $rec): ?>
<div class="grid grid-lightgrey grid-pad <?php echo ($i == 0) ? 'negative-margin' : ''; ?>">
<div class="col-1-1">
<div class="module module-grey">
<em>Project title: <?php echo $rec->project_title; ?></em><br>
<em>Category: </em><?php echo $rec->project_category; ?><br>
<em>Description: </em><?php echo $rec->project_description; ?>
<br>
<em>This project was started in: <?php echo $rec->project_year; ?></em><br>
<em>This project ID is: </em>
<?php echo $rec->project_id; ?>
</div>
</div>
</div>
<?php $i++; endforeach; ?>

Place DIV's in a containing DIV based on a numeric value

Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.

for loop will post everything, but I only want one post

I want to fetch data from my MySQL and echo it out only if $featured == 1.
It works, the only one with $featured == 1 will show title, but my problem is that it also makes posts from everything else in my database (empty posts). I only want it to show the posts with == 1 and nothing more nothing less. I can't manage to fix it.
<aside id="featured" class="body"><article>
<?php
for ($i=0; $i <$num_results; $i++){
$row1 = $result1->fetch_assoc();
$featured = ($row1['featured']);
if ($featured == 1) {
echo $row1['title'];
} else {
}
?>
<figure>
<img src="images/black2.gif" alt="Black 2" style="width: 300px;"/>
</figure>
<hgroup>
<h2>Featured Article</h2>
<h3><a href="goodies/black2.html">
</a></h3>
</hgroup>
<p> </p>
<footer class="post-info">
<abbr class="published" title="date">20 juli 2012</abbr>
<address class="vcard author">By<a class="url fn" href="portfolio.html">F4LLCON</a>
</address></footer><!-- /.post-info -->
</article>
<?php
}
$result1->free();
?>
</aside><!-- /#featured -->
Will look like:
But I want it like this:
Your for body contains printing large amount of HTML regardless of featured checking, that is, outside your if.
You probably should move two lines
} else {
}
line to the bottom of the code snippet.

PHP - limiting the output of a plugin

I'm using a plugin titled "WP Recent Links" which I first learned about via Eric Meyer's site. Eric uses to display a Link Log within his site and I'm doing the same on my test site - http://matala.jorgeledesma.net/ but I'm running into a little situation and that is that I don't know how to limit the output either on the sidebar or the actual page - http://matala.jorgeledesma.net/recent-links/
My goal is to have it echo only the first 5 entries on the sidebar and for the recent-links page only echo the current month. The code below displays the sidebar properly
<?php if (!is_page('48')) { ?>
<aside id="meta" class="widget">
<h1 class="widget-title">Link Log</h1>
<?php if ($links = rp_recentlinks_home()) { ?>
<ul>
<?php foreach ($links as $link) { ?>
<b><li><?php echo wptexturize($link->link_text); ?></b>
<?php if ('' != $link->link_caption) { ?>→
<?php echo wptexturize(convert_smilies($link->link_caption)); ?><?php } ?>
</li>
<?php } ?>
</ul>
<?php } ?>
</aside>
<?php } ?>
and this code display the actual recent-links page
<h1 class="page-title"><?php rp_recentlinks_archive_header(); ?></h1>
</header>
<div class="entry-content">
<?php $links = rp_recentlinks_archive_page(); ?>
</div>
<?php if ($links) { ?>
<ul>
<?php foreach ($links as $link) { ?>
<p id="rlink-<?php echo $link->ID; ?>"><?php echo wptexturize($link->link_text); ?>
<?php if ('' != $link->link_caption) { ?>→
<?php echo wptexturize(convert_smilies($link->link_caption)); ?><?php } ?>
</p>
<?php } ?>
</ul>
<?php } ?>
I tried putting the following code in the body.
$list = array_slice($input, 0, 5); // $list now only having first 5 item.
But I don't know how to apply it, if that's the command at all. So perhaps, someone can guide in the right direction. Thanks in advance, Jorge.
Looks like all you have to add is this before you pass $links to the foreach loop:
$links = array_slice($links,0,5);

Categories