Display sql query on module pdf

Hello everyone.

I’m new developing modules for dolibarr. I made some easy modules before for my own use, just to set up some functions to adapt dolibarr to my country. Right now. I’m a little stuck with showing some data on my PDF.

I made a module for internal control and I also made a dictionary for it but I cannot show the content of the dictionary on the PDF.

This is my code for the column to get dictionary info to the card.php
‘fk_variable’ => array(‘type’=>‘sellist:variable:l abel:code::active=1’, ‘label’=>‘VariableLang’, ‘enabled’=>‘1’, ‘position’=>23, ‘notnull’=>1, ‘visible’=>1,),

So if I but the code below on the PDF I could get the Integer value
$pdf->SetXY($posx +2,$posy+17);
$pdf->SetFont(’’,‘b’,8);
$pdf->SetTextColor(0,0,0);
$pdf->MultiCell($widthrecbox, 2, ‘RESULT’.": ".$object->fk_variable, 0, ‘L’);

then if I use this query directly to MySQL I could get the value I want to display

“SELECT m.variable, me.label as result FROM llx_mymodule m, llx_mydictionarie as me WHERE m.variable = me.code”

So I made a mysql_query on pdf to display that value but isn’t working. I hope someone could help me.

$query=mysqli_query("SELECT m.variable, me.label as result FROM "MAIN_DB_PREFIX."mymodule m, ".MAIN_DB_PREFIX.“mydictionarie as me WHERE m.variable = me.code”);
while($data=mysqli_fetch_array($query)){

        	$pdf->MultiCell($widthrecbox, 2, 'RESULT'.": ".$data['label'], 0, 'L');

}

Hello!

I already solved this trouble. I’ll leave here the solution just in case someone will need it.

I added a new function to fetch in mymodule.class.php.

public function fetch_display($object)
{
global $conf,$db;
// phpcs:enable
// looking for users
$sql = “SELECT label”;
$sql .= " FROM “.MAIN_DB_PREFIX.“mydicionarie”;
$sql .= " WHERE 1 = 1 and rowid=”.$object;
$sql .= " AND active=1";

    $row = $db->query($sql);
                
    if ($row)
    {
       $obj = $db->fetch_object($row); 
       return $obj->label;
    }
    else
    {
        dol_print_error($this->db);
    }
} 

Then I changed pdf data.

$pdf->SetXY($posx +2,$posy+17);
$pdf->SetFont(’’,‘b’,8);
$pdf->SetTextColor(0,0,0);
$pdf->MultiCell($widthrecbox +10, 2, ‘RESULT’.": ".$object->fetch_display($object->variable), 0, ‘L’);

2 Likes