PHP Array, each result into the next cell of a table

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
timmyc123
Forum Newbie
Posts: 3
Joined: Fri Jun 25, 2010 9:59 pm

PHP Array, each result into the next cell of a table

Post by timmyc123 »

Hello - I have some weather code that creates a forecast from the NWS XML feed. My problem is that I can't get each individual result into a different table cell.

Here is the code:

Code: Select all

<?php

    require_once("pxweather3.class.php");
    $w = new Weather3();
    $w->load();
    
    foreach($w->getDaycasts() as $daycast)
    {
?>
<span style="font-weight: bold";><span style="color:black;">
<?php
        echo "" . $daycast['day']. "\n<br>";
?> </span></span>
<span style="color:black;">
<?php
		
        echo $w->weatherString($daycast['weather']) . "\n<br>";

// Dynamic Weather Icons

	// Clear (Day & Night)
	if (($w->weatherString($daycast['weather']) == "Clear" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/sun.png' title='clear' alt='sunny' />\n<br>";

	// Partly Cloudy (Day & Night)
	elseif ( ($w->weatherString($daycast['weather']) == "Partly cloudy" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-pc.png' title='partly cloudy' alt='partly cloudy' />\n<br>";

	
	// Mostly Cloudy (Day & Night)
	elseif ( ($w->weatherString($daycast['weather']) == "Mostly cloudy" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-mc.png' title='mostly cloudy' alt='mostly cloudy' />\n<br>";
	
	// Cloudy
	elseif (($w->weatherString($daycast['weather']) == "Cloudy" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/c.png' title='cloudy' alt='cloudy' />\n<br>";
		
	// Flurries
	elseif ( ($w->weatherString($daycast['weather']) == "Flurries" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/flu.png' title='flurries' alt='flurries' />\n<br>";
	
	// Fog
	elseif ( ($w->weatherString($daycast['weather']) == "Fog" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/f.png' title='fog' alt='fog' />\n<br>";
	
	// Haze
	elseif ( ($w->weatherString($daycast['weather']) == "Haze" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-hz.png' title='hazy' alt='hazy' />\n<br>";
	
	// Mostly Sunny
	elseif ( ($w->weatherString($daycast['weather']) == "Mostly sunny" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-pc.png' title='mostly sunny' alt='mostly sunny' />\n<br>";
	
	// Partly Sunny
	elseif ( ($w->weatherString($daycast['weather']) == "Partly sunny" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-mc.png' title='partly sunny' alt='partly sunny' />\n<br>";
	
	// Rain (Day & Night)
	elseif ( ($w->weatherString($daycast['weather']) == "Rain" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-r.png' title='rain' alt='rain' />\n<br>";
		
	// Sleet
	elseif ( ($w->weatherString($daycast['weather']) == "Sleet" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/slt.png' title='sleet' alt='sleet' />\n<br>";
	
	// Snow (Day & Night)
	elseif ( ($w->weatherString($daycast['weather']) == "Snow" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-s.png' title='snow' alt='snow' />\n<br>";

	// Sunny
	elseif ( ($w->weatherString($daycast['weather']) == "Sunny" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/sun.png' title='sunny' alt='sunny' />\n<br>";	
		
	// Thunderstorms
	elseif ( ($w->weatherString($daycast['weather']) == "Thunderstorms" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-tss.png' title='thunderstorms' alt='thunderstorms' />\n<br>";	

	// Thunderstorm (Day & Night)
	elseif ( ($w->weatherString($daycast['weather']) == "Thunderstorm" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/d-ts.png' title='thunderstorm' alt='thunderstorm' />\n<br>";
		
	// Unknown
	elseif ( ($w->weatherString($daycast['weather']) == "Unknown" ))
		echo "<img src='http://news7newslinc.net/wxgfx/new/na.png' title='unknown' alt='unknown' />\n<br>";	
?>
</span>
<span style="color:red";>		
<?php
		  if (isset($daycast['high_temp']))
            echo "<b>Hi</b> " . $daycast['high_temp'] . "\n\n";
			
?>
</span>
<span style="color:black;">
 | 
 </span>
<span style="color:blue";>		
<?php
        if (isset($daycast['low_temp']))
            echo "<b>Lo</b> " . $daycast['low_temp'] . "\n<br>";
			echo "\n<br>";

      
    }
    
?>
</span>
And you can see the result here: http://www.news7newslinc.net/liveweathertest

It creates a big list. I would like to get each day into its own column in a table.

Thanks in advance for any help. I'm relatively new to PHP.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP Array, each result into the next cell of a table

Post by JakeJ »

Unless I'm completely off my nut, you're not even using a table. <span> is like a more local version of <div>. It has nothing to do with tables.

A table would be constructed like so:

Code: Select all

<table>
<tr>
<td>Left Column</td>
<td>Middle Column</td>
<td>Right Column</td>
</tr>
</table>
timmyc123
Forum Newbie
Posts: 3
Joined: Fri Jun 25, 2010 9:59 pm

Re: PHP Array, each result into the next cell of a table

Post by timmyc123 »

Putting a regular table in causes it to create a new table for each day that comes out of the array. I need each value to be in a new cell to the right of the previous.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP Array, each result into the next cell of a table

Post by JakeJ »

Try this:

Code: Select all

<?php
$qry = mysql_query("SELECT * FROM data");
echo '<table><tr>';
While ($row = mysql_fetch_array($qry)) {
    echo '<td>'.$row['day_of_week'].'</td>';
    echo '<td>'.$row['condition'].'</td>';
    echo '<td>'.$row['icon'].'</td>';
    echo '<td>'.$row['hi_lo'].'</td>';
}
echo '</tr></table>';
?>
timmyc123
Forum Newbie
Posts: 3
Joined: Fri Jun 25, 2010 9:59 pm

Re: PHP Array, each result into the next cell of a table

Post by timmyc123 »

What MySQL database am I querying? This is a php script that parses a NWS weather xml feed.
JakeJ
Forum Regular
Posts: 675
Joined: Thu Dec 10, 2009 6:27 pm

Re: PHP Array, each result into the next cell of a table

Post by JakeJ »

Sorry.. that's just my default. I forgot you were doing it that way.

Convert the while loop to a for each loop. It should work the same way. The basic theory being that the start and end of the table go outside of the loop and the <td> tags go inside the loop.
Post Reply