2017-09-25 62 views
0

我正在使用PHP OOP处理CMS。在这个项目中,用户可以添加新的电报频道。针对这一特点,我说这种形式也包含动作代码:如何用PHP OOP插入多个单选按钮值

<?php 
if(isset($_POST['submit'])){ 
    $token = $_POST['token']; 
    $cat = $_POST['cat']; 
    $ads = $_POST['ads']; 
    $key = $_POST['keyboard']; 
    $tel = new Telegram(); 
    $notice = $tel->AddNew($token,$cat,$ads,$key); 
} 
?> 
<div class='content-wrapper'> 
    <section class='content-header'> 
     <h1> 
      Add New Telegram Account 
      <small>You can add a new Telegram channel here</small> 
     </h1> 
     <ol class='breadcrumb'> 
      <li class='active'>telegram.php</li> 
     </ol> 
    </section> 
    <?php 
    if($dataSet->GetLevel()==1) 
    { echo " 
     <section class='content'> 
      <div class='row'> 
       <div class='col-md-6'> 
        <div class='box box-primary'> 
         <div class='box-header with-border'> 
          <h3 class='box-title'>Required Information</h3> 
         </div> 
         "; 
         if(isset($notice['success_message'])){ 
          echo " 
           <div class='alert alert-success'> 
            <strong>Hey!</strong> ".$notice['success_message'].". 
           </div> 
          "; 
         } 
         echo " 
         <form role='form' method='POST' action=''> 
          <div class='box-body'> 
           <div class='form-group'> 
            <label>Token Number</label> 
            <input type='text' class='form-control' placeholder='Enter token' name='token' required> 
            <a href='#' style='color:purple;'>Having problem while getting token</a> 
           </div> 
           <div class='form-group'> 
            <label>Select Category</label> 
            <select name='cat' class='form-control'> 
             <option value='empty'>---</option> 
             <option value='technology'>Technology</option> 
             <option value='4fun'>Game & Fun</option> 
             <option value='news'>News</option> 
             <option value='tools'>Tools</option> 
             <option value='learning'>Learning</option> 
             <option value='traditional'>Traditional</option> 
             <option value='media'>Media</option> 
            </select> 
           </div> 
           <div class='form-group'> 
            <div class='radio'> 
             <label> 
              <input type='radio' name='ads' id='optionsRadios1' value='on' checked> 
              Set ads on</br> 
              <input type='radio' name='ads' id='optionsRadios1' value='off'> 
              Set ads off 
             </label> 
            </div> 
           </div> 
           <div class='form-group'> 
            <div class='checkbox'> 
             <label> 
              <input type='checkbox' name='keyboard' value='with_keyboard'> 
              Use dedicated keyboard for this bot 
             </label></br> 
             <label> 
              <input type='checkbox' name='keyboard' value='without_keyboard'> 
              Show keyboard at groups 
             </label></br> 
             <label> 
              <input type='checkbox' name='answer' value='answer_messages_chats' checked> 
              In private chats, just anwser the pre defined messages 
             </label></br> 
             <label> 
              <input type='checkbox' name='answer' value='answer_messages_groups' checked> 
              In groups, just answer the pre defined messages 
             </label> 
            </div> 
           </div> 
          </div> 
          <div class='box-footer'> 
           Visit <a href='https://zite.pouyavagefi.com/documentation/telegram.php'>Telegram</a> Social Media Documentation. 
          </div> 
          <div class='box-footer'> 
           <button name='submit' type='submit' class='btn btn-primary'>Submit</button> 
          </div> 
         </form> 
        </div> 
       </div> 
      </div> 
     </section> "; 
    }else{ 
     echo " 
     <section class='content'> 
      <div class='alert alert-warning'> 
       <strong>Access Denied!</strong> You don\'t have permission to access this page. 
      </div> 
     </section> "; 
    } 
    ?> 
</div> 

正如你可以在上面,我已经叫一个被称为Telegram.class.php和这个类是这样的:

<?php 
class Telegram 
{ 
    protected $notice = array(); 
    private $db; 
    public function __construct() 
    { 
     $this->db = new Connection(); 
     $this->db = $this->db->dbConnect(); 
    } 
    public function AddNew($token,$cat,$ads,$key) 
    { 
     if(!empty($token)&&!empty($cat)&&!empty($ads)) 
     { 
      for ($i=0;$i<sizeof($ads);$i++) 
      { 
       for ($i=0;$i<sizeof($key);$i++) 
       { 
        $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, "/*.$ads[$i].*/", "/*.$key[$i].*/")"); 
        $new->bindParam(1,$token); 
        $new->bindParam(2,$cat); 
        $new->bindParam(3,$ads); 
        $new->bindParam(4,$key); 
        $new->execute(); 
        $notice['success_message'] = "New Telegram Channel was successfully added"; 
        return $this->notice; 
       } 
      } 

     } 
    } 
    public function getNotice() 
    { 
     return $this->notice; 
    } 
} 
?> 

因为我想多个复选框添加到表中,我使用该for循环的方法Add_New内(根据该qeustion):

for ($i=0;$i<sizeof($ads);$i++) 
     { 
      for ($i=0;$i<sizeof($key);$i++) 
      { 
       $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, "/*.$ads[$i].*/", "/*.$key[$i].*/")"); 
       $new->bindParam(1,$token); 
       $new->bindParam(2,$cat); 
       $new->bindParam(3,$ads); 
       $new->bindParam(4,$key); 
       $new->execute(); 
       $notice['success_message'] = "New Telegram Channel was successfully added"; 
       return $this->notice; 
      } 
     } 

我知道这是不是正确的,但我不知道添加这些$广告[$ i]$键[$ i]变量到插入声明...

的正确方法所以如果你知道如何按照正确的顺序来做到这一点,请让我知道..谢谢!

回答

0

只需使用implode和爆炸功能的PHP。在保存数据时,如下所示:

$key = implode(", ", $_POST['keyboard']); 
$tel = new Telegram(); 
$notice = $tel->AddNew($token,$cat,$ads,$key); 

根据您的需要替换implode“,”的第一个参数。

用于显示使用爆炸如下: $ id = explode(“,”,$ DbRes-> keyboard);

一旦你将你的数组转换为字符串,那么就不需要在你的类中循环。请参阅以下课程的更新代码:

<?php 
class Telegram 
{ 
    protected $notice = array(); 
    private $db; 
    public function __construct() 
    { 
     $this->db = new Connection(); 
     $this->db = $this->db->dbConnect(); 
    } 
    public function AddNew($token,$cat,$ads,$key) 
    { 
     if(!empty($token)&&!empty($cat)&&!empty($ads)) 
     { 

      $new = $this->db->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, ".$ads.", ".$key.")"); 
      $new->bindParam(1,$token); 
      $new->bindParam(2,$cat); 
      $new->bindParam(3,$ads); 
      $new->bindParam(4,$key); 
      $new->execute(); 
      $notice['success_message'] = "New Telegram Channel was successfully added"; 
      return $this->notice; 
     } 
    } 
    public function getNotice() 
    { 
     return $this->notice; 
    } 
} 
?> 

这将帮助您建立理解。你需要将数组转换为字符串,以便mysql数据库可以存储它。这篇文章也可以是有益的:Save PHP array to MySQL?

+1

那么关于类,在类文件没有变化? – wrephebiajou

+0

是的课程将会改变。如果值已被转换为字符串,则不需要在数组上循环。答案已更新。 – Star

4

利用构造的

第一件事,第一,你的addNew功能真的可以读作“创建该对象的新实例”,这是真正的__construct工作。这里的另一个问题是Telegram对象必须创建多个实例。但是,调用$tel = new Telegram();意味着只有一个实例的对象。因此,嵌套的for循环应该属于脚本页面,os与对象内部相对。

重构数据库连接

目前,数据库连接正在对象中初始化。现在,您的对象有两个职责:管理电报并与数据库通信。我会建议在电报之外创建连接对象,并尽可能保持为面向对象,将其传递给对象。 This answer很好地解释了这部分。

打破某些功能

眼下,对象的构造是做两件事情:创建对象的实例,它是它坚持到数据库中。理想情况下,你会希望这两个进程相互解耦。这允许你创建一个Telegram的实例并在它保存到数据库之前对它进行验证,让你的构造函数更加干净。

电报类:

<?php 
class Telegram 
{ 
    protected $notice = ''; 
    private $_token; 
    private $_cat; 
    private $_ads; 
    private $_key 

    public function __construct($token, $cat, $ads, $key) 
    { 
     $this->_token = $token; 
     $this->_cat = $cat; 
     $this->_ads = $ads; 
     $this->_key = $key; 
    } 

    public function saveToDb(PDO $con) 
    { 
     $new = $this->con->prepare("INSERT INTO channels (token_number, category_name, ads_set, keyboard_status) VALUES (?, ?, "/*.$ads[$i].*/", "/*.$key[$i].*/")"); 
     $new->bindParam(1,$this->_token); 
     $new->bindParam(2,$this->_cat); 
     $new->bindParam(3,$this->_ads); 
     $new->bindParam(4,$this->_key); 
     $new->execute(); 
     $this->notice['success_message'] = "New Telegram Channel was successfully added"; 
     return $this->notice; 
    } 

    public function getNotice() 
    { 
     return $this->notice; 
    } 

    public function getToken() 
    { 
     return $this->_token; 
    } 

    public function getCat() 
    { 
     return $this->_cat; 
    } 

    public function getAds() 
    { 
     return $this->_ads; 
    } 

    public function getKey() 
    { 
     return $this->_key; 
    } 
} 
?> 

表脚本:

<?php 
    if(isset($_POST['submit'])){ 
     $db = new Connection(); 
     $db = $this->db->dbConnect(); 

     $token = $_POST['token']; 
     $cat = $_POST['cat']; 
     $ads = $_POST['ads']; 
     $key = $_POST['keyboard']; 

     $notices = array(); 
     if(!empty($token)&&!empty($cat)&&!empty($ads)) 
     { 
      for ($i=0; $i < count($this->_ads); $i++) 
      { 
       for ($j=0; $j < count($this->_key);$j++) 
       { 
        $tel = new Telegram($token, $cat, $ads[$i], $key[$j]); 
        $notices[] = $tel->saveToDb($db); // keep a notice for each object created 
       } 
      } 
     } 
    } 
?> 
<div class='content-wrapper'> 
/* ... */