tbare Posted December 18, 2007 Share Posted December 18, 2007 I'm trying to learn functions, but what i've got here, i'm getting "Warning: Division by zero..." <?php function printDisplayItemsNumber(){ global $itemsPerPage; global $nav2; $itemsPerPage = 10; if (isset($_GET['items']) && is_numeric($_GET['items'])){ $itemsPerPage = $_GET['items']; } $nav2 = "</td>\n<td width='40%'>\n"; $nav2 .= "items per page: [10] 25 50"; //will be set up differently, just trying to get it done right now... $nav2 .= "</td></tr></table>\n"; return $nav2; } $sidePages = 4; $page = 1; if (isset($_GET['p']) && is_numeric($_GET['p'])){ $page=$_GET['p']; } $files_pages = ceil(count($files)/$itemsPerPage); //this line is dividing by zero //more stuff here ?> again, i'm trying to learn here... what am i doing wrong? please help Quote Link to comment Share on other sites More sharing options...
revraz Posted December 18, 2007 Share Posted December 18, 2007 Where are you calling the function in your code? You define it, but don't call it. Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Where do you define $files ? Quote Link to comment Share on other sites More sharing options...
tbare Posted December 18, 2007 Author Share Posted December 18, 2007 it's called further down... the code's a little long... but here it is.. <?php //array defining files, thumb, and title here function printLinkRow($file){ $type = "random"; $file_path = "files/video/$type"; $thumb_path = "$file_path/thumbs"; $filez = $file['file']; $title = $file['title']; $thumb = $file['thumb']; $size = filesize("$file_path/$filez"); include("text_files/filesize.php"); list($thumb_width, $thumb_height) = getimagesize("$thumb_path/$thumb"); if($thumb_width > 200){ $difference = $thumb_width / 200; $thumb_width = $thumb_width / $difference; $thumb_height = $thumb_height / $difference; } include("text_files/video_info.php"); $filez = preg_replace("/'/", "'" , $filez); $title = preg_replace("/'/", "'" , $title); $alt = $title; $message = "<tr><td width='35%'>\n"; $message .= "<a href='humor_video_play.php?file=$filez&title=$title&type=$type'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt='$alt'></a></td>\n"; $message .= "<td><a href='humor_video_play.php?file=$filez&title=$title&type=$type'>$title</a><br>\n"; $message .= "<a href='$file_path/$filez'>download file</a> | file size: "; $message .= "$size<br>\n"; $message .= "duration : "; $message .= "$duration<br>\n"; $message .= "</td></tr>\n"; return($message); } function addToNav($addPage) { global $page; global $nav; if ($addPage != $page){ $nav .= "<a href='humor_video_random.php?p=".$addPage."'>".$addPage."</a> "; }else{ $nav .= "[".$addPage."] "; } } function printPageNav(){ global $page; global $files_pages; global $nav; global $sidePages; $nav = "<table width='100%'><tr><td width='60%'>\n"; $nav .= "<div style='text-align:center;'>page: "; $excerpt_start = $page-$sidePages; $excerpt_end = $page+$sidePages; if ($excerpt_start < 2){ $excerpt_start = 2; } if ($excerpt_end > ($files_pages-1)){ $excerpt_end = $files_pages-1; } addToNav(1); if ($excerpt_start != 2){ $nav .= "... "; } for ($i = $excerpt_start; $i <= $excerpt_end; $i++){ addToNav($i); } if ($excerpt_end != ($files_pages-1)){ $nav .= "... "; } addToNav($files_pages); $nav .= "</div>\n"; return $nav; } function printDisplayItemsNumber(){ global $itemsPerPage; global $nav2; $itemsPerPage = 10; if (isset($_GET['items']) && is_numeric($_GET['items'])){ $itemsPerPage = $_GET['items']; } $nav2 = "</td>\n<td width='40%'>\n"; $nav2 .= "items per page: [10] 25 50"; $nav2 .= "</td></tr></table>\n"; return $nav2; } $sidePages = 4; $page = 1; if (isset($_GET['p']) && is_numeric($_GET['p'])){ $page=$_GET['p']; } $files_pages = ceil(count($files)/$itemsPerPage); if ($page > $files_pages){ $page = $files_pages; } if ($page < 1){ $page = 1; } $display_files = array_slice($files, ($page-1)*$itemsPerPage, $itemsPerPage); print(printPageNav()); print(printDisplayItemsNumber()); print "<table width='100%' border='0' cellspacing='10' cellpadding='0'>"; foreach($display_files as $file){ print(printLinkRow($file)); } print "</table>"; print(printPageNav()); print(printDisplayItemsNumber()); ?> Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 We don't need to see the calling code. Where do you define $files ? Quote Link to comment Share on other sites More sharing options...
tbare Posted December 18, 2007 Author Share Posted December 18, 2007 edited above while you responded.. you guys are quick today! Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 In this line.... $files_pages = ceil(count($files)/$itemsPerPage); you use the variable $files. $files is not defined anywhere in your code! Quote Link to comment Share on other sites More sharing options...
tbare Posted December 18, 2007 Author Share Posted December 18, 2007 <?php $files = array( array( 'file'=>"party_in_the_stomach.avi", 'title'=>"Jim Breuer: Party in the stomach", 'thumb'=>"party_in_the_stomach_thumb.png" ), // more here... ); Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 Add this line... $itemsPerPage = 10; Just before.... $sidePages = 4; You need to have it defined shomewhere outside your functions to make it available. ps: Globals within functions should really be avioded. Kinda defeat the purpose a little. Quote Link to comment Share on other sites More sharing options...
tbare Posted December 18, 2007 Author Share Posted December 18, 2007 but if i do that, when i finally do get it to where the user can set the number to display (10, 25, 50) won't setting that value override the $_GET['items'] from the URL? or would i have to do: if (isset($_GET['items']) && is_numeric($_GET['items'])){ $itemsPerPage = $_GET['items']; } down there, too? Quote Link to comment Share on other sites More sharing options...
trq Posted December 18, 2007 Share Posted December 18, 2007 No, because all that logic will still be called within the function. Quote Link to comment Share on other sites More sharing options...
tbare Posted December 18, 2007 Author Share Posted December 18, 2007 well, got it to work now... i did have to put if (isset($_GET['items']) && ($_GET['items'] == 10 || $_GET['items'] == 25 || $_GET['items'] == 50)){ $itemsPerPage = $_GET['items']; } under "$itemsPerPage = 10;" to get it to work properly... but, again.. it works thanks for all the help! 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.