flemingmike Posted May 28, 2010 Share Posted May 28, 2010 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'].' ℃</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']).' ℃'; echo '<br />Max: '.$this->convert($new->high['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; }; } } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/ Share on other sites More sharing options...
teamatomic Posted May 28, 2010 Share Posted May 28, 2010 Get rid of the line breaks in the display function and use no-break-spaces for spacing or put it in a table and use padding. HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064687 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 would i do that in the api? im kinda new Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064692 Share on other sites More sharing options...
teamatomic Posted May 28, 2010 Share Posted May 28, 2010 thats here, google_weather_api.php HTH Teamatomic Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064699 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 i keep trying different ways, but i feel like there is an error in how im displaying the array. it keeps going vertical, even in a table. Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064718 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 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'].' ℃ <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'].' ℃ <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(); } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064721 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 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'].' ℃ <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']).' ℃'; echo '<br />Max: '.$this->convert($new->high['data']).' ℃'; 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? Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064734 Share on other sites More sharing options...
MatthewJ Posted May 28, 2010 Share Posted May 28, 2010 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'].' &#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(); } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064737 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 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 Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064746 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 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']).' ℃'; echo '<br />Max: '.$this->convert($new->high['data']).' ℃'; 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>'; }; } } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064753 Share on other sites More sharing options...
hcdarkmage Posted May 28, 2010 Share Posted May 28, 2010 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']).' &#8451;'; echo '<br />Max: '.$this->convert($new->high['data']).' &#8451;'; echo '<br />'.$new->condition['data'].' '; echo '</div>'; echo '</td>'; } echo '</tr>'; echo '</table>'; } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064760 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 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;#8451;'; echo '<br />Max: '.$this->convert($new->high['data']).' &#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.; }; } } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064766 Share on other sites More sharing options...
hcdarkmage Posted May 28, 2010 Share Posted May 28, 2010 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;#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.; }; } } Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064778 Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 Thank you all very much!! Link to comment https://forums.phpfreaks.com/topic/203203-print-array-in-horizontal-table/#findComment-1064781 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.