За последние 24 часа нас посетили 16905 программистов и 1704 робота. Сейчас ищут 892 программиста ...

Помогите со страницами.

Тема в разделе "Прочие вопросы по PHP", создана пользователем Gektor, 23 фев 2009.

  1. Gektor

    Gektor Активный пользователь

    С нами с:
    18 фев 2009
    Сообщения:
    3
    Симпатии:
    0
    Помогите пожалуйста разобраться в системе вывода информации из таблиц и разбиванием ее на страницы. Вот написал код, но почему то не переходит на вторую и следующие страницы. Можете исправить ошибку и обьяснить в чем?
    PHP:
    1.  
    2. <?php
    3.  
    4. function pages($page, $pages_count)
    5. {
    6. for ($j = 1; $j <= $pages_count; $j++)
    7. {
    8. // Вывод ссылки
    9. if ($j == $page) {
    10. echo ' <a style="color: #808000;" ><b>'.$j.'</b></a> ';
    11. } else {
    12. echo ' <a style="color: #808000;" href=index.php?cwar=news&page='.$j.'>'.$j.'</a> ';
    13. }
    14. // Выводим разделитель после ссылки, кроме последней
    15. // например, вставить "|" между ссылками
    16. if ($j != $pages_count) echo ' ';
    17. }
    18. return true;
    19. }
    20.  
    21.  
    22. function news () {
    23. $perpage = 10; // Количество отображаемых данных из БД
    24.  
    25. if (empty($_get['page']) || ($_get['page'] <= 0)) {
    26. $page = 1;
    27. } else {
    28. $page = (int) $_get['page']; // Считывание текущей страницы
    29. }
    30.  
    31. $count = mysql_numrows(mysql_query('select * from news')) or die('error! Записей не найдено!');
    32. $pages_count = ceil($count / $perpage); // Количество страниц
    33.  
    34. // Если номер страницы оказался больше количества страниц
    35. if ($page > $pages_count) $page = $pages_count;
    36. $start_pos = ($page - 1) * $perpage; // Начальная позиция, для запроса к БД
    37.  
    38. // Вызов функции, для вывода ссылок на экран
    39. pages($page, $pages_count);
    40.  
    41. // Вывод информации из базы данных
    42. $result = mysql_query('select * from news ORDER BY id DESC limit '.$start_pos.', '.$perpage) or die('error!');
    43. while ($row = mysql_fetch_array($result)) {
    44.  
    45. $n_id = $row['id'];
    46. $clicks = "Просмотров: ".$row['clicks']."";
    47. $title =  $row['title'];
    48. $date =  $row['date'];
    49. $stext = $row['stext'];
    50. $text = $row['text'];
    51. $rating = $row['rating'];
    52.  
    53.  
    54. $cat1 = mysql_query("SELECT * FROM news_cats WHERE cat_id = '".$row['category']."'");
    55. $cat = mysql_fetch_array($cat1);
    56.  
    57. $at = mysql_query("SELECT * FROM users WHERE id = '".$row[author]."'");
    58. while ($atr = mysql_fetch_array($at)) {
    59.  
    60.  
    61. $id = $atr['id'];
    62. $author = "Автор: <a href='index.php?cwar=user&id=".$id."'>".$atr['login']."</a> ";
    63.  
    64.   }
    65.  
    66. $itemID = $n_id;
    67. $type = 'news';
    68. $link = "?cwar=news&op=show&id=".$_GET['id']."";
    69. $comnum = mysql_query("SELECT count(id) as num FROM comments WHERE type= '".$type."' AND itemID = ".$itemID."") or die(mysql_error());
    70. $comnum = mysql_fetch_array($comnum);
    71. $cnum = $comnum['num'];
    72.  
    73. echo "<table border= '1 ' width= '98% ' style='border-collapse: collapse' collspan'2'>
    74.     <tr>
    75.         <td background= 'http://cwar.lv/images/bg-bubplastic-h-blue.gif ' width= '100% ' colspan= '2 '  height= '25 ' class='title'>
    76.            <img src='".$cat['pic']. "'> <b>$title </b> (".$cnum.")</td>
    77.     </tr>
    78.     <tr>
    79.         <td bgcolor= '#e2e2e2 ' width= '98% ' colspan= '2 ' class= 'htd-text' >
    80.        <table width= '99% '>
    81.        <tr><td>$stext</tr></td></table></td>
    82.     </tr>
    83.     <tr bgcolor= '#e2e2e2 '>
    84.         <td width= '75% ' class= 'htd-text' >
    85.         <center>$clicks | $author | $date | Рейтинг: $rating</center></td>
    86.         <td width= '15% ' class= 'htd-text' ><center>
    87.         <b><a href='index.php?cwar=news&op=show&id=".$row['id']."'>Подробнее</a></b></center></td>
    88.     </tr>
    89. </table>
    90. <br>";
    91. }
    92.  
    93.  
    94. $res = mysql_query("SELECT * FROM news ORDER BY id DESC");
    95. while ($row = mysql_fetch_array($res)) {
    96.  
    97.  
    98.  
    99.  } }
    100.  
    101. function plus() {
    102.   global $userID;
    103. $id = $_GET['id'];
    104.     $type = $_GET['cwar'];
    105. mysql_query("INSERT INTO rating (userID, itemID, type) VALUES ('$userID', '$id', '$type')");
    106. mysql_query("UPDATE news SET rating=rating+1 WHERE id = '$id'");
    107.  
    108. mysql_query("UPDATE news SET plus=plus+1 WHERE id = '$id'");
    109.  
    110.   header("Location: ?cwar=news&op=show&id=".$id." ");
    111.  
    112. }
    113.  
    114.  
    115. function minus() {
    116.   global $userID;
    117. $id = $_GET['id'];
    118.  
    119. mysql_query("INSERT INTO rating (userID, itemID, type) VALUES ('$userID', '$id', 'ne')");
    120. mysql_query("UPDATE news SET rating=rating-1 WHERE id = '$id'");
    121. mysql_query("UPDATE news SET minus=minus+1 WHERE id = '$id'");
    122.  
    123.   header("Location: ?cwar=news&op=show&id=".$id." ");
    124.  
    125. }
    126.  
    127.  function show () {
    128.    global $userID;
    129.  
    130. $itemID = $_GET['id'];
    131. $type = $_GET['cwar'];
    132. $link = "?cwar=news&op=show&id=".$_GET['id']."";
    133.  
    134. $comnum = mysql_query("SELECT count(id) as num FROM comments WHERE type= '".$type."' AND itemID = ".$itemID."") or die(mysql_error());
    135. $comnum = mysql_fetch_array($comnum);
    136. $cnum = $comnum['num'];
    137.  
    138.  
    139.   mysql_query("UPDATE news SET clicks=clicks+1 WHERE id = '".$_GET['id']."'");
    140.  
    141. $res = mysql_query("SELECT * FROM news WHERE id = '".$_GET['id']."' ORDER BY id DESC");
    142.  
    143. while ($row = mysql_fetch_array($res)) {
    144.  
    145.  
    146. $clicks = "Просмотров: ".$row['clicks']."";
    147. $title =  $row['title'];
    148. $date =  $row['date'];
    149. $text = $row['text'];
    150. $stext = $row['stext'];
    151. $cat1 = mysql_query("SELECT * FROM news_cats WHERE cat_id = '".$row['category']."'");
    152. $cat = mysql_fetch_array($cat1);
    153.  
    154. $pic = $cat['pic'];
    155.  
    156. $at = mysql_query("SELECT * FROM users WHERE id = '".$row[author]."'");
    157. while ($atr = mysql_fetch_array($at)) {
    158.  
    159. $rating = $row['rating'];
    160.  
    161. if ($rating >0) {
    162.      $rating = "<font color='green'> <b>$rating</b></font>";
    163.  
    164.      } elseif ($rating <0)  {
    165.      $rating = "<font color='red'> <b>$rating</b></font>";
    166.      }  else {
    167.  
    168.      $rating = "<font color='blue'> <b>$rating</b></font>";
    169.      }
    170.  
    171. $type = $_GET['cwar'];
    172. $id = $atr['id'];
    173. $author = "Автор: <a href='index.php?cwar=user&id=".$id."'>".$atr['login']."</a>";
    174. $plus ='<font color="green">+</font>';
    175. $minus ='<font color="red">-</font>';
    176.  
    177.     $check = mysql_query("SELECT * FROM rating WHERE userID = '".$userID."' AND itemID = '".$_GET['id']."' AND type = '".$type."'");
    178.  
    179.     if (mysql_num_rows($check) > 0) {
    180.  
    181.  
    182.     $vote = ''.$rating.'';
    183.  
    184.     } else {
    185.  
    186.     $vote = ' <a href="?cwar=news&op=plus&id='.$_GET['id'].'">'.$plus.'</a> '.$rating.' <a href="?cwar=news&op=minus&id='.$_GET['id'].'">'.$minus.'</a>';
    187.  
    188.       }
    189.  
    190.   }
    191.  
    192.  
    193.  
    194. echo "<table border= '1 ' width= '98% ' style='border-collapse: collapse' collspan'2'> ";
    195. echo "<tr>
    196.         <td background= 'http://cwar.lv/images/bg-bubplastic-h-blue.gif ' width= '100% '   height= '25 ' class='title'>
    197.            <img src='".$pic."'> <b>$title</b> (".$cnum.")</td>
    198.     </tr>
    199.     <tr>
    200.         <td bgcolor= '#e2e2e2 ' width= '98% '  class= 'htd-text' >
    201.        <table width= '99% '>
    202.        <tr><td>$stext <br><br> $text</tr></td></table></td>
    203.     </tr>
    204.     <tr bgcolor= '#e2e2e2 '>
    205.         <td width= '75% ' class= 'htd-text' >
    206.         <center>$clicks | $author | $date | Рейтинг: $vote</center></td>
    207.     </tr>
    208. ";
    209. echo "</table>
    210. <br>";
    211.  
    212.  }
    213.  
    214.  
    215.  
    216.  include "functions/comments.php";
    217.  }
    218.  
    219. switch($op) {
    220.  
    221. default:
    222. news();
    223. break;
    224.  
    225. case "show":
    226. show();
    227. break;
    228.  
    229. case "plus":
    230. plus();
    231. break;
    232.  
    233.  
    234.  
    235.  case "minus":
    236. minus();
    237. break;
    238.  
    239. }
    240.  
    241.  
    242.  
    243.  
    244.  
    245.  
    246. ?>