Results 1 to 5 of 5
  1. #1

    Default Need PHP help: nested MySQL table


    naa koy table sa akngg mysql nga na ka foreign keys.akong ganahan buhaton ky akong ma combine ang tables dayn akng ma encode as JSON. so far, mao ra ni ako nabuhat

    Code:
        $consoleQuery = 'SELECT * '.                    
            'FROM consoleConsole '.
                'JOIN consoleModel '.
                    'ON consoleConsole.consoleId = consoleModel.consoleId '.
                'JOIN consoleGame '.
                    'ON consoleModel.modelId = consoleGame.modelId';
    
    
        $consoleResult = mysql_query($consoleQuery);
        
        $consoleFields = array_fill_keys(array(
            'consoleName',
            ), null);
        
        $modelFields = array_fill_keys(array(
            'modelName',
            ), null);
    
    
        $console = array();
        $rowConsole = array();
        
        while ($rowConsole = mysql_fetch_assoc($consoleResult, MYSQL_ASSOC)) {
            $consoleId = $rowConsole['consoleId'];
            $modelId = $row['modelId'];
            if (isset($console[$consoleId]['Information'])) {
                $console[$consoleId]['Information'][] = array_intersect_key($rowConsole, $modelFields);
            }
            
            else {
                $console[$consoleId] = array_intersect_key($rowConsole, $consoleFields);
                $console[$consoleId]['Information'] = array(array_intersect_key($rowConsole, $modelFields));
            }
        }
    
    
        $console = array_values($console);
        echo json_encode($console);
    ang result nya ky kini:

    Code:
    [
      {
        "consoleName": "PlayStation",
        "Information": [
          {
            "modelName": "PlayStation"
          },
          {
            "modelName": "PlayStation 2"
          },
          {
            "modelName": "PlayStation 3"
          },
          {
            "modelName": "PlayStation Portable"
          }
        ]
      },
      {
        "consoleName": "Wii",
        "Information": [
          {
            "modelName": "Wii"
          }
        ]
      },
      {
        "consoleName": "Xbox",
        "Information": [
          {
            "modelName": "Xbox"
          },
          {
            "modelName": "Xbox 360"
          }
        ]
      },
      {
        "consoleName": "SNES",
        "Information": [
          {
            "modelName": null
          }
        ]
      }
    ]
    but wat I want to hapen is,

    Code:
    [
      {
        "Console": "PlayStation",
        "Information": [
          {
            "Model": "PlayStation 3",
            "Title": [
              {
                "Game": "007 Legends",
                "Publisher": "Electronic Arts"
              },
              {
                "Game": "Ace Combat: Assault Horizon",
                "Publisher": "Namco"
              }
            ]
          },
          {
            "Model": "PlayStation 2",
            "Title": [
              {
                "Game": "007: Agent of Fire",
                "Publisher": "Electronic Arts"
              },
              {
                "Game": "Ace Combat 4: Shattered Skies",
                "Publisher": "Namco"
              }
            ]
          },
          {
            "Model": "PlayStation 1",
            "Title": [
              {
                "Game": "007 Racing",
                "Publisher": "Electronic Arts"
              },
              {
                "Game": "Ace Combat",
                "Publisher": "Namco"
              }
            ]
          }
        ]
      },
      {
        "Console": "Wii",
        "Information": [
          {
            "Model": "Wii",
            "Title": [
              {
                "Game": "007: Quantum of Solace",
                "Publisher": "Activision"
              },
              {
                "Game": "AC/DC Live: Rock Band Track Rack",
                "Publisher": "MTV Games"
              }
            ]
          }
        ]
      },
      {
        "Console": "Xbox",
        "Information": [
          {
            "Model": "Xbox",
            "Title": [
              {
                "Game": "AFL",
                "Publisher": "Acclaim"
              },
              {
                "Game": "American Chopper",
                "Publisher": "Activision"
              }
            ]
          },
          {
            "Model": "Xbox 360",
            "Title": [
              {
                "Game": "AFL Live",
                "Publisher": "Electronic Arts"
              },
              {
                "Game": "Akai Katana Shin",
                "Publisher": "Cave"
              }
            ]
          }
        ]
      }
    ]
    unsaon na pra mogawas ang title nga array ug naay data? akng query diay kwangan pa na ky libog au ang join.

  2. #2
    C.I.A. Platinum Member carmicael's Avatar
    Join Date
    Jul 2007
    Gender
    Male
    Posts
    2,142
    hatag daw image sa imong db relationships.

  3. #3
    sa phpmyadmin sql cli
    Code:
    	SELECT *
    	FROM consoleConsole
    		JOIN consoleModel
    			ON consoleConsole.consoleId = consoleModel.consoleId
    		JOIN consoleGame
    			ON consoleModel.modelId = consoleGame.modelId


    mao ra ni ako mkuha bos ky ala koy workbench pra erd.

  4. #4
    workbench sa ERD? try DeZign...erd creator that'll make SQL SCHEMA pa..

    Edited: The problem lies here

    while ($rowConsole = mysql_fetch_assoc($consoleResult, MYSQL_ASSOC)) {
    $consoleId = $rowConsole['consoleId'];
    $modelId = $row['modelId'];
    if (isset($console[$consoleId]['Information'])) {
    $console[$consoleId]['Information'][] = array_intersect_key($rowConsole, $modelFields);
    }

    else {
    $console[$consoleId] = array_intersect_key($rowConsole, $consoleFields);
    $console[$consoleId]['Information'] = array(array_intersect_key($rowConsole, $modelFields));
    }
    }

    STRUCTURE NIMO IS:
    WHILE(something) {
    IF (something) {

    }

    else (something) {


    }
    }

    SHOULD BE:

    WHILE (something) {
    WHILE (something) {

    IF (something) {

    }
    ELSE (something) {
    }
    }
    }
    SUGGESTION:
    kung PHP ka, i-do while nlang na...or basin na na sa imong platform

  5. #5
    i think ok nko alay erd, d mn jd unta ko mo focus sa php ug mysql kaso pre-req mn pra sa android programing aku gitun-an gd. pde ptabang solve ani?

  6.    Advertisement

Similar Threads

 
  1. HELP.. Need php programmer
    By bohawk in forum Programming
    Replies: 11
    Last Post: 10-21-2011, 12:08 PM
  2. need your help regarding zipping of files using php
    By Saedecode in forum Programming
    Replies: 2
    Last Post: 01-22-2011, 07:09 PM
  3. Replies: 22
    Last Post: 05-05-2006, 06:28 PM
  4. Independent contractor info needed---php and mysql
    By BayouPinoy in forum Networking & Internet
    Replies: 0
    Last Post: 03-17-2006, 10:45 PM
  5. Replies: 2
    Last Post: 06-16-2005, 03:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
about us
We are the first Cebu Online Media.

iSTORYA.NET is Cebu's Biggest, Southern Philippines' Most Active, and the Philippines' Strongest Online Community!
follow us
#top