I am displaying data from database which is linked to CMS. The problem I get is data from only one of my tabs is being displayed. Suppose I have two tabs, Tab 1, Tab 2. Instead of Tab 1 being active, Tab 2 is active and data is displayed of Tab 2. But, when I click on Tab 1, no data with respect to Tab 1 is displayed. The same data is present i.e Tab 2 data.
I am using mapping to get the ids of respective Tab.
<ul class="tabs" data-tab>
<?php
if(isset($X['list_by_parent_id'][0]) && !empty($X['list_by_parent_id'][0]))
{
$top_category_index=1;
foreach ($X['list_by_parent_id'][0] as $category_id)
{
?>
<li class="tab-title <?php if($top_category_index==1){ echo "active"; }?>"><?php echo $SITE['tmp']['product_categories'][$category_id] ?></li>
<?php
}
}
?>
</ul>
X is a global variable which is an array and stores everything.
This is the code that does mapping and displays the content.
<div class="category-content">
<div class="menu-cat-content tabs-content">
<?php if(isset($SITE['category_list_by_parent_id'][0]) && !empty($SITE['category_list_by_parent_id'][0]))
{
$top_category_index_mapped=1;
foreach ($SITE['category_list_by_parent_id'][0] as $category_id)
{ ?>
<div id="category-<?php echo $top_category_index_mapped; ?>" class="content <?php if($top_category_index_mapped++==1) { ?>active<?php } ?>">
//code for content
</div>
Rendered HTML:
<div class="category-content">
<div class="menu-cat-content tabs-content">
<div id="category-1" class="content active">
<!--sub category 1-->
</div><!--sub-category-clearfix2-->
<!--sub category 2 ends-->
</div> <!--category-div, index_mapped -->
<div id="category-2" class="content active">
<!--sub category 1-->
</div><!--sub-category-clearfix2-->
<!--sub category 2 ends-->
</div> <!--category-div, index_mapped -->
<div id="category-3" class="content active">
EDIT: I saw that top_category_index_mapped was set to 1 in category-*top_category_index_mapped. I replaced it and rendered html now looks like this.
This line:
$top_category_index=1;
sets the $top_category_index to 1 for the first tab. This line:
<li class="tab-title <?php if($top_category_index==1){ echo "active"; }?>
is equal to this the firs two times:
<li class="tab-title active">
because of the properties of the ++ operator. Read more here.
For example:
$a = 5;
echo "Should be 5: " . $a++ . "<br />\n";
echo "Should be 6: " . $a . "<br />\n";
Since you've only tested with two tabs it seems, that's why you are getting those results. It might also be helpful to include the rendered html.
Related
I have a simple MySQL table that stores four fields - CATEGORY, TITLE, DESCRIPTION, IMAGE as well as a unique ID for each row.
I use ORDER BY CATEGORY to display all of them on the page through one query.
SELECT
RESOLUTIONS.CATEGORY,
RESOLUTIONS.ID,
RESOLUTIONS.TITLE,
RESOLUTIONS.DESCRIPTION,
RESOLUTIONS.IMAGE
FROM
RESOLUTIONS
ORDER BY
RESOLUTIONS.CATEGORY
I want to create a jump menu that will jump to the first row of each category on the page. Is this possible using php? I know how to create a jump menu that jumps to an ID anchor of a div, but how can I make a unique identifier (that I can jump to) inside the repeat region?
Here is the repeat code I have now...
<?php
while(!$DETAILS->atEnd()) {
?>
<div class="row g-my-10 g-color-black">
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
In each iteration check if the category is different than the one before and create an anchor.
<?php
// set any initial value that does not match an empty category (if you have them)
$lastCategory = false;
while(!$DETAILS->atEnd()) {
<div class="row g-my-10 g-color-black">
<?php
// this category is different than the last: create anchor
if ($lastCategory !== $DETAILS->getColumnVal("CATEGORY")) {
echo '<a name="category-' . $DETAILS->getColumnVal("CATEGORY") . '"></a>';
// set the compare-value to the current category
$lastCategory = $DETAILS->getColumnVal("CATEGORY");
}
?>
<?php if($DETAILS->getColumnVal("IMAGE")!= "") { ?>
<div class="col-md-9">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<? } else { ?>
<div class="col-md-12">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php } ?>
</div>
<?php
$DETAILS->moveNext();
}
$DETAILS->moveFirst(); //return RS to first record
?>
Note: You might have to put the anchor within the column or even the <h2>, depending on your grid system (is that bootstrap?). You maybe also want to refactor the generation of the title/description column, so you don't repeat yourself. Something like
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
$cols = '9';
} else {
$cols = '12';
}
?>
<div class="col-md-<?php echo $cols ?>">
<h2><?php echo($DETAILS->getColumnVal("TITLE")); ?></h2>
<?php echo($DETAILS->getColumnVal("DESCRIPTION")); ?>
</div>
<?php
if ($DETAILS->getColumnVal("IMAGE")!= "") {
?>
<div class="col-md-3"><img src="images/<?php echo($DETAILS->getColumnVal("IMAGE")); ?>" class="img-fluid"></div>
<?php
}
?>
Also note that according to PSR-1 you should use either <?php or the echo shortcut <?= but not just <?
And I know it's a matter of personal choice and also depends on what your file mainly contains (HTML or PHP), but I personally prefer echoing HTML code snippets instead of opening and closing PHP tags, because I find it easier to read (there is also a very marginal performance advantage)
i'm trying to store html tag that includes php script.
This is for the navigation menu, i'm trying to display only the menu for a particular user role.
I'm storing it this way
public function edit_module(){
$id = $this->input->post('Module_ID', TRUE);
$update = $this->db->update('ref_modules', array('Module_Name'=>$this->input->post('Module_Name', TRUE),
'Module_Menu'=> htmlspecialchars($this->input->post('Module_Menu')),
'UpdatedBy'=>$this->session->userdata('user_id')), "Module_ID = '$id'");
if($update){
return TRUE;
}
}
For display
<!--Menu-->
<!--================================-->
<div id="mainnav-menu-wrap">
<div class="nano">
<div class="nano-content">
<ul id="mainnav-menu" class="list-group">
<!--Category name-->
<li class="list-header">Navigation</li>
<?php if($this->data['menu']): ?>
<?php foreach($this->data['menu'] as $row): ?>
<?php echo htmlspecialchars_decode($row->Module_Menu); ?>
<?php endforeach; ?>
<?php endif; ?>
</ul>
</div>
</div> <!-- nano -->
</div><!-- mainnav-menu-wrap -->
<!--================================-->
<!--End menu-->
When i view the source, its fine but when i click the link, special characters are showing.
http://localhost/folderName/%3C?php%20echo%20base_url(%27citizens%27);%20?%3E
Here's a sample value that i am inserting
<li class="<?php echo ($title == 'Citizens' ? 'active-link' : '');?>">
<a href="<?php echo base_url('citizens'); ?>">
<i class="fa fa-user"></i>
<span class="menu-title">
<strong>Citizens</strong>
</span>
</a>
</li>
Any idea guys? Thanks in advance!
Inserting Html into database For Different user role is not the right way. You can use if Conditions To show your Menu for different User roles.
I'm trying to insert 2 static div's inside a PHP loop, specifically one at the very beginning of the loop and one at the very end.
These 2 div's must appear within their corresponding .row parent which currently wraps around every 3 DIV's. How can I do this?
EDIT
Here's an image to describe what I need, the pink blocks are the manually inserted div's that will have different content to the blue divs. Those blue divs are just WP posts:
Here's my PHP, currently this creates 4 columns within the first and last rows where it should just be 3 columns:
<?php static $c=1;
$subs = new WP_Query( array( 'post_parent' => 14, 'post_type' => 'page' ));
if( $subs->have_posts() ) : while( $subs->have_posts() ) : $subs->the_post(); ?>
<?php if (($c % 3) === 1) {
// This creates part of the wrapper .row div
echo "<div class='row'>";
} ?>
<?php
if ($c == 1) {?>
<div class="col_4 card bar">
first card that is manually inserted with different content
</div>
<?php } ?>
<a href="<?php the_permalink(); ?>" class="col_4 card bar no-pad <?php if($c % 3 == 0) { echo 'last'; } ?>">
<?php if ( has_post_thumbnail() ) {?>
<div class="feature-image c-1">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php } ?>
<div class="excerpt-wrap">
This is a post from within Wordpress
</div>
</a>
<?php if ($c == 6) {?>
<div class="col_4 card bar">
Last card that is manually inserted with different content
</div>
<?php } ?>
<?php if (($c % 4) === 3) {
echo "</div>";
}?>
<?php $c++; endwhile; endif; wp_reset_postdata(); ?>
EDIT
This is the HTML structure I'd like to achieve:
<!-- very first row -->
<div class="row">
<!-- This is a static block followed by the very first two worpdress posts-->
<div class="static-block"></div>
</div>
<!-- I could have 3 or 30 wordpress posts repeating this format -->
<div class="row">
</div>
<!-- very last row -->
<div class="row">
<!-- These are the very two worpdress posts followed by a static block -->
<div class="static-block"></div>
</div>
<?php
$c = 1;
$subs = new WP_Query(array('post_parent' => 14, 'post_type' => 'page'));
if ($subs->have_posts()) :
?>
<div class='row'>
<?php
while ($subs->have_posts()) : $subs->the_post();
if (($c % 3) == 0 || $c == 3):
?>
</div><div class='row'>
<?php
endif;
?>
<?php
if ($c == 1):
?>
<div class="col_4 card bar">
first card that is manually inserted with different content
</div>
<?php endif; ?>
<a href="<?php the_permalink(); ?>" class="col_4 card bar no-pad <?php if ($c % 3 == 0) { echo 'last'; } ?>">
<?php if (has_post_thumbnail()) { ?>
<div class="feature-image c-1">
<?php the_post_thumbnail('medium'); ?>
</div>
<?php } ?>
<div class="excerpt-wrap">
This is a post from within Wordpress
</div>
</a>
<?php if ($c == 7) { ?>
<div class="col_4 card bar">
Last card that is manually inserted with different content
</div>
<?php } ?>
<?php
$c++;
endwhile;
?>
</div>
<?php
endif;
wp_reset_postdata();
?>
if have more then 7 page and u want to static block add on last just
change 7 to post count value ( $c == $sub->post_count)
EDIT: sorry, it seems that I saw your post before you uploaded the pictures of the wireframe.
It's not so clear to me the goal of your code. But, what I understand is you need to generate an structure like this:
<div class='row'>
<div class='static'>
</div>
<div class='static'>
</div>
#here the loop will create
<div></div>
<div></div>
<div></div>
<div class='static'>
</div>
<div class='static'>
</div>
</div>
and this will be duplicated for as many i in your while you have.
if that is what you need, then I think what you need to do is count 1,2,3 with your $c variable. Everytime you are in $c = 1 print the first 2 static divs, and when you are in $c = 3 print the final 2 static divs. Reset $c to 1 when you reach $c = 3 and include a conditional asking if its the last item and its $c != 3 so you print out the last 2 static divs.
I am using the Bootstrap navigation tabs to split a page (posts on my site. One page shows the posts which the user has made. The other page shows all posts. I am using $_GET variables to paginate the pages.
<ul class="nav nav-tabs" id="myTab">
<li class="active">My Posts</li>
<li>All Posts</li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="myPosts">
<?php include "myPosts.php"; ?></div>
<div class="tab-pane" id="allPosts">
<?php include "allPosts.php"; ?></div>
</div>
I am displaying the content on each tab by including the relevant page link.
Here is an example of one of the links which should take the user to page 2.
2
This works fine for myPosts, but when I click on the second tab( all posts) and attempt to paginate, it takes me to the second page of myPosts. The pagination is all url based, so is there anyway to feed into the url what tab/div is active?
In the code you posted, you have <div class="tab-pane active" id="myPosts">
So the active class is hard coded in that div. If that's what you want, great... otherwise, I think we need to do something like this untested code when we generate the list:
<?php
//dummy data
$pages=array(
array('id'=>'1','posts'=>array('id'=>1,'content'=>'Go Bucks')),
array('id'=>'2'),//more posts
array('id'=>'3'),
);
?>
<ul class="nav">
<?php foreach($pages as $page): ?>
<li class="<?= ($page['id']===$_GET['page']) ? ' active':null ?>">
Page <?= $page['id'] ?>
</li>
<?php endforeach; ?>
</ul>
<div class="tab-content">
<?php foreach($pages as $page): ?>
<div class="tab-pane<?php if($page['id']===$_GET['page']){echo ' active in';} ?>">
//foreach posts here
</div>
<?php endforeach; ?>
</div>
The idea I'm trying to demonstrate is to test the page ID vs the $_GET['page'] value.
Another idea for you: check out bootstrap's example" http://getbootstrap.com/javascript/#tabs
You may enjoy using javascript here, or at least to inspect their example to work out the syntax and behavior you want.
I have a parent page that acts as menu for my portfolio.
It pulls in thumbnail images from the child pages which i have been able to accomplish with magic fields and some code. It dumps the images into a grid layout. The thumbnails are pulled into one container div like so:
div id="folio-content">
<div class="thumb-container">
<div class="thumb"><img src="/images/pic.jpg"/>
</div>JCPenny</div>
... </div>`
when the div gets filled up with 2 thumbnails I want to create a new container div and fill it with 2 images again and so on after 2 images.
So, if you had 4 images it would look like this.
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
<div id="folio-content"><!--/Main Container/-->
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>JCPenny</div>
<div class="thumb-container">
<div class="thumb"><img src="/images/pic1.jpg"/>
</div>Champ Car</div></div>
this is the code I am using in my page.php file.
<?php get_header(); ?>
<div id="folio-content">
<?php
$projectpage = get_pages('child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
$count = 0;
foreach($projectpage as $page)
{
$content = $page->post_content;
if(!$content)
continue;
if ($count == 10) --- this is geting 10 images now, but I want to get them all.
break;
$count++;
$content = apply_filters('the_content', $content);
?>
<div class="thumb-container">
<div class="thumb"><a href="<?php echo get_permalink($page->ID); ?>"<?php echo get_image ("thumbnail",1,1,1,$page->ID);?></a>
</div><?php echo $page->post_title ?>
</div>
<?php
}
?>
</div><!--/close set!-->
</div>
also, how can I get ALL child pages? I have it set to 10 now with this if ($count == 10)
any help? thanks a ton again!!!!
I'm not familiar with "get_pages" but since Wordpress treats posts and pages in an identical manner you could use this.
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
The -1 removes the limit and gets ALL the specified pages.
I have cobbled together some code, that sort of sounds right but does not work at all! Which I am not surprised. But it is a starting point - please take a look at this code, maybe it is step in the right direction?
<?php
$projectpage = get_posts('numberposts=-1&post_type=page&child_of='.$post->ID.'&sort_column=post_date&sort_order=desc');
if (have_posts()) :
$i=0; // counter
while(get_posts()) : the_post();
if($i%2==0) { // if counter is multiple of 3, put an opening div ?>
<!-- <?php echo ($i+1).'-'; echo ($i+2); ?> -->
<div>
<?php } ?>
<div class="single_item">
<h2><?php the_title(); ?></h2>
</div>
<?php $i++;
if($i%2==0) { // if counter is multiple of 3, put an closing div ?>
</div>
<?php } ?>
<?php endwhile; ?>
<?php
if($i%2!=0) { // put closing div here if loop is not exactly a multiple of 3 ?>
</div>
<?php } ?>
<?php endif; ?>