Retrieving and displaying data from another db

For Joomla! 3.x Coding related discussions, you could also use: http://groups.google.com/group/joomla-dev-general

Moderators: ooffick, General Support Moderators

Forum rules
Locked
Rabble-1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Aug 04, 2017 5:41 pm

Retrieving and displaying data from another db

Post by Rabble-1 » Sun Oct 01, 2017 3:46 pm

Hello,

I need a little help. I've read the documentation and I'm only getting more confused.

I need to retrieve the data from the db and add it to another view, but I'm having no luck.

I have a form that I can input information about a project, and the view where I can see the information, it's the display view that isn't editable. I also have a form that I can input sub-contractor info.

I want to pull the data from the sub-contractor table and add it to the project display view. But nothing I've tried has worked'

Heres what I have in the Front end model

Code: Select all

 public function getSubcontractors()
	{

      $db = JFactory::getDbo();

      $query = $db->getQuery(true);

      $query->select('*');
      $query->from('#__projexpm_subcontractor');

      $subcons =  $db->loadObjectList();

      return $subcons;

     }
In the view, I added the line about the subcons

Code: Select all

public function display($tpl = null)
	{
		$app  = JFactory::getApplication();
		$user = JFactory::getUser();

		$this->state  = $this->get('State');
		$this->item   = $this->get('Data');
		$this->params = $app->getParams('com_projexpm');
		$subcons = $this->get('Subcontractors');

		if (!empty($this->item))
		{
			$this->form = $this->get('Form');
		}
And then in the display view I added.

Code: Select all

<tr>
			<th><?php echo ('test'); ?></th>
			 <td><?php echo $subcons->project_num; ?></td>
		</tr>
But I just get: Notice: Undefined variable:.......

Ultimately I want a repeated table showing the sub-contractors related to a project, which I'm sure I'll need to adjust the query, and add a "foreach" statement, but so far I can't get a single variable to display.

Can someone please explain or point me in the right direction as to the what and were I put the query, and subsequent info to make this work?

Thanks
Last edited by toivo on Sun Oct 01, 2017 3:51 pm, edited 1 time in total.
Reason: mod note: moved to 3.x Coding

User avatar
toivo
Joomla! Master
Joomla! Master
Posts: 17370
Joined: Thu Feb 15, 2007 5:48 am
Location: Sydney, Australia

Re: Retrieving and displaying data from another db

Post by toivo » Sun Oct 01, 2017 4:15 pm

Before the results can be retrieved, the query object needs to be set in the database object:

Code: Select all

$db->setQuery($query);
Toivo Talikka, Global Moderator

Rabble-1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Aug 04, 2017 5:41 pm

Re: Retrieving and displaying data from another db

Post by Rabble-1 » Sun Oct 01, 2017 4:30 pm

Thank you,

I've added the Line, but now i'm getting

Notice: Trying to get property of non-object in C:\.....

Is this because...in my test I'm using "LoadObjectIist" yet I'm only tying to display one variable, in lieu of a repeated table?

Should the return be something like loadrow for this temporarily?

Again thanks.

Rabble-1
Joomla! Apprentice
Joomla! Apprentice
Posts: 6
Joined: Fri Aug 04, 2017 5:41 pm

Re: Retrieving and displaying data from another db

Post by Rabble-1 » Sun Oct 01, 2017 4:54 pm

Thank you toivo,

I got it to work, I needed to use "LoadObject", I'm guessing since I only have 1 row in the table, and I'm not using a "foreach" statement as it's setup now.

Now that I can see the variable, I can work to refine the query.

Thank you, thank you, thank you!


Locked

Return to “Joomla! 3.x Coding”