Number total from Report Builder Query

By gadefgaertgqe - October 31, 2013

Hi,

I am wondering if there is a way to have a column number for each result from a MySQL query in Report Builder. For example:

1 | Bob

2| Jane

3| David

Here is an example of my current Query:

<?php $firstTable = true;?>
<?php foreach (getSchemaTables() as $tableName) :?>
  <?php if(strpos($tableName, "_main")): ?>
    <?php $schema = loadSchema($tableName); ?>
    <?php if(@$schema['name']): ?>
      <?php if($firstTable) {
        $firstTable = false;
        }
        else {
        echo "UNION";
      }
      ?>

      SELECT `name` as "Name", FROM `<?php echo $TABLE_PREFIX;?><?php echo $tableName;?>`

    <?php endif; ?>
  <?php endif ?>
<?php endforeach ?>

Thanks!

By Dave - November 4, 2013

Hi 8bitpixel,

I google'd for "MySQL Row Counter" and found this helpful article: http://stackoverflow.com/questions/3126972/mysql-row-number

Basically if you put this BEFORE your query:

SELECT @i:=@i+1 AS "Column Number", myquery.* FROM (

And this AFTER your query: 

) myquery, (SELECT @i:=0) myrowcounter

Then you'll get a column number column added:

SELECT @i:=@i+1 AS "Column Number", myquery.* FROM (
  SELECT * FROM <?php echo $TABLE_PREFIX ?>accounts
) myquery, (SELECT @i:=0) myrowcounter

Let me know if that works for you. 

Dave Edis - Senior Developer
interactivetools.com

By gadefgaertgqe - November 5, 2013

Well, after having one last ditch attempt, I now have it working! I revisited the earlier solution (the same as the one you posted Dave), as I was starting to wonder if I had gone to far down the rabbit hole!

So, this is the OLD BROKEN code:

<?php $firstTable = true;?>
<?php foreach (getSchemaTables() as $tableName) :?>
  <?php if(strpos($tableName, "_main")): ?>
    <?php $schema = loadSchema($tableName); ?>
    <?php if(@$schema['bike_stock']): ?>
      <?php if($firstTable) {
        $firstTable = false;
        }
        else {
        echo "UNION";
      }
      ?>
SELECT @i:=@i+1 AS "Column Number", myquery.* FROM (
      SELECT `subdomain_name` as "Subdomains List", `dealer_title` as "Dealer Title" FROM `<?php echo $TABLE_PREFIX;?><?php echo $tableName;?>`
WHERE bike_stock = '1'
) myquery, (SELECT @i:=0) myrowcounter

    <?php endif; ?>
  <?php endif ?>
<?php endforeach ?>

and here is the CURRENT FIXED Code:

<?php $firstTable = true;?>
<?php foreach (getSchemaTables() as $tableName) :?>
  <?php if(strpos($tableName, "_main")): ?>
    <?php $schema = loadSchema($tableName); ?>
    <?php if(@$schema['bike_stock']): ?>
      <?php if($firstTable) {
        $firstTable = false;
        }
        else {
        echo "UNION";
      }
      ?>

SELECT @i:=@i+1 AS "Column Number", myquery.* FROM (
      SELECT `subdomain_name` as "Subdomains List", `dealer_title` as "Dealer Title" FROM `<?php echo $TABLE_PREFIX;?><?php echo $tableName;?>`
WHERE bike_stock = '1'
) myquery, (SELECT @i:=0) myrowcounter

    <?php endif; ?>
  <?php endif ?>
<?php endforeach ?>

So, the only difference is the new empty line before the first SELECT. That's it. Can someone explain this please? So much time wasted because of a missing empty line. I kind of feel robbed but glad it's fixed.

Thought I would share just in case someone else falls foul of this.

By Dave - November 7, 2013

Hi 8bitpixel,

So, the only difference is the new empty line before the first SELECT. That's it. Can someone explain this please? So much time wasted because of a missing empty line. I kind of feel robbed but glad it's fixed.

PHP sometimes removes space around the beginning and end of <?php ?> tags.  My guess is PHP wasn't outputting any space before your "SELECT".  You could test it by copying and pasting your code into a test.php scren and seeing what MySQL it outputs.

Here's an example: 

1
<?php print "2"; ?>
3

Outputs:

1
23

Anyways, glad it's working and thanks for posting back!

Dave Edis - Senior Developer
interactivetools.com