Connecting Tech Pros Worldwide Help | Site Map

How do insert array results into columns?

Newbie
 
Join Date: Oct 2009
Posts: 5
#1: Oct 14 '09
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.

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $allfiles = scandir('.');
  3. $goodfiles = array();
  4. foreach($allfiles as $f){
  5.     if(strpos($f,'.')!==0){
  6.         array_push($goodfiles,$f);
  7.     }
  8. }
  9. $pages = array_chunk($goodfiles, 10);
  10. $columns = 2;
  11. $pgkey = (int)$_GET['showpage']; // forces $_GET['showpage'] to be an integer
  12. $pages[$pgkey];
  13. echo "<table border='0' cellpading='2' cellspacing='2' align='center'><tr>";
  14. foreach($pages[$pgkey-1] as $file){ echo '<td align=center><img src=files/'.$file.'></td>'; 
  15. }
  16. echo "</tr></table>";
  17.  ?>
  18. <? for($i=1; $i< count($pages)+1; $i++): ?>
  19.      <a href="index.php?showpage=<? echo $i;?>"><? echo $i; ?></a>
  20. <? 
  21. endfor;
  22. ?>
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#2: Oct 14 '09

re: How do insert array results into columns?


This loops needs modifying
Expand|Select|Wrap|Line Numbers
  1. foreach($pages[$pgkey-1] as $file){ echo '<td align=center><img src=files/'.$file.'></td>';  
  2. }
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.
Newbie
 
Join Date: Oct 2009
Posts: 5
#3: Oct 14 '09

re: How do insert array results into columns?


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.

Expand|Select|Wrap|Line Numbers
  1. for($i = 0; $i < $per_page; $i++) 
  2. {
  3.  
  4.   if($i % $columns == 0 and $i != 0) 
  5.   {
  6.     echo "</tr><tr>";
  7.   }
  8.  
  9.   # Add a column
  10. echo "<td align='center'><img src=files/'.$file.'></td>";
  11. }
  12.  
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#4: Oct 14 '09

re: How do insert array results into columns?


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>
Newbie
 
Join Date: Oct 2009
Posts: 5
#5: Oct 14 '09

re: How do insert array results into columns?


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

Expand|Select|Wrap|Line Numbers
  1. <table border='0' cellpading='2' cellspacing='2' align='center'>
  2. <tr></tr>
  3. </table>
  4.  
This is what my combine script looks like:

Expand|Select|Wrap|Line Numbers
  1. <?
  2. $allfiles = scandir('.');
  3. $goodfiles = array();
  4. foreach($allfiles as $f){
  5.     if(strpos($f,'.')!==0){
  6.         array_push($goodfiles,$f);
  7.     }
  8. }
  9. $pages = array_chunk($goodfiles, 10);
  10. $columns = 2;
  11. $pgkey = (int)$_GET['showpage']; // forces $_GET['showpage'] to be an integer
  12. $pages[$pgkey];
  13. echo "<table border='0' cellpading='2' cellspacing='2' align='center'><tr>";
  14. foreach($pages[$pgkey-1] as $file){ 
  15. for($i = 0; $i < $per_page; $i++) 
  16. {
  17.    if($i % $columns == 0 and $i != 0) 
  18.    {
  19.      echo "</tr><tr>";
  20.    }
  21.  
  22. # Add a column
  23. echo "<td align='center'><img src=files/'.$file.'></td>";
  24. }
  25. }
  26. echo "</tr></table>";
  27.  ?>
  28. <? for($i=1; $i< count($pages)+1; $i++): ?>
  29.      <a href="index2.php?showpage=<? echo $i;?>"><? echo $i; ?></a>
  30. <? 
  31. endfor;
  32. ?>
  33.  
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#6: Oct 14 '09

re: How do insert array results into columns?


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?
Newbie
 
Join Date: Oct 2009
Posts: 5
#7: Oct 14 '09

re: How do insert array results into columns?


I changed:

Expand|Select|Wrap|Line Numbers
  1. foreach($pages[$pgkey-1] as $file){ 
  2. for($i = 0; $i < $per_page; $i++) 
  3.  {
  4.    if($i % $columns == 0 and $i != 0) 
  5.    {
  6.      echo "</tr><tr>";
  7.   }
  8.  
to

Expand|Select|Wrap|Line Numbers
  1. foreach($pages[$pgkey-1] as $file){ 
  2. for($i = 0; $i < $pages[$pgkey]; $i++) 
  3. {
  4.    if($i % $columns == 0 and $i != 0) 
  5.    {
  6.      echo "</tr><tr>";
  7.    }
  8.  
$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.
code green's Avatar
Expert
 
Join Date: Mar 2007
Location: England
Posts: 1,076
#8: Oct 14 '09

re: How do insert array results into columns?


Expand|Select|Wrap|Line Numbers
  1. foreach($pages[$pgkey-1] as $file){  
  2. for($i = 0; $i < $pages[$pgkey]; $i++)  
  3.  
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?
Reply

Tags
array, php, scandir