2

I have this PHP code that should retrieve data from a MySQL database and embed it into a a QR code:

 <?php
    include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');

$db     = JFactory::getDbo(); 
$user   = JFactory::getUser();

$query = $db->getQuery(true);    
$query->select($db->quoteName(array('Nom', 'Cognoms', 'eCorreu')))
      ->from($db->quoteName('#__rsform_socis'))
      ->where($db->quoteName('Username') . ' = '. $db->quote($user->Username));     
$db->setQuery($query);
$codeContents = $db->loadObjectList();

$data  = $codeContents[0]->Nom;
$data .= $codeContents[0]->Cognoms;
$data .= $codeContents[0]->eCorreu;

$tempDir  = JPATH_SITE . '/images/';   
$fileName = 'qr_'.md5($data).'.png';   
$pngAbsoluteFilePath = $tempDir.$fileName;
$urlRelativeFilePath = JUri::root() .'images/' . $fileName;

if (!file_exists($pngAbsoluteFilePath)) {
    QRcode::png($data, $pngAbsoluteFilePath);
}       
echo '<img src="'.$urlRelativeFilePath.'" />';
    ?>

However, I just get an image icon. Can anyone help? Thanks!

Dani

1 Answer 1

1

Update:

Ok I've done a test. I connected to the #__users table and pulled the name and username of the ID of the current logged in user like so:

include(JPATH_LIBRARIES . '/phpqrcode/qrlib.php');

$db     = JFactory::getDbo(); 
$user   = JFactory::getUser();

$query = $db->getQuery(true);    
$query->select($db->quoteName(array('name', 'username')))
      ->from($db->quoteName('#__users'))
      ->where($db->quoteName('id') . ' = '. $db->quote($user->id));     
$db->setQuery($query);
$codeContents = $db->loadObjectList();

$data  = $codeContents[0]->name;
$data .= $codeContents[0]->username;
  
$tempDir  = JPATH_SITE . '/images/';   
$fileName = 'qr_'.md5($data).'.png';   
$pngAbsoluteFilePath = $tempDir.$fileName;
$urlRelativeFilePath = JUri::root() .'images/' . $fileName;

if (!file_exists($pngAbsoluteFilePath)) {
    QRcode::png($data, $pngAbsoluteFilePath);
}       
echo '<img src="'.$urlRelativeFilePath.'" />';

This created the QRCode. I then scanned it on my phone and it displayed the correct data. So in your case, change the dtabase table name to whatever you wish and type in the fields you want to get from that table.

array('name', 'username') // Add what you want here

Then you need to define them like so:

$data .= $codeContents[0]->NAME_OF_FIELD;

You will see a . (dot) before the =(equals) symbol. This needs to be done for all of them apart from the first one as shown on my code.

Hope this helps

12
  • I cannot check it right now, but I've been studying the code. So then, I would write $codeContents = $results to pass this information into the QR code? Oct 3, 2014 at 11:15
  • @DaniValverde - I've updated my answer with some more code which loops through all the results and applies them to the QR
    – Lodder
    Oct 3, 2014 at 11:29
  • You're awsome Lodder. I'll try it when I'm back at home. Thanks Oct 3, 2014 at 12:14
  • I've tried your code, but I get a MySQL error. Check my updated question for details. Oct 3, 2014 at 17:33
  • Table doesnt exist? Are you sure you you need to get the data from that table?
    – Lodder
    Oct 3, 2014 at 17:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.