9 Replies - 4248 Views - Last Post: 08 February 2012 - 08:04 PM Rate Topic: -----

#1 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Code not looping correctly

Posted 08 February 2012 - 10:06 AM

I am using jwplayer to play music that has been uploaded to my server. The code is not looping to pull all of the music correctly, as of now it only pulls the most recent song that has been uploaded. Any ideas on what I am doing wrong?


   else {
   while($row = mysql_fetch_array($result)) {
      $artistname = $row['artistname'];
      $songname = $row['songname'];
      $songid = $row['songid'];            

?>

<br />

                                                           
<p><?php echo "$artistname"; ?></p>

<p><?php echo "$songname"; ?></p>

<script type='text/javascript' src='jwplayer.js'></script>
 
<div id='mediaspace1'>This text will be replaced</div>
 
<script type='text/javascript'>
  jwplayer('mediaspace1').setup({
    'flashplayer': 'player.swf',
    'file': 'uploads/songs/<?php echo "$songid"; ?>',
    'controlbar': 'bottom',
    'width': '350',
    'height': '24'
  });
</script>




Is This A Good Question/Topic? 0
  • +

Replies To: Code not looping correctly

#2 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Code not looping correctly

Posted 08 February 2012 - 10:42 AM

That's because you're loading the same script and function call over and over again. Concentrate on organizing your code properly and you won't have these problems.
Was This Post Helpful? 0
  • +
  • -

#3 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Code not looping correctly

Posted 08 February 2012 - 10:53 AM

How would I make it load the next available song from the server? Sorry to ask but I'm just unsure
Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Code not looping correctly

Posted 08 February 2012 - 10:59 AM

You're unsure because you're thinking that this code all executes in the same place. What's really happening is that PHP sends the Javascript to the browser several times and the browser executes each of the function calls one at a time. You just see the last result.
Was This Post Helpful? 1
  • +
  • -

#5 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Code not looping correctly

Posted 08 February 2012 - 12:39 PM

So would I want to create another loop along with the loop pulling the info from the database?
Was This Post Helpful? 0
  • +
  • -

#6 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: Code not looping correctly

Posted 08 February 2012 - 12:56 PM

From the looks of things:
<script type='text/javascript'>
  jwplayer('mediaspace1').setup({
    'flashplayer': 'player.swf',
    'file': 'uploads/songs/<?php echo "$songid"; ?>',
    'controlbar': 'bottom',
    'width': '350',
    'height': '24'
  });
</script>



You are outputting that multiple times on the page, you have multiple divs with the id "mediaspace1", it's not unusual to see Javascript act unpredictably when you have multiple elements with the same id and then you try to reference an element by id. Id's should be unique.
Was This Post Helpful? 0
  • +
  • -

#7 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Code not looping correctly

Posted 08 February 2012 - 01:39 PM

That div is calling the function correct? Im confused, Im sorry
Was This Post Helpful? 0
  • +
  • -

#8 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Code not looping correctly

Posted 08 February 2012 - 02:38 PM

A div is just a divider. It calls nothing. Try this script:
<?php
echo "<script type='text/javascript'>var num = 0;</script>";
for($i = 0; $i < 4; $i++) {
	echo "<script type='text/javascript'>num++;alert('Hi '+num);</script>";
}
?>

What happens?
Why?
Was This Post Helpful? 0
  • +
  • -

#9 dbeaird727  Icon User is offline

  • D.I.C Head

Reputation: -2
  • View blog
  • Posts: 238
  • Joined: 13-December 10

Re: Code not looping correctly

Posted 08 February 2012 - 07:20 PM

Okay youre basically cancelling out each function because you have two scripts doing something similar.. I understand but the function in my code pulls the player and the song and then places it inside the div with the correct ID "<div id='mediaspace'>media</div>"
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2896
  • View blog
  • Posts: 10,031
  • Joined: 08-August 08

Re: Code not looping correctly

Posted 08 February 2012 - 08:04 PM

Yes. It does that over and over again until the last time. Then it stops, leaving only the last loaded version in the div.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1