472,373 Members | 2,110 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,373 software developers and data experts.

Reordering results for a report

I'm new to this list, so I'm not sure if this is the right place to post
this. If not, please direct me to where it would be better to post it.

Anyway, I'm creating a report generation tool of sorts in PHP for a
database. As part of this report generation tool, I'd like to allow the
user to reorder these results arbitrarily. In other words:

id | offer
---+------------
1 | Offer 1
2 | Offer 2
3 | Offer 3

could become

id | offer
---+------------
3 | Offer 3
1 | Offer 1
2 | Offer 2

However, I don't see any way of reordering the results arbitrarily,
short of creating a table that maps the id numbers to an arbitrary sort
order, joining them and doing an ORDER BY on the sort order numbers,
like so:

id | offer | sort
---+-----------+------
3 | Offer 3 | 1
1 | Offer 1 | 2
2 | Offer 2 | 3

The problems that I have with this solution are
--The sort order would be unique for anybody who uses the system, in
other words, one user may sort one way, and another user another way,
and perhaps simultaneously. I could fix this by using an additional
session identifier in the sort table, but that leads me to the next
problem...
--I'd have to garbage collect this data everytime I'm finished with it,
and since it's likely to only be used once for the actual report
generation and then discarded, it seems like a waste of effort.

So is there a way to make a query where I can sort arbitrarily without
having to create additional data in the database?

Thanks for your attention.

--
___________________________
Nathaniel Price
http://www.tesserportal.net
Webmaster

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster

Nov 12 '05 #1
4 1615
First, I'm crossposting this to pgsql-php, please remove the pgsql-general
header when next someone responds.

OK, here's how ya do it. something like this:

First, after you run a select, you can use pg_field_name to iterate over
the list of fields you're getting back. I.e. if your select was something
like:

select a1/a2 as div, a1+a2 as sum, a1-a2 as diff, a1, a2 from table;

you could use this:

$count = pg_num_fields($res);
if (isset($flds)) unset($flds);
for ($i=0;$i<$count;$i++){
$flds[]=pg_field_name($res,$i);
}

Now, when you're printing out the headers for each row, just make the link
have something like:

print "<url goes here...>?orderby=".$flds[$i]."moreurlstuffhere???";

Then, if the orderby is set when you build your query, just append it:

if (isset($orderby)){
$query.= "order by ".$orderby"
}

Add some directional control:

if (isset($dir)){
if ($dir=="down") $query.=" DESC";
}

There's more you can do, but does that kinda get the idea across? sorry
if it's not real detailed.

On Fri, 12 Dec 2003, Nathaniel Price wrote:
I'm new to this list, so I'm not sure if this is the right place to post
this. If not, please direct me to where it would be better to post it.

Anyway, I'm creating a report generation tool of sorts in PHP for a
database. As part of this report generation tool, I'd like to allow the
user to reorder these results arbitrarily. In other words:

id | offer
---+------------
1 | Offer 1
2 | Offer 2
3 | Offer 3

could become

id | offer
---+------------
3 | Offer 3
1 | Offer 1
2 | Offer 2

However, I don't see any way of reordering the results arbitrarily,
short of creating a table that maps the id numbers to an arbitrary sort
order, joining them and doing an ORDER BY on the sort order numbers,
like so:

id | offer | sort
---+-----------+------
3 | Offer 3 | 1
1 | Offer 1 | 2
2 | Offer 2 | 3

The problems that I have with this solution are
--The sort order would be unique for anybody who uses the system, in
other words, one user may sort one way, and another user another way,
and perhaps simultaneously. I could fix this by using an additional
session identifier in the sort table, but that leads me to the next
problem...
--I'd have to garbage collect this data everytime I'm finished with it,
and since it's likely to only be used once for the actual report
generation and then discarded, it seems like a waste of effort.

So is there a way to make a query where I can sort arbitrarily without
having to create additional data in the database?

Thanks for your attention.


---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #2
On Fri, Dec 12, 2003 at 10:37:27 -0800,
Nathaniel Price <np****@tesseract.net> wrote:

So is there a way to make a query where I can sort arbitrarily without
having to create additional data in the database?


You have to save the state somewhere. If there is somewhere else other
than the database you can do this (e.g. cookies, URLs, etc...) then
you might be able to do it.

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #3
On 12/12/2003 1:16 PM, Bruno Wolff III wrote :
On Fri, Dec 12, 2003 at 10:37:27 -0800,
Nathaniel Price <np****@tesseract.net> wrote:

So is there a way to make a query where I can sort arbitrarily without
having to create additional data in the database?


You have to save the state somewhere. If there is somewhere else other
than the database you can do this (e.g. cookies, URLs, etc...) then
you might be able to do it.

Drat. I was hoping that you could use some sort of subquery... ah well.

However, I was just looking at the documentation for CREATE TABLE and
noticed that you can create temporary tables which only last as long as
the "session". I think I'll probably use these to store the sorting
order by creating tables with a unique name (perhaps something generated
by PHP's uniqid()) and joining on that-- not exactly what I had in mind,
but at least that way I don't have to worry so much about garbage
collection. Although I'm not sure how they'd work with persistant
database connections in PHP...

--
___________________________
Nathaniel Price
http://www.tesserportal.net
Webmaster

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to ma*******@postgresql.org so that your
message can get through to the mailing list cleanly

Nov 12 '05 #4
Nathaniel Price <np****@tesseract.net> writes:
I'm new to this list, so I'm not sure if this is the right place to post this.
If not, please direct me to where it would be better to post it.

Anyway, I'm creating a report generation tool of sorts in PHP for a database.
As part of this report generation tool, I'd like to allow the user to reorder
these results arbitrarily. In other words:

id | offer
---+------------
1 | Offer 1
2 | Offer 2
3 | Offer 3

could become

id | offer
---+------------
3 | Offer 3
1 | Offer 1
2 | Offer 2


You could do something like

select id,offer
from tab
order by case when id = 3 then 1 when id = 1 then 2 when id = 2 then 3 end

But a) if there are thousands of records you're going to have rather a huge
query and b) where are you going to store this ordering?

--
greg
---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match

Nov 12 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: DaKoadMunky | last post by:
<CODE> #include <iostream> using namespace std; int Foo(int x,int y) { int result = x; result*=y; result+=y;
0
by: Ahmed | last post by:
Hi, I'm an Access newbie and I'm trying to create a simple report binded to a query which is: SELECT survey.q0_sex AS Gender, Count(survey.q0_sex) AS FROM survey WHERE q0_sex<>"" GROUP BY...
2
by: Johm | last post by:
I need some help in finding a way to remove the filter from the previous command. I open the report from a form where i have option group called Office.Depending on the option in the option...
15
by: Richard Hollenbeck | last post by:
For example, one college course has only 24 students in it, but the following code says there are zero As, 20 Bs, 16 Cs, 4 Ds, and 8 Fs. When it prints it then says 0 As, 40 Bs, 32 Cs, 8 Ds, and...
5
by: John Dann | last post by:
Can anyone point me to a tutorial on reordering items within a single listbox using the mouse, ie drag and drop, specifically with vb.net. Google shows me various half-references eg to related...
6
by: Uday | last post by:
Hi everyone, I have a ASP page that triggers a db-side stored procedure. At the end of the procedure, it spits out a log file, that this ASP page reads and displays for the users. But the...
5
by: Steven T. Hatton | last post by:
If find the following excerpt from the Standard a bit confusing: <quote> 3.3.6 - Class scope -1- The following rules describe the scope of names declared in classes. 1) The potential scope...
4
by: lorirobn | last post by:
Hi, I have a report displaying items that are missing from a room. I created 2 queries, the first getting the items IN the room, and the second being an "unmatched" query that references the...
1
by: anthonyjm | last post by:
I need to run a report for each record returned from a SQL Select statement. The report changes for each record, so they need to be run separately. So something like this (I have no idea what the...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge required to effectively administer and manage Oracle...
1
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web server and have made sure to enable curl. I get a...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the synthesis of my design into a bitstream, not the C++...
0
BLUEPANDA
by: BLUEPANDA | last post by:
At BluePanda Dev, we're passionate about building high-quality software and sharing our knowledge with the community. That's why we've created a SaaS starter kit that's not only easy to use but also...
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python has gained popularity among beginners and experts...
2
by: Ricardo de Mila | last post by:
Dear people, good afternoon... I have a form in msAccess with lots of controls and a specific routine must be triggered if the mouse_down event happens in any control. Than I need to discover what...
1
by: Johno34 | last post by:
I have this click event on my form. It speaks to a Datasheet Subform Private Sub Command260_Click() Dim r As DAO.Recordset Set r = Form_frmABCD.Form.RecordsetClone r.MoveFirst Do If...
0
by: jack2019x | last post by:
hello, Is there code or static lib for hook swapchain present? I wanna hook dxgi swapchain present for dx11 and dx9.
0
DizelArs
by: DizelArs | last post by:
Hi all) Faced with a problem, element.click() event doesn't work in Safari browser. Tried various tricks like emulating touch event through a function: let clickEvent = new Event('click', {...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.