foreach循环-仅返回一条记录 [英] foreach loop - return only one record

查看:129
本文介绍了foreach循环-仅返回一条记录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的数据库有两条记录带有多个字段(例如partnerName,image_url等).下面粘贴的foreach循环可以完美工作,从两条记录中返回两个值'partnerName'.
我现在只想从当前记录中返回该值.

My db has two records with multiple fields (e.g. partnerName, image_url, etc). The foreach loop pasted below works perfectly, returning the two values 'partnerName' from the two records.
I now only want to return the value from the current record.

我替换了以下行: <?php foreach($this->challengenames as $k=>$challengename) { ?>

I replaced the following line: <?php foreach($this->challengenames as $k=>$challengename) { ?>

<?php $challengename = $this->challengenames[0]; ?> 并摆脱了结尾的php括号. 这摆脱了循环,但是我丢失了索引k,所以没有任何值.关于如何修改此循环以保留索引k但仅返回当前记录的值的任何建议吗?

with <?php $challengename = $this->challengenames[0]; ?> and got rid of the ending php bracket. This gets rid of the loop, but I lose the index k so I get no values. Any advice on how to modify this loop to retain the index k but return only the value for the current record?

非常感谢!

<div id="defaultcontainerwrapper" class="maxwidth">
<?php foreach($this->challengenames as $k=>$challengename) { ?>
    <header>
        <h1>
            <div class="list">
                <span>Welcome to </span><?php echo $challengename['partnerName']; ?><span>'s Beat Waste Challenge!</span>
            </div>
        </h1>
    </header>
<?php } ?>
</div>

推荐答案

使用以下代码-

<div id="defaultcontainerwrapper" class="maxwidth">
    <header>
        <h1>
            <div class="list">
                <span>Welcome to </span><?php echo $this->challengenames[0]['partnerName']; ?><span>'s Beat Waste Challenge!</span>
            </div>
        </h1>
    </header>
</div>

这篇关于foreach循环-仅返回一条记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆