Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4240334
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T03:09:04+00:00 2026-05-21T03:09:04+00:00

I am trying to reproduce the following but my using rows from the database

  • 0

I am trying to reproduce the following but my using rows from the database

var allCustomers = [

    { name: 'Customer1', contacts: [
                { name: 'Bob', id: ['1'] },
                { name: 'Sue', id: ['2'] },
                { name: 'John', id: ['3'] }
        ]},

    { name: 'Customer2', contacts: [
                { name: 'Max', id: ['4'] },
                { name: 'Ross', id: ['5'] },
                { name: 'Sally', id: ['6'] }
        ]}
];

In PHP I am fetching the rows from the database, each customer has multiple contacts, which is the bit I am struggling with. Currently I am using the method below:

<script type="text/javascript">
var allCustomers = [
<?php
include('connection.php');
$stmt = $db->prepare("SELECT customer.customerID, customerName, contactID, contactName  FROM customer INNER JOIN customerContact ON customer.customerID = customerContact.customerID");
if ($stmt->execute()) 
{
    while ($row = $stmt->fetch(PDO::FETCH_OBJ)) 
    {
            echo "{ name: '".$row->customerName."', contacts: [
                { name: '".$row->contactName."', id: ['".$row->contactID."'] }                
        ]},";
        }           
}       
?>    
];

But obviously this isn’t a very neat way of doing this, and it only works if a customer has only one contact, otherwise it reproduces the customer and contact again.

What would you suggest to fix this, could I make use of the php decode json function or something similar?

Thanks for any suggestions :).

I am trying to produce something similar to this post, but I need the id of the contact to post back to the server.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-21T03:09:05+00:00Added an answer on May 21, 2026 at 3:09 am

    Something like this:

    $data = array(
      array("name" => "Customer1", "contacts" => array(
        array("name" => "Bob", "id" => 1),
        array("name" => "Sue", "id" => 2),
        array("name" => "John", "id" => 3)
      )),
      array("name" => "Customer1", "contacts" => array(
        array("name" => "Max", "id" => 4),
        array("name" => "Ross", "id" => 5),
        array("name" => "Sally", "id" => 6)
      ))
    );
    
    echo json_encode($data);
    

    In reply to the asker’s comment:

    $stmt = $db->prepare("SELECT customer.customerID, customerName, contactID, contactName  FROM customer INNER JOIN customerContact ON customer.customerID = customerContact.customerID");
    
    $data = array();
    
    if ($stmt->execute()) 
    {
        while ($row = $stmt->fetch(PDO::FETCH_OBJ)) 
        {
            if (!isset($data[$row->customerId])
            {
                $data[$row->customerId] = array('name' => $row->customerName, 'contacts' => array());
            }
    
            $data[$row->customerId]['contacts'][] = array('name' => $row->contactName, 'id' => $row->contactId);
        }           
    }   
    
    echo json_encode(array_values($data));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to reproduce the following IL using Mono.Cecil: call !!0 [mscorlib]System.Threading.Interlocked::CompareExchange<class [System]System.ComponentModel.PropertyChangedEventHandler>(!!0&, !!0,
I'm trying to reproduce the heatmap presented on this blog by following their tutorial,
I'm working with a colleague and we're trying to reproduce the following SQL Query
I'm trying to reproduce the following as a one-liner. if($l < 10) $next =
I'm trying to simply read and thus reproduce the following code. INSERT INTO `actions`
I am trying to write a String (lengthy but wrapped), which is from JTextArea
I am trying to replace a node using ReplaceWith(), but noticed that it results
I've got a weird error when trying to generate database from an EF model.
I am trying to reproduce the following SQL query in Linq and need some
I'm trying to reproduce the simple window interface objects in C# XNA like labels,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.