
[英]On all pages dynamic @yield does not show anything. How do I share data between all pages?
[英]How do i Get data to show on other pages?
我对Get&Post进行了更改,但是它什么也没有做,我认为它的形式使此工作变得很困难,这是此节下面的形式,我在做错什么吗? 我不能告诉但肯定知道一件事,当我使用“名称”而不是“提交”和“执行”进行GET和POST时出现错误,提示未定义索引:“名称”,这是怎么回事,我做到了你所说的,但一无所获!
/////////////表格/////////////////////
<form method="post" action="search_govern.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Yell">
</form>
////////////表单结尾//////////////////////
<?php
//if(isset($_POST['submit'])){
//if(isset($_GET['go'])){
//if(preg_match("/[A-Z | a-z]+/", $_POST['name'])){
//$name=$_POST['name'];
////////////////////////////////////////
我认为的问题在这里,但是我不确定我在做错什么,您可以指出我的错误或给我看这个例子!
if(isset($_POST['submit'])){
if(isset($_GET['go'])){
$raw_name = $_GET['name'] ;
if(preg_match("/[A-Z | a-z]+/", $raw_name )){
$name=$raw_name;
////////////////////////////////////////
include "connect/connect.php";
//-query the database table
$sql="SELECT userId, Attribute, Name FROM government WHERE Name LIKE '%$name%' OR Attribute LIKE '%$name%' ORDER BY userid ASC";
///////////////////////// PAGINATION CLASS////////////////////////////////////
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
///////////////////////////END OF PAGINATION CLASS///////////////////////////////
$curent_page = isset($_GET['page'])&&(int)$_GET['page'] !=0 ? (int)$_GET['page'] : 1 ;
$per_page = 7 ;
$sql = "SELECT COUNT(*) FROM government ";
$result = mysql_query($sql);
$total = mysql_result($result , 0);
$pagination = new Pagination($curent_page , $per_page , $total);
///////////////////////////////////////////////////////////////////////
//echo 'perpage : '.$pagination->per_page.'<br />';
//echo 'offset : '.$pagination->offset().'<br />';
///////////////////////////////////////////////////////////////////////
$rows_to_show = "SELECT userId, Attribute, Name FROM government WHERE Name LIKE '%$name%' OR Attribute LIKE '%$name%' LIMIT {$pagination->per_page} OFFSET {$pagination->offset()}";
if($pagination->total_pages() > 1 ){
echo '<div id="pagination_div">';
if($pagination->has_next_page()){
echo '<span><a href="search_govern.php?page='.$pagination->next_page();
echo '&name='.$name;
echo'" >> </a></span>' ;
}
for($i=1 ; $i <= $pagination->total_pages() ; $i++ ){
if($i == $pagination->current_page ){
echo ' <strong>'.$i.' </strong>';
}
else {
echo '<span><a href="search_govern.php?page='.$i;
echo '&name='.$name;
echo'"> '.$i.' </a></span>';
}
}
if($pagination->has_previous_page()){
echo'<span><a href="search_govern.php?page='.$pagination->previous_page();
echo '&name='.$name;
echo'" > < </a></span>';
}
echo ' </div> ';
}
$dtotal = mysql_query($rows_to_show);
echo "$dtotal";
if ( $dtotal === FALSE ){ //////////// check to see if the query fails
die(mysql_error());
}
else{
//-create while loop and loop through result set
while($row=mysql_fetch_array($dtotal)){
$userId=$row['userId'];
$Attribute=$row['Attribute'];
$Name=$row['Name'];
//-display the result of the array
echo "<p style=\"margin:2px 0px 5px 0px;\" >";
echo "<p>" ."<a href=\"search_govern.php?id=$userId\"> " . $Name . "</a></p>";
echo "</p>";
}
}
}
else{
echo "<p> Please enter a search query</p>";
}
}
}
?>
我建议一个更简单更干净的代码
这是一个简单的分页类:
class Pagination {
public $current_page;
public $per_page;
public $total_count;
public function __construct($page=1, $per_page=20, $total_count=0){
$this->current_page = (int)$page;
$this->per_page = (int)$per_page;
$this->total_count = (int)$total_count;
}
public function offset() {
return ($this->current_page - 1) * $this->per_page;
}
public function total_pages() {
return ceil($this->total_count/$this->per_page);
}
public function previous_page() {
return $this->current_page - 1;
}
public function next_page() {
return $this->current_page + 1;
}
public function has_previous_page() {
return $this->previous_page() >= 1 ? true : false;
}
public function has_next_page() {
return $this->next_page() <= $this->total_pages() ? true : false;
}
}
将其保存为.php文件,并将其包含在您的页面中,这是使用方法:
$ pagination =新分页($ curent_page,$ per_page,$ total);
$curent_page // get it from url with GET method
e.g
$curent_page = isset($_GET['page'])&&(int)$_GET['page'] !=0 ? (int)$_GET['page'] : 1 ;
--
$per_page // you have to define how many rows do you want to show in the page
e.g
$per_page = 20 ;
--
$total // you have to count all of the rows in your table
e.g
$sql = "SELECT COUNT(*) FROM `$tbl` ";
$result = mysql_query($sql , $db_connection);
$total = mysql_result($result , 0);
now you can create a pagination obj
$pagination = new Pagination($curent_page , $per_page , $total);
--
$pagination->offset() // use this as offset in your query in each
page
$pagination->per_page // use this as LIMIT in your query
e.g
$rows_to_show = "SELECT * FROM `$tbl` LIMIT {$pagination->per_page} OFFSET {$pagination->offset()}";
最后在页面底部创建html分页
if($pagination->total_pages() > 1 ){
echo '<div id="pagination_div">';
if($pagination->has_next_page()){
echo '<span><a href="index.php?page='.$pagination->next_page();
echo'" >> </a></span>' ;
}
for($i=1 ; $i <= $pagination->total_pages() ; $i++ ){
if($i == $pagination->current_page ){
echo ' <strong>'.$i.' </strong>';
}
else {
echo '<span><a href="index.php?page='.$i;
echo'"> '.$i.' </a></span>';
}
}
if($pagination->has_previous_page()){
echo'<span><a href="index.php?page='.$pagination->previous_page();
echo'" > < </a></span>';
}
echo ' </div> ';
}
/////////////////////////////// EDIT 2 ///// ///////////////////////////////////
/// $name may be sent to the page via post/form OR via get/url so you have to be prepare for both , so this is how you get $name at the top of page
if(isset($_POST['name']))
$raw_name = $_POST['name'] ;
if(isset($_GET['name']))
$raw_name = $_GET['name'] ;
if(preg_match("/[A-Z | a-z]+/", $raw_name )){
$name=$raw_name;
///// do other stuff
////////至于分页,您必须向每个页面发送$ name
if($pagination->total_pages() > 1 ){
echo '<div id="pagination_div">';
if($pagination->has_next_page()){
echo '<span><a href="index.php?page='.$pagination->next_page();
echo '&name='.$name;
echo'" >> </a></span>' ;
}
for($i=1 ; $i <= $pagination->total_pages() ; $i++ ){
if($i == $pagination->current_page ){
echo ' <strong>'.$i.' </strong>';
}
else {
echo '<span><a href="index.php?page='.$i;
echo '&name='.$name;
echo'"> '.$i.' </a></span>';
}
}
if($pagination->has_previous_page()){
echo'<span><a href="index.php?page='.$pagination->previous_page();
echo '&name='.$name;
echo'" > < </a></span>';
}
echo ' </div> ';
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.