php变量if else语句

时间:2019-12-18 03:50:45

标签: javascript php

我正在尝试为我的php民意测验定义php变量。我要回应“祝你今天愉快!”如果对“你好”的投票比对“其他”的投票更多,则回声“祝你晚安!”。 :

if ($votePercent < $votePercent) {
    echo "Have a good day!";
} else {
    echo "Have a good night!";
}
    ?>

如何定义变量$ votePercent?

    <div class="container">
<h3><?php echo $pollResult['poll']; ?></h3>
<p><b>Total Votes:</b> <?php echo $pollResult['total_votes']; ?></p>
<?php
if(!empty($pollResult['options'])){ $i=0;
    //options bar color class array
    $barColorArr = array('azure','emerald','violet','yellow','red');
    
    //generate option bars with votes count
    foreach($pollResult['options'] as $opt=>$vote){
      //calculate vote percent
      $votePercent = round(($vote/$pollResult['total_votes'])*100);
      $votePercent = !empty($votePercent)?$votePercent.'%':'0%';
      
      //define bar color class
      if(!array_key_exists($i, $barColorArr)){
          $i = 0;
      }
      $barColor = $barColorArr[$i];
?>
<div class="bar-main-container <?php echo $barColor; ?>">
    <div class="txt"><?php echo $opt; ?></div>
    <div class="wrap">
        <div class="bar-percentage"><?php echo $votePercent; ?></div>
        <div class="bar-container">
            <div class="bar" style="width: <?php echo $votePercent; ?>;"></div>
        </div>
    </div>
</div>
<?php $i++; } } ?>
<a href="index.php">Back To Poll</a>
</div>

这是我连接到mysql的方式。

<?php
class Poll{
    private $dbHost  = 'localhost';
    private $dbUser  = '';
    private $dbPwd   = '';
    private $dbName  = '';            
    private $db      = false;
    private $pollTbl = 'polls';
    private $optTbl  = 'poll_options';
    private $voteTbl = 'poll_votes';
    
    public function __construct(){
        if(!$this->db){ 
            // Connect to the database
            $conn = new mysqli($this->dbHost, $this->dbUser, $this->dbPwd, $this->dbName);
            if($conn->connect_error){
                die("Failed to connect with MySQL: " . $conn->connect_error);
            }else{
                $this->db = $conn;
            }
        }
    }
    
    /*
     * Runs query to the database
     * @param string SQL
     * @param string count, single, all
     */
    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;
    }
    
    /*
     * Get polls data
     * Returns single or multiple poll data with respective options
     * @param string single, all
     */
    public function getPolls($pollType = 'single'){
        $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['options'] = $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]['options'] = $optionResult;
                }
            }
        }
        return !empty($pollData)?$pollData:false;
    }
    
    /*
     * Submit vote
     * @param array of poll option data
     */
    public function vote($data = array()){
        if(!isset($data['poll_id']) || !isset($data['poll_option_id']) || isset($_COOKIE[$data['poll_id']])){
            return false;
        }else{
            $sql = "SELECT * FROM ".$this->voteTbl." WHERE poll_id = ".$data['poll_id']." AND poll_option_id = ".$data['poll_option_id'];
            $preVote = $this->getQuery($sql, 'count');
            if($preVote > 0){
                $query = "UPDATE ".$this->voteTbl." SET vote_count = vote_count+1 WHERE poll_id = ".$data['poll_id']." AND poll_option_id = ".$data['poll_option_id'];
                $update = $this->db->query($query);
            }else{
                $query = "INSERT INTO ".$this->voteTbl." (poll_id,poll_option_id,vote_count) VALUES (".$data['poll_id'].",".$data['poll_option_id'].",1)";
                $insert = $this->db->query($query);
            }
            return true;
        }
    }
    
    /*
     * Get poll result
     * @param poll ID
     */
    public function getResult($pollID){
        $resultData = array();
        if(!empty($pollID)){
            $sql = "SELECT p.subject, SUM(v.vote_count) as total_votes FROM ".$this->voteTbl." as v LEFT JOIN ".$this->pollTbl." as p ON p.id = v.poll_id WHERE poll_id = ".$pollID;
            $pollResult = $this->getQuery($sql,'single');
            if(!empty($pollResult)){
                $resultData['poll'] = $pollResult['subject'];
                $resultData['total_votes'] = $pollResult['total_votes'];
                $sql2 = "SELECT o.id, o.name, v.vote_count FROM ".$this->optTbl." as o LEFT JOIN ".$this->voteTbl." as v ON v.poll_option_id = o.id WHERE o.poll_id = ".$pollID;
                $optResult = $this->getQuery($sql2);
                if(!empty($optResult)){
                    foreach($optResult as $orow){
                        $resultData['options'][$orow['name']] = $orow['vote_count']; 
                    }
                }
            }
        }
        return !empty($resultData)?$resultData:false;
    }
}

这是index.php代码。

  <form method="post" action="">
            <h3><?php echo $pollData['poll']['subject']; ?></h3>
            <ul>
                <?php foreach($pollData['options'] as $opt){
                    echo '<li><input type="radio" name="voteOpt" value="'.$opt['id'].'" >'.$opt['name'].'</li>';
                } ?>
            </ul>
            <input type="hidden" name="pollID" value="<?php echo $pollData['poll']['id']; ?>">
            <input type="submit" name="voteSubmit" value="Vote">
            <a href="results.php?pollID=<?php echo $pollData['poll']['id']; ?>">Results</a>
        </form>

这是我的测试网址:php poll。如果您需要更多详细信息,请告诉我。我不确定需要在此处显示哪个代码。非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

尝试一下- results.php

structure(list(dt = structure(c(1576173947, 1576173957, 1576173976, 
1576173979, 1576174036, 1576177636), class = c("POSIXct", "POSIXt"
), tzone = ""), name = structure(c(1L, 1L, 2L, 2L, 2L, 2L), .Label = c("Jane Jetson", 
"John Doe"), class = "factor"), foo_id = c(106337L, 106337L, 
106338L, 106338L, 106338L, 106338L), foo_role = structure(c(1L, 
1L, 1L, 1L, 1L, 1L), .Label = "Mechanic", class = "factor"), 
    bikeId = c(12345L, 12345L, 12345L, 12345L, 12345L, 56789L
    ), station_name = structure(c(1L, 1L, 1L, 1L, 1L, 2L), .Label = c("FooStation", 
    "Some Station"), class = "factor"), station_id = c(1234.89, 
    1234.89, 1234.89, 1234.89, 1234.89, 4567.12), action = c("Release", 
    "Return", "Release", "Return", "Release", "Release")), row.names = c(NA, 
-6L), class = "data.frame")

index.php

在此页面中,单选按钮值为1和2

相关问题