它总是只进行一次民意调查。虽然我有不同的民意调查保存在数据库中。它始终可以显示数据库的第一个ID。
<form action="" method="post" name="pollFrm">
<?php
//include and initialize Poll class
include 'Poll.class.php';
$poll = new Poll;
//get poll and options data
$pollResult= $poll->getPolls();
?>
<div class="pollContent">
<?php echo !empty($statusMsg)?'<p class="stmsg">'.$statusMsg.'</p>':''; ?>
<form action="" method="post" name="pollFrm">
<h3><?php echo $pollResult['poll']['subject']; ?></h3>
<ul>
<?php foreach($pollResult['name'] as $opt){
echo '<li><input type="radio" name="voteOpt" value="'.$opt['id'].'" >'.$opt['name'].'</li>';
} ?>
</ul>
<input type="hidden" name="pollID" value="<?php echo $pollResult['poll']['id']; ?>">
<input type="submit" name="voteSubmit" class="button" value="Vote">
<a href="results.php?pollID=<?php echo $pollResult['poll']['id']; ?>">Results</a>
</form>
</div>
</form>
这是获取民意调查的功能。我尝试删除单曲。但它行不通。
public function getPolls($pollType){
$pollData = array();
$sql = "SELECT * FROM ".$this->pollTbl." WHERE status = '1' ORDER BY created DESC";
$pollResult = $this->getQuery($sql, $pollType);
if(!empty($pollResult)){
if($pollType == 'single'){
$pollData['poll'] = $pollResult;
$sql2 = "SELECT * FROM ".$this->optTbl." WHERE poll_id = ".$pollResult['id']." AND status = '1'";
$optionResult = $this->getQuery($sql2);
$pollData['name'] = $optionResult;
}else{
$i = 0;
foreach($pollResult as $prow){
$pollData[$i]['poll'] = $prow;
$sql2 = "SELECT * FROM ".$this->optTbl." WHERE poll_id = ".$prow['id']." AND status = '1'";
$optionResult = $this->getQuery($sql2);
$pollData[$i]['name'] = $optionResult;
}
}
}
return !empty($pollData)?$pollData:false;
}
/ *
*对数据库运行查询
* @param字符串SQL
* @param字符串计数,单个,全部
* /
private function getQuery($sql,$returnType = ''){
$result = $this->db->query($sql);
if($result){
switch($returnType){
case 'count':
$data = $result->num_rows;
break;
case 'single':
$data = $result->fetch_assoc();
break;
default:
if($result->num_rows > 0){
while($row = $result->fetch_assoc()){
$data[] = $row;
}
}
}
return !empty($data)?$data:false;
}
最佳答案
您的函数getPolls的默认参数为$pollType = 'single'
,如果不对该函数进行任何传递,则解析为默认的“单”。
由于该参数被传递给getQuery函数,因此始终只会获得一个参数。我建议完全删除默认参数,并将方法更改为:
public function getPolls($pollType){
关于javascript - 它不能循环其他民意调查。它只能显示民意调查的第一个ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/52915123/