• 0

Numeric Variables in PHP


Question

(sorry for the x-post, I put it in graphic design by accident... Mods can feel free to remove that :))

Ok, so I'm working on seting up Winamp What's Playing add-on. Here's the webpage for the proggy-

http://www.srijith.net/codes/whatsplaying/

About 1/2 way down, it explains how to set up a HTTP GET function for it. Ok, now I know a fair amount of PHP for a teenager, but this just flew over my head. I decided to "defy" the system, and i just set up a php file that would use the variables given in the URL that it makes, and fwrite() them into another file. This didn't work, because the variables in the URL are numeric, and PHP doesn't like $1, $2, or even $3...

So, I have 2 problems here, and solving either one is good enough for me-

Firstly, how am I SUPPOSED to set that up? What would I do with HTTP GET? What IS HTTP GET?

Secondly, is there any way to make PHP "get along" with numeric variables?

TIA, big time

Link to comment
Share on other sites

16 answers to this question

Recommended Posts

  • 0

Oh yeah, I suppose I should add that, yes, I CAN just use ws_ftp and have it autoload everytime my song changes... but that's such a bother, to have it launching every 2-3 minutes... So I'd like to avoid that :) That, and it's slightly a memory hog

Link to comment
Share on other sites

  • 0

Alrite, the Super Global HTTp_GET holds all the items sent through the get method.

So lets say that in the url you can see that it says &1=song&2=artist

or what not, you get the idea, to access that, you would do this.

$song_name=$_GET['1'];

now, $_GET['1'] is the same as saying $HTTP_GET['1']

If you have any questions, pm me and I will try to explain it better.

Or, head to http://php.net and read some of the documentation.

Link to comment
Share on other sites

  • 0

i dont see that it should have a problem with numeric veribles

you could also try changing them to non numeric - yes i know its a workaroud but hey

$val1 = "$1";

btw you getting the $1 etc from a preg_replace or some other REGEX?

Link to comment
Share on other sites

  • 0

Cubano- Thanks, I'll try looking at that a bit afterschool today

Smiffy- I have no clue what you're saying. The program sends out a link where the URL gives me numeric variables. So I can't quite change them, as far as i know.

Link to comment
Share on other sites

  • 0

In addition to Cubano's explanation. I'm using the same tool for my signature. Here's a part of the code I use

$song = ($_GET[1]);
$signature = "Now playing: ".$song;

In winamp use the following link syntax to your prog.

http:/www.yoursite.com/whatsplaying.php?

I'm at work now (don't use winamp here) so I'm not 100% sure about all this :p

Link to comment
Share on other sites

  • 0

Ok, so I can get at the variables, use them, write plenty of things to do w/ em after i've got them... But as far as I can tell, WinAmp will send the URL, the page goes through... and then that's it. From there, viewing the site in IE does nothing. So I need to fwrite it, right? I'm having problems here- Can anyone explain the best way to do this?

Link to comment
Share on other sites

  • 0

Here's some example code, with error checking too. I'd suggest copying it verbatim and just changing what you need. :)

$filename = 'test.txt'; 
$somecontent = "Add this to the file\n"; 

// Let's make sure the file exists and is writable first. 
if (is_writable($filename)) { 

   // In our example we're opening $filename in overwrite mode. 
   // The file pointer is at the beginning of the file hence 
   // that's where $somecontent will go when we fwrite() it. 
   if (!$fp = fopen($filename, 'w')) { 
        print "Cannot open file ($filename)"; 
        exit; 
   } 

   // Write $somecontent to our opened file. 
   if (!fwrite($fp, $somecontent)) { 
       print "Cannot write to file ($filename)"; 
       exit; 
   } 
    
   print "Success, wrote ($somecontent) to file ($filename)"; 
    
   fclose($fp); 
                    
} else { 
   print "The file $filename is not writable"; 
} 
?>

Link to comment
Share on other sites

  • 0

Ok, i got a working php file that will recieve the songs, and fwrite them.

I have a .txt to be fwrite-d to.

I have a php that will read the fwritten txt and get the variables from there.

However, notice that only the first one claimed to be "working".

Only question is- What exactly should I be writing to the txt file? I was thinking a list of "$s1 = song1; $s2 = song2;", so I could put that in the 2nd php, and have them all declared for me. From there, I could have fun with the variables as i wish :). But that's not working too well- Firstly, the variables arn't getting written very correctly (i think I've got it actually, so NM), and 2ndly, I can't get the txt file (which is saved as html, because it supports more ascii than the txt... long story- essentially, i needed to put some $'s in there, just to get it all working, rather than printing variables twice), I can't get the html song list to be read by my 2nd php script. I'll load up the 2nd script, and it won't have any songs listed, meaning the variables arn't in there.

Any other ideas?

Link to comment
Share on other sites

  • 0

Ok, here's LoadSongs.php

<?php
$p = ($_GET[p]);
$s1 = ($_GET[1]);
$s2 = ($_GET[2]);
$s3 = ($_GET[3]);
$s4 = ($_GET[4]);
$s5 = ($_GET[5]);
$s6 = ($_GET[6]);
$s7 = ($_GET[7]);
$s8 = ($_GET[8]);
$s9 = ($_GET[9]);
$s10 = ($_GET[10]);

$file = fopen('songs.html', w);

fwrite($file,"<?php");
fwrite($file," $p = ".$p.";");
fwrite($file," $s1 = ".$s1.";");
fwrite($file," $s2 = ".$s2.";");
fwrite($file," $s3 = ".$s3.";");
fwrite($file," $s4 = ".$s4.";");
fwrite($file," $s5 = ".$s5.";");
fwrite($file," $s6 = ".$s6.";");
fwrite($file," $s7 = ".$s7.";");
fwrite($file," $s8 = ".$s8.";");
fwrite($file," $s9 = ".$s9.";");
fwrite($file," $s10 = ".$s10.";");
fwrite($file," ?>");
?>

That gets the songs, and writes them to my HTML file. I'm trying to save them in format "$s1 = song1name", so I can load that directly into NowPlaying.php, which is here-

<?php
$filename = "songs.html";
$handle = fopen ($filename, "r"); 
$contents = fread ($handle, 10000);

	if($p == 1){
 ?$out1 = "Now Playing- <b>".$s1."</b>";
 ?$out2 = "<i>Previously Played-</i>";}

	else{
 ?$out1 = "<b>No songs playing right now</b>";
 ?$out2 = "<i>Last 10 songs played-</i><br>";}

echo $out1."<br>".$out2;

	if($p == 0){
 ?$y = 1;
 ?echo "1.".$s1."<br>";}

	else{
 ?$y=0;}

$y = ($y+1); echo $y.". ".$s2."<br>";
$y = ($y+1); echo $y.". ".$s3."<br>";
$y = ($y+1); echo $y.". ".$s4."<br>";
$y = ($y+1); echo $y.". ".$s5."<br>";
$y = ($y+1); echo $y.". ".$s6."<br>";
$y = ($y+1); echo $y.". ".$s7."<br>";
$y = ($y+1); echo $y.". ".$s8."<br>";
$y = ($y+1); echo $y.". ".$s9."<br>";
$y = ($y+1); echo $y.". ".$s10;
?>

I realize that it's sloppy coding, but i'd like to avoid the use of Arrays, because i don't fully understand them. Also, I want to be able to (in the end) just delete the entire Echo part, and have a script that just comes up with variables. This way, I can have a php snippet on my main page, or anywhere else that i can use, without just printing out the 10 songs. Like, have a small file that converts it to a PNG, or whatever.

Thanks alot for all the help, good learning experience :)r me :)

Link to comment
Share on other sites

  • 0

You read the html file into $contents but don't actually do anything with the content.

You can write all vars to a (php) file and just include() it in another php file (as you already mentioned yourself :p)

Link to comment
Share on other sites

  • 0

Gravy, Ok, working for the most part- it loads up the PHP file just fine... New problem time :)

Under my old code, it would literally write ; to the php file, and everything, it wouldn't give the symbols like I had wanted.

So here's my new loadsongs.php code-

$file = fopen('songs.php', w);
fwrite($file,"<?php
");
fwrite($file," $p = ".$p.";
");
fwrite($file," $s1 = ".$s1.";
");
fwrite($file," $s2 = ".$s2.";
");
fwrite($file," $s3 = ".$s3.";
");
fwrite($file," $s4 = ".$s4.";
");
fwrite($file," $s5 = ".$s5.";
");
fwrite($file," $s6 = ".$s6.";
");
fwrite($file," $s7 = ".$s7.";
");
fwrite($file," $s8 = ".$s8.";
");
fwrite($file," $s9 = ".$s9.";
");
fwrite($file," $s10 = ".$s10.";
");
fwrite($file," ?>");

What this does is outputs "song_1_name = song_1_name".

So how can I get it to give me a $s1 = song_1_name? Using the $ outputs "$", and using "$s1" outputs song one, even inside quotes.... Thanks alot for your patience :)

Link to comment
Share on other sites

  • 0

here's a cleaned up version:

<?php
$file = fopen( 'song_data.php', 'w' );

fwrite( $file, "<?php\n" );

fwrite( $file, '$playing = ' . $_GET['p'] . ";\n" );
for( $i = 1; $i < 11; $i++)
  fwrite( $file, " \$songs[$i] = ".$_GET[$i].";\n" );

fwrite( $file, "?>" );
fclose( $file );
?>

This one is slightly different. You'll have an $songs array for easier handling of data (plus, it allows you to increase or decrease the ammount of songs without too much code fiddling), and the $playing boolean.

Then just include like so:

<?php
include( './songs_data.php' );

if( $playing == 1 )
{
 ?print "Now Playing- <b>".$s[1]."</b><br>\n";
 ?print "<i>Previously Played-</i>\n";
}
else
{
? print "<b>No songs playing right now</b><br>\n";
? print "<i>Last 10 songs played-</i><br>\n";
}

if( $playing == 0 )
 ?$counter = 1;
else
 ?$counter = 2;


for(; $counter < 11; $counter++ )
  print "$counter. {$songs[$counter]}<br >\n";
?>

The only thing is this stuff is untested by me, so no guarantees that's going to work...But it's worth a:)hot :)

Link to comment
Share on other sites

  • 0

Major Kudos to Timdorr and Redmak! :)

One more problem to overcome- Timdorr, your script rocked.... Quite a few problems (look at the filename difference between the 2 snippets :)) but I manages to squash 'em.

Here it is, the last problem-

Parse error: parse error, unexpected T_STRING in /home/hazard/www/suphix57/music/song_data.php on line 3

Ok, looking at my song_data.php-

<?php
 $playing = 1;
 $songs[1] = Vertical Horizon - Gray Sky Morning;
 $songs[2] = Vertical Horizon - Fast Car/Wash Away (live);
 $songs[3] = Vertical Horizon - Miracle;
 $songs[4] = Vertical Horizon - Your A God;
 $songs[5] = Vertical Horizon - Everything You Want;
 $songs[6] = Vertical Horizon - Gray Sky Morning;
 $songs[7] = Vertical Horizon - Everything You Want;
 $songs[8] = Vertical Horizon - Fast Car/Wash Away (live);
 $songs[9] = Vertical Horizon - Finding Me;
 $songs[10] = Vertical Horizon - Your A God;
?>

I'll assume that the problem here is that the song title is not in quotes, so it's trying to subtract Gray Sky Morning from Vertical Horizon... and is having some understandable issues :).

(Yeah, I love the band, their old, but they still rock, K? :))

So, I've goofed around a bit in the loadsongs.php, and can't figure out how to change that... silly arrays :) Any ideas?

Link to comment
Share on other sites

  • 0

Well, here's a fiX0red version:

<?php
$file = fopen( 'songs_data.php', 'w' );
fwrite( $file, "<?php\n" );
fwrite( $file, '$playing = ' . $_GET['p'] . ";\n" );
for( $i = 1; $i < 11; $i++)
?fwrite( $file, " \$songs[$i] = '".$_GET[$i]."';\n" );
fwrite( $file, "?>" );
fclose( $file );
?>

Now, which script is which? I assume the first is like savesongs.php and the 2nd is loadsongs.php?

And what do you mean by that last question?

Glad I'm helping though:))

Link to comment
Share on other sites

  • 0

Uhmm... what are you asking? lol... My last question was just saying that i need to get the song names into quotes, but your script worked! It's running now! Thanks!

(ok, so it didn't TOTALLY work, but I kinked em out :))

Here's the final 2 snippets of code, for anyone else setting these up (i'll zip em, and post them on my site in a bit)

nowplaying.php

<?php
include( './song_data.php' );

if( $playing == 1 )
{
print "Now Playing- <b>".$songs[1]."</b><br>\n";
print "<i>Previously Played-</i><br>\n";
}
else
{
print "<b>No songs playing right now</b><br>\n";
print "<i>Last 10 songs played-</i><br>\n";
}

if( $playing == 0 )
$counter = 1;
else
$counter = 2;


for(; $counter < 11; $counter++ )
print "$counter. {$songs[$counter]}<br >\n";
?>

loadsongs.php

<?php
$file = fopen( 'song_data.php', 'w' );
fwrite( $file, "<?php\n" );
fwrite( $file, '$playing = ' . $_GET['p'] . ";\n" );
for( $i = 1; $i < 11; $i++)
fwrite( $file, " \$songs[$i] = '".$_GET[$i]."';\n" );
fwrite( $file, "?>" );
fclose( $file );
?>

Oh yeah, and incase you care- http://suphix57.xlprofile.com/music/nowplaying.php

Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.