Jump to content
php.lv forumi

myplaylist.js


ziedinjsh

Recommended Posts

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!

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

$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!

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

> 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.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...