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; }; } } Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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 Quote Link to comment 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. Quote Link to comment 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(); } Quote Link to comment 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? Quote Link to comment 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(); } Quote Link to comment 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 Quote Link to comment 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>'; }; } } Quote Link to comment 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>'; } Quote Link to comment 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.; }; } } Quote Link to comment 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.; }; } } Quote Link to comment Share on other sites More sharing options...
flemingmike Posted May 28, 2010 Author Share Posted May 28, 2010 Thank you all very much!! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.