2012-04-21 5 views
0

나는 & 포스트를 변경했지만 아무 것도주지 못했습니다.이 부분을 여기서 어렵게 만드는 형태라고 생각합니다. 잘못된 것입니까? 나는 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 '&nbsp;<strong>'.$i.'&nbsp;</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>&nbsp;&nbsp;&nbsp;Please enter a search query</p>"; 
} 
} 
} 

>

+0

$ pn = 2에 $ limit를 반향하면 어떻게됩니까? –

+0

무슨 소리 야? – JACrypto

+0

당신이 $ 한계 VAR를 선언 한 후 "에코 $ 제한"문을 넣습니다. 그런 다음 두 번째 페이지 ($ pn = 2)로 갈 때 그 울림을 확인하십시오. –

답변

0

나는 간단하고 깨끗한 코드 여기

간단한 매김 클래스입니다 제안 :?를 .PHP 파일에 저장

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; 
     } 
} 

및이를 포함 귀하의 페이지에 여기 사용 방법은 다음과 같습니다 :

$ 페이지 매김 = 새로운 쪽 매김 ($ 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 '&nbsp;<strong>'.$i.'&nbsp;</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> '; 

      } 

/////////////////////////////////////////// 편집 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 '&nbsp;<strong>'.$i.'&nbsp;</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> '; 

      } 
+0

괜찮습니다. 변경 사항을 적용했고 한 가지 얇은 것이주의를 끌었습니다.이 코드를 마치 $ 페이지 매기기 -> offset(); $ 페이지 매김 -> $ per_page; – JACrypto

+0

예. 당신은 코드를 편집하고 새로운 방법으로 게시 할 수 있습니다. 만약 당신이 틀린 것을 할 수 있다고 말할 수 있습니다. – max

+0

체크 아웃! 당신이 제안한 코드 바로 아래에서 내 페이지를 변경하십시오! 감사합니다. 도움을 주셔서 감사합니다. – JACrypto

관련 문제