0

I have multiple customers in DB, each customer is signed in for an event. I have multiple customer signed for the same event. So I would like to output on the screen Event name and then under that all customers signed in for that event. Here is example how my records look like in data base:

Event Name     Customer Name
Test 1          joe, allen
Test 2          tim, johnas 
Test 2          camil, gera
Test 1          john, paskins
Test 3          steve, mcines
Test 3          jym, haskynes
Test 2          kim, george

Here is my query:

SELECT EventName, Customer
From Reservations

So I was wondering if I can use JSON to store results with the same Event name? How I would check if they have same event name and how to store them in different row in JSON that why I can output records with the same name on the screen in separate rows below Event name?

Kappa
  • 1,015
  • 1
  • 16
  • 31
espresso_coffee
  • 5,980
  • 11
  • 83
  • 193

2 Answers2

1

JSON is a not assoziative format:

var a = [{"eventName" : "Test 1", "customerName": "joe, allen"},
         {"eventName" : "Test 2", "customerName": "tim, johnas"}]
alert(a[0].eventName);

Is the way to adress the eventName in JSON, so you cannot get the right entries without using a loop that way.

But you could use an object to store your JSON lists before printout:

var b = {"Test 1": [{ "customerName": "joe, allen" },
                    { "customerName": "john, paskins" }],
         "Test 2": [{"customerName": "tim, johnas"}]    
}

alert(b["Test 1"][1].customerName); // will return "john, paskins"

but that way you cannot work with b[0][0] since b[0] would only give a result if there was something like this in the object:

                0:[{customerName: "john, doe"}]
Gammeldags
  • 58
  • 3
0

The easiest way to understanding it would be to divide it into 2 different queries.

First: Get a list of eventNames that are Distinct

SELECT DISTINCT eventName From Reservations

That will return results like this:

eventName     
Test 1     
Test 2         
Test 3         

Then with those results, you can do another query for each on the results to get the customers attending.

foreach(ROW in RESULTS)
    SELECT customerName FROM Reservations WHERE eventName = ROW

This will give you a list of the customers attending that event.

From there building the JSON is really simple. Here is an example from an answer found on stackoverflow: How can I build a json string in javascript/jquery?

Community
  • 1
  • 1
Sari Rahal
  • 1,897
  • 2
  • 32
  • 53