ziedinjsh Posted January 12, 2012 Report Share Posted January 12, 2012 sveiki! Kaut kā interesanti sanāca un nesaprotu kas pa vainu! lietoju html5 music player playlist viņš izmanto js failu myplaylist.js var myPlaylist = [ { mp3:'mp3/1.mp3', title:'Sample', artist:'Sample', rating:5, duration:'0:30', cover:'mix/1.png' } ]; tas man taka neder, jo sarakstu lasa no mysql. Izveidoju šādi playlist.js.php <?php include("misc/dbase.php"); $songs = mysql_query("select * from songs order by id desc") or die(mysql_error()); echo "var myPlaylist = ["; while($data = mysql_fetch_array($songs)){ echo " { mp3:'mp3/".$data['songs']."', title:'".$data['title']."', artist:'".$data['artist']."', rating:0, duration:'".$data['duration']."', cover:'cover/".$data['cover']."' }, "; } echo "];"; ?> it kā viss labi.. strādāja līdz šim brīdim kad viss vnk pazuda un viss! kapēc tā? pievienojot atpakaļ myplaylist.js tad rādās playeris, bet kad pievienoju playlist.js.php nerādās! Quote Link to comment Share on other sites More sharing options...
Lauris Posted January 12, 2012 Report Share Posted January 12, 2012 Nāksies vien pastudēt par JSON un AJAX. Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 12, 2012 Author Report Share Posted January 12, 2012 savādāk tas nav iespējams?? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 12, 2012 Report Share Posted January 12, 2012 Tava cikla gadījumā beigās sanāk lieks komats, kas parasti ir cēlonis neejošam JS. Es personīgi ieteiktu visu salikt masīvā, un izvadīt pēc tam ar json_encode($masiivs); citādi tavā gadījumā, ja tekstā būs kāds neatļauts simbols, tavs JSON saturs tiks salauzts. Skatīt šeit: http://php.net/manual/en/function.json-encode.php Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 12, 2012 Author Report Share Posted January 12, 2012 tas laikam domāts ir šādi: $song = mysql_query("select * from songs order by id desc") or die(mysql_error()); while($data = mysql_fetch_array($song)){ $list = array( "mp3" => "mp3/".$data['song'], "title" => $data['title'], "artist" => $data['artist'], "rating" => "0", "duration" => $data['duration'], "cover" => "cover/".$data['cover'] ); } ?? kāds tad izskatīšies jquery? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 12, 2012 Report Share Posted January 12, 2012 $list = array(); while($data = mysql_fetch_array($song)){ $list[] = array( "mp3" => "mp3/".$data['song'], "title" => $data['title'], "artist" => $data['artist'], "rating" => "0", "duration" => $data['duration'], "cover" => "cover/".$data['cover'] ); } echo json_encode($list); Kā tas izskatīsies? Atver takš source un paskaties! Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 12, 2012 Author Report Share Posted January 12, 2012 var myPlaylist = [ { mp3:'mp3/1.mp3', title:'Sample', artist:'Sample', rating:5, duration:'0:30', cover:'mix/1.png' } ]; šo? kā tad apvienšu to ar to ko man dod array? Quote Link to comment Share on other sites More sharing options...
briedis Posted January 12, 2012 Report Share Posted January 12, 2012 var myPlaylist = <?=json_encode($masivs);?>; Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 13, 2012 Author Report Share Posted January 13, 2012 nesaprotu.. ir fails playlist.php kur tiek izveidots array un ir fails no kura lasa playeris informāciju.. kā viņi sasaistās, ja playeris lasa tikai no .js faila bet viņā netiek inclūdots php fails kur ir tā informācija! Quote Link to comment Share on other sites More sharing options...
daGrevis Posted January 13, 2012 Report Share Posted January 13, 2012 Dari kā Lauris teica. Iekš plejlist faila ir AJAX pieprasījums uz serveri ($.get() iz jQuery). Serveris atbild ar JSON datiem (json_encode() iz PHP). Klient-pusē iegūtos datus izmantojam kā, tavā gadījumā, myPlaylist. Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 13, 2012 Author Report Share Posted January 13, 2012 (edited) tad tas sanāk šādi? $.get('playlist.php', function(data){ var myPlaylist = jQuery.parseJSON(data); }); Edited January 13, 2012 by ziedinjsh Quote Link to comment Share on other sites More sharing options...
daGrevis Posted January 13, 2012 Report Share Posted January 13, 2012 Apmēram, jā. Tagad tikai sataisi servera pusē visu un vajadzētu iet! *woot* P.S. Tu vari padot trešo parametru $.get() kā stringu ar saturu 'json'. Tad tev nebūs pašam pāparsē JSON's. Quote Link to comment Share on other sites More sharing options...
ziedinjsh Posted January 13, 2012 Author Report Share Posted January 13, 2012 hmm.. pačekoju playlist.php ar echo json_encode($list);pārlūkā man parāda tikai [] un viss lai gan datubāzē man ir informācija Quote Link to comment Share on other sites More sharing options...
briedis Posted January 13, 2012 Report Share Posted January 13, 2012 tad tas sanāk šādi? $.get('playlist.php', function(data){ var myPlaylist = jQuery.parseJSON(data); }); A šitā var, a šitā nevar? Nu bitītmatos, tak IZMĒĢINI! Quote Link to comment Share on other sites More sharing options...
daGrevis Posted January 13, 2012 Report Share Posted January 13, 2012 > hmm.. pačekoju playlist.php ar echo json_encode($list);pārlūkā man parāda tikai [] un viss lai gan datubāzē man ir informācija Ja listā tev ir masīvs no datubāzes, tad problēma ir PEBKAC. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.