dqxm14187 2013-08-16 01:20
浏览 29
已采纳

从类的查询中获取值

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?

  • 写回答

2条回答 默认 最新

  • douju2474 2013-08-16 02:20
    关注

    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>...';
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行