PHP Help
- Started
- Last post
- 14 Responses
- D_Dot
Ok, I'm not a developer by any means, but I thought I'd take a stab at this solution a buddy of mine needs.
I've managed to make script that dynamically fills in a table 3 columns wide with every image in a directory (folder). What I need now is to be able to stop filling the table once there are 6 cells full (3 columns by 2 rows) then create a numbered breadcrumb list under the table that takes you to the next set.
can anyone point me to a resource for this sort of thing?Thanks,
D_Dot
- 3030
source please?
- ********0
just have a count to 6 then still an end table row etc and start new table etc... with your breadcrumbs inbetween
- exactlysection_014
- I know I should do this, the question is how to write it. Thanks.D_Dot
- doctor0
You can use a 'for' or a 'while' loop to do X while Y is < 7.
- D_Dot0
<?php
$path = "images/tees";
$path_large = "images/tees/large";
$title = 'Need formula here';
$extensions = Array('.gif','.jpg','.png');
$num_cols = 3;
$img_width = '215';$images = Array();
if(@$handle = opendir($path))
{
while(false!== ($file = readdir($handle)))
{
if($file!= '.' && $file!= '..' &&!is_dir($file) && in_array(substr($file,-4),$extensions))
{
$images[] = $file; // enters the image into
}
}
}echo "<table>\n";
echo "<colgroup>\n";
for($i=0;$i<$num_cols;$i++)
{
echo "<col width='".$img_width."' />\n";
}
echo "</colgroup>\n<tr>\n";if(count($images)>0)
{
$i=1; // sets column counter to 1
foreach($images as $image)
{
if($i==$num_cols+1)
{
echo "</tr>\n<tr>\n";
$i=1;
}
echo "<td><a href='$path_large/$image' rel='lightbox' title='$title'><img src='$path/$image' width='$img_width' /></a></td>\n";
$i++;
}
if($i <= $num_cols)
{
while($i<=$num_cols)
{
echo "<td> </td>\n";
$i++;
}
}
}
else
{
echo "<td>No images in specified folder</td>\n";
}
echo "</tr>\n</table>\n";
?>
- ********0
bloody hell, someone managed to post some code okay!
- dauntilus0
I wish I was one of the smart ones here that could decipher that. Im good with logic but Im bad with the syntax. They need to make some uber super smart code checker that completely fixes all your errors!
- I'm sure there's a ton of shit wrong with the code above. i pieced it together from other forum help...D_Dot
- Hey dauntilus, I've got one of the super-smart code checkers you're looking for — it's called a developer. =)********
- acescence0
well, you'll need to pass a page number to be able to nav between pages, so add a check at the top for page# and set it to 1 if it doesn't exist:
if (isset($_GET['page'])){
if (ctype_digit($_GET['page'])) {
$page = $_GET['page'];
} else {
die("invalid page number");
}
} else {
$page = 1;
}
- acescence0
now it's a simple matter of using the current page number, the number per page, and the count of how many images you have in you array to slice your array and get just the image you want on that page
- acescence0
oh and of course when you create the links to the pages they should be like myscript.php?page=2
- acescence0
ok, i'm avoiding work, so i'll write that bit for you as well:
$pagestart = ($page*$perpage)-$perpage;
$images = array_slice($images, $pagestart, $perpage);ok, so now if you pass this array to your code that builds the table, all should work. you'll just need to come up with the code to write out the page links. you can sort that one out ;)
- epikore0
How did you guys learn php at first? Books or online resources?
- acescence0
i learned some basics doing some online tutorials and reading the php docs, which are quite extensive. i then got an o'reilly book or something and gained a more thorough knowledge of it.
php wasn't my first language though, if it would be yours and you actually want a future in programming i would suggest python first.