Jump to content

chachew

New Members
  • Posts

    6
  • Joined

  • Last visited

chachew's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Answer: $.each(data, function(key, pack){ var id = key, clickLink = $('<span>' + key + '</span>') .attr({id: id}) .click(function() { doSomething(id, pack);}); $('#breadcrumbs').append(clickLink); }); var doSomething = function(id, pack) { alert(pack); }
  2. I am not able to successfully pass a JSON object as a parameter, i get an uncaught referenceError..any ideas? $.getJSON('./getMe.php', function(data){ $.each(data, function(key, pack){ var id = key.toLowerCase().replace(" ", "_"); $('#breadcrumbs').append(" <span id='" + id + " 'onclick='DoSomething(id, pack);'>" + key + "</span> "); }); $('#breadcrumbs').append('</br></br>'); }) .success(function(){ $('#loader').hide(); }); function DoSomething(id, pack){ $.each(pack, function(stage, items){ alert(stage); }); $('#packages').html('→ ' + id); }
  3. So i am looping through a JSON string and i want to get the value of the key itself. I want to get this so i can use it to populate some navigation type menus. I can access elements of the object by: obj.A1.description and it will give me the value of the description object but i want to get the value of the key itself. Instead of knowing that the key is A1 i want to pull the value of each key with the $.each loop. Current JSON string: {"pack1":{"A1":{"description":"some text goes here","image":"image1 here"}},"pack2":{"A2":{"description":"more text here","image":"image2 here"}}}
  4. Brilliant! Exactly what i've been banging my head against the wall to achieve
  5. So i am trying to get a list of items out of my MySql database but my loop is not creating the array correctly, its dropping multiple rows when the key is the same name. <?php $search = 'item1'; $con = require_once('./dbconnect.php'); mysql_select_db("packages", $con); $package = mysql_query("SELECT * FROM $search", $con); while($item = mysql_fetch_row($package)){ $arr[$item[1]] = array('description' => $item[2], 'image' => $item[3]); } echo json_encode($arr); mysql_close($con); ?> So the loop works and does actually create the array with the various 'packages' in their respective array. The issue that i am having is that mysql_fetch_row($package)) can and will contain multiple keys that are the same name but with different things in the array. For example, the database will be something like this: PACK LEV DESCRIPTION IMAGES item1 A1 description for stuff1 image1 item1 A2 description for more stuff image2 item1 B1 more stuff here image3 item2 A1 description here for item2 image1 item2 B1 description contents here image2 --------------------------------------------------------------------------------- My Above code will produce an array like this: Array ( [item1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [description] => description contents here [image] => image2 ) ) What i want the array to be is: Array ( [item1] => Array ( [A1] => Array ( [description] => description for stuff1 [image] => image1 ) [A2] => Array ( [description] => description for more stuff [image] => image2 ) [b1] => Array ( [description] => more stuff here [image] => image3 ) [item2] => Array ( [A1] => Array ( [description] => description here for item2 [image] => image1 ) [b1] => Array ( [description] => description contents here [image] => image2 ) ) ) I hope this makes sense..Thanks for looking
  6. Hello everyone...nice to be here
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.