如何将计数附加到php中的变量

时间:2016-03-09 07:06:08

标签: php counter

我刚刚开始学习PHP并解决问题我试图修改现有代码以匹配我尝试做的事情。我有多个变量,我试图定义所有,而部分使用计数器。我的问题是将计数附加到我单独定义的变量。另外,我确信有一种更简洁的方法(如循环或嵌套数组),我只是看不出来。

<?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 个答案:

答案 0 :(得分:1)

以下是使用数组的简短示例。代码中的评论

// 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' );
}

现在$icons[0]将包含第一个图标$icons[1],依此类推。同样的事情适用于其他变量。

注意:此代码只是输入此处而未选中。如果有任何语法错误,请修复。另请注意,这是一种方式,还有更多方法。

但我建议将每页的数据保存在一起。

修改:以下是您可以将各种内容保持在一起的方式(仅显示相关部分)

$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 } ?>

而不是foreach显示你可以使用索引也像

<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>

答案 1 :(得分:0)

我不确定你要做什么,因为你的问题对我来说不是很清楚。
但我认为你试图在你的最后一行回应 $ content 码。如果是,请将您的所有变量 $ content1 $ content2 $ content3 放入如下数组中:

$contents = array($content1, $content2, $content3);

然后使用foreach循环并像这样遍历这个数组。

$count = 0;
foreach($contents as $content) {
    echo $content.$count;
    $count++;   
}

尝试让我知道它是否适合您。

答案 2 :(得分:0)

我想那时(嵌套)数组没有别的办法了。我也认为数组是一个可以接受的解决方案。

foreach ($tabs as $tab) {
    $tab['content'] = 'somecontent';
    $tab['counter'] = 'counter';
}

foreach ($tabs as $tab) {
    $tab = [
      "content" => "content",
      "counter" => "counter"];
}