How to display multiple values in pagination

Asked by Peet

Hi, I'm trying to create a form to update our database with hosts and have the following SQL:
  protected $sql = "
    SELECT
      h.`id`, h.`hostname`, h.`domainname`, h.`description`, h.`url`, i.`vrf`, i.`ip_long`, i.`description` `i_description`, i.`active`
    FROM
      `netops`.`hosts` h
    JOIN
      `netops`.`ipv4_addresses` i
    ON
      i.`host_id` = h.`id`
    WHERE
      h.`active` = 1
    ";

Now when I try to display the results with
 $get_results = $pagination->query($this->sql);
it only displays the last entry.

I followed the example code in the getting started documentation but can't find why it only displays the last entry. Any idea what I'm doing wrong?

Thanks!

Question information

Language:
English Edit question
Status:
Expired
For:
PHPDevShell Edit question
Assignee:
No assignee Edit question
Last query:
Last reply:
Revision history for this message
Don Schoeman (don.sch) said :
#1

Hi Peet,

I'm just taking a wild guess here, is there a possibility that you perhaps entered something into the search bar? The search bar stores previous searches in a session and one sometimes forgets that the display results are still filtered from a previous search.

Revision history for this message
TitanKing (titan-phpdevshell) said :
#2

Hi Peet,

Yes, I think Don has it spot on. Make sure your search box is empty. Also make sure your results limit is not set to 1, this can be changed in your System Settings or by passing inside invoke $pagination->totalRows = 30.

Please be so kind as to let us know if you managed to solve this problem.

Revision history for this message
Peet (peetvandesande) said :
#3

Hi guys, thanks for the answers but unfortunately it did not solve my problem. There's nothing in the search form yet and the total rows was set to 50 using
$pagination->limitCount = 50;
The totalRows was still at default of 30.

When I enter the query in mysql directly it gives me two records but on this web page only one. I think it's more something like the query function of pagination only returning one row as the foreach loop that goes through the results only runs once.

Revision history for this message
TitanKing (titan-phpdevshell) said :
#4

Could you perhaps add a few more rows to your table? We would appreciated it if we can figure this out together, for in-case its a bug. I see, you are using a normal JOIN, can you use a LEFT JOIN in this scenario?

Revision history for this message
Peet (peetvandesande) said :
#5

Ok TitanKing, I'll add some more rows first and see how that works and then I'll update my query to use a LEFT JOIN and report back to you guys.

Revision history for this message
Don Schoeman (don.sch) said :
#6

I would also suggest using FireFox's FireBug and FirePHP plugins to help you resolve the issue. PHPDevShell adds pagination based SQL instructions to your SQL query before it is executed. This means that the query you are specifying is not the exact query that is actually executed in the background. Using FirePHP you will be able to see the exact query that is being executed and that may help resolve the issue.

Tips: Once you have installed FireBug and FirePHP, enable it in your PHPDevShell configuration (usually default.config.php):
$configuration['debug']['enable'] = true;
$configuration['debug']['level'] = 4;
$configuration['error']['firePHP'] = true;

Finally, make sure that FirePHP is enabled by opening the FireBug window in your browser, click on the blue beatle (FirePHP icon) and checking "FirePHP Enabled". While you have the FirePHP window open the actual SQL queries will be logged to the Console when you execute your script.

Revision history for this message
Peet (peetvandesande) said :
#7

I added another host with only a single IP address assigned to it and this displays fine. Updating the JOIN statement did not make a difference.

Now it looks as if the query function does some filtering on its own?

The SQL query used is:
SELECT SQL_CALC_FOUND_ROWS h.`id`, h.`hostname`, h.`domainname`, h.`description`, h.`url`, i.`vrf`, i.`ip_long`, i.`description` `i_description`, i.`active` FROM `netops`.`hosts` h LEFT JOIN `netops`.`ipv4_addresses` i ON i.`host_id` = h.`id` WHERE h.`active` = 1 LIMIT 0, 30

Yet again, in mysql it returns 3 rows as expected.

Revision history for this message
TitanKing (titan-phpdevshell) said :
#8

I just noticed, what happens if you remove WHERE h.`active` = 1

Revision history for this message
Don Schoeman (don.sch) said :
#9

Sorry, I am just trying to clear things up, now that you have 3 rows in your table, is it only showing 2 of the rows or is it still only showing 1 row? You may have to show us the loop where you pull the data from the query as well as the section in your Smarty template where you build the html for this data (if you are using Smarty).

Revision history for this message
Peet (peetvandesande) said :
#10

When I remove the WHERE clause the results stay the same, i.e. only two rows are displayed.

I expect to see three rows: the first device has two IP addresses so this id, hostname, domainname, description and url should be returned twice, each with a different vrf, ip address, ip description and active.

Mysql returns the three rows as expected but my plugin only shows both hosts but only displaying one ip address for the first host.

Below is the output from msyql which is correct:
id hostname domainname description url vrf ip_long i_description active
1 host-a NULL Server A NULL 0 285213975 eth0 1
1 host-a NULL Server A NULL 0 285218769 eth1 1
2 host-b domain.tld Server B NULL 0 285213959 NULL 1

My plugin only displays rows 2 and 3 of the above output.

Revision history for this message
Peet (peetvandesande) said :
#11

Hi, did any of you guys got any further with this? I was unable to get any more information unfortunately...

Thanks!

Revision history for this message
Peet (peetvandesande) said :
#12

This is my model:
class hostsActiveHostsQuery extends PHPDS_query
{
  protected $sql = "
    SELECT
      h.`id`, h.`hostname`, h.`domainname`, h.`description`, h.`url`, i.`vrf`, i.`ip_long`, i.`description` `i_description`, i.`active`
    FROM
      `netops`.`hosts` h
    LEFT JOIN
      `netops`.`ipv4_addresses` i
    ON
      i.`host_id` = h.`id`
    WHERE
      h.`active` = 1
    ";

  public function invoke($parameters)
  {
    $pagination = $this->factory('pagination');
    $pagination->columns = array(
      _('ID') => 'id',
      _('Hostname') => 'hostname',
      _('Domainname') => 'domainname',
      _('Description') => 'description',
      _('URL') => 'url',
      _('VRF') => 'vrf',
      _('IP Address') => 'ip_address',
      _('IP Description') => 'i_description',
      _('Active') => 'active',
      _('Edit') => 'edit',
      _('Delete') => 'delete',
      );
    $get_results = $pagination->query($this->sql);
    $RESULTS['th'] = $pagination->th();
    $RESULTS['pagination'] = $pagination->navPages();
    $RESULTS['searchForm'] = $pagination->searchForm();
    $counter = 1;

    foreach ($get_results as $e) {
      $RESULTS['list'][] = array(
        'id' => $e['id'],
        'hostname' => $e['hostname'],
        'domainname' => $e['domainname'],
        'description' => $e['description'],
        'url' => $e['url'],
        'vrf' => $e['vrf'],
        'ip_address' => long2ip($e['ip_long']),
        'i_description' => $e['i_description'],
        'active' => $e['active'],
        'edit' => 'edit',
        'delete' => 'delete'
      );
      print '$counter = ' . $counter . '<br>';
      $counter++;
    }

    if (!empty($RESULTS['list'])) {
      return $RESULTS;
    } else {
      $RESULTS['list'] = array();
      return $RESULTS;
    }
  }
}

Revision history for this message
Peet (peetvandesande) said :
#13

This is the view:
<table class="floatHeader">
  <thead>
    <tr>
      {$th}
    </tr>
  </thead>
  <tbody>
    {section name=hosts loop=$RESULTS}
    {strip}
    <tr>
      <td>
        {$RESULTS[hosts].id}
      </td>
      <td>
        {$RESULTS[hosts].hostname}
      </td>
      <td>
        {$RESULTS[hosts].domainname}
      </td>
      <td>
        {$RESULTS[hosts].description}
      </td>
      <td>
        {$RESULTS[hosts].url}
      </td>
      <td>
        {$RESULTS[hosts].vrf}
      </td>
      <td>
        {$RESULTS[hosts].ip_address}
      </td>
      <td>
        {$RESULTS[hosts].i_description}
      </td>
      <td>
        {$RESULTS[hosts].active}
      </td>
      <td>
        {$RESULTS[hosts].edit}
      </td>
      <td>
        {$RESULTS[hosts].delete}
      </td>
    </tr>
    {/strip}
    {sectionelse}
    <tr>
      <td class="no_results" colspan="11">
      {_e('Your search did not return any data.')}
      </td>
    </tr>
    {/section}
  </tbody>
  <tfoot>
    <tr>
      <td colspan="11">
        {$pagination}
      </td>
    </tr>
  </tfoot>
</table>

Revision history for this message
Peet (peetvandesande) said :
#14

And the controller:
<?
$RESULTS = $this->db->invokeQuery('hostsActiveHostsQuery');

$view = $this->factory('views');

$view->set('pagination', $RESULTS['pagination']);
$view->set('searchForm', $RESULTS['searchForm']);
$view->set('th', $RESULTS['th']);
$view->set('RESULTS', $RESULTS['list']);

$view->show();
?>

Revision history for this message
Launchpad Janitor (janitor) said :
#15

This question was expired because it remained in the 'Open' state without activity for the last 15 days.

Revision history for this message
TitanKing (titan-phpdevshell) said :
#16

Hi Peet,

We apologize profoundly for the delay in fixing this bug. This bug could finally be replicated and a fix is on its way.

Revision history for this message
Don Schoeman (don.sch) said :
#17

As per my bug report here: https://bugs.launchpad.net/phpdevshell/+bug/781565

A workaround until the bug is fixed is to use Smarty's {foreach}/{foreachelse} combination instead of {section}/{sectionelse}. For example, change the template from this:

    {section name=hosts loop=$RESULTS}
    {strip}
    <tr>
      <td>
        {$RESULTS[hosts].id}
      </td>
      <td>
        {$RESULTS[hosts].hostname}
      </td>
      .
      .
      .
    {/strip}
    {sectionelse}
    <tr>
      <td class="no_results" colspan="11">
      {_e('Your search did not return any data.')}
      </td>
    </tr>

to this:

    {foreach from=$RESULTS item=row}

    {strip}
    <tr>
      <td>
        {$row.id}
      </td>
      <td>
        {$row.hostname}
      </td>
      .
      .
      .
    {/strip}
    {foreachelse}
    <tr>
      <td class="no_results" colspan="11">
      {_e('Your search did not return any data.')}
      </td>
    </tr>
    {/foreach}

Revision history for this message
TitanKing (titan-phpdevshell) said :
#18

Hi Peet, 3.0.3 fixed this bug here : https://bugs.launchpad.net/phpdevshell/+bug/781565

We hope to release it today or tomorrow.

Regards,
Jason

Revision history for this message
Peet (peetvandesande) said :
#19

Brilliant, people. I just downloaded and tried the new version and it's resolved indeed.

Thanks for your support, I'll be continuing on my plugins again! :)