我正在使用PHP OOP开发CMS。在此项目中,用户可以添加新的Telegram Channel功能。对于此功能,我添加了此表单,其中还包含操作代码:
<?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;
}
}
?>
因为我想在表格中添加多个复选框,所以我在方法 Add_New 中使用了这个for循环(根据这个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;
}
}
我知道这不正确,但我不知道将这些 $ ads [$ i] 和 $ key [$ i] 变量添加到其中的正确方法插入声明......
因此,如果您知道如何以正确的顺序执行此操作,请告诉我..谢谢!
答案 0 :(得分:4)
首先,您的addNew
函数可以真正读作&#34;创建此对象的新实例&#34;,这实际上是__construct
的工作。这里的另一个问题是Telegram
对象必须创建多个实例。但是,调用$tel = new Telegram();
意味着该对象的一个且仅有一个实例。因此,嵌套的for
循环应属于脚本页面,os与对象内部相对。
目前,您的数据库连接正在对象中初始化。现在,您的对象有两个职责:管理电报并与数据库通信。我建议在Telegram之外创建连接对象,并尽可能保持面向对象,将其传递给对象。 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'>
/* ... */
答案 1 :(得分:1)
简单地使用implode并爆炸PHP的功能。在保存数据的同时,将其破坏如下:
$key = implode(", ", $_POST['keyboard']);
$tel = new Telegram();
$notice = $tel->AddNew($token,$cat,$ads,$key);
替换内爆的第一个参数&#34;,&#34;根据你的需要。
用于显示使用爆炸如下: $ id = explode(&#34;,&#34;,$ DbRes-&gt;键盘);
将数组转换为字符串后,无需在类中循环。请参阅以下更新的类代码:
<?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;
}
}
?>
这有助于您建立理解。您需要将Array转换为字符串,以便mysql数据库可以存储它。这篇文章也很有帮助:Save PHP array to MySQL?