Page 1 of 1

arrays and tables

Posted: Mon Nov 29, 2004 9:31 am
by irishmike2004
Hello all:

I am working with a webpage which I am changing and I am going to try to explain what I am doing without posting all the PHP code, just the segment I need help on is the goal :-)

I have two Shoutcast servers which we are pulling stats from in XML. We have a "class" that parses the XML and gives us PHP variables which we have defined in the document. This works in our current webpage, http://radioteal.no-ip.com. The problem is the servers are listed linear-ly instead of side by side like my new rendition calls for (http://radioteal.no-ip.com/miketest/concept.php). This is a dummy mock up of what we want. We are re-using our code from the current page and the problem is the current implenmentation just "stacks" the tables... not what we are going for. My idea for a solution is to use an array to store the tables for the number of SC servers. Since the code can be modified to accept any number of servers (just by making new instances of the "class") it would be nice to store the tables that house the data in an array (perhaps called $svrTable) where $svrTable[0] would be the table for $shoutcast[0] and $svrTable[1] would be the table for $shoutcast[1]... etc.

I am not sure how to setup the array as someone else did the class file for Shoutcast servers.

This is where the song history ( a part of the table we want) is produced, thanks for the earlier help getting every other row colorized :-)

Code: Select all

if (is_array($history)) {
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>';
			  $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF'; 
			  echo '<tr><td bgcolor="'.$bgColor.'">'.$song&#1111;$j].'</td></tr>';
			&#125;
This for loop makes the two current instances:

Code: Select all

for($i=0; $i<count($shoutcast); $i++) &#123;

    if ($shoutcast&#1111;$i]->openstats()) &#123;
        // format the output to suit your particular needs
        if ($shoutcast&#1111;$i]->GetStreamStatus()) &#123;
        
        //new code begin
        	$scname = $shoutcast&#1111;$i]->GetServerTitle();
            $currentSong = $shoutcast&#1111;$i]->GetCurrentSongTitle();
            $bitRate = $shoutcast&#1111;$i]->GetBitRate();
            $listeners = $shoutcast&#1111;$i]->GetCurrentListenersCount();
            $scport = $shoutcast&#1111;$i]->port;
            $servip = $shoutcast&#1111;$i]->host;
            $history = $shoutcast&#1111;$i]->GetSongHistory();
            $genre = $shoutcast&#1111;$i]->GetServerGenre();
            $peakListeners = $shoutcast&#1111;$i]->GetPeakListenersCount();
            
echo '
<table width="100%"  border="0">
  <tr>
    <td width="45%"><b>'.$scname.'</b> ('.$bitRate.' Bit) Stream <br>
    <font color="#FF0000">Genre:</font> '.$genre.'
    </td>
    <td width="20%" rowspan="2" align="center"><p>Current Listners: <font size="+1" color="#FF0000">'.$listeners.'</font></p>
    <a href="http://'.$servip.':'.$scport.'/listen.pls"><img src="images/tealspeaker.jpg" alt="Listen" border="0" align="middle">Listen</a></td>
    <td width="35%" rowspan="2">
    <p><b>Recently Played:</b></p>
    <table>';
    
    if (is_array($history)) &#123;
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>';
			  $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF'; 
			  echo '<tr><td bgcolor="'.$bgColor.'">'.$song&#1111;$j].'</td></tr>';
		
	&#125;
I want to build the whole table which looks like the concept page (one for each instance) and then simply echo the $svrTable[x] in the place where each table appears.

There are actually 10 rows and 1 column and the actual table would have these two (in this case) nested inside of the concept.php area where you see the server name etc.

I am not sure how to make this array is the problem.

Thanks for the help.

Need another approach

Posted: Mon Nov 29, 2004 7:27 pm
by irishmike2004
hello:

Tried the idea of putting the tables into an array by using two arrays and they don't appear to function... so I need another way of putting my two tables side by side... the problem is they go up and down (linear) fine, but I wish them to be side by side. I could have implemented my arrays wrong, so I am posting code showing what I tried, someone out there may have a better idea... hope someone can help :-)

The following is inside a loop as above.

Code: Select all

$svrTable = array();
$songHistoryTable = array();

...

$svrTable&#1111;$i] = 'some code' .$someVar.' some more code';

if (is_array($history)) &#123;
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>';
			  $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF'; 
			  $songHistoryTable&#1111;$i] = '<tr><td bgcolor="'.$bgColor.'">'.$song&#1111;$j].'</td></tr>';
			&#125;
The variables were supposed to hold the tables so I could go on and do the HTML and just plug in echoes to the tables like

HTML FOO '.$svrTable[0]. ' more foo' for sc server one for example.

Posted: Mon Nov 29, 2004 8:00 pm
by rehfeld
ok, so your outputting the data you want just fine, you just want it to be laid out differently in the html right?


instead of using echo inside your loops, build strings and store them into an array. then you can do like you want, echo $svrTables[0] and it will output all the html you generated for it



Code: Select all

<?php

$svrTables = array();

for($i=0; $i<count($shoutcast); $i++) {

    if ($shoutcast[$i]->openstats()) {
        // format the output to suit your particular needs
        if ($shoutcast[$i]->GetStreamStatus()) {
       
        //new code begin
           $scname = $shoutcast[$i]->GetServerTitle();
            $currentSong = $shoutcast[$i]->GetCurrentSongTitle();
            $bitRate = $shoutcast[$i]->GetBitRate();
            $listeners = $shoutcast[$i]->GetCurrentListenersCount();
            $scport = $shoutcast[$i]->port;
            $servip = $shoutcast[$i]->host;
            $history = $shoutcast[$i]->GetSongHistory();
            $genre = $shoutcast[$i]->GetServerGenre();
            $peakListeners = $shoutcast[$i]->GetPeakListenersCount();
           
$svrTable = '
<table width="100%"  border="0">
  <tr>
    <td width="45%"><b>'.$scname.'</b> ('.$bitRate.' Bit) Stream <br>
    <font color="#FF0000">Genre:</font> '.$genre.'
    </td>
    <td width="20%" rowspan="2" align="center"><p>Current Listners: <font size="+1" color="#FF0000">'.$listeners.'</font></p>
    <a href="http://'.$servip.':'.$scport.'/listen.pls"><img src="images/tealspeaker.jpg" alt="Listen" border="0" align="middle">Listen</a></td>
    <td width="35%" rowspan="2">
    <p><b>Recently Played:</b></p>
    <table>';
   
    if (is_array($history)) {
         for ($j=0;$j<sizeof($history);$j++) {
           $song[$j] = $history[$j]["title"];
           //echo $song[$j].'<br>';
           $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF';
           $svrTable .= '<tr><td bgcolor="'.$bgColor.'">'.$song[$j].'</td></tr>';
      
   }

    $svrTables[] = $svrTable;

} // end first for loop

?>


since you didnt post the whole code theres issues w/ brackets in the above code, but im sure you see where im going w/ this

Getting there but still not quite right

Posted: Mon Nov 29, 2004 10:25 pm
by irishmike2004
Hi and thanks for the help so far:

I am getting some weirdness once my error loop takes over and need to figure out how it can be fixed. Attached is the full code...

also, we need to call the $svrTables array with some kind of variable attached to the number of servers (see code) and thanks for the help.

the thing I need to do is have the errors show up in the "pane" I gave them instead of underneath. I am bashing my head against the wall on this one.

Thanks in advance and my sanity thanks you too...

Code: Select all

<?php

include("shoutcast.class.php");

// setup servers
$shoutcast = array();
$shoutcast&#1111;] = new ShoutCast();    // server1
$shoutcast&#1111;] = new ShoutCast();    // server2
// add more instances of ShoutCast class to support more servers

// server1 data
$shoutcast&#1111;0]->host = "192.168.1.2";    // no 'http://'
$shoutcast&#1111;0]->port = 8000;
$shoutcast&#1111;0]->passwd = "mypassword";

// server2 data
$shoutcast&#1111;1]->host = "65.27.27.130";
$shoutcast&#1111;1]->port = 8000;
$shoutcast&#1111;1]->passwd = "theotherpassword";

echo '

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv=refresh content=45>
<title>Welcome To Radio Teal</title>
<script language="JavaScript" type="text/JavaScript">
<!--
function MM_reloadPage(init) &#123;  //reloads the window if Nav4 resized
  if (init==true) with (navigator) &#123;if ((appName=="Netscape")&&(parseInt(appVersion)==4)) &#123;
    document.MM_pgW=innerWidth; document.MM_pgH=innerHeight; onresize=MM_reloadPage; &#125;&#125;
  else if (innerWidth!=document.MM_pgW || innerHeight!=document.MM_pgH) location.reload();
&#125;
MM_reloadPage(true);
//-->
</script>
<style type="text/css">
<!--
.style1 &#123;color: #FF0000&#125;
-->
</style>
</head>

<body>
<table border="0" cellpadding="0" cellspacing="0">
  <tr>
    <td width="115" height="101" rowspan="3" valign="top"><img src="images/newrtlogo.gif" width="100" height="99"></td>
    <td width="2" height="1" valign="top"></td>
    <td width="462" height="1" valign="top"></td>
    <td width="270" height="1" valign="top"></td>
    <td width="179" height="1" valign="top"></td>
  </tr>
  <tr>
    <td width="2" height="41" valign="top"></td>
    <td width="462" height="41" colspan="2" valign="top">
  <h1 class="style1">Radio Teal Project </h1>
</td>
    <td width="179" height="41" valign="top"></td>
  </tr>
  <tr>
    <td width="2" height="59" valign="top"></td>
    <td width="462" height="59" colspan="3" valign="top">
  <table width="100%"  border="0" mm_noconvert="TRUE">
    <tr>
      <td width="21%" align="center"><form action="https://www.paypal.com/cgi-bin/webscr" method="post">
<input type="hidden" name="cmd" value="_s-xclick">
<input type="image" src="https://www.paypal.com/en_US/i/btn/x-click-but04.gif" border="0" name="submit" alt="Make payments with PayPal - it''s fast, free and secure!">
<input type="hidden" name="encrypted" value="-----BEGIN PKCS7-----MIIHHgYJKoZIhvcNAQcEoIIHDzCCBwsCAQExggEwMIIBLAIBADCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwDQYJKoZIhvcNAQEBBQAEgYBMXpAmd0UQB2PFbFNTRs/Rc3VqJUCkqFKgNXunz2FC6D6JspHor3WwYbq++g++b90WO2ulg5GKFODV3BGtcSTym4xN8J6tL/nU2MiZhLdYFlHxE/vnyMcd5ssI1hjYaLffHmva7r2FNkwYj3lLFxw5LFtbQo/qdva+ANMIR81alTELMAkGBSsOAwIaBQAwgZsGCSqGSIb3DQEHATAUBggqhkiG9w0DBwQIPgNbn64wfgOAeM63HYIMpdfzIQ57R0UE1enBp6HmsRnjfPNCK/J8YMYPFz4qLCiakTFmnyu8O/ESRaYjTvKagsT2Fo9xBYdG6Xc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UdEwQFMAMBAf8wDQYJKoZIhvcNAQEFBQADgYEAgV86VpqAWuXvX6Oro4qJ1tYVIT5DgWpE692Ag422H7yRIr/9j/iKG4Thia/Oflx4TdL+IFJBAyPK9v6zZNZtBgPBynXb048hsP16l2vi0k5Q2JKiPDsEfBhGI+HnxLXEaUWAcVfCsQFvd2A1sxRr67ip5y2wwBelUecP3AjJ+YcxggGaMIIBlgIBATCBlDCBjjELMAkGA1UEBhMCVVMxCzAJBgNVBAgTAkNBMRYwFAYDVQQHEw1Nb3VudGFpbiBWaWV3MRQwEgYDVQQKEwtQYXlQYWwgSW5jLjETMBEGA1UECxQKbGl2ZV9jZXJ0czERMA8GA1UEAxQIbGl2ZV9hcGkxHDAaBgkqhkiG9w0BCQEWDXJlQHBheXBhbC5jb20CAQAwCQYFKw4DAhoFAKBdMBgGCSqGSIb3DQEJAzELBgkqhkiG9w0BBwEwHAYJKoZIhvcNAQkFMQ8XDTA0MTExMTIxMzIyMlowIwYJKoZIhvcNAQkEMRYEFLYXn5MaOCGo9AMYUOY0sB2AQgykMA0GCSqGSIb3DQEBAQUABIGANLd2HkDDmWFple5FCquRvwcDdWPWzt4hwuOLashwzgLj7I9N//ApaqELs82EeKrZZw4vn/seKCSpEixUSDqZ0rFCWIz0zxFTEqjFlm3jHV+ukt4UGEUcay6AfqAMGgDhJgCg6OidZsl+2oU/OHFuPEWmsvWYvGw8WakOGrQJReQ=-----END PKCS7-----
">
</form></td>
      <td width="24%" align="center"><a href="http://www.shoutcast.com"><img src="images/sc_power_btn.gif" alt="Get Shoutcast" width="88" height="31" border="0"></a></td>
      <td width="30%" align="center"><a href="http://www.apple.com/itunes/download/"><img src="file:///Macintosh%20HD/Users/jmikeneedham/Sites/radioteal/images/logo_itunes.jpg" alt="Get iTunes" width="130" height="44" border="0"></a></td>
      <td width="25%" align="center"><a href="mailto:radioteal@hotmail.com"><img src="file:///Macintosh%20HD/Users/jmikeneedham/Sites/radioteal/images/small_email.jpg" alt="E-Mail" width="40" height="33" border="0"></a></td>
    </tr>
  </table>
</td>
  </tr>
</table>
<h3 align="center">Available Streams </h3>
';

// array for table creation

$svrTables = array();


for($i=0; $i<count($shoutcast); $i++) &#123;

    if ($shoutcast&#1111;$i]->openstats()) &#123;
        // format the output to suit your particular needs
        if ($shoutcast&#1111;$i]->GetStreamStatus()) &#123;
        
        //new code begin
            $scname = $shoutcast&#1111;$i]->GetServerTitle();
            $currentSong = $shoutcast&#1111;$i]->GetCurrentSongTitle();
            $bitRate = $shoutcast&#1111;$i]->GetBitRate();
            $listeners = $shoutcast&#1111;$i]->GetCurrentListenersCount();
            $scport = $shoutcast&#1111;$i]->port;
            $servip = $shoutcast&#1111;$i]->host;
            $history = $shoutcast&#1111;$i]->GetSongHistory();
            $genre = $shoutcast&#1111;$i]->GetServerGenre();
            $peakListeners = $shoutcast&#1111;$i]->GetPeakListenersCount();
            
$svrTable = '
<table width="100%" height="246"  border="0">
  <tr>
    <td><table width="100%"  border="1">
      <tr>
        <td width="42%" align="center"><a href="http://'.$servip.':'.$scport.'/listen.pls"><img src="file:///Macintosh%20HD/Users/jmikeneedham/Sites/radioteal/images/smallspeaker.jpg" alt="Listen to Stream" width="15" height="14" border="0"> LISTEN! </a></td>
        <td width="58%" align="center">Make Request</td>
      </tr>
    </table></td>
  </tr>
  <tr>
    <td><strong>'.$scname.'</strong> ('.$bitRate.' Bit Stream)</td>
  </tr>
  <tr>
    <td align="center">(Listeners: <font color="#FF0000">'.$listeners.'</font> Peak: <font color="#FF0000">'.$peakListeners.'</font>) </td>
  </tr>
  <tr>
    <td><strong>Genre:</strong> '.$genre.' </td>
  </tr>
  <tr>
    <td height="57"><p><strong>Currently Playing: </strong></p>
    <p>'.$currentSong.'</p></td>
  </tr>
  <tr>
    <td><strong>Recently Played: </strong></td>
  </tr>
';
    
    if (is_array($history)) &#123;
			for ($j=0;$j<sizeof($history);$j++) &#123;
			  $song&#1111;$j] = $history&#1111;$j]&#1111;"title"];
			  //echo $song&#1111;$j].'<br>';
			  $bgColor = ($j % 2) ? '#00CCCC' : '#FFFFFF'; 
			  $svrTable .= '<tr><td bgcolor="'.$bgColor.'">'.$song&#1111;$j].'</td></tr>';
			&#125;
	&#125; else &#123;
			$svrTable = '<tr><td bgcolor="#FFFFFF">No Song History Available</td></tr>';
		&#125;

&#125; else &#123;
            $svrTable =  '<tr><td bgcolor="#FFFFFF">No Stream Available on Server '.$i.'</td></tr>';
        &#125;

    &#125; else &#123;
    	$svrTable = '<tr><td bgcolor="#FFFFFF">Server '.$i.' offline</td></tr>';
    &#125;
// insert array at end of FOR loop

$svrTable .= '</table>';
$svrTables&#1111;] = $svrTable;
&#125;

echo '
<p align="center">
<table width="85%" height="252" border="1" mm_noconvert="TRUEh">
<tr>
<td width="50%"> '.$svrTables&#1111;0].'</td>
<td width="50%"> '.$svrTables&#1111;1].'</td>
</tr>
</table>
</p>
<p align=center>©2004 Teal Communications, J. Mike Needham & R.L. Paul</p>
</body>
</html>';



?>
Above is the whole page... thanks.

Resolved

Posted: Mon Nov 29, 2004 11:37 pm
by irishmike2004
Hello all:

I actually resolved the table issues. Thanks for your help.

Posted: Mon Nov 29, 2004 11:46 pm
by rehfeld
i think you should change your errors from being like this:
$svrTable = '<tr><td bgcolor="#FFFFFF">Server '.$i.' offline</td></tr>';

to like this:
$svrTable = '<table><tr><td bgcolor="#FFFFFF">Server '.$i.' offline</td></tr></table>';

i think thats what you meant about not being in its "pane"

how many of these "panes" do you want to show on 1 row?
currently you have 2, but if you had 10 servers to display, your gonna have a long horizontal scrollbar lol

yeah

Posted: Mon Nov 29, 2004 11:51 pm
by irishmike2004
I agree, I would not put 10 servers on one row, but I might do 4 in a block and etc... I did kind of what you suggest to fix the errors.

still need to replace $svrTables[0] ...etc with some representation the number of Shoutcast arrays just for the code sake... it keeps it all reuseable and such :-) if I had 10 servers, it would be likely just current song and server name FYI :-)

Thanks,

Posted: Tue Nov 30, 2004 12:06 am
by rehfeld
im still not sure what you mean by
replace $svrTables[0] ...etc with some representation the number of Shoutcast arrays

but heres how to break them down to display so many per row

Code: Select all

<?php

$panes = "<table border="1">\n";
$per_row = 2; // you can use any number here, even or odd it will work

for ($i=0; $i<count($svrTables); $i++) {
    if (($i % $per_row) == 0) {
        $panes .= "  <tr>\n";
    }

    $panes .= '    <td>'.$svrTables[$i]."</td>\n";

    if (($i % $per_row) == ($per_row - 1)) {
        $panes .= "  </tr>\n";
    }
}

while ($i++ % $per_row) {
    $panes .= "    <td>&nbsp;</td>\n";
}

$panes .= "  </tr>\n</table>\n";



echo $panes;

?>