简体   繁体   中英

Getting rid of foreach loop to return a single record

This should be simple, but for me as a beginner, I am still struggling with the various View display calls. I borrowed this foreach loop from another part of my software, and it is working correctly. It returns values for each record on my page. However, I only want the current record. So, I need to get rid of the foreach loop but can't figure out how to convert this foreach loop to get only the current record.

Thanks so much for your help.

<?php echo $this->header_part; ?>

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

<?php echo $this->footer_part; ?>

this is the function from the controller:

public function yourChallengeAction(){
    //get activated challenge names and set variables
    $request = JO_Request::getInstance();
    $myChallenge=$this->getChallenge();
    $this->view->challengenames = array();
    foreach($myChallenge AS $k=>$challengename){
        $this->view->challengenames[$k]['partnerName'] =    $challengename['partnerName'];
        $this->view->challengenames[$k]['code'] = $challengename['code'];
        $this->view->challengenames[$k]['challengeTitle'] = $challengename['challengeTitle'];
        $this->view->challengenames[$k]['description'] = $challengename['description'];
        $this->view->challengenames[$k]['image_url'] = $challengename['image_url'];
    }

use reset() or use [0] as your array key.

See Also ¶

current() - Return the current element in an array
each() - Return the current key and value pair from an array and advance the array cursor
end() - Set the internal pointer of an array to its last element
next() - Advance the internal array pointer of an array
prev() - Rewind the internal array pointer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM