1

I have built 2 queries

$id = JFactory::getApplication()->input->getIn('vid');
$db = JFactory::getDbo();
$db->setQuery("SELECT `email` FROM `#__usedcar_variants` WHERE `id`='$id' LIMIT 1");
$_POST['form']['Email'] = $db->loadResult();

$id = JFactory::getApplication()->input->getIn('vid');
$db = JFactory::getDbo();
$db->setQuery("SELECT `mobile` FROM `#__usedcar_variants` WHERE `id`='$id' LIMIT 1");
$_POST['form']['Contact'] = $db->loadResult();

However - it seems not the best way for optimization, as ideally

  1. both the email and mobile can be collected in a single query,
  2. store them in an array,
  3. then assign each array value to the correct field

Can someone help pls on it

  • @Lodder - sure, thanks for noticing that. could you advise how to combine them to array. You are a real professional as i have gained by reading replies in joomla community – Ruchika Oct 17 '16 at 13:48
  • see my answer below. I believe that's what you want. – Lodder Oct 17 '16 at 15:02
3

There's no need for multiple queries in your case. You can simply select email and mobile from the table, like so:

$id = JFactory::getApplication()->input->getInt('vid', '');
$db = JFactory::getDbo();

$query = $db->getQuery(true);
$query->select(array('email', 'mobile'))
      ->from($db->qn('#__usedcar_variants'))
      ->where($db->qn('id') . ' = ' . (int)$id)
      ->setLimit(1);
$db->setQuery($query);

$results = $db->loadObjectList();

The $results variable is now the object that contains the results.

If you want to access them, simply do the following:

$something_1 = $results->email;
$something_2 = $results->mobile;

Note: I've also corrected getIn with getInt (it should have a t on the end)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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