Jump to content

Display query results in table.


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
https://forums.phpfreaks.com/topic/289866-display-query-results-in-table/
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>"; 

?> 

 

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.