我是PHP和编码的新手:
对于每条记录,以下功能仅返回"代码" $ myChallenge中的变量值。但是,我需要为每条记录使用其他变量值(例如partnerName,challengeTitle,image_url),以便在后续使用$ myChallenge的函数中使用它们。
public function getChallenge(){
//challengenames
$link=$this->dbPrototype();
$query="select * from challengestable where status=0 ORDER BY date DESC";
$result=$link->query($query);
while($row=mysqli_fetch_array($result)) {
$myChallenge[$row['challengeId']] = $row;
}
return $myChallenge;
}
public function viewChallengesAction(){
//get instance for request
$request = JO_Request::getInstance();
//get activated challengenames
$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=' . $k);
$this->view->challengenames[$k]['title'] = $challengename['title'];
}
<?php if ($this->challengenames && is_null($this->tag)) { ?>
<div id="browsecats">
<div class="list">
<ul>
<?php foreach($this->challengenames as $k=>$challengename) { ?>
<li><a href="<?php echo $challengename['href']; ?>"><?php echo $challengename['title']; ?></a></li>
<?php } ?>
</ul>
<div class="clear"></div>
</div>
</div>
<?php }?>
=============
<?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; ?>
答案 0 :(得分:0)
将整行放入$myChallenge
:
while ($row = mysqli_fetch_array($result)) {
$myChallenge[$row['code']] = $row;
}
现在$myChallenge
将是一个二维数组。您可以访问
$myChallenge[$code]['partnerName'];