Jump to content

Display query results in table.


rbush
Go to solution Solved by rbush,

Recommended Posts

Hello, 
I know this should be pretty simple to figure out, but everything I try is giving me absolutely no results. 
I have a mysql query selecting columns from my database and returning results. I have the results printing out right now, so I can see that this part is working. 

All I want to do is take the results and put them into a table to display on my page. Basically, take what's in the database table and copy it to a table I can put on the web. 

FYI I am using sourcerer so the "connect" code is taken care of for me in the "JFactory" bit of code. Here is the first part of my code, selecting the information from the database. 

{source} 
<?php 
$db = JFactory::getDbo(); 
$query = $db -> getQuery(true); 

$query -> SELECT($db -> quoteName(array('first_dept_name', 'last_name', 'dept', 'position', 'phone_num'))); 
$query -> FROM ($db -> quoteName('#__custom_contacts')); 
$query -> ORDER ('first_dept_name DESC'); 
$query -> WHERE ($db -> quoteName('contact_category')."=".$db -> quote('YTown Employees')); 
$db -> setQuery($query); 
$results = $db -> loadObjectList(); 

print_r($results); 

Here is where I am trying to print the results into a table. I got this code directly from a PHP book, but I am getting nothing at all returned back to me. I get table headers, but no data. 

<?php 

echo "<table><tr><th>Name</th><th>Department</th></tr>"; 

while ($row = mysqli_fetch_array ($result)){ 

echo "<tr>"; 

echo "<td>".$row['last_name']."</td>"; 

echo "<td>".$row['dept']."</td>"; 

echo "</tr>"; 



echo "</table>"; 

?> 
{/source}

Link to comment
Share on other sites

Why are you using mysqli_fetch_assoc when you're using Joomla's database library?

The only time you'd use mysqli_fetch_*() function would be if you had ran a query using mysqli_query().

 

Quote from Joomla's documentation on the use of loadObjectList

 

Method to get an array of the result set rows from the database query where each row is an object. The array of objects can optionally be keyed by a field name, but defaults to a sequential numeric array.

 

From that I'd hazard a guess this would be the correct way for looping over the results returned by that method.

<?php 

echo "<table><tr><th>Name</th><th>Department</th></tr>"; 

// loop over results return from loadObjectList()
foreach($results as $row)
{
    echo "<tr>"; 
    echo "<td>".$row->last_name."</td>"; 
    echo "<td>".$row->dept."</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

?> 
Edited by Ch0cu3r
Link to comment
Share on other sites

  • Solution

 

Why are you using mysqli_fetch_assoc when you're using Joomla's database library?

The only time you'd use mysqli_fetch_*() function would be if you had ran a query using mysqli_query().

 

Quote from Joomla's documentation on the use of loadObjectList

 

From that I'd hazard a guess this would be the correct way for looping over the results returned by that method.

<?php 

echo "<table><tr><th>Name</th><th>Department</th></tr>"; 

// loop over results return from loadObjectList()
foreach($results as $row)
{
    echo "<tr>"; 
    echo "<td>".$row->last_name."</td>"; 
    echo "<td>".$row->dept."</td>"; 
    echo "</tr>"; 
} 
echo "</table>"; 

?> 

I just realized that didn't make sense myself. Maybe that is part of the problem. I will give that a try.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.