Foreach循环(或者可能吗?) - 要根据页面只返回一个记录 [英] Foreach loop (or do while maybe?) - Want to return only one record depending on page
问题描述
用下面的编辑代码解决这个问题。感谢所有的帮助!
我在db中有两条记录。每个记录有6个字段(challengeId,partnerName,code,challengeTitle,description,image_url)。我从我的父页面视图中选择一个给定的partnerName去我的子页面视图。
我正在使用foreach循环并遇到问题。我已经拿出我的foreach循环,并用<?php $ challengename = $ this-> challengenames [$ k] = current($ this-> challengenames)替换它。 ?>
,但现在无法获取子页面显示与我在我的父页面上选择的partnerName相对应的challengeTitle的值。它始终提供第一条记录的challengeTitle值,而不是当前记录。我需要知道如何使challengeTitle值的变化取决于我在我的父页面上选择的哪一个partnerName。
将这个做为while循环或if语句页面控制器修复这个问题?
任何建议(和代码更改)非常赞赏。 b
父页面控制器
public function viewChallengesAction(){
// get instance for请求
$ request = JO_Request :: getInstance();
//获得激活的挑战名称和设置变量
$ 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 =defaultcontainerwrapperclass =maxwidth>
<?php $ challengename = $ this-> challenge; ?>
< header>
< h1>
< div class =list>
< span>欢迎使用< / span><?php echo $ challengename ['partnerName']; ?>< span>的Beat Waste Challenge!< / span>
< / div>
< / h1>
< / header>
<?php}?>
< / div>
CHILD CONTROLLER
public function yourChallengeAction(){
//获取激活的挑战名称和设置变量
$ 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;
$ b
如果您不知道该键,并且想要数组的当前元素,您可以使用:
$ challengename =当前($ this-> challengenames);
或者数组的第一个元素:
$ challengename = reset($ this-> challengenames);
或者数组的最后一个元素:
$ challengename = end($ this-> challengenames);
请注意,结束和重置将改变数组的指针位置。 $ b
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;
}
}
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.
这篇关于Foreach循环(或者可能吗?) - 要根据页面只返回一个记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!