Note that there are some explanatory texts on larger screens.

plurals
  1. POforeach, JSON & PHP
    text
    copied!<p>I've looked at various answers, and I am assuming it is due to the lack of a foreach loop that I am only receiving one variable. There are 32 categories, with multiple links for each category.</p> <p><strong>Table Structure</strong>:</p> <pre><code>CREATE TABLE `cattb` ( `catNo` int(11) NOT NULL, `catName` varchar(100) NOT NULL, PRIMARY KEY (`catNo`) ) CREATE TABLE `linktb` ( `catId` int(11) NOT NULL, `URL` varchar(255) NOT NULL, `title` varchar(50) NOT NULL, `description` longtext NOT NULL, KEY `catId` (`catId`) ) </code></pre> <p><strong>SQL Query</strong>:</p> <pre><code>SELECT catId, catName, URL, title, description FROM linktb, cattb WHERE linktb.catId=cattb.catNo </code></pre> <p><strong>PHP</strong>:</p> <pre><code>while ($row = mysql_fetch_assoc($result)){ $json['categories'][] = array( "catId"=&gt; $row[catId], "catName"=&gt; $row[catName], 'links' =&gt; array( "URL"=&gt; $row[URL], "title"=&gt; $row[title], "description"=&gt; $row[description] ) ); </code></pre> <p><strong>Response:</strong></p> <pre><code>"categories":[ {"catId":"1", "catName":"Career Resources", "links": {"URL":"http:\/\/www.designanddesign.com\/jobs.php", "title":"Design &amp; Design", "description":"World-Wide Job Offers" } }, {"catId":"1", "catName":"Career Resources", "links": {"URL":"https:\/\/www.nuans.com\/rts-str\/en\/home-accueil_en.cgi", "title":"NUANS Report", "description":"Naming A Company (Federal)" } } </code></pre> <p><strong>What I want to see:</strong></p> <pre><code>{ categories: [ { catName: "Career Resources", catId: "1", links: [ {URL:"http://www.designanddesign.com/jobs.php",title:"Design &amp; Design",description:"World-Wide Job Offers"}, {URL:"https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/hm.html?locale=en_CA",title:"Corporations Canada",description:"Incorporating a Business"}, {URL:"http://lea.verou.me/css3patterns/",title:"CSS3 Patterns",description:"Patterns to use for backgrounds"}, {URL:"http://webdesignerwall.com/tutorials/cross-browser-css-gradient",title:"CSS Gradients",description:"Cross-browser Compatible"} ], }, { catName: "CSS", catId: "2", links: [ {URL:"http://www.designanddesign.com/jobs.php",title:"Design &amp; Design",description:"World-Wide Job Offers"}, {URL:"https://www.ic.gc.ca/app/scr/cc/CorporationsCanada/hm.html?locale=en_CA",title:"Corporations Canada",description:"Incorporating a Business"}, {URL:"http://lea.verou.me/css3patterns/",title:"CSS3 Patterns",description:"Patterns to use for backgrounds"}, {URL:"http://webdesignerwall.com/tutorials/cross-browser-css-gradient",title:"CSS Gradients",description:"Cross-browser Compatible"} ], } </code></pre> <p>How would I write the for-each loop for the arrays? </p>
 

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