Welcome Guest, Not a member yet? Register   Sign In
Pagination issue: need to get a variable
#1

[eluser]developer10[/eluser]
I'm trying to implement pagination. i've set up everything except for $config['total_rows']

This is my model function

Code:
function svimodel() {
        
        $default = array('djelatnost', 'grad', 'slovo', 'strana');
        $array = $this->uri->uri_to_assoc(3, $default);
        
        $pDjelatnost = humanize($array['djelatnost']);
        $pGrad = $array['grad'];
        $pSlovo = $array['slovo'];
        $pStrana = $array['strana'];
    
        $base = 'http://localhost/_zzz/index.php?/';
        
        $config['base_url'] = $base . uri_string();

        $config['per_page'] = '10';
        $config['num_links'] = 8;

        
        $upit = "    SELECT *, count(f_id) AS number, tbl_djelatnosti.djelatnost FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE ";
                        if($pDjelatnost && $pDjelatnost != 'Sve')
                            $out = " djelatnost = '$pDjelatnost' AND ";
                        else $out = "";
                            $upit .= $out;
                            
                        if($pGrad && $pGrad != 'Svi')
                            $out = " _f_grad = '$pGrad' AND ";
                        else $out = "";
                            $upit .= $out;
                            
                        if($pSlovo)
                            $out = " _f_ime LIKE '$pSlovo%' AND ";
                        else $out = "";
                            $upit .= $out;
                        
                $upit .= " f_vidljivo = 1 GROUP BY f_id ORDER BY f_istaknuto DESC, _f_ime ASC LIMIT ";
                        
                        if($pStrana)
                            $out = $pStrana;
                        else $out = 0;
                            $upit .= $out;
                
                $upit .= ", " . $config['per_page'];

        $query = $this->db->query($upit);

        
        $config['total_rows'] = '';
        
        $this->pagination->initialize($config);
        
        if($query->num_rows() > 0) {
            $result = $query->result();
        }
        return $result;
    }

notice $config['total_rows'] = ''; where a variable should be placed.
at the beginning of the query i tried to put count(f_id) as number in order to get
the correct number of items (with filters, ie. uri segments, applied or without them)
but i was not able to pull that value (number) out. in fact, at some instance, i was getting
no errors, but $row->number returned nothing (i remove code for this, to avoid messy code)

this variable is needed for pagination to be rendered correctly (proper number of pagination links)

any suggestions?
#2

[eluser]Herb[/eluser]
I would suggest that you test your sql with something like phpMyadmin in every possible assemblage your code could render, then see if it returns the information you would expect. If it doesn't then you need to rework your sql until it does.

Also based on the variables you set early on, $pDjelatnost and $pGrad are arrays and your if statements forming your query will always evaluate to FALSE as presented. You need to test your if statements by inserting the appropriate values that could occur to ensure they evaluate correctly, then modify as necessary.

Hope this is helpful.
#3

[eluser]developer10[/eluser]
[quote author="Herb" date="1247360498"]I would suggest that you test your sql with something like phpMyadmin in every possible assemblage your code could render, then see if it returns the information you would expect. If it doesn't then you need to rework your sql until it does.

Also based on the variables you set early on, $pDjelatnost and $pGrad are arrays and your if statements forming your query will always evaluate to FALSE as presented. You need to test your if statements by inserting the appropriate values that could occur to ensure they evaluate correctly, then modify as necessary.

Hope this is helpful.[/quote]

look, my sql query works perfectly, i tested it with one, two, etc. parameters, and without any parameters, and it always returns
what it is supposed to.

when i want to apply pagination, i need that variable $config['total_rows'] because my pagination links wont render properly, since
pagination function needs to know how many rows were returned by sql query. sometimes that number is , lets say 1000, and sometimes (when
filters, like city and starting letter are applied, its only 80).

so, that is the parameter i need. otherwise, pagination wont initialize.

i dont know how to put count in my query so i can later retrieve number of rows that were returned by the query.

whats already in the query simply doesn't return anything:

Code:
SELECT *, [b]count(f_id) AS cifra[/b], tbl_djelatnosti.djelatnost FROM

so if you have any suggestions about that, i'd like to hear it. maybe this IS correct way, and i just dont know how to retrieve it and make
it equal to $config['total_rows'] parameter.
#4

[eluser]Herb[/eluser]
Sorry. Misunderstood the problem.

http://www.techonthenet.com/sql/count.php

Code:
"SELECT count(*) as my_count FROM ......."

You confused me with all the code.




Theme © iAndrew 2016 - Forum software by © MyBB