Fernando Andrade
Fernando Andrade

Reputation: 875

loop through query in oop php

So in my project i need to retrieve some data from a query that gives 2 rows(i tested the query in php my admin)

but i am using Object oriented Programing and my method looks like this:

function __construct($pid){

    Sys::database_connect();

    $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

    $array_data = mysql_fetch_array($prod_data);

    $this->produto_id = $array_data['produto_id'];
    $this->nome = $array_data['nome'];
    $this->descricao = $array_data['descricao'];
    $this->pvp = $array_data['pvp'];
    $this->categoria = $array_data['categoria'];

            //this is the picture name (the first one will be the main pic of the product)
    $this->file = $array_data['file'];

    $pre_img_galery = array();
    $max_pics=count($array_data);
    for ($pci_for=0; $pci_for < $max_pics; $pci_for++) { 
        //array_push($pre_img_galery, $array_data['file'][$pci_for]);
        echo $this->$array_data['file'].'<br>';
    }
    $this->img_galery = $pre_img_galery;

}

the database have 2 pictures and that results in 2 rows with different values at the pictures field...

my for loop just echoes the characters of the first file name characters...

how can i define the pictures file name to an array(img_galery)?

my workaround:

function __construct($pid){

    Sys::database_connect();

    $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

    /*while($row = mysql_fetch_array($prod_data)) {
        echo $prod_data['file'].'<br>';
    }*/

    while ($row =  mysql_fetch_array($prod_data)){

    echo $this->produto_id = $row['produto_id'];
    echo $this->nome = $row['nome'];
    echo $this->descricao = $row['descricao'];
    echo $this->pvp = $row['pvp'];
    echo $this->categoria = $row['categoria'];
    //echo $this->online = $array_data['online'];
    echo $this->file = $row['file'];

    $pre_img_galery = array();

    array_push($this->img_galery, $row['file']); 
    echo '<br><br><br>';
    print_r($this->img_galery);
    }

}

Upvotes: 0

Views: 767

Answers (1)

Teena Thomas
Teena Thomas

Reputation: 5239

This is what needs to be done, you need to loop through the resultset,

 $prod_data = mysql_query("SELECT Produto.produto_id, Produto.nome, Produto.descricao, Produto.pvp, Produto.iva_id, Produto.fornecedor_id, Produto.views, Categoria.nome AS categoria, Imagem.file FROM Produto, Categoria, Imagem WHERE Produto.produto_id = '$pid' AND Produto.categoria_id=Categoria.categoria_id AND Imagem.Produto_produto_id = Produto.produto_id ORDER by Produto.produto_id") or die(mysql_error());

while($row =  mysql_fetch_array($prod_data) { 
  $pre_img_galery[]['produto_id'] = $row['produto_id'];
   ... //and so on for other indexes per row.
} 

Note: Mysql_* extensions are deprecated. It would be better to use Mysqli_* or PDO extensions. See this question MySQL vs MySQLi when using PHP and find installation instructions for Mysqli_* here.

Upvotes: 1

Related Questions