【问题标题】:How to append count to variables in php如何将计数附加到php中的变量
【发布时间】:2016-03-09 07:06:07
【问题描述】:

我刚刚开始学习 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 } ?>

【问题讨论】:

  • 有点不清楚,但我认为你应该使用array
  • 我能做些什么来清理一下吗?我对如何将数组与 opt('first_tab_page') 片段一起使用感到困惑
  • 你期望有多少页

标签: php counter


【解决方案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 } ?>

你也可以使用 index 来代替 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>

【讨论】:

【解决方案2】:

我想除了(嵌套)数组之外别无他法。另外我认为数组是一个可以接受的解决方案。

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

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

【讨论】:

    【解决方案3】:

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

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

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

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

    尝试告诉我它是否适合您。

    【讨论】:

    • 我很困惑如何插入它,它已经在某种循环中(我认为)所以它只是重复每个 中的所有 $content 变量
    • 不,代码的前几行定义了 3 个变量 $content0、$content1 和 $content2,它们不在循环内。只需将它们放在 $contents 数组中,然后在我的代码中使用上面的 foreach 循环将它们显示在您希望它们显示的任何位置。
    猜你喜欢
    • 2014-02-06
    • 1970-01-01
    • 2016-07-14
    • 2016-06-18
    • 1970-01-01
    • 2015-11-27
    • 2018-11-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多