jimboppin Posted March 26, 2017 Share Posted March 26, 2017 (edited) Hi, ive got a few functions now for my media and felt it would be a good idea to put them in a class, but i cant seem to get my variables from outside...here is my class: class media { //START MEDIA private $class_var; //get media from media directory. public function get_user_defined_media($media_cat, $display_recent) { if (!empty($this->class_var = $media_id)) { if ($this->class_var = $media_id == $media_cat) { $display_recent = null; } } $files = glob(MEDIA_BASEDIR.$media_cat.'/*.txt'); usort($files, function($x, $y) { return $x < $y; }); $recent_content = array_slice($files, 0, $display_recent); foreach ($recent_content as $media) { $line = file($media); $media_title = ($this->class_var = $purifier->purify($line[0])); $date_time = basename($media, '.txt'); $content = file_get_contents($media); $content = str_replace($media_title,'',$content); thememediatop($media_title, $media_cat, $this->class_var = $media_id); print_r ($this->class_var = $purifier->purify($content)); thememediabottom($date_time); } } //get media go back link when in view all. public function media_goback_link () { if (!empty($this->class_var = $media_id)) { print ("<p><a href='./?page=Media'>↩ Go Back To Media</a></p>\n"); } } //require the html for media when no catagorie is selected. public function get_media_page() { if (empty($this->class_var = $media_id)) { require_once PAGES_BASEDIR.'Media/page.html'; themeopendiv('Media Statistics'); $this->media_stats(null); themeclosediv(); } } //media stats block public function media_stats($media_stats_display) { $catagories = glob(MEDIA_BASEDIR.'*', GLOB_ONLYDIR); $catagories_slice = array_slice($catagories, 0, $media_stats_display); $totalfiles = glob(MEDIA_BASEDIR.'*/*.txt'); if ($totalfiles !== false) { $totalfilecount = count($totalfiles); } else { $totalfilecount = 0; } if ($catagories !== false) { $catcount = count($catagories); } else { $catcount = 0; } if ($media_stats_display == null) { print ("<p>All Catagories.<br>\n"); } else { if ($catcount < $media_stats_display) { $media_stats_display = $catcount; } print ("<p>Displaying <strong>{$media_stats_display}</strong> of <strong>{$catcount}</strong> Catagories.<br>\n"); } print ("There are <strong>{$totalfilecount}</strong> total posts.<p/>\n"); $catagories_slice = str_replace(MEDIA_BASEDIR,'',$catagories_slice); foreach ($catagories_slice as $catagorie) { $files = glob(MEDIA_BASEDIR.$catagorie.'/*.txt'); if ($files !== false) { $filecount = count($files); } else { $filecount = 0; } print ("<strong>{$filecount}</strong> in <a href='./?page=Media&id={$catagorie}'>{$catagorie}</a><br>\n"); } } //media index page with pagination public function media_index() { $media_files = glob(MEDIA_BASEDIR.$this->class_var = $media_id.'/*.txt'); usort($media_files, function($x, $y) { return $x < $y; }); $row_count = $this->class_var = $media_row_count; $total_pages = ceil(count($media_files)/$row_count); if (isset($_GET['p']) && (is_numeric($_GET['p']))) { $p = htmlentities($_GET['p'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); if ($p > $total_pages) { $p = 1; } } else { $p = 1; } $offset = ($p-1)*$row_count; $media_files = array_slice($media_files, $offset, $row_count); foreach ($media_files as $media) { $line = file($media); $media_title = ($this->class_var = $purifier->purify($line[0])); $date_time = basename($media, '.txt'); $content = file_get_contents($media); $content = str_replace($media_title,'',$content); thememediatop($media_title, $this->media_id, $this->media_id); print_r ($this->class_var = $purifier->purify($content)); thememediabottom($date_time); } print ("<div style='text-align:center;'>"); if ($total_pages > 1) { print ("<p>Currently viewing page {$p} of {$total_pages}</p>\n"); if ($p != 1) { print ("⇽ <a href='./?page=Media&id=".$this->class_var = $media_id."&p=".($p-1)."'>Prev</a> \n"); } $media_pages = range(1, $total_pages); foreach ($media_pages as $pages) { if ($p != $pages) { $link_num = $pages; } else { $link_num = ("<strong>{$pages}</strong>"); } print (" <a href='./?page=Media&id=".$this->class_var = $media_id."&p={$pages}'>{$link_num}</a> \n"); } if ($p != $total_pages) { print (" <a href='./?page=Media&id=".$this->class_var = $media_id."&p=".($p+1)."'>Next</a> ⇾\n"); } } print ("</div>\n"); } //END MEDIA } in my mainfile i add $media = new media($purifier, $media_id, $media_row_count); then calling my functions: $media->media_index();//for media page $media->get_user_defined_media('Announcements', 1);//to place other pages $media->media_stats(4);//getting stats for media - 4 is the number of catagories to show i would like to get this working as it shrinks the number of variables i have to useany help would be great,thanks Edited March 26, 2017 by jimboppin Quote Link to comment Share on other sites More sharing options...
Jacques1 Posted March 26, 2017 Share Posted March 26, 2017 (edited) Several things: When you have no experience with object-oriented programming, don't start with hundreds of lines of code. Nobody wants to debug that. Write simple classes until you know what you're doing, then move to more complex ones. Classes aren't just big collections of functions. This is not how OOP works. Replace your current editor with a proper IDE which automatically shows obvious errors in the code. Then we don't have to waste our time with routine stuff. Be specific. Giving us 250 lines of code with the comment “doesn't work” doesn't work. Edited March 26, 2017 by Jacques1 Quote Link to comment Share on other sites More sharing options...
jimboppin Posted March 26, 2017 Author Share Posted March 26, 2017 so how do i wrap my media functions with a simple class? i only want a class because of how many times my variables are used and if i can link my variables from outside my class to my class for my functions then ive shrunk my code? i have reverted to befor the class heres my code.. //START MEDIA //get media from media directory. function get_user_defined_media($media_cat, $display_recent, $purifier, $media_id) { if (!empty($media_id)) { if ($media_id == $media_cat) { $display_recent = null; } } $files = glob(MEDIA_BASEDIR.$media_cat.'/*.txt'); usort($files, function($x, $y) { return $x < $y; }); $recent_content = array_slice($files, 0, $display_recent); foreach ($recent_content as $media) { $line = file($media); $media_title = ($purifier->purify($line[0])); $date_time = basename($media, '.txt'); $content = file_get_contents($media); $content = str_replace($media_title,'',$content); thememediatop($media_title, $media_cat, $media_id); print_r ($purifier->purify($content)); thememediabottom($date_time); } } //get media go back link when in view all. function media_goback_link ($media_id) { if (!empty($media_id)) { print ("<p><a href='./?page=Media'>↩ Go Back To Media</a></p>\n"); } } //require the html for media when no catagorie is selected. function get_media_page($media_id) { if (empty($media_id)) { require_once PAGES_BASEDIR.'Media/page.html'; themeopendiv('Media Statistics'); media_stats(null); themeclosediv(); } } //media stats block function media_stats($media_stats_display) { $catagories = glob(MEDIA_BASEDIR.'*', GLOB_ONLYDIR); $catagories_slice = array_slice($catagories, 0, $media_stats_display); $totalfiles = glob(MEDIA_BASEDIR.'*/*.txt'); if ($totalfiles !== false) { $totalfilecount = count($totalfiles); } else { $totalfilecount = 0; } if ($catagories !== false) { $catcount = count($catagories); } else { $catcount = 0; } if ($media_stats_display == null) { print ("<p>All Catagories.<br>\n"); } else { if ($catcount < $media_stats_display) { $media_stats_display = $catcount; } print ("<p>Displaying <strong>{$media_stats_display}</strong> of <strong>{$catcount}</strong> Catagories.<br>\n"); } print ("There are <strong>{$totalfilecount}</strong> total posts.<p/>\n"); $catagories_slice = str_replace(MEDIA_BASEDIR,'',$catagories_slice); foreach ($catagories_slice as $catagorie) { $files = glob(MEDIA_BASEDIR.$catagorie.'/*.txt'); if ($files !== false) { $filecount = count($files); } else { $filecount = 0; } print ("<strong>{$filecount}</strong> in <a href='./?page=Media&id={$catagorie}'>{$catagorie}</a><br>\n"); } } //media index page with pagination function media_index($purifier, $media_id, $media_row_count) { $media_files = glob(MEDIA_BASEDIR.$media_id.'/*.txt'); usort($media_files, function($x, $y) { return $x < $y; }); $row_count = $media_row_count; $total_pages = ceil(count($media_files)/$row_count); if (isset($_GET['p']) && (is_numeric($_GET['p']))) { $p = htmlentities($_GET['p'], ENT_QUOTES | ENT_SUBSTITUTE, 'UTF-8'); if ($p > $total_pages) { $p = 1; } } else { $p = 1; } $offset = ($p-1)*$row_count; $media_files = array_slice($media_files, $offset, $row_count); foreach ($media_files as $media) { $line = file($media); $media_title = ($purifier->purify($line[0])); $date_time = basename($media, '.txt'); $content = file_get_contents($media); $content = str_replace($media_title,'',$content); thememediatop($media_title, $media_id, $media_id); print_r ($purifier->purify($content)); thememediabottom($date_time); } print ("<div style='text-align:center;'>"); if ($total_pages > 1) { print ("<p>Currently viewing page {$p} of {$total_pages}</p>\n"); if ($p != 1) { print ("⇽ <a href='./?page=Media&id=".$media_id."&p=".($p-1)."'>Prev</a> \n"); } $media_pages = range(1, $total_pages); foreach ($media_pages as $pages) { if ($p != $pages) { $link_num = $pages; } else { $link_num = ("<strong>{$pages}</strong>"); } print (" <a href='./?page=Media&id=".$media_id."&p={$pages}'>{$link_num}</a> \n"); } if ($p != $total_pages) { print (" <a href='./?page=Media&id=".$media_id."&p=".($p+1)."'>Next</a> ⇾\n"); } } print ("</div>\n"); } //END MEDIA ignore the 124 lines of code. im interested in shrinking how many times i use the same variable in my functions ie function media_index($purifier, $media_id, $media_row_count) { $purifier, $media_id are the main ones use here but $media_row_count is needed to change how many posts i have in a pagination can you help me? 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.