Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Jak używać php w smarty.
ratosluaf
post
Post #1





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Jak mogę użyć PHP w szablonie smarty? Użycie tagów {php} nie przynosi skutków.
Posiadam taki kod:
Kod
$pagination->render();

Odpowiada on za wyświetlanie się paginacji.
Kod
$smarty->assign("pagination_render",$pagination->render());

Nie działa.
Wrzucenie kodu paginacji do funkcji i wywoływanie funkcji przez assign też nie działa.

Jak więc dodać tam ten kod?
Go to the top of the page
+Quote Post
nospor
post
Post #2





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Cytat
Wrzucenie kodu paginacji do funkcji i wywoływanie funkcji przez assign też nie działa.


A robisz potem w smarty:
{$pagination_render}
?


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #3





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


robię
{pagination_render}

Kod
function paginacja(){
require_once("include/db.config.inc.php");
// how many records should be displayed on a page?
$records_per_page = 50;

// include the pagination class
require 'Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the MySQL statement to fetch the rows
// note how we build the LIMIT
// also, note the "SQL_CALC_FOUND_ROWS"
// this is to get the number of rows that would've been returned if there was no LIMIT
// see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
$mysql = mysql_connect($config->db_host,$config->db_user,$config->db_pass);
$MySQL = 'SELECT SQL_CALC_FOUND_ROWS bid FROM amx_bans LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '';

// if query could not be executed
if (!($result = @mysql_query($MySQL))) {

    // stop execution and display error message
    die(mysql_error());
    }
// pass the total number of records to the pagination class
$pagination->records($rows['rows']);

// records per page
$pagination->records_per_page($records_per_page);

// render the pagination links
$pagination->render();
}


$smarty->assign("paginacja",paginacja());

to jest w pliku ban_list.php

Kod
{$paginacja}
{paginacja}

Niestety, nie działa.

Ten post edytował ratosluaf 29.10.2012, 15:40:28
Go to the top of the page
+Quote Post
nospor
post
Post #4





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Przecież wyraźnie napisałem {$pagination_render}

znajdź 10 różnic
{$pagination_render}
{pagination_render}


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #5





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


I to i to nie działa, patrz post wyżej.
Go to the top of the page
+Quote Post
nospor
post
Post #6





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




1) Z kodu wynika, że o to:
$smarty->assign("paginacja",paginacja());
wywołujesz w funkcji paginacja()... Przecież ty się tym zapętlasz w nieskończonosc

2) Funkcja paginacja() nic nie zwraca wiec trudno oczekiwać by o to:
$smarty->assign("paginacja",paginacja());
przypisała ci colowiek prócz NULLa


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #7





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Napisałem tak, bo nie chciałem podawać całego amx_bans.php. W rzeczywistości assign znajduje się dalej.

Czyli
Kod
$pagination->render();

Zamienić na
Kod
return $pagination->render();

Dokładnie o to ci chodzi?
Go to the top of the page
+Quote Post
nospor
post
Post #8





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No ale funkcja render() też musi zwracać kod. Jeśli tego nie robi to nadal będzie kicha.

Cytat
Napisałem tak, bo nie chciałem podawać całego amx_bans.php. W rzeczywistości assign znajduje się dalej.
Nikt nie kazał podawać całości. ale pisać skrótowo że wygląda to totalnie błędnie, to też nie nalezy.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #9





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Okej, ale jeżeli w amx_bans.php nie użyję funkcji i będzie samo $pagination->render();, to paginacja się pojawia, lecz za szablonem.
Go to the top of the page
+Quote Post
nospor
post
Post #10





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Bo pewnie twoja funkcja render() ma ECHO które pluje cały kod od razu. No i masz problem

ALbo to popraw i nie rób ECHO tylko zamiast tego zapamietuj wszystko do zmiennej a potem zwracaj tę zmienną
Albo przy pomocy funkcji z rodziny ob_ wyłapuj to co plują twoje ECHO z render()
Do wyboru do koloru


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #11





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Kod
   /**
     *  Generates the output.
     *
     *  <i>Make sure your script references the CSS file!</i>
     *
     *  <code>
     *  //  generate output but don't echo it
     *  //  but return it instead
     *  $output = $pagination->render(true);
     *  </code>
     *
     *  @param  boolean     $return_output      Setting this argument to TRUE will instruct the script to return the
     *                                          generated output rather than outputting it to the screen.
     *
     *                                          Default is FALSE.
     *
     *  @return void
     */
    function render($return_output = false)
    {

        // get some properties of the class
        $this->get_page();

        // if there is a single page, or no pages at all, don't display anything
        if ($this->_total_pages <= 1) return '';

        // start building output
        $output = '<div class="pagination">';

        // if the number of total pages available is greater than the number of selectable pages
        // it means we can show the "previous page" link
        if ($this->_total_pages > $this->_selectable_pages) {

            $output .= '<a href="' .

                // the href is different if we're on the first page
                ($this->page == 1 ? 'java script:void(0)' : $this->_build_uri($this->page - 1)) .

                // if we're on the first page, the link is disabled
                '" class="navigation left' . ($this->page == 1 ? ' disabled' : '') . '"' .

                // good for SEO
                // http://googlewebmastercentral.blogspot.de/2011/09/pagination-with-relnext-and-relprev.html
                ' rel="prev"' .

                '>previous page</a>';

        }


Dokładnie, czyli wystarczy użyć tej zmiennej i potem przypisać ją do templatki?
Go to the top of the page
+Quote Post
nospor
post
Post #12





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




No render() ma mieć na końcu:
return $output;
a potem możesz z tym robić co chcesz.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #13





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Kod
$output = return $pagination->render(true);

Tak to ma wyglądać? (Pytanie jest głupie i pyta o banały, ale gdybym nie raczkował w php, to bym o takie rzeczy się nie pytał, a dokumentacja mówi mało o używaniu funkcji w zmiennych)

Cały kod:
Kod
require_once("include/db.config.inc.php");
// how many records should be displayed on a page?
$records_per_page = 50;

// include the pagination class
require 'Zebra_Pagination.php';

// instantiate the pagination object
$pagination = new Zebra_Pagination();

// the MySQL statement to fetch the rows
// note how we build the LIMIT
// also, note the "SQL_CALC_FOUND_ROWS"
// this is to get the number of rows that would've been returned if there was no LIMIT
// see http://dev.mysql.com/doc/refman/5.0/en/information-functions.html#function_found-rows
$mysql = mysql_connect($config->db_host,$config->db_user,$config->db_pass);
$MySQL = 'SELECT SQL_CALC_FOUND_ROWS bid FROM amx_bans LIMIT ' . (($pagination->get_page() - 1) * $records_per_page) . ', ' . $records_per_page . '';

// if query could not be executed
if (!($result = @mysql_query($MySQL))) {

    // stop execution and display error message
    die(mysql_error());
    }
// pass the total number of records to the pagination class
$pagination->records($rows['rows']);

// records per page
$pagination->records_per_page($records_per_page);

// render the pagination links
$output = echo $pagination->render(true);

$smarty->assign("output",$output);


Nie wyświetla się nic...

Ten post edytował ratosluaf 29.10.2012, 15:54:26
Go to the top of the page
+Quote Post
nospor
post
Post #14





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




return $output;
To ma być na końcu funkcji render. Jeśli nie wiesz jak się uzywa RETURN to zajrzyj do manuala do działu funkcji - tam masz to opisane.


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #15





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Jest już takie coś:
Kod
echo $output;
Go to the top of the page
+Quote Post
nospor
post
Post #16





Grupa: Moderatorzy
Postów: 36 557
Pomógł: 6315
Dołączył: 27.12.2004




Przecież napisałem wyraźnie:
return $output;
echo $output ma zniknąć.
Funkcja ma zwracać tekst a nie go wyświetlać....

A tak poza tym z kodu co pokazałes to wynika, ze funkcja render() i tak już chyba zwraca tekst. Wystrczy wywołąć ją z parametrem true.

Wówczas nie tak jak ty pisałes:
$output = echo $pagination->render(true);

$smarty->assign("output",$output);


a:
$smarty->assign("output",$pagination->render(true));

A w szablonie smarty:
{$output}


--------------------

"Myśl, myśl, myśl..." - Kubuś Puchatek || "Manual, manual, manual..." - Kubuś Programista
"Szukaj, szukaj, szukaj..." - Kubuś Odkrywca || "Debuguj, debuguj, debuguj..." - Kubuś Developer

Go to the top of the page
+Quote Post
ratosluaf
post
Post #17





Grupa: Zarejestrowani
Postów: 91
Pomógł: 4
Dołączył: 14.10.2012

Ostrzeżenie: (0%)
-----


Dzięki, w końcu działa smile.gif
Jeszcze nigdy nie spotkałem się z tak szybką pomocą na forum.
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 09:02