0

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 } ?>
3
  • bit unclear, but I think you should use array Commented Mar 9, 2016 at 7:20
  • Is there anything I can do to clear it up a little? I'm confused on how to use an array with the opt('first_tab_page') pieces Commented Mar 9, 2016 at 7:23
  • how many page you are expecting there Commented Mar 9, 2016 at 7:29

3 Answers 3

1

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>
Sign up to request clarification or add additional context in comments.

2 Comments

This seems to be the correct answer but now I have two loops so everything is shown twice. How do I modify the tab_position code to still count through the number of posts and append the proper number in the <a href="tab<?php echo $tab_position[$i];?>"> and applies the selected field but doesn't reprint everything another 2 times. Here's what I have so far: pastebin.com/q20xPGCA
looked into your code. I didn't understand the tab_position stuff. what does the opt('default_selected_tab') return?
0

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"];
}

Comments

0

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.

2 Comments

I'm confused how to plug this in, it's already inside some sort of loop (I think) so it just repeats all the $content variables inside each <a>
No, the first few lines of your code defines 3 variables $content0, $content1 and $content2 and they are not inside a loop. just put them in $contents array and then where ever you want them to display just lop them using foreach loop above in my code.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.