How to handle results of a query with PHP OO?

3

I'm creating a simple website, for photography, and I'm doing all of PHP object-oriented. I know PHP procedural, but I would like to mix PHP with HTML as little as possible. My connection and my query are running perfectly, however I would like to know how to treat the query result as best as possible.

My function for the query:

function consultarDados($query){
    $conexao = mysql_connect($this->host, $this->usuario, $this->senha);
    mysql_select_db($this->banco, $conexao);
    $rs = mysql_query($query, $conexao);
    return $rs;
    mysql_close($conexao);
}

And as I'm dealing with it on the index.php page:

include 'connectDB.php';
$conexao = new connectDb();
$retorno = $conexao->consultarDados('select * from slides');
    if(mysql_num_rows($retorno) > 0){
        while($row = mysql_fetch_assoc($retorno)){
            echo $row ['imagem'];//esta parte eu fiz apenas para verificar se os resultados da query estão sendo obtidos de maneira correta.
        }
    }

I need to insert the query result into the image name of this piece of HTML:

<img src="img/slide_1.jpg"  class="img-slide ativa">
<img src="img/slide_2.jpg"  class="img-slide">
<img src="img/slide_3.jpg"  class="img-slide">

Thank you!

    
asked by anonymous 20.05.2015 / 01:58

1 answer

1
  

Simple form

while($row = mysql_fetch_assoc($retorno)){
            echo '<img src="img/'.$row ['imagem'].'"  class="img-slide">';
        }
  

Using Template in this case Dwoo

You create a template dwoo

<html>
  <head></head>
  <body>
   {loop $items}
     <img src="img/{escape($item)}"  class="img-slide">
   {/loop}
  </body>
</html>

and create the php script that will create the array and go to the template:

<?php
include 'dwooAutoload.php';

try {
  $dwoo = new Dwoo();

  $tpl = new Dwoo_Template_File('tmpl/list.tpl');

  $data = new Dwoo_Data();
  $items = array();
  $items[] = array('item' => 'slide_1');
  $items[] = array('item' => 'slide_2');
  $items[] = array('item' => 'slide_3');
  $items[] = array('item' => 'slide_4');
  $data->assign('items', $items);

  // injeta os valores e exibe a pagina, note que o nome do indices são identicos as 
//variaveis dentro do template, o dwoo irá fazer a injeção paseado nos nomes identicos.
  $dwoo->output($tpl, $data);
} catch (Exception $e) {
  echo "Error: " . $e->getMessage();      
}
?>

Dwoo download link: link

In-depth tutorial link for Dwoo : In-depth tutorial

    
20.05.2015 / 02:15