允许用户从选择框中选择SET time_zone



我已经尝试了一段时间,现在在我的网站上处理时区。我的网站包含运动时间表。我输出匹配细节到一个表中。

我试图找出一种方法,让用户选择(从一个选择框填充的时区值,如GMT +1:00, GMT 2:00等)他们所在的时区。当用户选择时区时,应该更新匹配细节(时间戳字段)中的日期和时间值,以反映用户选择的时区。

我使用了下面的代码来设置时区,它将日期和时间字段的时区更改为我在set语句中正确使用的偏移量。

$DB->exec("SET time_zone='+0:00';");`

让我困惑的是我怎么能让+0:00改变用户从选择框中选择的东西,而不是总是被设置为"+0:00"。

我尝试了一个if语句,如

if (offset = "+1:00") {
$DB->exec("SET time_zone='+1:00';");
}

但是当我从选择框中选择+1:00并单击提交按钮时,URL显示offset=+1:00,但是日期和时间字段的值不会改变以反映if语句中的时区变化,而只是保持

下面SET语句中的值
$DB->exec("SET time_zone='+0:00';");

我这样做对吗?或者我可以用别的方法吗?此外,我还认为,当按下提交按钮时,输出被放入URL字符串而不是SET time_zone语句。是否有一种方法,我可以有按钮时单击更改SET语句的值从下拉框中选择的偏移量?

下面是我的HTML和PHP代码

Config.php(用于数据库连接)

<?php
error_reporting( E_ALL & ~E_DEPRECATED & ~E_NOTICE );
define('DB_DRIVER', 'mysql');
define('DB_SERVER', '*******');
define('DB_SERVER_USERNAME', '****"');
define('DB_SERVER_PASSWORD', '*****"');
define('DB_DATABASE', 'sportsch_sport');

define('PROJECT_NAME', 'SportSchedule Easy Sports Schedules');
$dboptions = array(
          PDO::ATTR_PERSISTENT => FALSE, 
          PDO::ATTR_DEFAULT_FETCH_MODE =>        PDO::FETCH_ASSOC, 
          PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION,
          PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8',
        );
     try {
     $DB = new   PDO(DB_DRIVER.':host='.DB_SERVER.';dbname='.DB_DATABASE, DB_SERVER_USERNAME, DB_SERVER_PASSWORD , $dboptions);  
     $DB->exec("SET time_zone='+0:00';");
     } catch (Exception $ex) {
      echo $ex->getMessage();
      die;
      }

     ?>

Index.php(主页面,用户可以通过本页面的搜索框搜索球队,默认情况下它将显示数据库中的所有比赛)

<?php
date_default_timezone_set('Europe/London');
echo date('Y-m-d H:i:s');
include './config.php';
include './header.php';
if (offset = "+1:00") {
$DB->exec("SET time_zone='+1:00';");
}
try{
 $keyword = trim($_GET["keyword"]);
 if ($keyword <> "" ) {
  $sql = "SELECT f.hometeam, f.versus, f.awayteam,    f.sport, f.competition, f.dateandtime,
   Group_concat(s.name SEPARATOR ',') name,
    Group_concat(x.channelid_fc SEPARATOR ',') channelid_fc
   FROM footballfixtures f
  LEFT JOIN fixturechannels x
  ON x.matchid_fc=f.matchid
  LEFT JOIN satellite s
  ON x.channelid_fc=s.channelid
  WHERE  "   . " (hometeam LIKE :keyword)
  OR awayteam LIKE :keyword
 OR competition LIKE :keyword
 GROUP BY f.hometeam, f.versus, f.awayteam, f.sport, f.competition, f.dateandtime,
 ORDER BY f.dateandtime ";
 $stmt = $DB->prepare($sql);
 $stmt->bindValue(":keyword", $keyword."%");

 } else {
      $sql = "SELECT f.hometeam, f.versus, f.awayteam,   f.sport, f.competition, f.dateandtime,
     Group_concat(s.name SEPARATOR ',') name,
     Group_concat(x.channelid_fc SEPARATOR ',').   channelid_fc
   FROM footballfixtures f
  LEFT JOIN fixturechannels x
  ON x.matchid_fc=f.matchid
  LEFT JOIN satellite s  
 ON x.channelid_fc=s.channelid
 GROUP BY f.hometeam, f.versus, f.awayteam, f.sport, f.competition, f.dateandtime
  ORDER BY f.dateandtime";
   $stmt = $DB->prepare($sql);
    }
    $stmt->execute();
   } catch (Exception $ex) {
    echo 'Error loading match data';
  }


  ?>
 <html>
<head>
 <script src="css/footable.filter.js"></script>
</head>
    <body>
    <div class="container mainbody">
    <div class="mainpagetitle">
     <h11>Sports Schedule</h11> <br> <br>
      <p>We aim to provide you with sports schedule in an easy to view format</p> <br> <br> <br>
    <form class="form-inline">
   </div>

  <div class="clearfix"></div>
 <div class="col-xs-12">
      <img src="css/tv3.png" class="img-responsive" />
               <div id=class="container-fluid">
    <div class="row">
    <h2>Whats On Today</h2> <br>
    <p> All todays matches/events are displayed below, you can search by team/player or competition/event using the search box. If you want to filter by sport, please select the sport name from the select box and filtered results will be displayed. </p>
         <br>
        <br>
   <br>
        <br>
   </div>  
   </div>
   </div>

       <div class="searchform">
       <form action="index.php" method="get" >
        <label class="col-xs-12" for="offset";>
          <select name="offset" id="offset">
      <option value="+1:00">+1:00</option>
    <option value="+2:00">+2:00</option>
   <option value="+3:00">+3:00</option>
   <option value="+4:00">+4:00</option>
  </select>
        </label>
        <button class="btn btn-info">search</button>
     </form>
     </div>



     <div class="searchform">
<h2> Search </h2>
    <form action="index.php" method="POST" >
        <label class="col-xs-12" for="keyword";>
          <input type="text" value="<?php echo htmlspecialchars($_POST["keyword"]); ?>" placeholder="Enter Team or Competition" id="" class="form-control" name="keyword">
        </label>
      <button class="btn btn-info">search</button>
       </form>
     </div>


      <div class="clearfix"></div>

     <div class="searchform2">
        <p> Select Sport Below to Display Events</p>
      <div class="dropbox1">
      <input type="hidden" id="filter" type="text"></input>
    <select class="filter-status">
<option> </option>
<option value="aussierules">Aussie Rules</option>
<option value="basketball">Basketball</option>
<option value="boxing">Boxing</option>
  <option value="football">Football</option>
<option value="gaelic">Gaelic Sports</option>
<option value="handball">Handball</option>
<option value="icehockey">Ice Hockey</option>
<option value="mma">MMA</option>
<option value="motorsports">Motorsports</option>
<option value="rugbyleague">Rugby League</option>
<option value="rugbyunion">Rugby Union</option>
<option value="tennis">Tennis</option>
<option value="volleyball">Volleyball</option>
   </select>
   </div>
   <div class="clearlinks">
    <a class="clear-filter" href="#clear" title="clear filter">. [clear]</a>
   </div>
   </div>
   <div class="container">
    <div class="row">
    <div class="tables">
    <div class="col-xs-12">
    <table class="table table-hover footable" data-filter="#filter" data-filter-text-only="true">
   <thead>
    <tr>
   <th>Home Team</th>
   <th> vs </th>
   <th>Away Team</th>
   <th data-hide="phone, tablet">Sport</th>
        <th data-hide="phone, tablet">Competition</th>
           <th data-hide="phone, tablet"> Date and Time</th>
                        <th data-hide="phone, tablet">Channels</th>
   </tr>
 </thead>
       <?php 
   if($stmt->rowCount() >0) {   
    while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
   {

   $hometeam = $row['hometeam'];
$versus= $row['versus'];
         $awayteam= $row['awayteam'];
         $sport= $row['sport'];
         $competition = $row['competition'];
         $dateandtime=$row['dateandtime'];
         $name=explode(',', $row['name']);
         $channelid=explode(',', $row['channelid_fc']);
?>
<tbody>
    <td> <?php echo $row[hometeam] ; ?> </td>
     <td> <?php echo $row[versus] ; ?> </td>
     <td> <?php echo $row[awayteam] ; ?> </td>
     <td> <?php echo $row[sport] ; ?> </td>
    <td> <?php echo $row[competition] ; ?> </td>
     <td> <?php echo $row[dateandtime] ; ?> </td>
    <td>
   <?php for ($i = 0; $i < count($channelid) && $i <  count($name); ++$i) {
    $achannelid = $channelid[$i];
    $aname = $name[$i];
    //here the code you wanted
             echo "<a href='http://sportschedule.xyz/view_channels.php?channelid=" .$achannelid."'> ".$aname." </br> </a> ";
         }
        ?>

非常感谢任何人提供的任何帮助或建议,或者任何阅读这篇文章的人。

找到解决方案

if (isset($_GET['offset'])) { 
$os = $_GET['offset'];
if ($os >= "1" && $os < "5" && (strlen($os)==1) ) { // basic attempt at  sanitising the input
 $DB->exec("SET time_zone='+" . $os . ":00';");
}
}

相关内容

  • 没有找到相关文章