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[0];
$blist = $text[0];


$text2[0] holds the value of 13 but is viewed as a string.
$text[0] 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.


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

// $text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// $text[0] = 1 = number of listeners connected
// $text[1] = 1 = stream state 0 = down, 1 = active
// $text[2] = 14 = Highest peak of listeners
// $text[3] = 50 = max number of listeners
// $text[4] = 1 = Unique number of listeners
// $text[5] = 128 = stream bitrate
// $text[6] = 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");
$read = fread($open,1000);
$text = explode("content-type:text/html",$read);
$text = explode(",",$text[1]);
} 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] = 0 = number of listeners connected
// $text2[1] = 1 = stream state 0 = down, 1 = active
// $text2[2] = 14 = Highest peak of listeners
// $text2[3] = 75 = max number of listeners
// $text2[4] = 0 = Unique number of listeners
// $text2[5] = 24 = stream bitrate
// $text2[6] = 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");
$read2 = fread($open2,1000);
$text2 = explode("content-type:text/html",$read2);
$text2 = explode(",",$text2[1]);
} else { $er2="Connection Refused!"; }

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

$dlist = $text2[0];
$blist = $text[0];
$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[6]</div>"; // Song title
echo "<div id='r3'>$dlist2 $blist2 $tlist/125</div>"; // Total listeners

// Broadband stats
if ($er) { echo "<div id='r4'>$er</div>"; } else { echo "<div id='r4'>$state</div>"; } // Stream state
echo "<div id='r5'>$text[0]/$text[3]</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[0]/$text2[3]</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
<?
// Broadband stream
// Opens http://ss3.myshoutcastserver.com:8142/7.html

// $text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// $text[0] = 1 = number of listeners connected
// $text[1] = 1 = stream state 0 = down, 1 = active
// $text[2] = 14 = Highest peak of listeners
// $text[3] = 50 = max number of listeners
// $text[4] = 1 = Unique number of listeners
// $text[5] = 128 = stream bitrate
// $text[6] = 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");
$read = fread($open,1000);
$text = explode("content-type:text/html",$read);
$text = explode(",",$text[1]);
} 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] = 0 = number of listeners connected
// $text2[1] = 1 = stream state 0 = down, 1 = active
// $text2[2] = 14 = Highest peak of listeners
// $text2[3] = 75 = max number of listeners
// $text2[4] = 0 = Unique number of listeners
// $text2[5] = 24 = stream bitrate
// $text2[6] = 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");
$read2 = fread($open2,1000);
$text2 = explode("content-type:text/html",$read2);
$text2 = explode(",",$text2[1]);
} else { $er2="Connection Refused!"; }

if ($text[1]==1) { $state = "Active"; } else { $state = "Down"; } // sets the state of the broadband stream
if ($text2[1]==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[0];
$blist = $text[0];
$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[6]</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:


// Broadband stats
if ($er) { echo "<div id='r4'>$er</div>"; } else { echo "<div id='r4'>$state</div>"; } // Stream state
echo "<div id='r5'>$text[0]/$text[3]</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[0]/$text2[3]</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:


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

// $text[#] is an array containing: 1,1,14,50,1,128,Sepultura - Amen
// $text[0] = 1 = number of listeners connected
// $text[1] = 1 = stream state 0 = down, 1 = active
// $text[2] = 14 = Highest peak of listeners
// $text[3] = 50 = max number of listeners
// $text[4] = 1 = Unique number of listeners
// $text[5] = 128 = stream bitrate
// $text[6] = 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");
$read = fread($open,1000);
$text = explode("content-type:text/html",$read);
$text = explode(",",$text[1]);
} else { $er="Connection Refused!"; }

$blist = $text[0];
$bstate = $text[1];
$bpeak = $text[2];
$bmax = $text[3];
$buni = $text[4];
$bbit = $text[5];
$bsong = $text[6];

// 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] = 0 = number of listeners connected
// $text2[1] = 1 = stream state 0 = down, 1 = active
// $text2[2] = 14 = Highest peak of listeners
// $text2[3] = 75 = max number of listeners
// $text2[4] = 0 = Unique number of listeners
// $text2[5] = 24 = stream bitrate
// $text2[6] = 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");
$read2 = fread($open2,1000);
$text2 = explode("content-type:text/html",$read2);
$text2 = explode(",",$text2[1]);
} else { $er2="Connection Refused!"; }

$dlist = $text2[0];
$dstate = $text2[1];
$dpeak = $text2[2];
$dmax = $text2[3];
$duni = $text2[4];
$dbit = $text2[5];
$dsong = $text2[6];

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[0]);
$blist = trim($text[0]);
$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

// Broadband stats
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!

All I did was this and it started working for me:


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