offboard
offboard

Reputation: 269

get values from a query of a class

I developed a class for paging url friendly, the problem is that you can not pull the array variable to define the values ​​of the database

private function query($page){

    $u = Url::getURL($page);
    $numreg = $this->max_reg; // Quantos registros por página vai ser mostrado

    if (!isset($u)) {
        $u = 1;
    }

    @$inicial = $u * $this->max_reg;
    $sql = mysql_query("SELECT * FROM {$this->table} ORDER BY id desc LIMIT $inicial, $numreg") or die(mysql_error());          
    $sql_conta = mysql_query("SELECT * FROM {$this->table}") or die(mysql_error());

    $out = '';

    while ($data = mysql_fetch_array($sql_conta)) {         
        $out.= $this->HTML;     
    }

    return print $out;      
}

so far everything is working

problem:

global $data;
$p = new pagination;
$p->HTML    = '
                        <li> 
                            <div class="clbthumb"><a href="#"><img src="asset/images/noticias/news4.gif" alt="" /></a></div>
                            <div class="clbdes">
                                <p class="clbtitle"><a class="colr4" href="#">'.$data['titulo'].'</a></p>  
                                <p>'.$data['texto'].'</p>
                                <div class="clear"></div>
                                <div class="clbinfo">
                                    <ul>
                                        <li class="datetag">
                                            <span class="colr3">Data:</span> 
                                            <span class="pink padr">Tue, 26/01/11</span> 
                                        <li class="moreinfo"><a href="#">:: Ler Mais</a></li>

                                    </ul>
                                </div>
                            </div>
                        </li>';
$p->af_HTML = '
                    </ul>
                </div>';
$p->_build(2);

is there any method to do this?

Upvotes: 0

Views: 48

Answers (2)

AaA
AaA

Reputation: 3694

You cannot access a variable that is assigned inside a class from outside such way. The $data variable in your usage (assigned to HTML) is not accessible (in your second section of code).

if you insist to set HTML from outside of your class, you can add place holders to your HTML for each variable and then replace them inside your class.

so your code will look like

(assuming class is pagination)

class pagination
    private function query($page){

        ...
        while ($data = mysql_fetch_array($sql_conta)) {         
            $out.= str_replace('##DATA_TITULO##', $data['titulo'] , $this->HTML, $out);
            $out.= str_replace('##DATA_TEXTO##', $data['texto'] , $this->HTML, $out);
        }
        ...
    }
}

and second part will be

$p = new pagination;
$p->HTML    = '... <a class="colr4" href="#">##DATA_TITULO##</a></p>  <p>##DATA_TEXTO##</p>...';

Upvotes: 1

Brad Mace
Brad Mace

Reputation: 27886

$data needs to be declared global in the function where it is defined (and in any other functions that use it)

private function query($page){
    global $data;
    ...
}

Otherwise you're only setting a locally-scoped variable which happens to also be named $data.

It'd probably be better though if your pagination class had a html($data) function to avoid the global variable and ensure that you're getting the current values from $data, rather than whatever the values were when you initially set $p->HTML.

Upvotes: 0

Related Questions