我从数据库中提取信息并将其显示在我的页面上。我想通过单击链接按字母顺序对其中一个数据库表中的数据进行排序。我能够使用纯PHP实现这一点,但由于MVC结构,无法使用CodeIgniter。使用纯PHP代码就像:
<?php
if(isset($_GET['let']))
$let = $_GET['let'];
else
$let = '';
$query = "SELECT supplier, contact, telephone, email FROM suppliers WHERE supplier LIKE '$let%'";
// other codes
使用代码单独的sort.php:
<a href="sort.php"> All </a> |
<a href="sort.php?let=A"> A </a> |
<a href="sort.php?let=B"> B </a>
这就是我的页面的样子:
这是我的模型通过使用名为getSort()
的函数看起来的样子class Supplier_model extends CI_Model
{
public function __construct()
{
/* Call the Model constructor */
parent::__construct();
}
function getCity()
{
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
function getPaymentMode()
{
$query = $this->db->query('SELECT id,paymentmode FROM paymentmodes');
return $query->result_array();
}
function getBank()
{
$query = $this->db->query('SELECT id,bankname FROM banks');
return $query->result_array();
}
function getCategories()
{
$query = $this->db->query('SELECT id,supplycategory FROM supplycategories');
return $query->result_array();
}
function getStaff()
{
$query = $this->db->query('SELECT firstname,lastname FROM employees');
return $query->result_array();
}
function getBankBranch($bank_id)
{
$query = $this->db->query('SELECT id,bankbranch FROM bankbranches WHERE bank='.$bank_id);
return $query->result_array();
}
function getPosts()
{
$this->db->select("supplier,contact,telephone,email");
$this->db->from('suppliers');
$this->db->order_by("supplier", "asc");
$query = $this->db->get();
return $query->result();
}
function getSort()
{
$query = $this->db
->select('supplier,contact,telephone,email')
->from('suppliers')
->where('supplier')
->like('supplier', 'let', 'after')
->get();
return $query->result();
}
function addSupplier()
{
$this->load->database();
$supplier = $this->input->post('supplier');
$taxpin = $this->input->post('taxpin');
$contact = $this->input->post('contact');
$addresss = $this->input->post('addresss');
$citys = $this->input->post('citys');
$telephone = $this->input->post('telephone');
$email = $this->input->post('email');
$website = $this->input->post('website');
$paymentmodes = $this->input->post('paymentmodes');
$kesaccount = $this->input->post('kesaccount');
$usdaccount = $this->input->post('usdaccount');
$banks = $this->input->post('banks');
$bankbranches = $this->input->post('bankbranches');
$bankcode = $this->input->post('bankcode');
$swiftcode = $this->input->post('swiftcode');
$mobilepaymentnumber = $this->input->post('mobilepaymentnumber');
$mobilepaymentname = $this->input->post('mobilepaymentname');
$chequeddressee = $this->input->post('chequeddressee');
$status = $this->input->post('status');
$categorysuppliers = $this->input->post('categorysuppliers');
$staff = $this->input->post('staff');
$data = array(
'supplier' => $supplier, 'taxpin' => $taxpin, 'contact' => $contact, 'addresss' => $addresss,
'citys' => $citys, 'telephone' => $telephone, 'email' => $email, 'website' => $website,
'paymentmodes' => $paymentmodes, 'kesaccount' => $kesaccount, 'usdaccount' => $usdaccount,
'banks' => $banks, 'bankbranches' => $bankbranches, 'bankcode' => $bankcode,
'swiftcode' => $swiftcode, 'mobilepaymentnumber' => $mobilepaymentnumber,
'chequeddressee' => $chequeddressee, 'status' => $status,
'categorysuppliers' => $categorysuppliers, 'staff' => $staff);
$this->db->insert('suppliers', $data);
}
}
这是我的控制器使用一个名为sort_suppliers的函数的样子:
<?php
class Supplier extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->helper('url', 'form');
$this->load->model('supplier_model');
$this->load->database();
}
public function index()
{
$this->load->helper('form', 'html');
$data['cities'] = $this->supplier_model->getCity();
$data['modes'] = $this->supplier_model->getPaymentMode();
$data['banks'] = $this->supplier_model->getBank();
$data['categories'] = $this->supplier_model->getCategories();
$data['staffs'] = $this->supplier_model->getStaff();
$this->load->view('supplier_add', $data);
}
public function save()
{
$this->load->model('supplier_model');
if($this->input->post('submit'))
{
$this->supplier_model->addSupplier();
}
redirect('supplier/view_suppliers');
}
public function view_suppliers()
{
$this->load->helper('form');
$this->data['posts'] = $this->supplier_model->getPosts();
$this->load->view('suppliers_view', $this->data);
}
public function sort_suppliers()
{
$this->load->helper('form');
$this->data['sorts'] = $this->supplier_model->getSort();
$this->load->view('suppliers_view', $this->data);
}
public function get_branches()
{
$bank_id = $this->input->post('bank_id');
$bankbranch = $this->supplier_model->getBankBranch($bank_id);
foreach($bankbranch as $branch)
{
echo '<option value="'.$branch['id'].'">'.$branch['bankbranch'].'</option>';
}
}
}
我对我的视图应该是什么样子感到困惑,因为我甚至不确定我的模型和控制器是否正确。我希望当用户点击href链接ALL以从数据库获取所有列表时,当用户点击任何字母时,数据库中的列表应仅显示基于用户点击的字母的排序列表。这是我的混合观点:
<!DOCTYPE html>
<html>
<head>
<title>Supplier</title>
<style>
table {
border-collapse: separate;
border-spacing: 0;
}
th,
td {
padding: 10px 15px;
}
tbody tr:nth-child(even) {
background: #f0f0f2;
}a:link {
text-decoration: none;
}
</style>
</head>
<body>
<strong>Suppliers</strong><br><br>
<a href="sort.php?let=Y"> Active </a> |
<a href="sort.php?let=Y"> Disabled </a> <br><br>
<a href="<?php echo site_url('supplier/sort_suppliers') ?>">All</a> |
<a href="sort.php?let=A"> A </a> |
<a href="sort.php?let=B"> B </a> |
<a href="sort.php?let=C"> C </a> |
<a href="sort.php?let=D"> D </a> |
<a href="sort.php?let=E"> E </a> |
<a href="sort.php?let=F"> F </a> |
<a href="sort.php?let=G"> G </a> |
<a href="sort.php?let=H"> H </a> |
<a href="sort.php?let=I"> I </a> |
<a href="sort.php?let=J"> J </a> |
<a href="sort.php?let=K"> K </a> |
<a href="sort.php?let=L"> L </a> |
<a href="sort.php?let=M"> M </a> |
<a href="sort.php?let=N"> N </a> |
<a href="sort.php?let=N"> O </a> |
<a href="sort.php?let=P"> P </a> |
<a href="sort.php?let=Q"> Q </a> |
<a href="sort.php?let=R"> R </a> |
<a href="sort.php?let=S"> S </a> |
<a href="sort.php?let=T"> T </a> |
<a href="sort.php?let=U"> U </a> |
<a href="sort.php?let=V"> V </a> |
<a href="sort.php?let=W"> W </a> |
<a href="sort.php?let=X"> X </a> |
<a href="sort.php?let=Y"> Y </a> |
<a href="sort.php?let=Z"> Z </a> |<br><br>
<hr>
<table>
<thead>
<tr>
<th align='left'>Supplier</th>
<th align='left'>Contact</th>
<th align='left'>Telephone</th>
<th align='left'>Email</th>
<th align='left'>LPO</th>
<th align='left'>Invoice</th>
<th align='left'>Profile</th>
</tr>
</thead>
<?php foreach($posts as $post)
{ ?>
<tr>
<td><?php echo $post->supplier; ?></td>
<td><?php echo $post->contact; ?></td>
<td><?php echo $post->telephone; ?></td>
<td><?php echo $post->email; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>
答案 0 :(得分:1)
你的模型和控制器都有正确的想法,虽然它们可能更多一点。这个答案显示了一种可能的方法。
在整个答案中,问题中显示的许多功能都不包括在内,因为它们不会影响答案。包含一些例外,因为我想评论您的代码。 (对不起,不能帮助自己。)
首先是控制器。
<强> Suppliers.php 强>
class Suppliers extends CI_Controller
{
function __construct()
{
parent::__construct();
$this->load->database();
$this->load->helper('url', 'form');
$this->load->model('supplier_model');
}
您倾向于多次装载物品。 CodeIgniter类(帮助程序,库,模型)主要是“singletons”。因此,如果它们已经加载,则任何再次加载它们的尝试都会返回对先前加载的实例的引用,并且不会创建新实例。
下一个方法的第一行是否已加载已加载的帮助程序。
public function index()
{
//$this->load->helper('form', 'html'); 'form' helper already loaded in constructor
$this->load->helper('html');
$data['cities'] = $this->supplier_model->getCity();
$data['modes'] = $this->supplier_model->getPaymentMode();
$data['banks'] = $this->supplier_model->getBank();
$data['categories'] = $this->supplier_model->getCategories();
$data['staffs'] = $this->supplier_model->getStaff();
$this->load->view('supplier_add', $data);
}
Suppliers
类中的下一个功能将view_suppliers()
和sort_suppliers()
功能合并为一个。为函数的参数赋予默认值允许在不传递值的情况下调用它。
public function view($filter = "All")
{
if($filter === "All")
{
$filter = NULL;
}
$data['filter'] = $filter;
$data['suppliers'] = $this->supplier_model->get_suppliers($filter);
$this->load->view('suppliers_view', $data);
}
}
使用程序方法创建像这样的链接
<a href="sort.php?let=A"> A </a>
不适用于Codeigniter的面向对象世界。
您不能只链接到文件,您必须链接到控制器,可能还有该控制器的功能。 CodeIgniter使用基于段的URL方法。 (Documentation)
example.com/controller/function/parameter
将浏览器定向到example.com/suppliers
会调用suppliers->index()
和example.com/suppliers/view
来电suppliers->view()
。
您可以使用锚标记中URI的“param”段将参数传递给suppliers->view
,例如<a href='example.com/suppliers/view/K'>
会调用suppliers->view("K")
。
<强> Supplier_model.php 强>
class Supplier_model extends CI_Model
{
public function __construct()
{
parent::__construct();
$this->load->database();
}
function getCity()
{
$query = $this->db->query('SELECT cityidd,city FROM citys');
return $query->result_array();
}
您永远不会检查您是否确实从查询中获取了有时可能会咬你的数据。但是如果你不打算检查你是否有任何行,你真的不需要$query
。该功能可以简化为此。
function getCity()
{
return $this->db->query('SELECT cityidd,city FROM citys')->result_array();
}
获取供应商列表可简化为一个功能,该功能将处理已排序(a.k.a. filtered)和“All”记录。
(过滤和排序数据库结果是我心目中的两个不同但相关的琐事。我不得不将搜索变量命名为$filter
,因为我一直对我正在处理的工作感到困惑。)
此代码说明了查询生成器的一个很酷的事情 - 您不必按照有效SQL语句所需的顺序运行各种函数。查询生成器在调用get()
之前不会实际“构建”该语句,因此在like
或任何其他查询生成器方法之前设置select
短语没有问题。
public function get_suppliers($filter)
{
if(isset($filter))
{
// $filter is not NULL
$this->db->like('supplier', $filter, 'after');
// Uncomment the next line of code and remove the other call
// to order_by if you only want filtered results sorted
//$this->db->order_by("supplier", "ASC");
}
$query = $this->db
->select("supplier,contact,telephone,email")
->from('suppliers')
// Remove (or comment) the next line of code
// if you do not want the suppliers in alphabetical order.
->order_by("supplier", "ASC")
->get();
return $query->num_rows() > 0 ? $query->result() : NULL;
}
如果您不熟悉PHP的三元运算符这一行
return $query->num_rows() > 0 ? $query->result() : NULL;
与if / else块完全相同。
if($query->num_rows() > 0)
{
return $query->result();
}
else
{
return NULL;
}
您更喜欢打字?
(如果你知道三元运算符是什么我为迂腐而道歉。)
包括下一个类功能,因为我再次迂腐。
将发布的值分配给变量然后使用这些变量来设置数组中的值是没有意义的。在填充$this->input->post
数组时使用$data
可以节省大量的输入和处理能力。
function addSupplier()
{
//$this->load->database(); do this in the constructor (or in autoload.php)
$data = array(
'supplier' => $this->input->post('supplier'),
'taxpin' => $this->input->post('taxpin'),
'contact' => $this->input->post('contact'),
'addresss' => $this->input->post('addresss'),
'citys' => $this->input->post('citys'),
'telephone' => $this->input->post('telephone'),
'email' => $this->input->post('email'),
'website' => $this->input->post('website'),
'paymentmodes' => $this->input->post('paymentmodes'),
'kesaccount' => $this->input->post('kesaccount'),
'usdaccount' => $this->input->post('usdaccount'),
'banks' => $this->input->post('banks'),
'bankbranches' => $this->input->post('bankbranches'),
'bankcode' => $this->input->post('bankcode'),
'swiftcode' => $this->input->post('swiftcode'),
'mobilepaymentnumber' => $this->input->post('mobilepaymentnumber'),
'mobilepaymentname' => $this->input->post('mobilepaymentname'),
'chequeddressee' => $this->input->post('chequeddressee'),
'status' => $this->input->post('status'),
'categorysuppliers' => $this->input->post('categorysuppliers'),
'staff' => $this->input->post('staff')
);
$this->db->insert('suppliers', $data);
}
}
以下是视图文件的<body>
部分,修改后使用控制器中传递的变量。 foreach
循环可以轻松构建过滤器链接。 anchor
函数HERE上的文档。
<强> suppliers_view.php 强>
<body>
<strong>Suppliers</strong><br><br>
<strong>Suppliers</strong><br><br>
<!-- These next two lines would cause the list to be suppliers that start with "Y". What do you really want? These links wouldn't work anyway. -->
<!-- <a href="sort.php?let=Y"> Active </a> |-->
<!-- <a href="sort.php?let=Y"> Disabled </a> <br><br>-->
<?php
$filter_values = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'];
echo anchor("suppliers/view", "All");
//make <a> tags for "A" through "Z"
foreach($filter_values as $value)
{
echo anchor("suppliers/view/{$value}", " | $value");
}
?>
<br><br>
<hr>
<table>
<thead>
<tr>
<th align='left'>Supplier</th>
<th align='left'>Contact</th>
<th align='left'>Telephone</th>
<th align='left'>Email</th>
<th align='left'>LPO</th>
<th align='left'>Invoice</th>
<th align='left'>Profile</th>
</tr>
</thead>
<?php
if(isset($suppliers)):
foreach($suppliers as $supplier):
?>
<tr>
<td><?= $supplier->supplier; ?></td>
<td><?= $supplier->contact; ?></td>
<td><?= $supplier->telephone; ?></td>
<td><?= $supplier->email; ?></td>
</tr>
<?php
endforeach;
else:
$msg = isset($filter) ? "No supplier names starting with the letter $filter." : "No Suppliers found.";
echo "<td>$msg</td>";
endif;
?>
</table>
</body>
很抱歉这个冗长的回答。我希望它有所帮助。