Php Scandir to Mysql Which is Good Practice? -
i have file structure below.
ali --543 --01.jpg --02.jpg --544 --545 veli --002
like have 50 main folder , every main folder has 500 chapter folder. every chapter folder has 30 images in it. gonna save image paths , folder names mysql. , saved data pull out json data using wtih angularjs. method keep server less busy?
table a: every image path takes 1 cell.
name folder path ali 787 01.jpg ali 787 02.jpg ali 787 03.jpg ali 787 04.jpg . . . . . . . . . ali 788 01.jpg . . . . . . veli 332 01.jpg veli 332 02.jpg veli 332 03.jpg
so have thousands of images dont think above method not practice.
table b: table in every chapter folder's images take 1 cell. said not practice. 1 should prefer? or other suggustions?
name folder path ali 787 "01.jpg","02.jpg","03.jpg","04.jpg","05.jpg","06.jpg.......... veli 332 "01.jpg"................... veli 333 "01.jpg","02.jpg"...........
wanted json output: , how can recieve json output below:
[ { "name":"ali", "random": [ { "folder": "787", "path": ["1.jpg", "2.jpg", "3.jpg"] }, { "folder": "788", "path": ["1.jpg", "2.jpg", "3.jpg"] } ] }, { "name":"veli", "random": [ { "folder": "332", "path": ["1.jpg", "2.jpg", "3.jpg"] }, { "folder": "333", "path": ["1.jpg", "2.jpg", "3.jpg"] } ] } ]
edit-1:
ok gonna use option b. take php code of mine getting desired output.
$sqlx = 'select name,folder,path series'; $result = $dbx->query($sqlx); $narray = array(); while($r = mysqli_fetch_assoc($result)) { $narray[$r["name"]][] = array('folder' => $r['folder'], 'path' => explode(",", $r['path'])); } print json_encode($narray);
if absolutely have no need query database individual images, , ever intend store strings intend replicate in json data , else, there's no harm in storing folder aggregations in single column, in approach b. underlying reasons practice of not storing aggregate data in columns not apply use case. might store ready json outputs there... (see comment below.)
however if foresee possibility of wanting example find folder(s) have image111.jpg
etc., whether in searching duplicates, trying locate parent folder of individual image, etc. should adopt approach a -- instead of trying use find_in_set
, or more desperate like '%image.jpg%'
, in figuring out image resides in folder.
if want best of both worlds, insert file records per a, , have "cache" table per b call json. should make both pragmatist , idealist satisfied.
as far desired json output, can generate json data php's json_encode(). feed in array in format matches desired json structure. can "reverse-engineer" json data json_decode(). fed in desired json target output decode, , result json_encode()
of following array:
array ( 0 => array ( 'name' => 'ali', 'random' => array ( 0 => array ( 'folder' => '787', 'path' => array ( 0 => '1.jpg', 1 => '2.jpg', 2 => '3.jpg', ), ), 1 => array ( 'folder' => '788', 'path' => array ( 0 => '1.jpg', 1 => '2.jpg', 2 => '3.jpg', ), ), ), ), 1 => array ( 'name' => 'veli', 'random' => array ( 0 => array ( 'folder' => '332', 'path' => array ( 0 => '1.jpg', 1 => '2.jpg', 2 => '3.jpg', ), ), 1 => array ( 'folder' => '333', 'path' => array ( 0 => '1.jpg', 1 => '2.jpg', 2 => '3.jpg', ), ), ), ), )
result of $json_array = json_decode($your_json_string); echo json_encode($json_array)
below. can use test arrays. same data, less whitespace. (i've added few line breaks readability; default it's single chunk of line works same.)
[ {"name":"ali","random":[ {"folder":"787","path":["1.jpg","2.jpg","3.jpg"]}, {"folder":"788","path":["1.jpg","2.jpg","3.jpg"]} ]}, {"name":"veli","random":[ {"folder":"332","path":["1.jpg","2.jpg","3.jpg"]}, {"folder":"333","path":["1.jpg","2.jpg","3.jpg"]} ]} ]
Comments
Post a Comment