How to append count to variables in php - php

I am just starting to learn PHP and to compound the issue I am trying to modify existing code to match what I am attempting to do. I have multiple variables I am trying to define all while partially using a counter. My problem is appending the count to my individually defined variables. Also, I'm sure there is a cleaner way to do this (like a loop or nested array), I just can't see how.
<?php
$pageId0 = opt('first_tab_page');
$post0 = get_post($pageId0);
$content0 = apply_filters('the_content', $post0->post_content);
$icon0 = wp_get_attachment_image_src( get_post_thumbnail_id( $post0->ID ), 'full' );
$pageId1 = opt('second_tab_page');
$post1 = get_post($pageId1);
$content1 = apply_filters('the_content', $post1->post_content);
$icon1 = wp_get_attachment_image_src( get_post_thumbnail_id( $post1->ID ), 'full' );
$pageId2 = opt('third_tab_page');
$post2 = get_post($pageId2);
$content2 = apply_filters('the_content', $post2->post_content);
$icon2 = wp_get_attachment_image_src( get_post_thumbnail_id( $post2->ID ), 'full' );
?>
<div>
<?php echo $icon0[0]; ?>
</div>
<div>
<?php echo $icon1[0]; ?>
</div>
<div>
<?php echo $icon2[0]; ?>
</div>
<?php
$tab_position = opt('tab_position');
if ($tab_position == '' || count($tab_position) != 3) {
$tab_position = array(0, 1, 2);
}
for($i=0; $i < count($tab_position); $i++)
{
$selected = '';
if (opt('default_selected_tab') == $tab_position[$i]){
$selected = 'class="selected"';
}
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $content//should be 0 to start; ?>
</a>
<?php } ?>

Here is a short example on using arrays. comment in code
// Storing tab names to an array, just add more tabs if required
// and details for all those will be loaded in the following arrays
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
//declare arrays to store (not required, but better practice)
$pageids = array();
$posts = array();
$content = array();
$icons = array();
//Iterate through the tabs array
foreach ($tabs as $tab){
//Store page id and post in a variable, we require it
$pageid = opt($tab);
$post = get_post($pageid);
// Store pageid in pageids. note the [] this means it will store pageid at next index
//also store other items
$pageids[] = $pageid;
$posts[] = $post;
$contents[] = apply_filters('the_content', $post->post_content);
$icons[] = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
}
now $icons[0] will contain first icon $icons[1] the second one and so on. Same thing applies to other variables.
Note: this code is just typed in here and not checked. Please fix if there is any syntax errors. Also note this is one way and there are more ways.
But I would suggest keeping data of each page together.
Edit: Here is how you can keep things together (only relevant parts shown)
$tabs = array('first_tab_page', 'second_tab_page', 'third_tab_page');
$pages = array();
foreach ($tabs as $tab){
$pageid = opt($tab);
$post = get_post($pageid);
$content = apply_filters('the_content', $post->post_content);
$icon = wp_get_attachment_image_src( get_post_thumbnail_id( $post->ID ), 'full' );
//Store everything to $pages array
$pages[] = array('pageid' => $pageid, 'post' => $post, 'content' => $content, 'icon', $icon);
}
foreach ($pages as $page){
?>
<div>
<?php echo $page['icon'][0]; ?>
</div>
<?php } ?>
foreach ($pages as $page){
?>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $page['content']; //echo the content of page ?>
</a>
<?php } ?>
Instead of foreach to display you can use index also like
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[0]['content']; //echo the content of the first page ?>
</a>
<a <?php echo $selected; ?> href="#tab<?php echo $tab_position[$i];?>">
<?php echo $pages[1]['content']; //echo the content of the second page ?>
</a>

I guess there is no other way then (nested) arrays. Also I'm thinking that arrays are a acceptable solution.
foreach ($tabs as $tab) {
$tab['content'] = 'somecontent';
$tab['counter'] = 'counter';
}
or
foreach ($tabs as $tab) {
$tab = [
"content" => "content",
"counter" => "counter"];
}

I'm not sure what you are trying to do as your question is not very clear to me. But i think you are trying to echo $content in your last line of code. If yes, just put your all your variables $content1, $content2 and $content3 in an array like below:
$contents = array($content1, $content2, $content3);
Then use foreach loop and traverse this array like this.
$count = 0;
foreach($contents as $content) {
echo $content.$count;
$count++;
}
Try and let me know if it works for you.

Related

How to change loadObjectList() item positions?

I tried to change the positions of the loadObjectList(). but it is not working. can anyone help me to solve this problem?
This is the code i used.
$catID = 8;
//echo $catID;
$doc = JFactory::getDocument();
$page_title = $doc->getTitle();
$db = JFactory::getDBO();
$db->setQuery("SELECT title,alias FROM #__content WHERE catid = ".$catID);
$articles = $db->loadObjectList(); ?>
<div>
<?php foreach($articles as $article){
$title = $article->title;
if($title == $page_title){?>
<h4><?php echo $article->title; ?></h4>
<?php
}else{ ?>
<h4><?php echo $article->title; ?></h4>
<?php }
}
?>
</div>
what i want to do is, i want to display the page title as the first element of the list. Thats means if condition's item should display first.
can anyone helpe me?
A quick dry solution is to add 2 foreach loops and first display the value if condition match and after the rest values.
<?php
foreach($articles as $article){
$title = $article->title;
if($title == $page_title){
echo '<h4>'. $article->title .'</h4>';
}
}
foreach($articles as $rest_articles){
$title = $rest_articles->title;
if($title != $page_title){
echo '<h4>'. $rest_articles->title .'</h4>';
}
}
?>
A better solution is to store values in a new array and display them afterwards.
Hope this helps
Just had a double take here. Multiple foreach loops can be used, however are not required. Try the following:
<?php
foreach($articles as $article) {
$title = $article->title;
$alias = $article->alias;
if($title == $page_title) {
echo '<h4>'. $title .'</h4>';
}
else {
echo '<h4>'. $title .'</h4>';
}
}
?>

Building a html list of links with groups of sublists that are based on a common id

What I have:
I have an array of links and their meta data.
Some links need to be grouped together and some are regular links.
What I'm trying to do:
I'm trying to iterate through my array and group li's with their relative ul, based on a common id.
The problem:
One problem is only making unique ul's based on said id and not make a ul for each item it iterates through.
The second more difficult problem is grouping each item within the relative ul.
Here is an example array I have to work with:
<?php
$array = array();
$array['page']['colours']['link'] = '/colours';
$array['page']['colours']['link-title'] = 'Colours';
$array['page']['colours']['subpage']['green']['aside']['link'] = '/colours/green';
$array['page']['colours']['subpage']['green']['aside']['link-title'] = 'Green';
$array['page']['colours']['subpage']['blue']['aside']['collapse']['id'] = 'cool-colours';
$array['page']['colours']['subpage']['blue']['aside']['link'] = '/colours/blue';
$array['page']['colours']['subpage']['blue']['aside']['link-title'] = 'Blue';
$array['page']['colours']['subpage']['purple']['aside']['collapse']['id'] = 'cool-colours';
$array['page']['colours']['subpage']['purple']['aside']['link'] = '/colours/purple';
$array['page']['colours']['subpage']['purple']['aside']['link-title'] = 'Purple';
$array['page']['colours']['subpage']['orange']['aside']['collapse']['id'] = 'bright-colours';
$array['page']['colours']['subpage']['orange']['aside']['link'] = '/colours/orange';
$array['page']['colours']['subpage']['orange']['aside']['link-title'] = 'Orange';
$array['page']['colours']['subpage']['yellow']['aside']['collapse']['id'] = 'bright-colours';
$array['page']['colours']['subpage']['yellow']['aside']['link'] = '/colours/yellow';
$array['page']['colours']['subpage']['yellow']['aside']['link-title'] = 'Yellow';
$array['page']['colours']['subpage']['red']['aside']['collapse']['id'] = 'bright-colours';
$array['page']['colours']['subpage']['red']['aside']['link'] = '/colours/red';
$array['page']['colours']['subpage']['red']['aside']['link-title'] = 'Red';
$array['page']['colours']['subpage']['pink']['aside']['collapse']['id'] = 'bright-colours';
$array['page']['colours']['subpage']['pink']['aside']['link'] = '/colours/pink';
$array['page']['colours']['subpage']['pink']['aside']['link-title'] = 'Pink';
?>
Here is what I have currently for iterating through this array:
<aside id="sidebar">
<?php
if ( !empty( $array ) ) { ?>
<ul class="menu">
<?php foreach ( $array['page']['colours']['subpage'] as $subpage ) {
if( !empty( $subpage['aside'] ) ) {
$historyArray = array();
?>
<li>
<?php if( !empty( $subpage['aside']['collapse'] ) ) { ?>
<?php if( !in_array( $subpage['aside']['collapse']['id'], $historyArray ) ) { ?>
<a data-toggle="collapse" data-parent="#sidebar" href="#<?php echo $subpage['aside']['collapse']['id'] ?>" class="<?php echo $currentPageLink === $subpage['aside']['link'] ? 'active' : '' ?>"><?php echo $subpage['aside']['link-title'] ?></a>
<ul id="<?php echo $subpage['aside']['collapse']['id'] ?>">
<li>
<?php echo $subpage['aside']['link-title'] ?>
</li>
</ul>
<?php
$historyArray[] = $subpage['aside']['collapse']['id'];
}
?>
<?php } else { ?>
<?php echo $subpage['aside']['link-title'] ?>
<?php } ?>
</li>
<?php } } ?>
</ul>
<?php } ?>
</aside>
Wow, now THAT'S a confusing array! But everything is doable, right? It's just a matter of what you're willing to try. :)
I think you need to create a new array that fits the listing purpose, then iterate through that for the list (The huge complex array will remain intact for whatever else it's used for). Consider this:
<?php
if ( !empty( $array ) ) {
//Create the new array $uls[id][color]['link'/'link-title']
foreach ( $array['page']['colours']['subpage'] as $color => $subpage ) {
$uls[$subpage['aside']['collapse']['id']][$color]['link'] = $subpage['aside']['link'];
$uls[$subpage['aside']['collapse']['id']][$color]['link-title'] = $subpage['aside']['link-title'];
}
}
if ( !empty( $uls ) ) {
echo '<aside id="sidebar">';
echo '<ul class="menu">';
//iterate through the new $uls array
foreach($uls as $id => $color) {
//for each id, we create a ul, then iterate through that id's array
echo '<li><ul id="' . $id . '">';
foreach($color as $link_array) {
//echo the list item for each link in the id
$active = ($currentPageLink === $link_array['link']) ? 'active' : '';
echo '<li>' . $link_array['link-title'] . '</li>';
}
echo '</ul></li>';
}
echo '</ul>';
echo '</aside>';
}
?>
One thing I didn't add was the data-collapse link. I wasn't sure exactly what you were going for there. That shouldn't be too hard to stick in there though.

simplexml_load_file generates same post repeatedly from WP feed

I'm using simplexml_load_file to get rss feed from from a wordpress blog. Here is my code
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
foreach( $items as $item ) {
$article = array();
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['category'] = $item->category;
}
foreach( $items as $item ) { ?>
<?php if($article['category']=="Uncategorized") { ?>
<div><?php echo $article['title'];?></div>
<?php
} } ;
?>
The Problem: It outputs the same post repeatedly x times, where x is total number of posts. Right now there are only two posts in the Uncategorized category and three more posts in other categories. But the code echos the following:
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
<div>Hello world!</div>
Your problem is on the fifth row of your posted code. You must take out the array definition of the first foreach loop:
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
$article = array(); // <- put it here
foreach( $items as $item ) {
$article['title'] = $item->title;
$article['link'] = $item->link;
$article['category'] = $item->category;
}
...
as your current solution resets the $article array for each row. But why don't you loop everything in one foreach loop? If you're not using $article for some other purpose I don't see the use of assigning $item data to an array. Code could be simplified:
$rssfile = simplexml_load_file( "http://blog.sufuraamathi.com/?feed=rss2" );
$items = $rssfile->channel->item ;
foreach( $items as $item ) { ?>
<?php if($item->category=="Uncategorized") { ?>
<div><?php echo $item->title;?></div>
<?php
} } ?>

php foreach : each link in separate variable

I get some links from feed with this code on simplepie :
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links = $item->get_permalink();
echo $links;
endforeach; endif;
thats result me:
http://link1....
http://link2....
http://link3....
i want to put each link in separate variable like :
$links1 = 'http://link1....';
$links1 = 'http://link2....';
$links1 = 'http://link3....';
thanks , mori
$i = 0;
foreach($feed->get_items(0,3) as $item)) {
${'link' . ++$i} = $item->get_permalink();
}
Maybe what you want is array variable?
try this:
if ($check) :
foreach ($feed->get_items(0,3) as $item):
$links[] = $item->get_permalink();
endforeach; endif;
if ($check) :
$i=0;
foreach ($feed->get_items(0,3) as $item):
$links.$i = $item->get_permalink();
echo $links.$i;
$i++;
endforeach; endif;
I think it may works...

How do I Alter this wordpress php to output into an array?

<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
if($child_cats) :
echo '{ ';
foreach($child_cats as $cat) {
echo $sep . $cat->cat_name;
$sep = ', ';
}
echo ' }';
endif;
?>
The above code outputs a number of categorys in this format:
A Cut Above,A20Labs,AMCH,
how would I add ' ' around each of the elements for output like this?
'A Cut Above','A20Labs','AMCH',
2nd question, how would I code it so that that the output goes into this array code like this?
<?php $type_array = array('A Cut Above','A20Labs','AMCH',)?>
Thanks so much!
Azeem
For your first question, change echo $sep . $cat->cat_name; to echo $sep . '\''.$cat->cat_name.'\'';
This will change it to output the name with single quotes around them.
To return an array instead, try this:
<?php
$parent_cat = 57;
$child_cats = get_categories('child_of='.$parent_cat);
$type_array = array();
if($child_cats) :
foreach($child_cats as $cat) {
$type_array[] = $cat->cat_name;
}
endif;
?>
This will place the names into a new array instead of echoing them.
You can get the array you're wanting with a lot less work:
<?php
$child_cats = get_categories(array(
'child_of' => $parent_cat,
'fields' => 'names'
));
?>

Categories