Jump to content

[SOLVED] Is this the best way to do this?


tbare

Recommended Posts

Hello, all... i've got a list of files and i'm trying to have to manually define as little as possible... here's what i've got:

<?php
//new file
$file = "Abbott and Costello - Who's On First.avi";
$title = "Abbott and Costello - Who's On First";
$thumb = "wof_thumb.jpg";
list($width, $height) = getimagesize("$thumb_path/$thumb");
$thumb_width = "$width";
$thumb_height = "$height";
include("text_files/video_info.php");
$size = filesize("$file_path/$file");
$file = preg_replace("/'/", "&#39;" , $file);
$title = preg_replace("/'/", "&#39;" , $title);

print "<tr><td>\n";
print "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
print "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
print "<a href='$file_path/$file'>download file</a> | file size: ";
include("text_files/filesize.php");
print "$size<br>\n";
print "duration : ";
print "$duration\n";
print "</td></tr>\n";
?>

where video_info.php contains:

<?php
$movie = new ffmpeg_movie('files/video/' . $file);
$video_height = $movie->getFrameHeight();
$video_width = $movie->getFrameWidth();

$duration = $movie->getDuration();
$minutes = floor($duration / 60);
$seconds = round($duration % 60,2);
if($seconds < 10){
$seconds = "0" . $seconds;
}
$duration = $minutes.':'.$seconds;

?>

then the same thing for the next file and so on...

 

this works great, but i'm wondering if there's a better way to do this to where i'm not using <rant>SO MUCH FREAKIN CODE!</rant> :)

 

i'm open to suggestions... someone suggested using an array, and i'm open to that... i just want this to be as compact as possible, basically...

 

Thanks in advance :)

 

Link to comment
Share on other sites

Well, if you can guarantee the naming convention will be the title like that... you can parse the title by using substrings to cut off the .avi. 

 

You could also write an array like this.

 

$files = array( 
             array(
               'file'=>'Abbot and Costello - Who's On First.avi', 
               'title'=>'Abbot and Costello- Who's on First'
               'thumb'=>'wof_thumb.jpg'
             )
             array(
               'file'=>'Abbot and Costello - Who's On First2.avi', 
               'title'=>'Abbot and Costello- Who's on First2'
               'thumb'=>'wof_thumb2.jpg'
             )
           );

 

And then turn the printing into a function, and use something like this.

 

foreach($files as $file){
  printLinkRow($file);
}

 

That way you can reuse most of your code.

Link to comment
Share on other sites

i like that...

so, using that array style, i could do this:

<?php

//create the array as in your example here

foreach($files as $file){
  printLinkRow($file);
  print "<tr><td>\n";
  print "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  print "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  print "<a href='$file_path/$file'>download file</a> | file size: ";
  include("text_files/filesize.php");
  print "$size<br>\n";
  print "duration : ";
  print "$duration\n";
  print "</td></tr>\n";
}
?>

 

one thing i noticed there is that after 'file'=>'blah.avi' you have a comma, but not after title... should there be one there? (again, i'm new to arrays...)

 

thanks for the help

Link to comment
Share on other sites

Yes there should be LOL... good catch.

 

And I guess you could do this

foreach($files as $file){
  printLinkRow($file);
  print "<tr><td>\n";
  print "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  print "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  print "<a href='$file_path/$file'>download file</a> | file size: ";
  include("text_files/filesize.php");
  print "$size<br>\n";
  print "duration : ";
  print "$duration\n";
  print "</td></tr>\n";
}

But I was suggesting something like this

<?php
include("text_files/filesize.php");

$files = array( 
             array(
               'file'=>'Abbot and Costello - Who\'s On First.avi', 
               'title'=>'Abbot and Costello- Who\'s on First',
               'thumb'=>'wof_thumb.jpg'
             )
             array(
               'file'=>'Abbot and Costello - Who\'s On First2.avi', 
               'title'=>'Abbot and Costello- Who\'s on First2',
               'thumb'=>'wof_thumb2.jpg'
             )
           );

function printLinkRow($file){
  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$file'>download file</a> | file size: ";
  //Note, you don't want to include this file over & over
  $message .= "$size<br>\n";
  $message .= "duration : ";
  $message .= "$duration\n";
  $message .= "</td></tr>\n";
  return($message);
}

foreach($files as $file){
  print(printLinkRow($file));
}

 

P.S. Threw the backslashes in so that we didn't prematurely end the string.  (Single quotes are a little faster since php doesn't try to parse through them looking for variables.

Link to comment
Share on other sites

<?php
include("text_files/filesize.php");
$file_path = "files/video";
$thumb_path = "$file_path/thumbs";

$files = array( 
             array(
               'file'=>'Abbot and Costello - Who\'s On First.avi', 
               'title'=>'Abbot and Costello- Who\'s on First',
               'thumb'=>'wof_thumb.jpg'
             )
             array(
               'file'=>'getamac-misprint.avi', 
               'title'=>'Mac Ad: Misprint',
               'thumb'=>'getamac-misprint_thumb.png';
             )
           );

function printLinkRow($file){
  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$file'>download file</a> | file size: ";
  $message .= "$size<br>\n";
  $message .= "duration : ";
  $message .= "$duration\n";
  $message .= "</td></tr>\n";
  return($message);
}

foreach($files as $file){
  print(printLinkRow($file));
}
?>

 

returns:

Parse error: syntax error, unexpected T_ARRAY, expecting ')' in /home/tbare/wannafork/text_files/humor_video2.php on line 12

 

?? i don't know enough about arrays to know why what you showed didn't work...

Link to comment
Share on other sites

also... why do i not want to include the filesize.php file there every time? just including it once didn't want to work...

here's what it contains:

<?php
//make file sizes user friendly by converting to KB, MB, or GB

if($size >= 1024 && $size < 1048576) {                 //make KB
$size = number_format(($size) / 1024, 2);
$size = "$size KB\n";
}
else if($size >= 1048576 && $size < 1073741824) {     //make MB
$size = number_format(($size) / 1048576, 2);
$size = "$size MB\n";
}
else if($size >= 1073741824) {                         //make GB
$size = number_format(($size) / 1073741824, 2);
$size = "$size  GB\n";
}
else {                                                 //leave as bytes.
$size = "$size bytes";
}
?>

 

just wondering... i would think that i would have to include that after i set *$size = filesize("$file_path/$file");* ??

Link to comment
Share on other sites

You were right about the include... sorry I didn't think about what I was doing very well :)

 

As for the parse error... I forgot another, LOL... I'm just messing those up all over today.

<?php


$files = array( 
             array(
               'file'=>'Abbot and Costello - Who\'s On First.avi', 
               'title'=>'Abbot and Costello- Who\'s on First',
               'thumb'=>'wof_thumb.jpg'
             ),                                                                                                 //forgot this comma
             array(
               'file'=>'Abbot and Costello - Who\'s On First2.avi', 
               'title'=>'Abbot and Costello- Who\'s on First2',
               'thumb'=>'wof_thumb2.jpg'
             )
           );

function printLinkRow($file){
  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$file'>download file</a> | file size: ";
  include("text_files/filesize.php");                                                                          //put this back in
  $message .= "$size<br>\n";
  $message .= "duration : ";
  $message .= "$duration\n";
  $message .= "</td></tr>\n";
  return($message);
}

foreach($files as $file){
  print(printLinkRow($file));
}

Link to comment
Share on other sites

<?php
$file_path = "files/video";
$thumb_path = "$file_path/thumbs";

$files = array( 
             array(
               'file'=>'Abbot and Costello - Who\'s On First.avi', 
               'title'=>'Abbot and Costello- Who\'s on First',
               'thumb'=>'wof_thumb.jpg'
             ),
             array(
               'file'=>'CircleCircleDotDot.avi', 
               'title'=>'I got my cootie shot.',
               'thumb'=>'CircleCircleDotDot_thumb.png'
             )
           );

function printLinkRow($file){
  list($thumb_width, $thumb_height) = getimagesize("$thumb_path/$thumb");
  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$file&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$file&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$file'>download file</a> | file size: ";
  include("text_files/filesize.php");
  $message .= "$size<br>\n";
  $message .= "</td></tr>\n";
  return($message);
}

foreach($files as $file){
  print(printLinkRow($file));
}

 

and i get this: http://www.wannafork.com/humor_video2.php

originally, looked like this: http://www.wannafork.com/humor_video.php

 

obviously, the latter has more files, but you get the idea...

Link to comment
Share on other sites

got it... (finally)

<?php


$files = array( 
             array(
               'file'=>'Abbott and Costello - Who\'s On First.avi', 
               'title'=>'Abbott and Costello- Who\'s on First',
               'thumb'=>'wof_thumb.jpg'
             ),
             array(
               'file'=>'CircleCircleDotDot.avi', 
               'title'=>'I got my cootie shot.',
               'thumb'=>'CircleCircleDotDot_thumb.png'
             )
           );

function printLinkRow($file){
  $file_path = "files/video";
  $thumb_path = "$file_path/thumbs";
  $filez = $file['file'];
  $title = $file['title'];
  $thumb = $file['thumb'];
  $size = filesize("$file_path/$filez");
  list($thumb_width, $thumb_height) = getimagesize("$thumb_path/$thumb");
  $filez = preg_replace("/'/", "&#39;" , $filez);
  $title = preg_replace("/'/", "&#39;" , $title);
  $message =  "<tr><td>\n";
  $message .= "<a href='humor_video_play.php?file=$filez&title=$title'>\n<img border='0' src='$thumb_path/$thumb' width='$thumb_width' height='$thumb_height' alt=' '></a></td>\n";
  $message .= "<td><a href='humor_video_play.php?file=$filez&title=$title'>$title</a><br>\n";
  $message .= "<a href='$file_path/$filez'>download file</a> | file size: ";
  include("text_files/filesize.php");
  $message .= "$size<br>\n";
  $message .= "</td></tr>\n";
  return($message);
}

foreach($files as $file){
  print(printLinkRow($file));
}
?>

 

seen @http://www.wannafork.com/humor_video2.php

 

eventually, when i get the rest of the videos put in the array, i'll make it live, then on to pagination! (woohoo)..

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.