Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: php ograniczenie znaków
Forum PHP.pl > Forum > PHP
lol99
  1. <?php
  2. /**
  3. * This class can display latest threads or posts
  4. * formated in unordered list (<ul/>)
  5. * It doesn't depend on place where script is being run
  6. * just initialize class and execute proper method
  7. *
  8. * @author Mariusz "marines" Kujawski <marinespl@gmail.com>
  9. * @link <a href="http://marines.jogger.pl/" target="_blank">http://marines.jogger.pl/</a>
  10. * @version 0.1
  11. * @license <a href="http://opensource.org/licenses/gpl-license.php" target="_blank">http://opensource.org/licenses/gpl-license.php</a> GNU Public License
  12. */
  13. class MyBBLatest {
  14. // mysql db handler
  15. private $db;
  16. // tables prefix
  17. private $prefix;
  18. // url to mybb
  19. private $url;
  20. /**
  21.   * constructor
  22.   *
  23.   * @param string $mybb path to MyBB instalation
  24.   * @param string $url URL of MyBB instalation
  25.   * @return boolean
  26.   */
  27. public function __construct($mybb = '', $url) {
  28. // include mybb config file
  29. @include('./' . $mybb . 'inc/config.php');
  30. // db connect
  31. $this->db = @mysql_connect($config['database']['hostname'], $config['database']['username'], $config['database']['password']);
  32. // db choose
  33. @mysql_select_db($config['database']['database'], $this->db);
  34. // stop executing if db connection isn't availible
  35. if (!$this->db) return false;
  36. // set db prefix
  37. $this->prefix = $config['database']['table_prefix'];
  38. // set base url of mybb
  39. $this->url = $url;
  40. // return
  41. return true;
  42. }
  43. /**
  44.   * display latest threads
  45.   *
  46.   * @param integer $many indicates how many threads have to be retrieved from database
  47.   * @param boolean $lastpost indicates whether link has to direct to last post in thread
  48.   * @param integer $fid ID of forum which threads have to be retrieved from
  49.   * @return string list of threads
  50.   */
  51. public function threads($many = 10, $lastpost = false, $fid = false) {
  52. // forum id select
  53. if ($fid) {
  54. $where = 'WHERE `fid` = ' . $fid;
  55. }
  56. if ($lastpost) {
  57. $last = '&action=lastpost';
  58. }
  59. // initialize temporary var
  60. $tmp = '<ul class="last-threads">';
  61. // select data
  62. $query = @mysql_query('SELECT `tid`, `subject` FROM `' . $this->prefix . 'threads` ' . $where . ' ORDER BY `dateline` DESC LIMIT ' . $many);
  63. // check if query has result
  64. if (!$query) return false;
  65. // collect data into list
  66. while ($row = mysql_fetch_array($query)) {
  67. $tmp .= '<li><a href="' . $this->url . 'showthread.php?tid=' . $row[0] . $last . '">' . $row[1] . '</a></li>';
  68. }
  69. $tmp .= '</ul>';
  70. // return list of threads
  71. return $tmp;
  72. }
  73. /**
  74.   * display latest posts
  75.   *
  76.   * @param integer $many indicates how many posts have to be retrieved from database
  77.   * @param integer $fid ID of forum which posts have to be retrieved from
  78.   * @return string list of posts
  79.   */
  80. public function posts($many = 10, $fid = false) {
  81. // forum id select
  82. if ($fid) {
  83. $where = 'WHERE `fid` = ' . $fid;
  84. }
  85. // initialize temporary array
  86. $tmp = '<ul class="last-threads">';
  87. // select db data
  88. $query = @mysql_query('SELECT `pid`, `tid`, `subject` FROM `' . $this->prefix . 'posts` ' . $where . ' ORDER BY `dateline` DESC LIMIT ' . $many);
  89. // check if query has result
  90. if (!$query) return false;
  91. // collect data into list
  92. while ($row = mysql_fetch_array($query)) {
  93. $tmp .= '<li><a href="' . $this->url . 'showthread.php?tid=' . $row[1] . '&pid=' . $row[0] . '#pid' . $row[0] . '">' . $row[2] . '</a></li>';
  94. }
  95. $tmp .= '</ul>';
  96. // return posts
  97. return $tmp;
  98. }
  99. } // MyBBLatest end
  100. ?>


  1. <?php
  2. echo $mybb->posts();
  3. ?>


jak ograniczyć wyświetlane znaki do 5
np mamy nazwe tematu "ala ma kota"
co zmieni na "ala m..."
erix
No bez przesady, poszukaj sobie:
string
Mlodycompany
  1. $zmienna = substr($str, 0, 5);
erix
Po co dyskusje, skoro można gotowca...
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.