Jump to content

Print array in horizontal table


flemingmike

Recommended Posts

hi all,

 

i have 2 sets of code.  it currently displays weather vertically, i cant for the life of me figure out how to get it into a horizontal table.  any help is appreciated.

http://www.durhamit.ca/test/example.php

 

example.php

<?php

require_once('google_weather_api.php');


$weather = new weather();

if (!empty($_GET['loc'])) {

$weather->location = $_GET['loc'];
}

$weather->get();

if($weather->error){
die('We couldn\'t find your location.');
}
else{
echo '
<div id="currentWeather">

<h1>Now in '.ucwords($weather->location).': '.$weather->current->temp_c['data'].' &#8451;</h1>

<img src="http://www.google.com/' .$weather->current->icon['data'] . '"/>
<p>'.$weather->current->condition['data'].'</p>

<p>'.$weather->current->humidity['data'].'</p>

<p>'.$weather->current->wind_condition['data'].'</p>
</div>
';

// display more days info

// print_r($weather->nextdays);

$weather->display();
}

 

 

 

google_weather_api.php

<?php 


class weather
{
public static 
$response;
public static 
$location;
public static 
$current;
public static 
$nextdays;
public static 
$error = false;

public 
function weather()

{

$this->location = 'Pickering, ON';
}



public function get()
{
	if (empty($this->location)) {

$this->error = true;
		return false;
	}

$requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";

$xml_str = file_get_contents($requestAddress,0);

$xml = new SimplexmlElement($xml_str);
	if (!$xml->weather->problem_cause) {

$this->response = $xml->weather;
		$this->parse();
	}else{

$this->error = true;
	}
}


public function parse()
{

foreach($this->response as $item) {

$this->current = $item->current_conditions;

foreach($item->forecast_conditions as $new) {

$this->nextdays[] = $new;		

}	
	}
}


public function display()
{

foreach($this->nextdays as $new) {			

echo '<div class="weatherIcon">';

echo '<h2>'.$new->day_of_week['data'].'</h2>';

echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';

echo '<br />Min: '.$this->convert($new->low['data']).' &#8451;';

echo '<br />Max: '.$this->convert($new->high['data']).' &#8451;';

echo '</div>';			
	}	
}


public function convert($value, $unit = "C"){

switch($unit){
		case "C":

return number_format(($value - 32)/1.;

break;

case "F":

return round($value * 1.8 + 32);

break;

default:

return $value;

break;
	};
}	

}

Link to comment
https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/
Share on other sites

im stuck here.

 

<?php

include("style.php");
require_once('pickering_api.php');
$weather = new weather();

if (!empty($_GET['loc'])) {

$weather->location = $_GET['loc'];
}

$weather->get();

if($weather->error){
die('We couldn\'t find your location.');
}
else{
echo '
<div id="currentWeather">



<table border=1 cellpadding=2 align=center>

<tr align=center>

<td> <h2>Right Now In '.ucwords($weather->location).'</h2>
<br /><img src="http://www.durhamit.ca/test/' .$weather->current->icon['data'] . '"/>
<br />'.$weather->current->temp_c['data'].' &#8451;
<br />'.$weather->current->condition['data'].'

<br />'.$weather->current->humidity['data'].'

<br />'.$weather->current->wind_condition['data'].'
</td>

<td> <h2>Right Now In '.ucwords($weather->location).'</h2>
<br /><img src="http://www.durhamit.ca/test/' .$weather->current->icon['data'] . '"/>
<br />'.$weather->current->temp_c['data'].' &#8451;
<br />'.$weather->current->condition['data'].'

<br />'.$weather->current->humidity['data'].'

<br />'.$weather->current->wind_condition['data'].'
</td>

<td>$weather->display();
}
</td>

   
</tr>

</table>    

</div>
';



// display more days info

// print_r($weather->nextdays);

$weather->display();
}


i think im getting closer.

 

index.php:

<?php

include("style.php");
require_once('pickering_api.php');
$weather = new weather();

if (!empty($_GET['loc'])) {

$weather->location = $_GET['loc'];
}

$weather->get();

if($weather->error){
die('We couldn\'t find your location.');
}
else{
echo '
<div id="currentWeather">



<table border=1 cellpadding=2 align=center>

<tr align=center>

<td> <h2>Right Now In '.ucwords($weather->location).'</h2>
<br /><img src="http://www.durhamit.ca/test/' .$weather->current->icon['data'] . '"/>
<br />'.$weather->current->temp_c['data'].' &#8451;
<br />'.$weather->current->condition['data'].'

<br />'.$weather->current->humidity['data'].'

<br />'.$weather->current->wind_condition['data'].'
</td>

<td>



</td>

<td>



</td>

   
</tr>

</table>    
</div>';


// display more days info

// print_r($weather->nextdays);

$weather->display();
}

 

 

pickering_api.php:

<?php 


class weather
{
public static 
$response;
public static 
$location;
public static 
$current;
public static 
$nextdays;
public static 
$error = false;

public 
function weather()

{

$this->location = 'Pickering';
}



public function get()
{
	if (empty($this->location)) {

$this->error = true;
		return false;
	}

$requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";

$xml_str = file_get_contents($requestAddress,0);

$xml = new SimplexmlElement($xml_str);
	if (!$xml->weather->problem_cause) {

$this->response = $xml->weather;
		$this->parse();
	}else{

$this->error = true;
	}
}


public function parse()
{

foreach($this->response as $item) {

$this->current = $item->current_conditions;

foreach($item->forecast_conditions as $new) {

$this->nextdays[] = $new;		

}	
	}
}


public function display()
{

foreach($this->nextdays as $new) {			

echo '<div class="weatherIcon">';

echo '<h2>'.$new->day_of_week['data'].'</h2>';

echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';

echo '<br />Min: '.$this->convert($new->low['data']).' &#8451;';

echo '<br />Max: '.$this->convert($new->high['data']).' &#8451;';

echo '<br />'.$new->condition['data'] . 
' ';

echo '</div>';			
	}	
}


public function convert($value, $unit = "C"){

switch($unit){
		case "C":

return number_format(($value - 32)/1.;

break;

case "F":

return round($value * 1.8 + 32);

break;

default:

return $value;

break;
	};
}	

}

 

 

am i getting closer?

Except for the part where he said remove the break tags... break tags tell the browser to move the displayed text to the next line.

 

Try this

 

<?php

include("style.php");
require_once('pickering_api.php');
$weather = new weather();

if (!empty($_GET['loc'])) {
   
$weather->location = $_GET['loc'];
}

$weather->get();

if($weather->error){
   die('We couldn\'t find your location.');
}
else{
   echo '
   <div id="currentWeather">
      
      

<table border=1 cellpadding=2 align=center>

<tr align=center>

<td> <h2>Right Now In '.ucwords($weather->location).'</h2></td>
<td><img src="http://www.durhamit.ca/test/' .$weather->current->icon['data'] . '"/></td>
<td>'.$weather->current->temp_c['data'].' &#38;#8451;</td>
<td>'.$weather->current->condition['data'].'</td>
      
<td>'.$weather->current->humidity['data'].'</td>

<td>'.$weather->current->wind_condition['data'].'</td>

   
</tr>

</table>    
</div>';

   
// display more days info
   
// print_r($weather->nextdays);
   
$weather->display();
}

 

that sepparated all the info for the current day.  that part is fine how i had it.  im trying to get fri, sat, sun and mon to be side by side.

 

the part that says

 

$weather->display();}

 

i believe this is just running a loop on the data??

 

i think this is where i am failing

ok, so i have everything in a table now, but the table is vertical.  how can i make it horizontal?

 

<?php 


class weather
{
public static 
$response;
public static 
$location;
public static 
$current;
public static 
$nextdays;
public static 
$error = false;

public 
function weather()

{

$this->location = 'Pickering, ON';
}



public function get()
{
	if (empty($this->location)) {

$this->error = true;
		return false;
	}

$requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";

$xml_str = file_get_contents($requestAddress,0);

$xml = new SimplexmlElement($xml_str);
	if (!$xml->weather->problem_cause) {

$this->response = $xml->weather;
		$this->parse();
	}else{

$this->error = true;
	}
}


public function parse()
{

foreach($this->response as $item) {

$this->current = $item->current_conditions;

foreach($item->forecast_conditions as $new) {

$this->nextdays[] = $new;		

}	
	}
}


public function display()
{

foreach($this->nextdays as $new) 
{			



echo '<table border=1 cellpadding=2 align=center>';
echo '<tr align=center>';
echo '<td>';
echo '<div class="weatherIcon">';

echo '<h2>'.$new->day_of_week['data'].'</h2>';

echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';

echo '<br />Min: '.$this->convert($new->low['data']).' &#8451;';

echo '<br />Max: '.$this->convert($new->high['data']).' &#8451;';

echo '<br />'.$new->condition['data'] . 
' ';


echo '</td>  '; 


echo '</div>';			
	}	
}


public function convert($value, $unit = "C"){

switch($unit){
		case "C":

return number_format(($value - 32)/1.;


echo '</tr>';
echo '</table>';

	};

}
}

It helps if you keep your tags in the proper order. Try this:

public function display()
  {
    echo '<table border=1 cellpadding=2 align=center>';
    echo '<tr align=center>';
    
    foreach($this->nextdays as $new) 
    {         
      echo '<td>';
      echo '<div class="weatherIcon">';
      echo '<h2>'.$new->day_of_week['data'].'</h2>';
      echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';
      echo '<br />Min: '.$this->convert($new->low['data']).' &#38;#8451;';
      echo '<br />Max: '.$this->convert($new->high['data']).' &#38;#8451;';
      echo '<br />'.$new->condition['data'].' ';
      echo '</div>';
      echo '</td>';    
    }
    echo '</tr>';
    echo '</table>';
}

thanks for that, page cannot be shown.  i think i might have an extra } in there.

 

<?php 


class weather
{
public static 
$response;
public static 
$location;
public static 
$current;
public static 
$nextdays;
public static 
$error = false;

public 
function weather()

{

$this->location = 'Pickering, ON';
}



public function get()
{
	if (empty($this->location)) {

$this->error = true;
		return false;
	}

$requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";

$xml_str = file_get_contents($requestAddress,0);

$xml = new SimplexmlElement($xml_str);
	if (!$xml->weather->problem_cause) {

$this->response = $xml->weather;
		$this->parse();
	}else{

$this->error = true;
	}
}


public function parse()
{

foreach($this->response as $item) {

$this->current = $item->current_conditions;

foreach($item->forecast_conditions as $new) {

$this->nextdays[] = $new;		

}	
	}
}




public function display()  
{    
echo '<table border=1 cellpadding=2 align=center>';    
echo '<tr align=center>';        
foreach($this->nextdays as $new)     {               
echo '<td>';      
echo '<div class="weatherIcon">';      
echo '<h2>'.$new->day_of_week['data'].'</h2>';      
echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';      
echo '<br />Min: '.$this->convert($new->low['data']).' &#38;#38;#8451;';      
echo '<br />Max: '.$this->convert($new->high['data']).' &#38;#38;#8451;';      
echo '<br />'.$new->condition['data'].' ';      
echo '</div>';      echo '</td>';        }    
echo '</tr>';    
echo '</table>';}


	}	
}


public function convert($value, $unit = "C"){

switch($unit){
		case "C":

return number_format(($value - 32)/1.;

	};

}
}

You actually had 2 extra } in there. Here is the cleaned code:

<?php 


class weather {
public static $response;
public static $location;
public static $current;
public static $nextdays;
public static $error = false;
   
public function weather() {
	$this->location = 'Pickering, ON';
}

public function get() {
	if (empty($this->location)) {
		$this->error = true;
		return false;
	}

	$requestAddress = "http://www.google.com/ig/api?weather=".trim(urlencode($this->location))."&hl=en";
	$xml_str = file_get_contents($requestAddress,0);
	$xml = new SimplexmlElement($xml_str);

	if (!$xml->weather->problem_cause) {
		$this->response = $xml->weather;
		$this->parse();
	}else{
		$this->error = true;
	}
}

public function parse() {
	foreach($this->response as $item) {
		$this->current = $item->current_conditions;
		foreach($item->forecast_conditions as $new) {
			$this->nextdays[] = $new;
		}
	}
}

public function display() {
	echo '<table border=1 cellpadding=2 align=center>';
	echo '<tr align=center>';
	foreach($this->nextdays as $new) {
		echo '<td>';
		echo '<div class="weatherIcon">';
		echo '<h2>'.$new->day_of_week['data'].'</h2>';
		echo '<img src="http://www.durhamit.ca/test/' .$new->icon['data'] . '"/><br/>';
		echo '<br />Min: '.$this->convert($new->low['data']).' &#38;#38;#38;#8451;';
		echo '<br />Max: '.$this->convert($new->high['data']).' &#38;#38;#38;#8451;';
		echo '<br />'.$new->condition['data'].' ';
		echo '</div>';
		echo '</td>';
	}
	echo '</tr>';
	echo '</table>';
}

public function convert($value, $unit = "C") {
	switch($unit){
		case "C":
		return number_format(($value - 32)/1.;
	};
}
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.