How do insert array results into columns?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ShenPixel
    New Member
    • Oct 2009
    • 5

    How do insert array results into columns?

    I've been working with the following code and would like to output the results into a customizable column variable, how would I go about doing such a thing, thanks in advance for any help. I hard coded the columns in the code below, but I would like the variable to be set so that a new row will start after a set number of columns.

    Code:
    <?
    $allfiles = scandir('.');
    $goodfiles = array();
    foreach($allfiles as $f){
    	if(strpos($f,'.')!==0){
    		array_push($goodfiles,$f);
    	}
    }
    $pages = array_chunk($goodfiles, 10);
    $columns = 2;
    $pgkey = (int)$_GET['showpage']; // forces $_GET['showpage'] to be an integer
    $pages[$pgkey];
    echo "<table border='0' cellpading='2' cellspacing='2' align='center'><tr>";
    foreach($pages[$pgkey-1] as $file){ echo '<td align=center><img src=files/'.$file.'></td>'; 
    }
    echo "</tr></table>";
     ?>
    <? for($i=1; $i< count($pages)+1; $i++): ?>
         <a href="index.php?showpage=<? echo $i;?>"><? echo $i; ?></a>
    <? 
    endfor;
    ?>
  • code green
    Recognized Expert Top Contributor
    • Mar 2007
    • 1726

    #2
    This loops needs modifying
    Code:
    foreach($pages[$pgkey-1] as $file){ echo '<td align=center><img src=files/'.$file.'></td>';  
    }
    You simply need a column tally of how many columns have been written.
    When the set amount is reached, start a new row then reset the tally.

    Or you could use a for loop which already has a tally.
    Then check if the loop tally is an exact multiple of the set value, if so start a new row.

    Comment

    • ShenPixel
      New Member
      • Oct 2009
      • 5

      #3
      Thanks, I thought that was the problem, but I cannot wrap my head around it, I've tried the following for loop, I know I'm missing something simple.

      Code:
      for($i = 0; $i < $per_page; $i++) 
      {
      
        if($i % $columns == 0 and $i != 0) 
        {
          echo "</tr><tr>";
        }
       
        # Add a column
      echo "<td align='center'><img src=files/'.$file.'></td>";
      }

      Comment

      • code green
        Recognized Expert Top Contributor
        • Mar 2007
        • 1726

        #4
        This looks alright. What is happening when used?
        Don't forget to open the very first row and close the last as in the first post <table><tr>

        Comment

        • ShenPixel
          New Member
          • Oct 2009
          • 5

          #5
          thanks again, when I insert the for loop, I get pagination and a table with no columns.

          Code:
          <table border='0' cellpading='2' cellspacing='2' align='center'>
          <tr></tr>
          </table>
          This is what my combine script looks like:

          Code:
          <?
          $allfiles = scandir('.');
          $goodfiles = array();
          foreach($allfiles as $f){
          	if(strpos($f,'.')!==0){
          		array_push($goodfiles,$f);
          	}
          }
          $pages = array_chunk($goodfiles, 10);
          $columns = 2;
          $pgkey = (int)$_GET['showpage']; // forces $_GET['showpage'] to be an integer
          $pages[$pgkey];
          echo "<table border='0' cellpading='2' cellspacing='2' align='center'><tr>";
          foreach($pages[$pgkey-1] as $file){ 
          for($i = 0; $i < $per_page; $i++) 
          {
             if($i % $columns == 0 and $i != 0) 
             {
               echo "</tr><tr>";
             }
            
          # Add a column
          echo "<td align='center'><img src=files/'.$file.'></td>";
          }
          }
          echo "</tr></table>";
           ?>
          <? for($i=1; $i< count($pages)+1; $i++): ?>
               <a href="index2.php?showpage=<? echo $i;?>"><? echo $i; ?></a>
          <? 
          endfor;
          ?>

          Comment

          • code green
            Recognized Expert Top Contributor
            • Mar 2007
            • 1726

            #6
            An empty array and/or empty variables is the problem.
            Echo out their contents when testing to help yourself.
            For example what is the value of $per_page?
            what is in $pages?
            is there a value in $pgkey?

            Comment

            • ShenPixel
              New Member
              • Oct 2009
              • 5

              #7
              I changed:

              Code:
              foreach($pages[$pgkey-1] as $file){ 
              for($i = 0; $i < $per_page; $i++) 
               {
                 if($i % $columns == 0 and $i != 0) 
                 {
                   echo "</tr><tr>";
                }
              to

              Code:
              foreach($pages[$pgkey-1] as $file){ 
              for($i = 0; $i < $pages[$pgkey]; $i++) 
              {
                 if($i % $columns == 0 and $i != 0) 
                 {
                   echo "</tr><tr>";
                 }
              $per_page was empty and now I get an endless loop with two columns but one image

              I feel like pulling my hair out lol I know it's something simple I'm overlooking/leaving out.

              Comment

              • code green
                Recognized Expert Top Contributor
                • Mar 2007
                • 1726

                #8
                Code:
                foreach($pages[$pgkey-1] as $file){  
                for($i = 0; $i < $pages[$pgkey]; $i++)
                In the second loop the contents of the current $pages element is used as the loop limit.
                That is why it is hanging.
                $i < must be an integer.
                Do you mean $pgkey?

                Comment

                Working...