Loop Won不返回给定记录的所有值

时间:2015-04-25 07:04:27

标签: php mysql while-loop

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

=============

ViewChallenge

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

1 个答案:

答案 0 :(得分:0)

将整行放入$myChallenge

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

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

之类的值
$myChallenge[$code]['partnerName'];