PDA

View Full Version : Adding two values of two different arrays

GothyStitch
02-08-2008, 02:23 PM
I'm attempting to add together two values of two different arrays.

the arrays are handled as strings.

\$dlist = \$text2;
\$blist = \$text;

\$text2 holds the value of 13 but is viewed as a string.
\$text holds the value of 22 but is viewed as a string.

so I try to use settype(); to convert the values to integers so that I can perform mathmatical equations with the values, but first checking to make sure that the values are indeed considered strings. And then trying to perform a simple addition with the two variables.

if (is_string(\$dlist)) settype(\$dlist, "int");
if (is_string(\$blist)) settype(\$blist, "int");
\$newval = \$blist + \$dlist;

echo "blist equals \$blist";
echo "dlist equals \$dlist";
echo "newval equals \$newval";

Now \$newval should have the value of 35, but instead \$newval always ends up having a value of 0.

GothyStitch
02-08-2008, 04:24 PM
here's the full script I have at the moment with comments.

<?
// Opens http://ss3.myshoutcastserver.com:8142/7.html

// \$text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// \$text = 1 = number of listeners connected
// \$text = 1 = stream state 0 = down, 1 = active
// \$text = 14 = Highest peak of listeners
// \$text = 50 = max number of listeners
// \$text = 1 = Unique number of listeners
// \$text = 128 = stream bitrate
// \$text = Sepultura - Amen = song title

\$open = fsockopen("66.90.104.128","8142");
if (\$open) {
fputs(\$open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text = explode(",",\$text);
} else { \$er="Connection Refused!"; }

// Dialup stream
// Opens http://ss3.myshoutcastserver.com:8140/7.html

// \$text2[#] is an array containing: 0,1,14,75,0,24,Sepultura - Amen
// \$text2 = 0 = number of listeners connected
// \$text2 = 1 = stream state 0 = down, 1 = active
// \$text2 = 14 = Highest peak of listeners
// \$text2 = 75 = max number of listeners
// \$text2 = 0 = Unique number of listeners
// \$text2 = 24 = stream bitrate
// \$text2 = Sepultura - Amen = song title

\$open2 = fsockopen("66.90.104.128","8140");
if (\$open2) {
fputs(\$open2,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text2 = explode(",",\$text2);
} else { \$er2="Connection Refused!"; }

if (\$text==1) { \$state = "Active"; } else { \$state = "Down"; } // sets the state of the broadband stream
if (\$text2==1) { \$state2 = "Active"; } else { \$state2 = "Down"; } // sets the state of the dialup stream

\$dlist = \$text2;
\$blist = \$text;
\$dlist2 = intval(\$dlist, 10);
\$blist2 = intval(\$blist, 10);
\$tlist = (\$dlist + \$blist);

// Song info and total listeners
echo "<div id='r1'>DJ Name Goes Here (coming soon)</div>"; // DJ's Name - Need to find a way to set this
echo "<div id='r2'>\$text</div>"; // Song title
echo "<div id='r3'>\$dlist2 \$blist2 \$tlist/125</div>"; // Total listeners

if (\$er) { echo "<div id='r4'>\$er</div>"; } else { echo "<div id='r4'>\$state</div>"; } // Stream state
echo "<div id='r5'>\$text/\$text</div>"; // Listeners
echo "<div id='r6'><a href='http://ss3.myshoutcastserver.com:8142'>128kbps</a>, audio/mpeg</div>"; // URL for Stream

// Dialup stats
if (\$er2) { echo "<div id='r7'>\$er2</div>"; } else { echo "<div id='r7'>\$state2</div>"; } // Stream state
echo "<div id='r8'>\$text2/\$text2</div>"; // Listeners
echo "<div id='r9'><a href='http://ss3.myshoutcastserver.com:8140'>24kbps</a>, audio/mpeg</div>"; // URL for stream
?>

GothyStitch
02-08-2008, 04:27 PM
<?
// Opens http://ss3.myshoutcastserver.com:8142/7.html

// \$text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// \$text = 1 = number of listeners connected
// \$text = 1 = stream state 0 = down, 1 = active
// \$text = 14 = Highest peak of listeners
// \$text = 50 = max number of listeners
// \$text = 1 = Unique number of listeners
// \$text = 128 = stream bitrate
// \$text = Sepultura - Amen = song title

\$open = fsockopen("66.90.104.128","8142");
if (\$open) {
fputs(\$open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text = explode(",",\$text);
} else { \$er="Connection Refused!"; }

// Dialup stream
// Opens http://ss3.myshoutcastserver.com:8140/7.html

// \$text2[#] is an array containing: 0,1,14,75,0,24,Sepultura - Amen
// \$text2 = 0 = number of listeners connected
// \$text2 = 1 = stream state 0 = down, 1 = active
// \$text2 = 14 = Highest peak of listeners
// \$text2 = 75 = max number of listeners
// \$text2 = 0 = Unique number of listeners
// \$text2 = 24 = stream bitrate
// \$text2 = Sepultura - Amen = song title

\$open2 = fsockopen("66.90.104.128","8140");
if (\$open2) {
fputs(\$open2,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text2 = explode(",",\$text2);
} else { \$er2="Connection Refused!"; }

if (\$text==1) { \$state = "Active"; } else { \$state = "Down"; } // sets the state of the broadband stream
if (\$text2==1) { \$state2 = "Active"; } else { \$state2 = "Down"; } // sets the state of the dialup stream

Here's the part that gives me problems, and I think it's due to variable type, but I don't really know.

\$dlist = \$text2;
\$blist = \$text;
\$dlist2 = intval(\$dlist, 10);
\$blist2 = intval(\$blist, 10);
\$tlist = (\$dlist + \$blist);

Then things work fine:

// Song info and total listeners
echo "<div id='r1'>DJ Name Goes Here (coming soon)</div>"; // DJ's Name - Need to find a way to set this
echo "<div id='r2'>\$text</div>"; // Song title

And then right here is where it displays all the wrong info:

echo "<div id='r3'>\$dlist2 \$blist2 \$tlist/125</div>"; // Total listeners

Then the rest is fine:

if (\$er) { echo "<div id='r4'>\$er</div>"; } else { echo "<div id='r4'>\$state</div>"; } // Stream state
echo "<div id='r5'>\$text/\$text</div>"; // Listeners
echo "<div id='r6'><a href='http://ss3.myshoutcastserver.com:8142'>128kbps</a>, audio/mpeg</div>"; // URL for Stream

// Dialup stats
if (\$er2) { echo "<div id='r7'>\$er2</div>"; } else { echo "<div id='r7'>\$state2</div>"; } // Stream state
echo "<div id='r8'>\$text2/\$text2</div>"; // Listeners
echo "<div id='r9'><a href='http://ss3.myshoutcastserver.com:8140'>24kbps</a>, audio/mpeg</div>"; // URL for stream
?>

GothyStitch
02-08-2008, 06:05 PM
Ok, I got it working

here's what I got:

<?
// Opens http://ss3.myshoutcastserver.com:8142/7.html

// \$text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// \$text = 1 = number of listeners connected
// \$text = 1 = stream state 0 = down, 1 = active
// \$text = 14 = Highest peak of listeners
// \$text = 50 = max number of listeners
// \$text = 1 = Unique number of listeners
// \$text = 128 = stream bitrate
// \$text = Sepultura - Amen = song title

\$open = fsockopen("66.90.104.128","8142");
if (\$open) {
fputs(\$open,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text = explode(",",\$text);
} else { \$er="Connection Refused!"; }

\$blist = \$text;
\$bstate = \$text;
\$bpeak = \$text;
\$bmax = \$text;
\$buni = \$text;
\$bbit = \$text;
\$bsong = \$text;

// Dialup stream
// Opens http://ss3.myshoutcastserver.com:8140/7.html

// \$text2[#] is an array containing: 0,1,14,75,0,24,Sepultura - Amen
// \$text2 = 0 = number of listeners connected
// \$text2 = 1 = stream state 0 = down, 1 = active
// \$text2 = 14 = Highest peak of listeners
// \$text2 = 75 = max number of listeners
// \$text2 = 0 = Unique number of listeners
// \$text2 = 24 = stream bitrate
// \$text2 = Sepultura - Amen = song title

\$open2 = fsockopen("66.90.104.128","8140");
if (\$open2) {
fputs(\$open2,"GET /7.html HTTP/1.1\nUser-Agent:Mozilla\n\n");
\$text2 = explode(",",\$text2);
} else { \$er2="Connection Refused!"; }

\$dlist = \$text2;
\$dstate = \$text2;
\$dpeak = \$text2;
\$dmax = \$text2;
\$duni = \$text2;
\$dbit = \$text2;
\$dsong = \$text2;

if (\$bstate==1) { \$state = "Active"; } else { \$state = "Down"; } // sets the state of the broadband stream
if (\$dstate==1) { \$state2 = "Active"; } else { \$state2 = "Down"; } // sets the state of the dialup stream

\$dlist = trim(\$text2);
\$blist = trim(\$text);
\$dlist = \$dlist{strlen(\$dlist)-1}; // this makes it work, not sure why
\$blist = \$blist{strlen(\$blist)-1}; // but if it works, why complain?
\$tlist = (\$dlist + \$blist);

// Song info and total listeners
echo "<div id='r1'>DJ Name Goes Here (coming soon)</div>"; // DJ's Name - Need to find a way to set this
echo "<div id='r2'>\$bsong</div>"; // Song title
echo "<div id='r3'>\$tlist</div>"; // Total listeners

if (\$er) { echo "<div id='r4'>\$er</div>"; } else { echo "<div id='r4'>\$state</div>"; } // Stream state
echo "<div id='r5'>\$blist/\$bmax</div>"; // Listeners
echo "<div id='r6'><a href='http://ss3.myshoutcastserver.com:8142'>128kbps</a>, audio/mpeg</div>"; // URL for Stream

// Dialup stats
if (\$er2) { echo "<div id='r7'>\$er2</div>"; } else { echo "<div id='r7'>\$state2</div>"; } // Stream state
echo "<div id='r8'>\$dlist/\$dmax</div>"; // Listeners
echo "<div id='r9'><a href='http://ss3.myshoutcastserver.com:8140'>24kbps</a>, audio/mpeg</div>"; // URL for stream
?>

Not sure why doing it that way worked and the other way didn't, but I'm not going to complain if it's working!