Jump to content

[SOLVED] help w/ functions and globals...


tbare

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/82192-solved-help-w-functions-and-globals/
Share on other sites

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("/'/", "&#39;" , $filez);
  $title = preg_replace("/'/", "&#39;" , $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());

?>

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?

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!

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.