Foreach loop (or do while maybe?) - Want to return only one record depending on page

烂漫一生 提交于 2019-12-02 02:53:38

问题


Solved this with edited code below. Thanks to all who helped!

I have two records in my db. Each record has 6 fields (challengeId, partnerName, code, challengeTitle, description, image_url). I select a given partnerName from my parent page view to go to my child page view.

I was using a foreach loop and having problems. I have now taken out my foreach loop and replaced it with <?php $challengename = $this->challengenames[$k] = current($this->challengenames); ?> but now cannot get the child page to display the values for challengeTitle that correspond to 'partnerName' I chose on my parent page. It always provides the challengeTitle value of the first record instead of the current record. I need to know how to make the challengeTitle value change depending on which partnerName I chose on my parent page.

Will making this a do while loop or if statement in the child page controller fix this?

Any advice (and code changes) is very much appreciated.


parent page controller

public function viewChallengesAction(){
    //get instance for request
    $request = JO_Request::getInstance();
    //get activated challenge names and set variables
    $myChallenge=$this->getChallenge();
    $this->view->challengenames = array();
    foreach($myChallenge AS $k=>$challengename){
        $this->view->challengenames[$k]['href'] = WM_Router::create($request->getBaseUrl() . '?module=challenges&controller=index&action=yourChallenge?code=' . $challengename['partnerName']);
        $this->view->challengenames[$k]['partnerName'] = $challengename['partnerName'];
    }

CHILD VIEW

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

CHILD CONTROLLER

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

    }

回答1:


If you don't know the key and want the current element of the array, you could use:

$challengename = current($this->challengenames);

Or the first element of the array:

$challengename = reset($this->challengenames);

Or the last element of the array:

$challengename = end($this->challengenames);

Just note that end and reset will change the pointer location of the array.




回答2:


 <?php $challengename = $this->challengenames[0] ?> 

You are missing a last ; try add it agaign; try below:

  <?php $challengename = $this->challengenames[0]; ?> 


来源:https://stackoverflow.com/questions/29886181/foreach-loop-or-do-while-maybe-want-to-return-only-one-record-depending-on

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!