我正在使用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]变量到插入声明...
的正确方法所以如果你知道如何按照正确的顺序来做到这一点,请让我知道..谢谢!
那么关于类,在类文件没有变化? – wrephebiajou
是的课程将会改变。如果值已被转换为字符串,则不需要在数组上循环。答案已更新。 – Star