Dynamic HTML Table with Pagination (Flat File)

Discussion in 'PHP' started by johnd123, Aug 10, 2013.

  1. #1
    Hi

    I have a list of youtube videos in a flat file database. I would like to print a thumbnail of these videos out to the end user in a table.

    The results need to have pagination as well as a dynamic table so i can display say 3 rows and 3 columns per page.

    The column amount will also need to be adjustable in case i wish to change the columns from 3 to 4 for example:

    
    
    
    $video_file_data[];  //This is the array in which all video info will be stored
    $start = $_REQUEST['start'];  //pagination setting
    $videos_per_page = 9;  //Show 9 Vids per page
    $table_columns = 3;  //Amount of columns needed to display results per page
    $num = count ($video_file_data);  //Number of lines in data array
    $max_pages = @ceil($num / $videos_per_page);  //Maximum number of Pages
    $cur = @ceil($start / $videos_per_page)+1;
    for($i=$start;$i<min($num,($start+$videos_per_page)+0);$i++)
     {
     list($date_added, $video_title, $youtube_url) = explode('|', trim($video_file_data[$i]));
    
      // <--- Table needs to go
    
     }
    
    
    
    Code (markup):
    The date from the file is stored in

    $video_file_data[]
    PHP:
    The text file data would look like this:

    
    date 1."|".title 1."|".youtube url 1
    date 2."|".title 2."|".youtube url 2
    date 3."|".title 3."|".youtube url 3
    date 4."|".title 4."|".youtube url 4
    
    PHP:
    The above code displays the results with pagination but i need help with adding the dynamic table
    with the amount of colums indicated by

    $table_columns = 3;
    PHP:
    Can anyone please help :)

    This is a Flat file project so NO SQL - I have no idea how to use SQL and its overkill for this project

    many thanks
     
    Solved! View solution.
    johnd123, Aug 10, 2013 IP
  2. #2
    I have created a function that you can use:
    function buildTable($array, $number_of_columns)
    {
        $rows =  array_chunk($array, $number_of_columns);
        $last_row = count($rows)-1;
        $rows[$last_row] = array_pad($rows[ $last_row], $number_of_columns, "");
        echo "<table border=1>";
        foreach($rows as $row)
        {
            echo "<tr>";
            foreach($row as $data)
                echo "<td>".$data."</td>";
            echo "</tr>";
          
        }
        echo "</table>";
    }
    PHP:
    To use this function create a separate list of videos that need to go on this page and add it to an array. You can use the same method you have used to separate the title, url and date, but now format this in nice way and add it to an array. :
    
    
    $vid_data = array();
    for($i=$start;$i<min($num,($start+$videos_per_page)+0);$i++)
    {
    list($date_added, $video_title, $youtube_url) = explode('|', trim($video_file_data[$i]));
    $vid_data[] = "<p>Title: $video_title<br />
    URL: $youtube_url<br />
    Date: $date_added</p>";
      // <--- Table needs to go
    }
    
    PHP:
    Then you can call
    
    buildTable($vid_data, $table_columns);
    PHP:
    to actually create the table
     
    samyak, Aug 13, 2013 IP
  3. johnd123

    johnd123 Active Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #3
    Hi Samyak
    Really appropriate you taking the time to reply and help with this :)
    Ill try your code

    Many thanks
     
    johnd123, Aug 13, 2013 IP
  4. johnd123

    johnd123 Active Member

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    56
    #4
    Thanks after a little alteration that code worked a treat :)

    I changed the "echo" so i could just return the who table with you function

    
    function BuildTable($array, $number_of_columns)
    {
    $rows =  array_chunk($array, $number_of_columns);
    $last_row = count($rows)-1;
    $rows[$last_row] = array_pad($rows[ $last_row], $number_of_columns, "");
    
    $table_start = "<table border=0>";
    
    foreach($rows as $row)
    {
    $table_contents = $table_contents . "<tr>";
    foreach($row as $data)
    $table_contents = $table_contents . "<td>".$data."</td>";
    $table_contents = $table_contents . "</tr>";
    }
    $table_end = "</table>";
    
    $table = $table_start.$table_contents.$table_end;
    
    return $table;
    }
    
    PHP:
    Thanks so much for the help!
     
    johnd123, Aug 13, 2013 IP
  5. samyak

    samyak Active Member

    Messages:
    280
    Likes Received:
    7
    Best Answers:
    4
    Trophy Points:
    90
    #5
    Glad i could help :)
     
    samyak, Aug 13, 2013 IP