dongya9346 2014-11-11 14:17
浏览 40
已采纳

PHP数组无法正常工作

I am having some trouble getting my array to work correctly. I am pulling the params from my Joomla template and then using them on the main index file: Here is what I have so far:

This is held in an include file that I pull in first

//Social Icons
$tbFacebook = $this->params->get('tbFacebook');
$tbTwitter = $this->params->get('tbTwitter');
$tbGoogle = $this->params->get('tbGoogle');
$tbLinkedIn = $this->params->get('tbLinkedIn');
$tbPinterest = $this->params->get('tbPinterest');
$tbYouTube = $this->params->get('tbYouTube');
$tbVimeo = $this->params->get('tbVimeo');

//Check all the text boxes for content and assign array information
//This may be able to be handled differently but for simplicity I left it alone 

if ($tbFacebook != null) { 
$arrFacebook= array("href" => $tbFacebook, "title" => "Like Us On Facebook", "icon" => "<i class=\"fa fa-facebook".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbTwitter != null) {
$arrTwitter = array("href" => $tbTwitter , "title" => "Follow Us On Twitter", "icon" => "<i class=\"fa fa-twitter".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbGoogle != null) {
$arrGoogle = array("href" => $tbGoogle , "title" => "Follow Us On Google Plus", "icon" => "<i class=\"fa fa-google-plus".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbLinkedIn != null) {
$arrLinkedIn = array("href" => $tbLinkedIn , "title" => "Connect With Us On LinkedIn", "icon" => "<i class=\"fa fa-linkedin".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbPinterest != null) {
$arrPinterest = array("href" => $tbPinterest , "title" => "Pin Us On Pinterest", "icon" => "<i class=\"fa fa-pinterest".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbYouTube != null) {
$arrYouTube = array("href" => $tbYouTube , "title" => "Watch Us On YouTube", "icon" => "<i class=\"fa fa-youtube".$varSquare." fa-2x\"></i>", "target" => "blank");
};

if ($tbVimeo != null) {
$arrVimeo = array("href" => $tbVimeo , "title" => "Watch Us On Vimeo", "icon" => "<i class=\"fa fa-vimeo-square fa-2x\"></i>", "target" => "blank");
};

//Create Social Array
$arrSocial = array(
    $arrFacebook,
    $arrTwitter,
    $arrGoogle,
    $arrLinkedIn,
    $arrPinterest,
    $arrYouTube,
    $arrVimeo           
);

//Remove the empties
foreach($arrSocial as $key => $value) {
    if (empty($value)) {
        unset($arrSocial[$key]);
        $arrSocial = array_values($arrSocial);
    };
}

//Variable to determine fist item, incase its not phone
$first = true;

my Joomla code

<?php if ($this->countModules('footer_mobile')) : ?>
                <jdoc:include type="modules" name="footer_mobile" style="html5" />
                <?php else : 
                //Limit to the first 5 items
                for($i=0;$i<5;$i++){
                    if ( $first ) { 
                    $first = false;
                        echo '<div class="col-xs-2 col-xs-offset-1"><a href="'.$arrMobileFooter[$i]["href"].'" title="'.$arrMobileFooter[$i]["title"].'" target="_'.$arrMobileFooter[$i]["target"].'" ">'.$arrMobileFooter[$i]["icon"].'</a></div>';
                    } else { 
                    echo '<div class="col-xs-2"><a href="'.$arrMobileFooter[$i]["href"].'" title="'.$arrMobileFooter[$i]["title"].'" target="_'.$arrMobileFooter[$i]["target"].'">'.$arrMobileFooter[$i]["icon"].'</a></div>';
                    };
                };
                endif; ?>

When the file outputs it only will pull in the Facebook link, even though the for loop is creating the rest just not populating it:

<div class="col-xs-2 col-xs-offset-1">
    <a href="https://www.facebook.com/pages/United-Methodist-Camp-Tekoa-Inc/304907509358" title="Like Us On Facebook" target="_blank">
        <i class="fa fa-facebook-square fa-2x"></i>
    </a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_" "=""></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>
<div class="col-xs-2">
    <a href="/" title="" target="_"></a>
</div>

Can someone please tell me what I am doing wrong?

  • 写回答

1条回答 默认 最新

  • dongpian2559 2014-11-11 17:02
    关注

    I would guess that the problem is when you trying to manipulate $arrSocial inside the foreach. I would recommend that you rewrite this and only add the object to the array if they aren't null using $arrSocial[] = value for adding to an existing array.

    Try this:

    //Social Icons
    $tbFacebook = $this->params->get('tbFacebook');
    $tbTwitter = $this->params->get('tbTwitter');
    $tbGoogle = $this->params->get('tbGoogle');
    $tbLinkedIn = $this->params->get('tbLinkedIn');
    $tbPinterest = $this->params->get('tbPinterest');
    $tbYouTube = $this->params->get('tbYouTube');
    $tbVimeo = $this->params->get('tbVimeo');
    
    $arrSocial = array();
    
    if ($tbFacebook != null) { 
        $arrSocial[] = array("href" => $tbFacebook, "title" => "Like Us On Facebook", "icon" => "<i class=\"fa fa-facebook".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbTwitter != null) {
        $arrSocial[]  = array("href" => $tbTwitter , "title" => "Follow Us On Twitter", "icon" => "<i class=\"fa fa-twitter".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbGoogle != null) {
        $arrSocial[] = array("href" => $tbGoogle , "title" => "Follow Us On Google Plus", "icon" => "<i class=\"fa fa-google-plus".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbLinkedIn != null) {
        $arrSocial[] = array("href" => $tbLinkedIn , "title" => "Connect With Us On LinkedIn", "icon" => "<i class=\"fa fa-linkedin".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbPinterest != null) {
        $arrPinterest = array("href" => $tbPinterest , "title" => "Pin Us On Pinterest", "icon" => "<i class=\"fa fa-pinterest".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbYouTube != null) {
        $arrSocial[] = array("href" => $tbYouTube , "title" => "Watch Us On YouTube", "icon" => "<i class=\"fa fa-youtube".$varSquare." fa-2x\"></i>", "target" => "blank");
    };
    
    if ($tbVimeo != null) {
        $arrSocial[] = array("href" => $tbVimeo , "title" => "Watch Us On Vimeo", "icon" => "<i class=\"fa fa-vimeo-square fa-2x\"></i>", "target" => "blank");
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用