Last active
December 23, 2015 14:29
-
-
Save aurelkurtula/6648857 to your computer and use it in GitHub Desktop.
getting content from database and adding it into a multidimensional array
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
App = Ember.Application.create(); | |
App.IndexRoute = Ember.Route.extend({ | |
model: function() { | |
return App.Item.all(); //get the item object and it all() function | |
} | |
}); | |
App.Item = Ember.Object.extend(); | |
App.Item.reopenClass({ | |
all: function() { | |
var links = []; | |
$.getJSON("file.json").then(function(response) { | |
response.post.forEach(function (child) { | |
links.pushObject(App.Item.create(child)); | |
}); | |
}); | |
return links; | |
} | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"post": [ | |
{ | |
"title": "smathing magazine", | |
"description": "some description", | |
"url": "http://smashingmag.com" | |
}, | |
{ | |
"title": "Boagworld", | |
"description": "some one", | |
"url": "http://boagworld" | |
}, | |
{ | |
"title": "Css tricks", | |
"description": "chris coyer", | |
"url": "http://css-tricks.com" | |
} | |
] | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
$info=array(); | |
while($row = mysqli_fetch_array($result)) | |
{ | |
$info['post'][]=$row; | |
} | |
// then adding it to a json file | |
$posts = array('post' => $posts); | |
$fp = fopen('file.json', 'w'); | |
fwrite($fp, json_encode($posts)); | |
fclose($fp); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<script type="text/x-handlebars" data-template-name="index"> | |
<h2>Welcome to Ember.js test</h2> | |
<ul> | |
{{#each link in model}} | |
<li> | |
<a {{bindAttr href="link.url"}} target="_blank"> {{link.title}} </a> | |
<a {{bindAttr href="link.delete"}}> (delete) </a> | |
</li> | |
{{/each}} | |
</ul> | |
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!-- reading the json file and sorting into a multidimentional array --> | |
$string = file_get_contents('http://localhost/2013new/emberforbeg/my/test.json'); | |
$json_articles=json_decode($string,true); | |
$json_entries = count($json_articles['post']); | |
foreach ($json_articles as $key => $value){ | |
$posts = array(); | |
foreach ($value as $key => $values) { | |
array_push($posts, | |
array( | |
'title' => $values['title'], | |
'description' => $values['description'], | |
'url' => $values["url"] | |
) | |
); | |
}; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment