Ruth Posted March 24, 2008 Share Posted March 24, 2008 I cannot get my avg function to display. When I take out the function code and leave the body everything works fine. Can someone help me figure out the problem. <?php require_once ('dbconnection.php'); //avg function //fix function calculate_avg () { $global = $calculate_review; $query = "SELECT review_rating FROM reviews WHERE review_movie_id = '".$_GET['movie_id']."'"; $result = mysql_query($query)or die (mysql_error()); $total = mysql_num_rows($result); $current = 0; while ($row = mysql_fetch_array($result)) { $review_rating = $row['review_rating']; $current = $current + $review_rating; } $calculate_review = $current / $total; } //function to calc profit or loss function calculate_differences ($takings, $cost) { $difference = $takings - $cost; if ($difference < 0) { //substr will not display negative just num $difference = substr($difference, 1); $font_color = 'red'; $profit_or_loss = "$" . $difference . "m"; } elseif ($difference > 0) { $font_color = 'green'; $profit_or_loss = "$" . $difference . "m"; } else { $font_color = 'blue'; $profit_or_loss = "Broke even"; } return "<font color=\"$font_color\">" .$profit_or_loss."</font>"; } //function to get director from db function get_director() { global $movie_director; global $director; $query_d = "SELECT people_fullname FROM people WHERE people_id = '$movie_director'"; $result_d = mysql_query ($query_d) or die (mysql_error()); $row_d = mysql_fetch_array($result_d); extract ($row_d); $director = $people_fullname; } //function to get actor from db function get_leadactor() { global $movie_leadactor; global $leadactor; $query_a = "SELECT people_fullname FROM people WHERE people_id = '$movie_leadactor'"; $result_a = mysql_query($query_a) or die (mysql_error()); $row = mysql_fetch_array($result_a); $leadactor = $row['people_fullname']; } //function to display image function generate_ratings($review_rating) { $movie_rating = ''; for ($i = 0; $i < $review_rating; $i++) { $movie_rating .= "<img src = \"thumbsup.gif\"> "; } return $movie_rating; } $movie_query = "SELECT * FROM movie WHERE movie_id = '".$_GET['movie_id']."' "; $movie_result = mysql_query($movie_query) or die(mysql_error()); $movie_table_headings = <<<stop <tr> <th>Movie Title</th> <th>Year of Release</th> <th>Movie Director</th> <th>Movie Lead Actor</th> <th>Movie Running Time</th> <th>Movie Health</th> <th>Avg Rating</th> </tr> stop; $review_table_headings = <<<stop <tr> <th>Date of Review</th> <th>Review Title</th> <th>Reviewer Name</th> <th>Movie Review Comments</th> <th>Rating</th> </tr> stop; while ($row = mysql_fetch_array($movie_result)) { $movie_name = $row['movie_name']; $movie_director = $row['movie_director']; $movie_leadactor = $row['movie_leadactor']; $movie_year = $row['movie_year']; $movie_running_time = $row['movie_running_time']. "mins"; $movie_takings = $row['movie_takings']; $movie_cost = $row['movie_cost']; //call functions get_director(); get_leadactor(); calculate_avg (); } $review_query = "SELECT * FROM reviews WHERE review_movie_id ='".$_GET['movie_id']."' ORDER BY review_date DESC"; $review_result = mysql_query($review_query) or die(mysql_error()); while ($review_row = mysql_fetch_array($review_result)) { //put reviews into an array $review_flag = 1; $review_title[] = $review_row['review_name']; //ucwords capitalizes the 1st letter of each word $reviewer_name[] = ucwords($review_row['review_reviewer_name']); $review[] = $review_row['review_comment']; $review_date[] = $review_row['review_date']; $review_rating[] = generate_ratings($review_row['review_rating']); } $movie_health = calculate_differences($movie_takings, $movie_cost); $page_start = <<<stop <html> <head> <title>Details and Reviews for: $movie_name</title> </head> <body> stop; $movie_details = <<<stop <table width = "70%" border = "0" cellspacing = "2" cellpadding = "2" align = "center"> <tr> <th colspan = "6"><u><h2>$movie_name: Details</h2></u></th> </tr> $movie_table_headings <tr> <td width = "33%" align = "center">$movie_name</td> <td align = "center">$movie_year</td> <td align = "center">$director</td> <td align = "center">$leadactor</td> <td align = "center">$movie_running_time</td> <td align = "center">$movie_health</td> <td align = "center">$calculate_review</td> </tr> </table> <br> <br> stop; //check for reviews if ($review_flag) { $movie_details .= "<table width = '95%' border = '0' cellspacing = '2' cellpadding = '20' align='center'>". $review_table_headings. $review_details. "</table>"; } $i = 0; $review_details = ''; //sizeof count how many records have been returned while ($i < sizeof($review)) { $review_details .= <<<stop <table> <tr> <td width = "15%" valign = "top" align = "center">$review_date[$i]</td> <td width = "15%" valign = "top">$review_title[$i]</td> <td width = "10%" valign = "top">$reviewer_name[$i]</td> <td width = "50%" valign = "top">$review[$i]</td> <td width = "10%" valign = "top" align = "center">$review_rating[$i]</td> </tr> </table> stop; $i++; } $page_end = <<<stop </body> </html> stop; $detailed_movie_info = <<<stop $page_start $movie_details $review_details $page_end stop; echo $detailed_movie_info; mysql_close(); ?> EDITED BY WILDTEEN88 Link to comment https://forums.phpfreaks.com/topic/97651-function/ Share on other sites More sharing options...
papaface Posted March 24, 2008 Share Posted March 24, 2008 Use code tags. Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499632 Share on other sites More sharing options...
Ruth Posted March 24, 2008 Author Share Posted March 24, 2008 What are code tags? Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499634 Share on other sites More sharing options...
cooldude832 Posted March 24, 2008 Share Posted March 24, 2008 your code seems to try and use a lot of variables you might be defining in the global realm. These are not directly accessible inside a function you must use proper scope this will help http://us.php.net/manual/en/language.variables.scope.php Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499637 Share on other sites More sharing options...
papaface Posted March 24, 2008 Share Posted March 24, 2008 These Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499638 Share on other sites More sharing options...
chronister Posted March 24, 2008 Share Posted March 24, 2008 are you getting any errors? try this line at the top of the page. <?php ini_set('error_reporting', E_ERROR); ?> Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499641 Share on other sites More sharing options...
Ruth Posted March 24, 2008 Author Share Posted March 24, 2008 I'll try this line but I wasn't getting any errors my var is not being set $movie_table_headings <tr> <td width = "33%" align = "center">$movie_name</td> <td align = "center">$movie_year</td> <td align = "center">$director</td> <td align = "center">$leadactor</td> <td align = "center">$movie_running_time</td> <td align = "center">$movie_health</td> <td align = "center">$calculate_review</td> $calculate_review isn't being set Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499647 Share on other sites More sharing options...
Ruth Posted March 24, 2008 Author Share Posted March 24, 2008 I tired ini_set('error_reporting', E_ERROR); and no errors Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499650 Share on other sites More sharing options...
BlueSkyIS Posted March 24, 2008 Share Posted March 24, 2008 i think you mean this: function calculate_avg () { $global = $calculate_review; to be this: function calculate_avg () { global $calculate_review; Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499654 Share on other sites More sharing options...
Ruth Posted March 24, 2008 Author Share Posted March 24, 2008 I get Syntax Error: unexpected '$calculate_review' Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499656 Share on other sites More sharing options...
BlueSkyIS Posted March 24, 2008 Share Posted March 24, 2008 works fine here. <?php $calculate_review = "hello world"; function calculate_avg () { global $calculate_review; echo $calculate_review; } calculate_avg(); ?> Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499663 Share on other sites More sharing options...
Ruth Posted March 24, 2008 Author Share Posted March 24, 2008 I had $global that was the problem thanks for your help. Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499669 Share on other sites More sharing options...
Barand Posted March 24, 2008 Share Posted March 24, 2008 you could use this to get the average $query = "SELECT AVG(review_rating) as av_rating FROM reviews WHERE review_movie_id = '".$_GET['movie_id']."'"; Link to comment https://forums.phpfreaks.com/topic/97651-function/#findComment-499739 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.