Menu
  • HOME
  • TAGS

How to iterate over JSON children from a variable - JAVASCRIPT

Tag: javascript,php,json,loops

I have the following JSON:

{
  "82": {
    "name": "Camión",
    "brands": [
      {
        "203": {
          "name": "Chevrolet"
        },
        "85": {
          "name": "Ford",
          "196": "1721",
          "194": "815"
        },
        "221": {
          "name": "Freightliner"
        },
        "222": {
          "name": "International"
        },
        "84": {
          "name": "Iveco"
        },
        "223": {
          "name": "JAC"
        },
        "229": {
          "name": "Kamaz"
        },
        "224": {
          "name": "Kenworth"
        },
        "86": {
          "name": "Mack"
        },
        "225": {
          "name": "Mitsubishi"
        },
        "226": {
          "name": "Pegaso"
        },
        "228": {
          "name": "Volkswagen"
        },
        "227": {
          "name": "Volvo"
        }
      }
    ]
  },
  "78": {
    "name": "Camioneta",
    "brands": [
      {
        "204": {
          "name": "Acura",
          "236": "MDX",
          "235": "RDX"
        },
        "252": {
          "name": "Audi",
          "249": "Q7"
        },
        "153": {
          "name": "BMW",
          "260": "X3",
          "261": "X5",
          "262": "X6"
        },
        "155": {
          "name": "Cadillac",
          "271": "Escalade"
        },
        "210": {
          "name": "Chana",
          "274": "Mini Pick-Up",
          "275": "Pick Up",
          "276": "Super Van"
        },
        "205": {
          "name": "Changhe",
          "279": "Pick Up"
        },
        "154": {
          "name": "Chery",
          "284": "Grand Tiger",
          "156": "Tigo",
          "282": "Van H5"
        },
        "157": {
          "name": "Chevrolet",
          "313": "Avalanche",
          "314": "Blazer",
          "315": "C-10",
          "316": "C-20",
          "317": "C-30",
          "318": "Captiva",
          "319": "Cheyenne",
          "320": "Colorado",
          "158": "Dmax",
          "321": "Expedition",
          "322": "Geo Tracker",
          "323": "Grand Blazer",
          "324": "Grand Vitara",
          "325": "Jimmy",
          "326": "Lumina APV",
          "327": "Luv",
          "328": "Montana",
          "159": "Orlando",
          "329": "Pick Up",
          "330": "S-10",
          "331": "Silverado",
          "332": "Suburban",
          "333": "Super Carry",
          "334": "Tahoe",
          "335": "Trail Blazer",
          "336": "Van",
          "337": "Venture",
          "338": "Vitara",
          "339": "Wagon R",
          "340": "Yukon"
        },
        "356": {
          "name": "Chrysler",
          "357": "Town & Country"
        },
        "358": {
          "name": "Citroën",
          "359": "Berlingo",
          "366": "Jumpy"
        },
        "380": {
          "name": "Daewoo",
          "381": "Damas",
          "382": "Labo",
          "383": "Musso"
        },
        "160": {
          "name": "Dodge",
          "384": "Dakota",
          "161": "RAM",
          "388": "RAM Van"
        },
        "392": {
          "name": "Fiat",
          "393": "Adventure",
          "397": "Ducato",
          "398": "Fiorino",
          "409": "Strada"
        },
        "81": {
          "name": "Ford",
          "414": "Bronco",
          "415": "Club Wagon",
          "422": "Eco Sport",
          "423": "Econoline",
          "424": "Escape",
          "427": "Expedition",
          "428": "Explorer",
          "429": "F-100",
          "430": "F-150",
          "431": "F-250",
          "432": "F-350",
          "433": "F-750",
          "446": "Ranger",
          "448": "Sport Trac",
          "449": "Sport Wagon",
          "453": "Winstart"
        },
        "162": {
          "name": "Great Wall",
          "457": "Deer",
          "458": "Hover",
          "459": "Safe"
        },
        "206": {
          "name": "Hafei",
          "461": "Mini Van",
          "462": "Super Minyi",
          "463": "Zhongyi"
        },
        "207": {
          "name": "Haima",
          "596": "7",
          "465": "Freema"
        },
        "163": {
          "name": "Honda",
          "467": "CR-V",
          "472": "Odyssey",
          "473": "Pilot"
        },
        "164": {
          "name": "Hummer",
          "165": "H1",
          "166": "H2"
        },
        "167": {
          "name": "Hyundai",
          "480": "Galloper",
          "482": "Grace",
          "483": "H1",
          "484": "H100",
          "487": "Santa Fe",
          "488": "Santamo",
          "493": "Tucson",
          "494": "Veracruz"
        },
        "168": {
          "name": "Infiniti",
          "495": "FX"
        },
        "169": {
          "name": "Isuzu",
          "496": "Amigo",
          "497": "Caribe",
          "498": "Rodeo",
          "499": "Trooper"
        },
        "79": {
          "name": "Jeep",
          "121": "Cherokee",
          "503": "CJ",
          "504": "Comanche",
          "505": "Commander",
          "506": "Compass",
          "122": "Grand Cherokee",
          "507": "Llanero",
          "508": "Pick Up",
          "509": "Renegado",
          "510": "Rubicon",
          "511": "Sahara",
          "512": "Wagoneer",
          "513": "Willys",
          "514": "Wrangler"
        },
        "118": {
          "name": "JMC",
          "500": "Pick Up"
        },
        "170": {
          "name": "Kia",
          "197": "Carens",
          "524": "Pregio",
          "525": "Sedona",
          "171": "Sportage"
        },
        "172": {
          "name": "Lada",
          "531": "Niva"
        },
        "173": {
          "name": "Land Rover",
          "534": "Defender",
          "535": "Discovery",
          "536": "Freelander",
          "537": "Range Rover"
        },
        "174": {
          "name": "Lexus",
          "543": "GX",
          "544": "LX",
          "545": "RX"
        },
        "176": {
          "name": "Lincoln",
          "547": "Navigator"
        },
        "177": {
          "name": "Mazda",
          "566": "B-2600",
          "567": "B-4000",
          "568": "BT-50",
          "569": "CX-7",
          "570": "CX-9",
          "571": "MPV",
          "572": "Navajo"
        },
        "178": {
          "name": "Mercedes Benz",
          "582": "Clase G",
          "179": "Clase M"
        },
        "208": {
          "name": "Mercury",
          "586": "Villager"
        },
        "180": {
          "name": "Mitsubishi",
          "609": "Expo",
          "610": "Grandis",
          "611": "L-200 Sportero",
          "612": "L-300",
          "181": "Montero",
          "613": "Outlander",
          "614": "Panel",
          "615": "Space Wagon",
          "616": "Sport Wagon",
          "617": "Star Wagon"
        },
        "182": {
          "name": "Nissan",
          "631": "Armada",
          "627": "Frontier",
          "628": "Murano",
          "629": "Pathfinder",
          "630": "Patrol",
          "632": "Pick Up",
          "633": "Quest",
          "634": "Terrano",
          "635": "X-Terra",
          "636": "X-Trail"
        },
        "193": {
          "name": "Otras Marcas"
        },
        "647": {
          "name": "Peugeot",
          "648": "Expert"
        },
        "183": {
          "name": "Pontiac",
          "654": "Trans Sport"
        },
        "184": {
          "name": "Porsche",
          "660": "Cayenne"
        },
        "185": {
          "name": "Renault",
          "676": "Kangoo",
          "677": "Sandero",
          "678": "Trafic"
        },
        "209": {
          "name": "Saic Wuling",
          "679": "Minivan",
          "680": "Panel",
          "681": "Supervan"
        },
        "187": {
          "name": "Subaru",
          "694": "Forester"
        },
        "188": {
          "name": "Suzuki",
          "695": "Grand Vitara"
        },
        "189": {
          "name": "Tata",
          "699": "Ace"
        },
        "80": {
          "name": "Toyota",
          "190": "4Runner",
          "700": "Autana",
          "701": "Burbuja",
          "702": "FJ Cruiser",
          "703": "FJ-40",
          "704": "Fortuner",
          "705": "Hiace",
          "706": "Highlander",
          "707": "Hilux",
          "191": "Land Cruiser",
          "708": "Macho",
          "709": "Macho Pick Up",
          "199": "Meru",
          "710": "Prado",
          "711": "Previa",
          "712": "RAV-4",
          "713": "Roraima",
          "714": "Samurai",
          "715": "Sequoia",
          "731": "Sienna",
          "716": "Tacoma",
          "718": "Tercel",
          "717": "Terios",
          "719": "Tundra"
        },
        "736": {
          "name": "Tyanye",
          "737": "Pick Up"
        },
        "192": {
          "name": "Volkswagen",
          "753": "Caddy",
          "754": "Kombi",
          "755": "Multivan",
          "756": "Saveiro",
          "757": "Touareg"
        },
        "593": {
          "name": "Zhongxing",
          "594": "Admiral",
          "758": "Grand Tiger"
        },
        "591": {
          "name": "Zotye",
          "592": "Nomada"
        }
      }
    ]
  },
  "71": {
    "name": "Carro",
    "brands": [
      {
        "211": {
          "name": "Acura",
          "233": "Integra",
          "234": "Legend"
        },
        "90": {
          "name": "Alfa Romeo",
          "237": "146",
          "238": "156",
          "239": "Giulieta",
          "240": "Spider"
        },
        "212": {
          "name": "Aston Martin",
          "242": "DBS",
          "241": "Vantage"
        },
        "91": {
          "name": "Audi",
          "243": "90",
          "244": "A3",
          "245": "A4",
          "246": "A5",
          "247": "A6",
          "248": "A8",
          "250": "S4",
          "251": "TT"
        },
        "92": {
          "name": "BMW",
          "265": "320D",
          "253": "Serie 1",
          "255": "Serie 3",
          "256": "Serie 5",
          "257": "Serie 6",
          "258": "Serie 7",
          "259": "Serie 8",
          "263": "Z3",
          "264": "Z4"
        },
        "93": {
          "name": "Buick",
          "266": "Century",
          "267": "Le Sabre",
          "269": "Regal",
          "268": "Riviera"
        },
        "94": {
          "name": "Cadillac",
          "270": "Deville",
          "272": "Fleetwood"
        },
        "95": {
          "name": "Chana",
          "273": "Benni"
        },
        "277": {
          "name": "Changhe",
          "278": "Ideal"
        },
        "77": {
          "name": "Chery",
          "96": "Arauca",
          "280": "Cowin",
          "97": "Orinoco",
          "281": "QQ",
          "283": "X1"
        },
        "72": {
          "name": "Chevrolet",
          "285": "Astra",
          "98": "Aveo",
          "286": "Camaro",
          "287": "Camino",
          "288": "Caprice",
          "289": "Cavalier",
          "290": "Celebrity",
          "291": "Century",
          "292": "Chevette",
          "293": "Chevy",
          "294": "Corsa",
          "295": "Corsica",
          "296": "Corvette",
          "99": "Cruze",
          "297": "Epica",
          "298": "Esteem",
          "299": "Impala",
          "300": "Lumina",
          "301": "Malibu",
          "302": "Meriva",
          "303": "Monte Carlo",
          "304": "Monza",
          "305": "Nova",
          "306": "Optra",
          "307": "Ranchero",
          "308": "Sierra",
          "309": "Spark",
          "310": "Sunfire",
          "311": "Swift",
          "312": "Vectra"
        },
        "100": {
          "name": "Chrysler",
          "341": "300 C",
          "345": "Coronet",
          "348": "Le Baron",
          "349": "Neon",
          "350": "New Yorker",
          "351": "PT Cruiser",
          "353": "Sebring",
          "354": "Spirit",
          "355": "Stratus"
        },
        "101": {
          "name": "Citroën",
          "360": "C2",
          "361": "C3",
          "362": "C4",
          "365": "Pluriel",
          "363": "Xsara",
          "364": "Xsara Picasso"
        },
        "102": {
          "name": "Corvette",
          "367": "Corvette"
        },
        "103": {
          "name": "Daewoo",
          "368": "Chairman",
          "369": "Cielo",
          "370": "Espero",
          "371": "Lanos",
          "372": "Leganza",
          "373": "Matiz",
          "374": "Nubira",
          "375": "Prince",
          "376": "Racer",
          "377": "Super Saloon",
          "378": "Tacuma",
          "379": "Tico"
        },
        "104": {
          "name": "Dodge",
          "342": "Brisa",
          "105": "Caliber",
          "344": "Charger",
          "385": "Dart",
          "346": "Grand Caravan",
          "347": "Intrepid",
          "386": "Journey",
          "387": "Neon"
        },
        "106": {
          "name": "Ferrari",
          "389": "F40",
          "390": "F430"
        },
        "107": {
          "name": "Fiat",
          "391": "147",
          "394": "Brava",
          "395": "Coupe",
          "396": "Croma",
          "399": "Idea",
          "400": "Marea",
          "401": "Mirafiori",
          "198": "Palio",
          "402": "Premio",
          "403": "Punto",
          "404": "Regatta",
          "405": "Ritmo",
          "406": "Siena",
          "407": "Spazio",
          "408": "Stilo",
          "410": "Tempra",
          "411": "Tipo",
          "412": "Tucan",
          "413": "Uno"
        },
        "73": {
          "name": "Ford",
          "416": "Cobra",
          "417": "Conquistador",
          "418": "Corcel",
          "419": "Cougar",
          "420": "Crown Victoria",
          "421": "Del Rey",
          "425": "Escort",
          "426": "Excursion",
          "434": "Fairline",
          "435": "Festiva",
          "108": "Fiesta",
          "109": "Focus",
          "110": "Fusion",
          "436": "Galaxie",
          "437": "Granada",
          "438": "Grand Marquis",
          "439": "Ka",
          "441": "Laser",
          "440": "LTD",
          "442": "Maverick",
          "443": "Mustang",
          "444": "Probe",
          "445": "Ranchera",
          "447": "Sierra",
          "450": "Taurus",
          "451": "Thunderbird",
          "452": "Zephyr"
        },
        "111": {
          "name": "Geely",
          "454": "CK",
          "455": "HA",
          "456": "MK"
        },
        "112": {
          "name": "Great Wall"
        },
        "213": {
          "name": "Hafei",
          "460": "Lobo"
        },
        "214": {
          "name": "Haima",
          "464": "Family"
        },
        "113": {
          "name": "Honda",
          "466": "Accord",
          "114": "Civic",
          "468": "CR-X",
          "469": "Fit",
          "470": "Integra",
          "471": "Legend",
          "474": "Prelude"
        },
        "115": {
          "name": "Hyundai",
          "475": "Accent",
          "476": "Atos",
          "477": "Azera",
          "478": "Elantra",
          "479": "Excel",
          "481": "Getz",
          "485": "H10",
          "486": "Matrix",
          "489": "Scoupe",
          "490": "Sonata",
          "491": "Tiburon",
          "492": "Verna"
        },
        "119": {
          "name": "Jaguar",
          "501": "S-Type",
          "502": "XJ"
        },
        "76": {
          "name": "Kia",
          "515": "Carens",
          "516": "Carnival",
          "517": "Cerato",
          "518": "Opirus",
          "519": "Optima",
          "520": "Picanto",
          "521": "Rio",
          "522": "Shuma",
          "523": "Spectra",
          "123": "Stylus"
        },
        "124": {
          "name": "Lada",
          "526": "21053",
          "527": "2106",
          "528": "2107",
          "529": "Acuario",
          "530": "Matriska",
          "532": "Samara",
          "533": "Strada"
        },
        "125": {
          "name": "Lexus",
          "538": "ES",
          "539": "GS",
          "540": "IS",
          "541": "LS",
          "542": "SC"
        },
        "126": {
          "name": "Lifan",
          "546": "520"
        },
        "127": {
          "name": "Lincoln",
          "548": "Town Car"
        },
        "128": {
          "name": "Maserati",
          "549": "228",
          "550": "70",
          "551": "Gran Turismo"
        },
        "129": {
          "name": "Mazda",
          "552": "2",
          "553": "323",
          "554": "626",
          "555": "929",
          "556": "Allegro",
          "557": "Demio",
          "130": "Mazda 3",
          "560": "Mazda 5",
          "131": "Mazda 6",
          "561": "Miata",
          "562": "Millenia",
          "558": "MX-3",
          "559": "MX-6",
          "563": "Protege",
          "564": "RX-7",
          "565": "RX-8"
        },
        "132": {
          "name": "Mercedes Benz",
          "133": "Clase A",
          "573": "Clase B",
          "574": "Clase C",
          "575": "Clase CLK",
          "576": "Clase CLS",
          "577": "Clase E",
          "578": "Clase S",
          "579": "Clase SL",
          "581": "Clase SLC",
          "580": "Clase SLK",
          "134": "Mercury"
        },
        "216": {
          "name": "Mercury",
          "583": "Cougar",
          "584": "Grand Marquis",
          "585": "Tracer"
        },
        "217": {
          "name": "Mini",
          "588": "Cooper",
          "587": "Cooper S",
          "589": "Cord",
          "590": "John Cooper Works"
        },
        "75": {
          "name": "Mitsubishi",
          "597": "3000 GT",
          "598": "Colt",
          "599": "Diamante",
          "600": "Eclipse",
          "601": "Evolution",
          "602": "Galant",
          "135": "Lancer",
          "603": "MF",
          "606": "Mirage",
          "604": "MS",
          "605": "MX",
          "607": "Sigma",
          "608": "Signo"
        },
        "136": {
          "name": "Nissan",
          "618": "200 SX",
          "619": "200 ZX",
          "620": "350 Z",
          "621": "Almera",
          "622": "Altima",
          "623": "Lucino",
          "624": "Maxima",
          "625": "Primera",
          "137": "Sentra",
          "626": "Tiida"
        },
        "152": {
          "name": "Otras Marcas"
        },
        "138": {
          "name": "Peugeot",
          "637": "205",
          "638": "206",
          "639": "207",
          "139": "305",
          "640": "306",
          "641": "307",
          "642": "309",
          "643": "405",
          "644": "407",
          "645": "605",
          "646": "607"
        },
        "140": {
          "name": "Pontiac",
          "649": "Fiero",
          "650": "Firebird",
          "651": "Grand AM",
          "652": "Grand Prix",
          "653": "Sunfire"
        },
        "141": {
          "name": "Porsche",
          "655": "911",
          "656": "924",
          "657": "968",
          "658": "Boxster",
          "659": "Cayman"
        },
        "142": {
          "name": "Renault",
          "661": "Clio",
          "662": "Fuego",
          "663": "Gala",
          "664": "Laguna",
          "665": "Logan",
          "666": "Megane",
          "668": "R 11",
          "667": "R 12",
          "669": "R 18",
          "670": "R 19",
          "671": "R 21",
          "672": "R 5",
          "673": "Scenic",
          "674": "Symbol",
          "675": "Twingo"
        },
        "219": {
          "name": "Scion",
          "682": "TC"
        },
        "143": {
          "name": "Seat",
          "683": "Altea",
          "684": "Cordoba",
          "685": "Cupra",
          "686": "Ibiza",
          "144": "Leon",
          "687": "Toledo"
        },
        "145": {
          "name": "Skoda",
          "688": "Fabia",
          "689": "Forman",
          "690": "Octavia"
        },
        "220": {
          "name": "Smart",
          "691": "Passion"
        },
        "146": {
          "name": "Subaru",
          "693": "Impreza",
          "692": "Legacy"
        },
        "147": {
          "name": "Suzuki"
        },
        "148": {
          "name": "Tata",
          "696": "Indica",
          "697": "Indiga",
          "698": "Indigo"
        },
        "74": {
          "name": "Toyota",
          "720": "Avalon",
          "721": "Avensis",
          "89": "Camry",
          "722": "Carina",
          "723": "Celica",
          "87": "Corolla",
          "724": "Cressida",
          "725": "Crown",
          "726": "Echo",
          "728": "Matrix",
          "727": "MR2",
          "730": "Paseo",
          "729": "Picnic",
          "732": "Sky",
          "733": "Solara",
          "734": "Starlet",
          "735": "Supra",
          "88": "Yaris"
        },
        "149": {
          "name": "Volkswagen",
          "738": "Bora",
          "739": "Brasilia",
          "740": "Buggy",
          "151": "Crossfox",
          "741": "Escarabajo",
          "150": "Fox",
          "742": "Gol",
          "743": "Golf",
          "744": "Jetta",
          "745": "New Bettle",
          "746": "Parati",
          "747": "Passat",
          "748": "Polo",
          "749": "Polo Classic",
          "750": "Puma",
          "751": "Spacefox",
          "752": "Vento"
        }
      }
    ]
  },
  "200": {
    "name": "Motos",
    "brands": [
      {
        "231": {
          "name": "Bera",
          "768": "BR",
          "769": "BRZ",
          "777": "BWS",
          "770": "Corvette",
          "771": "DT",
          "774": "Jaguar",
          "767": "Mustang",
          "776": "New Cobra",
          "773": "New Majesty",
          "772": "R1",
          "775": "Runner"
        },
        "810": {
          "name": "BMW",
          "816": "C1",
          "815": "F",
          "817": "FGS",
          "813": "GS",
          "814": "K",
          "818": "KGT",
          "811": "R",
          "812": "X Country"
        },
        "201": {
          "name": "Empire Keeway",
          "766": "Arsen",
          "760": "Horse",
          "761": "Outlook",
          "763": "Owen",
          "202": "RKV 200",
          "765": "Speed",
          "764": "Super Shadow",
          "762": "Superlight",
          "759": "TX"
        },
        "232": {
          "name": "Skygo",
          "787": "Elegance",
          "781": "Executive",
          "784": "Flash",
          "782": "Fly Dragon",
          "786": "Force",
          "778": "Freedom",
          "783": "Paseo",
          "785": "Scorpion",
          "779": "SG",
          "780": "Spirit"
        },
        "800": {
          "name": "Suzuki",
          "801": "DR",
          "804": "EN",
          "809": "Freewind",
          "803": "GN",
          "802": "GSX",
          "806": "Hayabusa",
          "807": "Intrude",
          "808": "SV",
          "805": "V-Strom"
        },
        "788": {
          "name": "Yamaha",
          "790": "Artistic",
          "789": "DT",
          "796": "Fazer",
          "795": "R6",
          "792": "RX100",
          "794": "RXZ",
          "793": "V-Max",
          "791": "WR",
          "799": "XJ",
          "797": "XTR",
          "798": "YT"
        }
      }
    ]
  },
  "2": {
    "name": "Otros",
    "brands": [
      {}
    ]
  }
}

And, for example, I received a "82" from onchange event.

I want to iterate over top json level and match this 82. After this, I have to iterate over their children and print "brands" content.

How can I do it like "foreach" in php?.

Regards.

Best How To :

The accurate iteration based on the structure of your objects and array is this:

var my82 = data['82'];
var brands = my82['brands'];
for (var item in brands[0]) {
    console.log(brands[0][item].name);
}

Result:

Iveco

Ford

Mack

Chevrolet

Freightliner

International

JAC

Kenworth

Mitsubishi

Pegaso

Volvo

Volkswagen

Kamaz

It will print the brand names on console. I hope it helps.

How can I replace the white rectangle within an image using ImageMagick?

php,image-processing,imagemagick

I think you can locate the shape pretty accurately with a simple threshold, like this: convert image.jpg -threshold 90% result.jpg and you can then do a Canny edge detection like this: convert image.jpg -threshold 90% -canny 0x1+10%+30% result.jpg The next things I would be looking at are, using the -trim...

Sockets make no sense?

javascript,node.js,sockets

The client doesn't get to cause arbitrary events to fire on the socket. It is always a message event. Using the same client, try this server code in your connection handler: socket.on('message', function(data) { // data === "pressed", since that's what the client sent console.log("Pressed!"); socket.close(); }); ...

Not able to access variables in required file

javascript,gulp,require,browserify

Without custom logic, it's not possible to achieve what you want. However I believe you could find a compromise by using ES6 modules instead of CommonJS modules. You would be able to write export var test = 'test'; which declares and exports a variable test. You can then import it...

Get all prices with $ from string into an array in Javascript

javascript,regex,currency

It’s quite trivial: RegEx string.match(/\$((?:\d|\,)*\.?\d+)/g) || [] That || [] is for no matches: it gives an empty array rather than null. Matches $99 $.99 $9.99 $9,999 $9,999.99 Explanation / # Start RegEx \$ # $ (dollar sign) ( # Capturing group (this is what you’re looking for) (?: #...

Replacing elements in an HTML file with JSON objects

javascript,json,replace

obj.roles[0] is a object {"name":"with whom"}. you cant replace string with object. you need to refer to property "name" in the object obj.roles[0].name Another problem is that var finalXML get a new value every line. you need to add a new value to the variable, not replcae it. var finalXML...

Error: [$injector:unpr] Unknown provider: RestangularProvider <- Restangular <- ctrlAG

javascript,angularjs,restangular

You didn't inject module of 'Restangular' service. Try like this angular.module('AngApp', ['angularGrid','restangular']); ...

Laravel 4.2 Sending email error

php,email,laravel,laravel-4

Closures work just like a regular function. You need to inject your outer scope variables into function's scope. Mail::send('invoices.mail', array($pinvoices,$unpinvoices), function($message) use ($email) { $message->to($email , 'Name')->subject('your invoices '); }); ...

access the json encoded object returned by php in jquery

php,jquery,ajax,json

Try: $.ajax({ url: "functions.php", dataType: "JSON", data: {id: id}, type: 'POST', success: function(json){ for(var i=0;i<json.length;i++){ alert(json[i].fname); } } }); ...

array and function php

php,arrays

$x and $y are only defined within the scope of the function. The code outside of the function does not know what $x or $y are and therefore will not print them. Simply declare them outside of the function as well, like so: <?php function sum($x, $y) { $z =...

Parsing XML array using Jquery

javascript,jquery,xml,jquery-mobile

EMI and CustomerName are elements under json so you can use .find() to find those elements and then text() to get its value. $(data).find("json").each(function (i, item) { var heures = $(item).find("CustomerName").text(); var nbr = $(item).find("EMI").text(); console.log(heures); }); .attr() is used to get the attribute value of an element like in...

php include capitalization on files

php

Your local host must be Windows, that doesn't differentiate between upper and lower case in file names and your web server Unix Based which does, simple as that.

Php Mysql Query not working properly

php,mysql

No need to use union as it will give a lots of duplicate data What you want to achieve can be done with simple left join or inner join SELECT m.issue_name ,m.issue_type , m.priority ,m.status,m.description , m.start_date,m.end_date,m.duration, s.name as server_name,p.name as product_name from mod_networkstatus as m LEFT JOIN tblservers as...

Saving data using promises

javascript,parse.com,promise

Instead of going for recursive, you can try this: classPromise = array.map(function(obj){ return obj.save();}); in es6, same thing can be: classPromise = array.map(obj => obj.save()); Edit You can reduce the whole function to: function myFunction(array, value) { if ( !array || !array.length) return; console.log("array: " + array.length); if (!value) value...

Javascript: Forloop Difference between i++ and (i+1)

javascript,loops,for-loop

The i++ is using post increment, so the value of the expression i++ is what the value was in the variable i before the increment. This code: if(sortedLetters[i] !== sortedLetters[i++]) return true; does the same thing as: if(sortedLetters[i] !== sortedLetters[i]) return true; i = i + 1; As x !==...

Teechart HTML5, line color and thickness

javascript,html5,teechart

Having a Line series: To modify the line thickness, change the series format.stroke.size property. Ie: Chart1.series.items[0].format.stroke.size=2; To modify the series color, change the series format.stroke.fill property. Ie: Chart1.series.items[0].format.stroke.fill="red"; ...

Why am getting this error?: Unknown column 'firstname' in 'field list'

php,database,mysqli

$query = "INSERT INTO `myDatabaseForAll`.`users` (`id`, `firstname`, `lastname`, `username`, `password`) VALUES (NULL, $firstname, $lastname,$username,$password)"; you need single quote around text feilds in sql queries change above query to $query = "INSERT INTO `myDatabaseForAll`.`users` (`id`, `firstname`, `lastname`, `username`, `password`) VALUES (NULL, '$firstname', '$lastname','$username','$password')"; ...

Can't call fetch directly in Backbone model listenTo

javascript,backbone.js,coffeescript

The handler for each type of event is passed a certain set of arguments. The Catalog of Events has this to say about a "change" event: "change" (model, options) — when a model's attributes have changed. So if you say this: this.listenTo(members, 'change', this.fetch) then fetch will be called like...

Time format conversion with PHP

php,time

<?php $duration="1H10M5S"; $display=str_replace(array('H','M','S'), array(' Hour(s) ',' Minute(s) ',' Seconds'), $duration); echo $display; Output 1 Hour(s) 10 Minute(s) 5 Seconds Fiddle...

php redirection working in chorme but not on firefox

php,google-chrome,mozilla

have you tried using header('location') function? example : <?php if (isset($_POST['putonline'])) { $query = "UPDATE user SET status= '1' WHERE id= '$new_id'"; $result = $cid-> query($query); if ($result== TRUE) { header("location:EidEmp.php"); die(); } else { echo "Failed"; } } ?> Edited : Maybe Change Your header function with javascript function...

Dynamically select from a dynamically generated dropdown

php,html,select,drop-down-menu

It is because you aren't ending the value attribute, so your selected option becomes <option value="optionvalueselected" -- 'optionvalue' being the value of your selected option, and 'selected' being the attribute you want to set, but won't be set because you never ended value The following should work: <select name="course_id" id="course_id">...

How to get my node.js mocha test running?

javascript,node.js,mocha,supertest

Create a separate file called app.js. The only purpose of this file would be to run the server. You'll also need to export your app object from server.js. So, your server.js would look like this // set up ====================================================================== var express = require('express'); var app = express(); // create our...

Cant submit form

javascript,php

Your PHP is checking if $_POST['submit'] contains a value. Your form does not contain a form element with the attribute name="submit", so therefore it fails and moves straight to the else statement. If you want to check if the form was posted then you should instead check for: if (!empty($_POST))...

How do I display my mysql table column headers in my php/html output?

php,html,mysql,table,data

Note: You can just make a single file out of it to achieve your wanted output Use mysql_real_escape_string() to sanitize the passed-on value to prevent SQL injections You should use mysqli_* instead of the deprecated mysql_* API Form them in a single file like this (display.php): <html> <form method="post" name="display"...

Include both local and server at the same time

php

file_exists: http://php.net/manual/en/function.file-exists.php is_dir: http://php.net/manual/en/function.is-dir.php Best way to do that is use dirname(__FILE__) which gets the directory's full path of the current file in ether unix of windows format. Then we use realpath() which conveniently returns false if file does not exist. All you have to do is specify a relative...

Javscript Replace Text in tags without changing children element HTML and Content

javascript,jquery

The way I think you will have to do it is in each element individually and use this jquery small plugin I rewrite here is the code and also fiddle the html <div id="parent"> thisi sthe fpcd <p>p</p> </div> plugin to find the content of the selector text without child...

slideToggle state not working with multiple boxes

javascript,jquery,cookies

Use onbeforeunload function of javascript window.onbeforeunload = function() { //Declare cookie to close state } This function will be called every time page refreshes Update: To make loop through every value use this $.each this way: var new_value = ""; window.onbeforeunload = function() { $.each($('div.box_container div.box_handle'),function(index,value){ new_value = ($(value).next('.box').css('display') ==...

submitting form then showing loading image by javascript

javascript,html

Let suppose on button click you are calling ajax method <button onclick="LoadData();"/> before ajax call show image and onComplete ajax method hide this image function LoadData(){ $("#loading-image").show(); $.ajax({ url: yourURL, cache: false, success: function(html){ $("Your div id").append(html); }, complete: function(){ $("#loading-image").hide(); } }); } ...

show div only when printing

javascript,html,css

You need some css for that #printOnly { display : none; } @media print { #printOnly { display : block; } } ...

MySQL Query returning strange values

php,mysql

You need to join by account_id and also question_id SELECT * FROM `quiz_questions` INNER JOIN `quiz_answers` ON `quiz_questions`.`account_id` = `quiz_answers`.`account_id` AND `quiz_questions`.`question_id` = `quiz_answers`.`question_id` WHERE `quiz_questions`.`account_id` = '1840979156127491' ORDER BY `quiz_questions`.`question_id` ASC LIMIT 5 ...

Javascript change the souce of all images present inside a string

javascript,jquery

You can wrap your string into a jQuery-object and use the .find()-method to select the images inside the message-string: var msg = '<span class="user_message">hiiiiiii<img title=":benztip" src="path../files/stickers/1427956613.gif" /><img src="path../files/stickers/416397278.gif" title=":happy" /></span>'; var $msg = $(msg); $msg.find('img').attr('src', 'path_to_img'); $("#chat_content").append($msg); Demo...

how to multiply two column names using codeigniter validation rule

php,codeigniter,validation

You done need to do anything with your controller. Add this change your view to <script> function calculate() { var myBox1 = document.getElementById('crop_quantity').value; var myBox2 = document.getElementById('per_rate').value; var result = document.getElementById('income_amount'); var myResult = myBox1 * myBox2; result.value = myResult; } window.onload = calculate(); </script> <div class="control-group"> <label class="control-label">Crop Quantity</label>...

Google map infowindow position

javascript,google-maps,events,infowindow

Two issues: There is a typo in your click listener code, javascript is case sensitive, infowindow and infoWindow are different objects, so you are not setting the position of the infowindow correctly. infowindow.open(map); infoWindow.setPosition(event.latLng); You are currently placing the infowindow at the place that is clicked infoWindow.setPosition(event.latLng);. If you want...

PHP Regular Expressions Counting starting consonants in a string

php,regex

This is one way to do it, using preg_match: $string ="SomeStringExample"; preg_match('/^[b-df-hj-np-tv-z]*/i', $string, $matches); $count = strlen($matches[0]); The regular expression matches zero or more (*) case-insensitive (/i) consonants [b-df-hj-np-tv-z] at the beginning (^) of the string and stores the matched content in the $matches array. Then it's just a matter...

Automatically calling server side class without

javascript,html,ajax

Trigger the click event like this: $('._repLikeMore').trigger('click'); ...

Javascript sort array of objects in reverse chronological order

javascript,arrays,sorting

As PM 77-1 suggests, consider using the built–in Array.prototype.sort with Date objects. Presumably you want to sort them on one of start or end: jobs.sort(function(a, b) { return new Date(a.ys, a.ms-1) - new Date(b.ys, b.ms-1); }) ...

How to search images by name inside a folder?

php,mysql,image

This looks like a job for glob, which returns an array of file names matching a specified pattern. I'm aware of the other answer just posted, but let's provide an alternative to regex. According to the top comment on the docs page, what you could do is something like this:...

Angular $http and Fusion Tables in IE9

javascript,angularjs,internet-explorer-9,google-fusion-tables

You should use the Angular $http.jsonp() request rather than $http.get(). JSONP or “JSON with padding” is the communication technique which allows for data to be requested from a server under a different domain (also known as a Cross Origin Request). Which is what you have used in your jQuery AJAX...

PHP / MySQLi: How to prevent SQL injection on INSERT (code partially working)

php,mysql,mysqli,sql-injection,sql-insert

In the New PHP code snippet, you are still vulnerable to injections. You are using a prepared statement in the insert part, but you are not actually using the preparations strengths correctly. When creating a prepared statement, you create a query in which you add placeholders instead of the raw...

session value in javascript cannot be set

javascript,function,session

Javascript is a client-side language. Session are server-side component. If you want to update session when user does something on your page, you should create a ajax request to the server. Or maybe use some client side variables that, for some aspects, are similar to session (they will be forever...

compare today's date with unix timestamp value in database

php,mysql

If I understand correctly you have a unix timestamp in a varchar field and you can't change this. If you compare the unix timestamp directly you will only get results that match the exact second of the timestamp. You can use FROM_UNIXTIME() to convert the timestamp in a date value...

Converting “i+=n” for-loop to $.each

javascript,jquery

A jQuery only way would be to iterate over the nth-child(4n) $('.thumbnail:nth-child(4n)').each(function(){ $(this) .prevAll('.thumbnail').andSelf() .wrapAll($('<div/>',{class:"new"})) }); Demo Considering the complexity, not sure whether the prevAll() performs better than the plain for loop. Referring one of my similar answer here...

How to find the days b/w two long date values

javascript,jquery,date

First you need to get your timestamps in to Date() objects, which is simple using the constructor. Then you can use the below function to calculate the difference in days: var date1 = new Date(1433097000000); var date2 = new Date(1434479400000); function daydiff(first, second) { return (second - first) / (1000...

Get elements containing text from array

javascript,jquery,html,arrays,contains

You can use :contains selector. I think you meant either one of those values, in that case var arr = ['bat', 'ball']; var selectors = arr.map(function(val) { return ':contains(' + val + ')' }); var $lis = $('ul li').filter(selectors.join()); $lis.css('color', 'red') <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script> <ul> <li>cricket bat</li> <li>tennis ball</li> <li>golf ball</li>...

want to show and hide text using “this” jquery

javascript,jquery

I guess OP wants to link the button more with corresponding click span class $('.clickme').click(function(){ $(this).parent().prev().find(".click").toggle(); }); FIDDLE DEMO...

Login Signup PopUp

javascript

In your code you have 2 options to solve it first is jquery and second one is css. 1) Jquery On load trigger click event. $('#cont-1').click(); OR 2) CSS Add hide class in second tab <div class="tabcontent hide" id="cont-2-1"> and add activeLink class for login <a href="javascript:;" class="inactive activeLink" id="cont-1">Login</a>...

Substring of a file

javascript,arrays,substring

To get your desired output, this will do the trick: var file = "a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d, a|b|c|d"; var array = file.split(", ") // Break up the original string on `", "` .map(function(element, index){ var temp = element.split('|'); return [temp[0], temp[1], index + 1]; }); console.log(array); alert(JSON.stringify(array)); The split converts...

Javascript function to validate contents of an array

javascript,arrays

You can use a simple array based test like var validCodes = ['IT00', 'O144', '6A1L', '4243', 'O3D5', '44SG', 'CE64', '54FS', '4422']; function validItems(items) { for (var i = 0; i < items.length; i++) { if (validCodes.indexOf(items[i]) == -1) { return items[i]; } } return ''; } var items = ["IT00",...

How To Check Value Of String

javascript,css,string,numeric

document.GetElementById("tombolco").style = "display:block"; That's not the right way. This is document.getElementById("tombolco").style.display = 'block'; Also note that it is getElementById, not with a capital G. Same with 'none',Rest of your code is fine. Fiddle...

RecursiveIteratorIterator to fetch subdirectories

php

I would change if ( $dir->isDir() ) to if ( $dir->isDir() && $dir != $root) to remove the root directory...