有人可以帮我分页吗?

时间:2011-08-12 18:34:02

标签: php

编辑>> 你的帖子对我没有帮助 我做了这个

function lgsl_query_css_all($request)
  {
    global $lgsl_config;

    lgsl_database();

     $eiluciu_psl = 2;  
            $psl_num = 1;   

            if (isset($_GET['puslapis']))
                {
                    $psl_num = $_GET['puslapis'];
                }

            $offset = ($psl_num-1)* $eiluciu_psl;
    $mysql_query  = "SELECT `type`,`ip`,`c_port`,`q_port`,`s_port` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `disabled`=0 ORDER BY `cache_time` ASC LIMIT  $offset,$eiluciu_psl";
    $mysql_result = mysql_query($mysql_query) or die(mysql_error());

    $server_list  = array();

    while ($mysql_row = mysql_fetch_array($mysql_result, MYSQL_ASSOC))
    {
      if (strpos($request, "c") === FALSE && lgsl_timer("check")) { $request .= "c"; }

      $server = lgsl_query_cached($mysql_row['type'], $mysql_row['ip'], $mysql_row['c_port'], $mysql_row['q_port'], $mysql_row['s_port'], $request);

      if ($lgsl_config['hide_offline'][0] && !$server['b']['status']) { continue; }

      $server_list[] = $server;
    }
 $query_count = "SELECT COUNT(id) AS numrows FROM lgsl";
  $result_count = mysql_query($query_count) or die (mysql_error());
  $row = mysql_fetch_array($result_count,MYSQL_ASSOC);
  $numrows = $row['numrows'];

        $max_psl = ceil($numrows/$eiluciu_psl);

        $self = $_SERVER['PHP_SELF'];
        $nav  = '';

for($puslapis = 1; $puslapis <= $max_psl; $puslapis++)
{
   if ($puslapis == $psl_num)
   {
      $nav .= " $puslapis "; 
   }
   else
   {
      $nav .= " <a href=\"$self?puslapis=$puslapis\">$puslapis</a> ";
   } 
}


    if ($psl_num > 1)
{
   $puslapis  = $psl_num - 1;
   $prev  = " <a href=\"$self?puslapis=$puslapis\" class=\"prevnext\">« <<</a> ";

   $first = " <a href=\"$self?puslapis=1\" class=\"currentpage\">1</a> ";
} 
else
{
   $prev  = '&nbsp;'; 
   $first = '&nbsp;'; 
}

if ($psl_num < $max_psl)
{
   $puslapis = $psl_num + 1;
   $next = " <a href=\"$self?puslapis=$puslapis\" class=\"prevnext\">>></a> ";

   $last = " <a href=\"$self?puslapis=$max_psl\" class=\"currentpage\">$max_psl</a> ";
} 
else
{
   $next = '&nbsp;'; 
   $last = '&nbsp;'; 
}

// 1-4 
$vienas = $psl_num + 1;
if($vienas < $max_psl)
{    
$vienas_echo = "<a href=\"$self?puslapis=$vienas\">$vienas</a>";
} else {
$vienas_echo = "&nbsp;";
}


$du = $psl_num + 2;
if($du < $max_psl)
{ 
$du_echo = "<a href=\"$self?puslapis=$du\">$du</a>";
} else {
$du_echo = "&nbsp;";
}


$trys = $psl_num + 3;
if($trys < $max_psl)
{ 
$trys_echo = "<a href=\"$self?puslapis=$trys\">$trys</a>";
} else {
$trys_echo = "&nbsp;";
}


$keturi = $psl_num + 4;
if($keturi < $max_psl)
{
$keturi_echo = "<a href=\"$self?puslapis=$keturi\">$keturi</a>";
} else {
$keturi_echo = "&nbsp;";
}
// 10-40

$desimt = $psl_num + 10;
if($desimt < $max_psl)
{
$desimt_echo = "<a href=\"$self?puslapis=$desimt\">$desimt</a>";
} else {
$desimt_echo = "&nbsp;";
}


$dvim = $psl_num + 20;
if($dvim < $max_psl)
{
$dvim_echo = "<a href=\"$self?puslapis=$dvim\">$dvim</a>";
} else {
$dvim_echo = "&nbsp;";
}

$trim = $psl_num + 30;
if($trim < $max_psl)
{
$trim_echo = "<a href=\"$self?puslapis=$trim\">$trim</a>";
} else {
$trim_echo = "&nbsp;";
}


$kiam = $psl_num + 40;
if($kiam < $max_psl)
{
$kiam_echo = "<a href=\"$self?puslapis=$kiam\">$kiam</a>";
} else {
$kiam_echo = "&nbsp;";
}

$be = $puslapis -1;
$dabar = "<a href=\"$self?puslapis=$be\">" . $be . "</a>";


echo "<div class=\"pagination\" align=\"center\"><ul><li>" . $prev . 
"</li><li>" . $first . "</li><li>$dabar</li><li>  </li><li>" . $vienas_echo . "</li><li>" . $du_echo . 
"</li><li>" . $trys_echo . "</li><li>" . $keturi_echo . "</li><li>   <strong>...</strong>   </li><li>" . $desimt_echo . 
"</li><li>" . $dvim_echo . "</li><li>" . $trim_echo . 
"</li><li>" . $kiam_echo . "</li><li>" . $last . "</li><li>" . $next . "</li></ul></div>";
    return $server_list;
  }

  $output .= "
  <div style='text-align:center; font-size:10px; font-face:arial'>

<table id='myTable' cellpadding='3' cellspacing='1' style='margin:auto' class='boxes' >
     <thead>
        <tr class='lgsl'>
                <th class='lgsl' style='width:20px;' title='Server Status'>On/Off</th>
                <th class='lgsl' style='width:25px;' title='Game'>Gra</th>
                <th class='lgsl' style='width:150px;' title='IP address and Port'>IP Serwera</th>
                <th class='lgsl' style='width:200px;' title='Server Name'>Nazwa Serwera</th>
                <th class='lgsl' style='width:85px;' title='Current Map'>Mapa</th>
                <th class='lgsl' style='width:60px;' title='Players online'>Graczy</th>
                <th class='lgsl' style='width:100px;' title='User Rating'>Szczegoly</th>
        </tr>
     </thead>";
    foreach ($server_list as $server)
    {
      $misc   = lgsl_server_misc($server);
      $server = lgsl_server_html($server);

      $output .= "

   <tr style='".lgsl_bg().";table-layout:fixed'>

    <td>
          <img alt='' src='{$misc['icon_status']}' title='{$misc['text_status']}' />
        </td>

        <td>
          <img alt='' src='{$misc['icon_game']}' title='{$misc['text_type_game']}' />
        </td>
        <td  title='{$lgsl_config['text']['slk']}' style='text-align:center'>

<a href='steam://{$misc['software_link']}'  class='Tips1' title=' Connect {$server['b']['ip']}:{$server['b']['c_port']}'>

            {$server['b']['ip']}:{$server['b']['c_port']}
        </a> 
        </td>

        <td title='{$server['s']['name']}' style='text-align:center'>
          <div style='width:100%; overflow:hidden; height:1.3em'>
          {$misc['name_filtered']}
          </div>
        </td>

        <td style='white-space:nowrap; text-align:center'>
          {$server['s']['map']}
        </td>

        <td style='white-space:nowrap; text-align:center'>
          {$server['s']['players']} / {$server['s']['playersmax']}
        </td>

        <td style='white-space:nowrap; text-align:center'>
          <a href='".lgsl_link($server['o']['id'])."'>
            <img alt='' src='{$misc['icon_details']}' style='border:none' title='{$lgsl_config['text']['vsd']}' />
          </a>
        </td>

      </tr>";
    }
    $output .= "
    </table>
  </div>";

但它看起来很难看,所以我觉得有人有更好的解决方案

http://img851.imageshack.us/img851/820/unledoxy.png 我需要以某种方式将它放在服务器列表

2 个答案:

答案 0 :(得分:2)

制作分页的最简单方法是计算总行数,然后为其添加一个LIMIT。

计算行数:

SELECT COUNT(*) WHERE `disabled`=0 ORDER BY `cache_time` ASC;

然后添加你的限制:

SELECT `type`,`ip`,`c_port`,`q_port`,`s_port` FROM `{$lgsl_config['db']['prefix']}{$lgsl_config['db']['table']}` WHERE `disabled`=0 ORDER BY `cache_time` ASC LIMIT $start, $perpage;

$perpage=20;或您想要的任何号码$start=(int)$_GET['page']*$perpage;

只需在底部为您要显示的每个页面添加链接:

for($a=0;$a<$rows;$a++){
    echo"<a href='table.php?page=$a'>$a</a>";
}

无论如何,这都是让你开始的要点。

答案 1 :(得分:0)

要对mysql查询进行分页,请添加限制子句

LIMIT $begin, $end

其中:

$begin = ($page_num-1)*$NUM_PER_PAGE;
$end = $begin + $NUM_PER_PAGE;