2011-12-02 23 views
14
$objConnect = mysql_connect("localhost","root","") or die(mysql_error()); 
$objDB = mysql_select_db("Test"); 

$strSQL = "SELECT * FROM UserAddedRecord WHERE (Name LIKE '%".$getname."%' and State LIKE '%".$getstate."%' and Cell LIKE '%".$getcell."%' and Custgroup LIKE '%".$getgroup."%') AND user_id=$id"; 

$objQuery = mysql_query($strSQL) or die ("Error Query [".$strSQL."]"); 
$Num_Rows = mysql_num_rows($objQuery); 

$Per_Page = 5; 

if (!isset($_GET['Page'])) { 
    $Page = 1; 
} else { 
    $Page = $_GET['Page']; 
} 

$Prev_Page = $Page - 1; 
$Next_Page = $Page + 1; 

$Page_Start = (($Per_Page * $Page) - $Per_Page); 
if ($Num_Rows <= $Per_Page) { 
    $Num_Pages = 1; 
} elseif (($Num_Rows % $Per_Page) == 0) { 
    $Num_Pages = ($Num_Rows/$Per_Page) ; 
} else { 
    $Num_Pages = ($Num_Rows/$Per_Page) + 1; 
    $Num_Pages = (int) $Num_Pages; 
} 


$strSQL .=" order by addedrec_ID DESC LIMIT $Page_Start , $Per_Page"; 
$objQuery = mysql_query($strSQL) or trigger_error(mysql_error());; 

if ($Prev_Page) { 
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Prev_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'><< Back</a> "; 
} 

for ($i=1; $i <= $Num_Pages; $i++) { 
    if ($i != $Page) { 
     echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$i&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>$i</a> "; 
    } else { 
     echo "<b> $i </b>"; 
    } 
} 

if ($Page!=$Num_Pages) { 
    echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=$Next_Page&txtName=$getname&txtState=$getstate&txtCell=$getcell&txtGroup=$getgroup'>Next>></a> ";   
} 

mysql_close($objConnect); 

इस कोड मैं डेटा के लिए पृष्ठांकन पेज बनाने के लिए उपयोग है search.But मैं सिर्फ देखा है कि मैं भी कई रिकॉर्ड तो इसे सीमित करने की कोशिश कर भी कई पेज numbers.I'm होगा पृष्ठ संख्या दिखाया गया है, और कई उदाहरण ..it यह करने के लिए कई तरीके हैं लेकिन मैं अभी भी विचार कैसे मेरे विधि के लिए सीमित करने के लिए नहीं है के लिए देखो ...सीमा पृष्ठांकन पृष्ठ संख्या

उत्तर

28

ठीक है अगर तुम करने के लिए एक समारोह है कि संसाधित कर सकते हैं बनाने है

Prev 1 2 3 4 5 6 .. 40 41 Next 
Prev 1 2 .. 6 7 8 9 10 .. 40 41 Next 

पहली बात की तरह कुछ हम की जरूरत है दिखाने का मतलब हमें अंकन को आउटपुट करने की क्या ज़रूरत है। एक समारोह है जिसका मैं उपयोग करता हूं और यह अच्छी तरह से काम करता है।

function get_paging_info($tot_rows,$pp,$curr_page) 
{ 
    $pages = ceil($tot_rows/$pp); // calc pages 

    $data = array(); // start out array 
    $data['si']  = ($curr_page * $pp) - $pp; // what row to start at 
    $data['pages']  = $pages;     // add the pages 
    $data['curr_page'] = $curr_page;    // Whats the current page 

    return $data; //return the paging data 

} 

अब यह कार्य बहुत ठोस है और मेरे लिए बहुत अच्छा काम करता है।

तो आप इस समारोह पारित

  • $ tot_rows = गिना क्वेरी
  • $ पीपी = प्रति पृष्ठ वस्तुओं
  • $ curr_page = वर्तमान पृष्ठ संख्या

ठीक है, अब के लिए पंक्तियों आपके पास आवश्यक डेटा है, आपको इसे प्रदर्शित करने की आवश्यकता होगी।

यहाँ मैं क्या का उपयोग करें और कृपया इसे पढ़ा से पहले आपको लगता है, 'आह, यह बहुत लंबा है'। यह वास्तव में बहुत आसान है।

यहाँ का एक स्नैपशॉट क्या यह वापस आ जाएगी

enter image description here

<!-- Create the query --> 
    <?php $count = mysql_fetch_assoc(mysql_query ("SELECT COUNT(rows) as count FROM table")) ; 

    <?php $count = $count[0]['count']; ?> 

<!-- Call our function from above --> 
<?php $paging_info = get_paging_info($count,5,34); ?> 


<p> 
    <!-- If the current page is more than 1, show the First and Previous links --> 
    <?php if($paging_info['curr_page'] > 1) : ?> 
     <a href='' title='Page 1'>First</a> 
     <a href='' title='Page <?php echo ($paging_info['curr_page'] - 1); ?>'>Prev</a> 
    <?php endif; ?> 



    <?php 
     //setup starting point 

     //$max is equal to number of links shown 
     $max = 7; 
     if($paging_info['curr_page'] < $max) 
      $sp = 1; 
     elseif($paging_info['curr_page'] >= ($paging_info['pages'] - floor($max/2))) 
      $sp = $paging_info['pages'] - $max + 1; 
     elseif($paging_info['curr_page'] >= $max) 
      $sp = $paging_info['curr_page'] - floor($max/2); 
    ?> 

    <!-- If the current page >= $max then show link to 1st page --> 
    <?php if($paging_info['curr_page'] >= $max) : ?> 

     <a href='' title='Page 1'>1</a> 
     .. 

    <?php endif; ?> 

    <!-- Loop though max number of pages shown and show links either side equal to $max/2 --> 
    <?php for($i = $sp; $i <= ($sp + $max -1);$i++) : ?> 

     <?php 
      if($i > $paging_info['pages']) 
       continue; 
     ?> 

     <?php if($paging_info['curr_page'] == $i) : ?> 

      <span class='bold'><?php echo $i; ?></span> 

     <?php else : ?> 

      <a href='' title='Page <?php echo $i; ?>'><?php echo $i; ?></a> 

     <?php endif; ?> 

    <?php endfor; ?> 


    <!-- If the current page is less than say the last page minus $max pages divided by 2--> 
    <?php if($paging_info['curr_page'] < ($paging_info['pages'] - floor($max/2))) : ?> 

     .. 
     <a href='' title='Page <?php echo $paging_info['pages']; ?>'><?php echo $paging_info['pages']; ?></a> 

    <?php endif; ?> 

    <!-- Show last two pages if we're not near them --> 
    <?php if($paging_info['curr_page'] < $paging_info['pages']) : ?> 

     <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.($paging_info['curr_page'] + 1); ?>' title='Page <?php echo ($paging_info['curr_page'] + 1); ?>'>Next</a> 

     <a href='<?php echo str_replace('/page'.$paging_info['curr_page'], '', $paging_info['curr_url']) . '/page'.$paging_info['pages']; ?>' title='Page <?php echo $paging_info['pages']; ?>'>Last</a> 

    <?php endif; ?> 
</p> 
+0

आपके उदाहरण और सभी निर्देशों के लिए बहुत बहुत धन्यवाद, अब मैं इसे समझने और सीखने की कोशिश कर रहा हूं। –

+0

मदद करने के लिए खुशी हुई। जाहिर है, मैंने इसे अपने कोड से अभी पकड़ लिया है और इसे थोड़ा बदल दिया है, लेकिन आपके साथ खेलने के लिए संरचना वहां है। –

+1

इस प्रकार के पृष्ठ नेविगेशन के साथ समस्या यह है कि '...' श्रेणी के बीच में पृष्ठों को प्राप्त करना मुश्किल हो सकता है (कम से कम क्लिक करके)। आप [इस प्रश्न] (http://stackoverflow.com/q/7835752/999120) में वर्णित दृष्टिकोण का उपयोग करने पर विचार कर सकते हैं, जो कुछ माउसक्लिक में किसी भी पृष्ठ तक आसानी से पहुंचने की अनुमति देता है, भले ही पृष्ठों के 1000s हों। – Doin

2

आप में एक LIMIT खंड का उपयोग करने की आवश्यकता होगी आपकी एसक्यूएल कथन।

SELECT <column> 
    FROM <table> 
    LIMIT 0, 5 

संदर्भ:: SELECT Syntax

+0

आपके उत्तर के लिए धन्यवाद। क्या यह यह पंक्ति है? '$ StrSQL। =" Addrecc_ID द्वारा आदेश डीईएससी LIMIT $ Page_Start, $ Per_Page "; ' –

+0

यह http://php.about.com/od/mysqlcommands/g/Limit_sql.htm – Cyclonecode

2

मैं एक बहुत ही इसी तरह की स्क्रिप्ट के साथ किसी और के लिए एक बहुत ही इसी तरह की समस्या हल हो जाती: How to limit pages shown in pagination script

इसके अलावा, मैं अपनी स्क्रिप्ट वाली कुछ अन्य विसंगतियों देखा (उदाहरण के लिए अपरिवर्तनीय स्वरूपण के अलावा मैंने तय किया)। आप $_SERVER['SCRIPT_NAME'] करने के लिए $_SERVER[SCRIPT_NAME] की सभी घटनाओं बदलना चाहिए। अपनी स्क्रिप्ट में, उदाहरण के लिए:

echo " <a href ='$_SERVER[SCRIPT_NAME]?Page=..."; 

बन गयी:

echo " <a href ='{$_SERVER['SCRIPT_NAME']}?Page=..."; 
+0

अपने लिंक और सुधार के लिए धन्यवाद, मैं इसे आजमाउंगा। –

2

मेरी कोड हमेशा कुल पृष्ठों के मामले में कम से कम 9 पृष्ठों को दिखा> 9 जो कुछ भी आप पेज 1,2,3

पर हैं
<ul class="pagination pagination-sm" style="align:center"> 
    <li><a href="./?page=1"><</span></a></li> 
    <?php 
    if ($current_page <4){ 
     if ($total_pages<9) 
     { 
      for($i = 1; $i <= $total_pages; $i++) 
      { 

       if ($i == $current_page) 
       { 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 
     else 
     { 
      for($i = 1; $i <= 9; $i++) 
      { 

       if ($i == $current_page) 
       { 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 
    } 
    elseif ($current_page >$total_pages-4) 
    { 
     if ($total_pages<9) 
     { 
      for($i = 1; $i <= $total_pages; $i++){ 

       if ($i == $current_page){ 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     }else{ 
      for($i = $total_pages-8; $i <= $total_pages; $i++){ 

       if ($i == $current_page){ 
        echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
       } else { 
        echo ' <li><a href="./?page='.$i.'">'; echo $i.'</a></li>'."\n"; 
       } 
      } 
     } 

    }else{ 
     for($i = max(1, $current_page - 4); $i <= min($current_page + 4, $total_pages); $i++){ 

      if ($i == $current_page){ 
       echo '<li class="active"><a href="#">'; echo $i.'</a></li>'."\n"; 
      } else { 
       echo ' <li><a href="./?trang='.$i.'">'; echo $i.'</a></li>'."\n"; 
      } 
     } 
    } 
    ?> 
    <li> 
     <a href="./?trang=<?php echo $total_pages ?>">></a> 
    </li> 
</ul> 
1

इस कोड का उद्देश्य केवल पृष्ठांकन में निश्चित (कुछ) पृष्ठों की संख्या को दिखाने के लिए है

यहाँ url से वर्तमान पृष्ठ नंबर प्राप्त करें और mysql से कुल पृष्ठों की $curr_pg_numb

प्राप्त संख्या को परिभाषित करने और $total_pages

को परिभाषित करने और पृष्ठांकन के लिए कोड है

$counter=0; 
$init_num_to_left = 3;//initial number of pages to show to left from current page 
$init_num_to_right = 4; 

if($curr_pg_numb - $init_num_to_left < 1){ 
$init_num_to_right = $init_num_to_right + (1 - ($curr_pg_numb - $init_num_to_left)); 
$init_num_to_left = $curr_pg_numb - 1; 
} 
else if($curr_pg_numb + $init_num_to_right > $total_pages){ 
$init_num_to_left = $init_num_to_left + (($curr_pg_numb + $init_num_to_right) - $total_pages); 
$init_num_to_right = ($curr_pg_numb + $init_num_to_right) - $total_pages; 
} 

while($counter <= $total_pages) { 
    if ($counter > 0){ 
    if ($curr_pg_numb==$counter){ 
    echo '<strong> '. (htmlspecialchars($curr_pg_numb , ENT_QUOTES, "UTF-8") + 0). '</strong> '; 
    } 
    else { 
    if ($counter >= ($curr_pg_numb - $init_num_to_left) and $counter <= ($curr_pg_numb + $init_num_to_right)) { 
    echo '<a href="?cpn='. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '" style="margin:5px;">'. htmlspecialchars(($counter), ENT_QUOTES, "UTF-8"). '</a>'; 
    } 
    }//else { 
    }//if ($counter > 0){ 
$counter++; 
}//while($counter <= $total_pages) { 
3

$ count_pages = प्लस्तर लगाना ($ कुल/$ items_per_page);

2

यह आसान बनाने के लिए, यहाँ कोड है:

<?php  
    //put this code on the begining of the page 
    $page=$_GET['page']; //Getting page number from url | example: www.mydomain.com/index.php?page=3 
    if(!is_numeric($page)) $page=1; //If there is no page number specified we set number 1 
$queryCount = "SELECT * FROM table "; //select from db 
$query = $DB->query($queryCount); 
    $num = mysql_num_rows ($query); //count num rows 
    $per_page=5; //default 5 results per page 
$start=$per_page*($page-1); //start for select on next page (page2, 3, 4,5) 
$end=min($num,$page*$per_page); //end 


//here is select for your results. Be careful for LIMIT in the select! 
$query = "SELECT * FROM page LIMIT $start, $per_page "; 


//page bottom, where you want to put your numbers 
$pages=ceil($num/$per_page); 
    for($s=1; $s<=$pages; $s++) 
    { 
    if($s==$page) 
     $numPage .= "[$s] "; 
    else 
     $numPage .= "<a href='index.php?page=$s'>$s</a> "; 
    } 

echo $numPage; 

>

मैं इसे परीक्षण नहीं किया था, लेकिन मुझे लगता है कि यह काम करना चाहिए :-)

Cind संबंध है, इवान

?
+0

कोड काम करता है लेकिन यह बस सभी पेज नंबरों को प्रिंट करता है। प्रश्न पृष्ठ संख्याओं को सीमित करने के लिए उदाहरण के लिए केवल 10 पृष्ठ संख्याएं हैं। यदि उपयोगकर्ता पृष्ठ 12 पर है तो इसे उदाहरण के लिए 10 से 20 आदि दिखाया जाना चाहिए। –

संबंधित मुद्दे