Jump to content

Complex Question :/


Chevy

Recommended Posts

Well I am making a PHP music player and I was wondering how would I skip to the next song if the song was NULL or it said "URL Of Song"

 

I have tried this so far:

 


$userstuff = mysql_fetch_array(mysql_query("SELECT * FROM `users` WHERE `username`='$user'"));

if ($song == "song1"){
$next = "song2";
}
if ($userstuff['song2'] == NULL || $userstuff['song2'] == "URL Of Song"){
$next = "song3";
}

 

But that is not right logically

 

Say I start at song1 and song2 is nothing, when I click Next Track I want it to go to song3, if song3 is nothing I want it to go to song4...all the way to ten, if you get what I mean.

Link to comment
Share on other sites

A while loop would probably satisfy your request.

 

$i=0;
$x=true;
while ((($userstuff[$i] == NULL || $userstuff[$i] == "URL Of Song") || ($x))) {
      // do song stuff here
     // if the song is good than exit by setting $x=false;

      // kill the loop after 10 tries. 
      if ($i > 10) {
            $x=false;
      }

      $i++; //(very important)
}

 

 

Link to comment
Share on other sites

It is still looping continesly on me....

 

$i=0;
$x = true;
while ((($userarray['song$i'] == NULL || $userarray['song$i'] == "URL Of Song") || ($x))) {

$next = "song$i";
echo $next;
$x=false;

      if ($i > 10) {
            $x=false;
      }

      $i++; //(very important)
}

Link to comment
Share on other sites

i'd pull all the songs into an array, and use a switch statement.

<?php
        /*get songs stored in database*/
        $sql = "SELECT song_name FROM your_table";
        $query = mysql_query($sql);

        /*pump matches into an array*/
        while($row = mysql_fetch_array($query)){
                $songArray[] = $row['song_name'];
        }

        /*set $_GET['song'] value*/
        ((!isset($_GET['song'])) ? ($_GET['song'] = 0) : (""));

        /*switch through songs*/
        switch($_GET['song']){
                case $_GET['song']:
                        /*refresh the page to skip to next song if empty*/
                        if(empty($songArray[$_GET['song']])){
                                echo "<meta http-equiv=\"refresh\" content=\"0\"\;url=http://www.yoursite.com/?song=". $_GET['song']++ ."\">\n";
                        }else{
                                /*play $songArray[$_GET['song']];*/
                        }
                        break;

                default:
                        break;
        }
?>

Link to comment
Share on other sites

$songs = array("song1", "song2", "song3", "song4", "song5", "song6", "song7", "song8", "song9", "song10");

foreach ($songs as $song){
if ($userarray[$song] != NULL && $userarray[$song] != "URL Of Song"){
     echo $song;
}
}

 

I think that is part of it, now I just need it to grab the first element of the array and the next

 

I tried using current($song) and next($song) but they are not in the arrya form anymore  :-/ this is really confusing to me.

Link to comment
Share on other sites

Hm. I guess I am not familiar with those at all...The code I posted last works fine echoing the songs that have a URL in them, but I want to know how I can get the first one and the next one.

 

there are many ways i'm sure. one way i've already shown you.

Link to comment
Share on other sites

<?php
$songs = array("song1", "song2", "song3", "song4", "song5", "song6", "song7", "song8", "song9", "song10");

foreach ($songs as $key => $song){
if ($song != NULL && $song != "URL Of Song"){
     echo $song;
     break;
}
}
?>

 

 

Link to comment
Share on other sites

I am still having an EXTREAMLY hard time doing this...

 

  Starting to think it is impossible

 

This is what I want it to do

 

Display a link to go to the next song which if the current song was song1 then it would got to song2 unless it did not exist then it would got to song3 unless that didnt exist then it would go to song4, ect all the way to song10 and then die.

Link to comment
Share on other sites

<?php
$currentSong = "song1";
$songs = array("song1", "URL Of Song", "song3", "song4", "song5", "song6", "song7", "song8", "song9", "song10");

$start = false;
foreach ($songs as $key => $song){
  if ($start) {
      if ($song != NULL && $song != "URL Of Song"){
            echo '<a href=link.php?song=' . $song . '>Next Song</a>';
            break;
       }
  }

  if ($song != $currentSong) {
    $start = true;
  }
}

?>

 

Is that what you want?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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

  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.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.