Note that there are some explanatory texts on larger screens.

plurals
  1. POLooping through data
    primarykey
    data
    text
    <p>I'm having a little bit of trouble figuring out how to go about this.</p> <p>I am trying to dynamically create drop downs in the end. I currently have this working but I am making 4 ajax calls (1 for each category to get its children) which is unnecessary.</p> <p>My database structure is like so:</p> <p>Columns: id, name, location, category</p> <p>Rows sample data:</p> <p>1, blue, room, color <br> 2, red, garage, color <br> 3, ball, yard, toy <br> 4, truck, box, toy <br> 5, doll, room, toy <br></p> <p>What I am trying to do is first find out all the categories in my table and get a unique value for them. I dont want color listed twice and toy listed 3 times, just 1 for color and 1 for toy as they are both unique.</p> <p>Next, I need to loop through everything again and say Here are all the names under each category.</p> <p>Result would look like:</p> <p>Color = Blue, Red <br> Toy = Ball, Truck, Doll</p> <pre><code>function makeDataPointsTest() { $.ajax({ url: "../db_api.php", type: 'GET', dataType: 'xml', cache: false, data: { sp: "Watson_DataPointsList", type: "xml", params: { category: '' } }, error: function(err) { alert(err.statusText); }, success: function(data) { //This is the data I am getting back from the database. // It is returned as an XML object. var dataTmp = []; //temporary array var dataCats; //var to hold the unique categories $(data).find('dataPoints').each(function(i) { //Node of XML is called DataPoints. var tmp = $(this).find('category').text(); //In each Node (row) find the category name dataTmp.push(tmp); //Push that category name to an array }); dataCats = _.uniq(dataTmp); //using underscore.js I now have each unique category in //the database //Here is where I am stuck //I now need to loop through each node again and create an array that contains each of //the names under each of the categories. } }); } </code></pre> <p>Results structure (data):</p> <pre><code>&lt;root&gt; &lt;dataPoints&gt; &lt;id&gt;1&lt;/id&gt; &lt;name&gt;Blue&lt;/name&gt; &lt;location&gt;Room&lt;/location&gt; &lt;category&gt;Color&lt;/category&gt; &lt;/dataPoints&gt; &lt;dataPoints&gt; &lt;id&gt;2&lt;/id&gt; &lt;name&gt;Red&lt;/name&gt; &lt;location&gt;Garage&lt;/location&gt; &lt;category&gt;Color&lt;/category&gt; &lt;/dataPoints&gt; &lt;dataPoints&gt; &lt;id&gt;3&lt;/id&gt; &lt;name&gt;Ball&lt;/name&gt; &lt;location&gt;Yard&lt;/location&gt; &lt;category&gt;Toy&lt;/category&gt; &lt;/dataPoints&gt; &lt;dataPoints&gt; &lt;id&gt;4&lt;/id&gt; &lt;name&gt;Truck&lt;/name&gt; &lt;location&gt;Box&lt;/location&gt; &lt;category&gt;Toy&lt;/category&gt; &lt;/dataPoints&gt; &lt;dataPoints&gt; &lt;id&gt;5&lt;/id&gt; &lt;name&gt;Doll&lt;/name&gt; &lt;location&gt;Room&lt;/location&gt; &lt;category&gt;Toy&lt;/category&gt; &lt;/dataPoints&gt; &lt;/root&gt; </code></pre> <p>Is there an easy way to go about doing this jquery at this point?</p> <p><strong>This is what I am trying to create dynamically</strong></p> <p>Image: <a href="http://i.stack.imgur.com/3Emec.png" rel="nofollow">http://i.stack.imgur.com/3Emec.png</a></p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload