gpt4 book ai didi

php - While 循环不会返回给定记录的所有值

转载 作者:行者123 更新时间:2023-11-29 22:30:23 26 4
gpt4 key购买 nike

我是 PHP 和编码新手:

对于每条记录,下面的函数仅返回 $myChallenge 中的“code”变量值。但是,我需要每个记录的其他变量值(例如,partnerName、challengeTitle、image_url),以便在使用 $myChallenge 的后续函数中使用它们。

<小时/>

Controller

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'];

关于php - While 循环不会返回给定记录的所有值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29862141/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com