我是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; ?>

最佳答案

将整行放入$myChallenge

while ($row = mysqli_fetch_array($result)) {
    $myChallenge[$row['code']] = $row;
}


现在$myChallenge将是一个二维数组。您可以访问类似的值

$myChallenge[$code]['partnerName'];

10-08 02:45