Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

Array question

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

Array question

Postby jasonthomasfrance » Fri Jan 03, 2003 3:19 am

Hello,

I'm having some trouble understanding arrays... I've borrowed and rewritten scripting that gets the contents of a folder, sorts by file date, then prints out the contents.

I'm having trouble pulling certain information out, like only a certain number of files, and not the entire array.

I don't think I understand how my array is working... I grasp the keys/values but can't quite apply it to this code...

What I'd like to do is only print say the first 5 entries...

Here is the link to the page:
http://www.jasonthomasfrance.com/testscript/

And here is the coding I'm working with:
Code: Select all
<?php

echo "This script gets all image files, sorts by date, and prints them out.  Next I'm working out how to display X number per page and traverse the pages...<br><br> \n";

$dir='thumbs';
if ($handle = opendir($dir))

   $lasttime=0;
   $images=array();
    while (false !== ($file = readdir($handle)))
    {   
      if(is_file($file))$images[$file]=filemtime($file);
    }
}
arsort($images,SORT_NUMERIC);
reset($images);
while(list($filenames,$v)=each($images))
echo "<a href=\"$filenames\"><img src=\"$dir/$filenames\" alt=\"$filenames\" border=\"0\"></a> \n";
$result = count ($images);
echo "<br><br>File count: $result <br>";

reset($images);
for ($n = 0; $n < count($images); $n++) {
   $Line = each ($images);
   print ("$Line[0], ");
}
?>
jasonthomasfrance
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jan 03, 2003 3:07 am

Postby jasonthomasfrance » Fri Jan 03, 2003 4:57 am

Okay, using a for loop instead of a while will work...

for($i = 0; $i < 5; $i++) {
list($filenames,$v)=each($images);

But can you pull a range out of an array? Like 5-10?
jasonthomasfrance
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jan 03, 2003 3:07 am

Maybe you can use for

Postby them » Fri Jan 03, 2003 5:00 am

Hello,

An array is a list of data. You can look as it as a tabel. Every record has an unique identifier that could be a name but you can als call them bij the record nuber. If you have an array with this entry $array(Blue, Red, Green) you can call the value of red as $array[Red] but also as $array[1]. Keep in mind that it is counting up from 0 and not 1.

If you use this you can say he may only print out 5 items and than has to stop. Mak a hyperlink for next an previous or for the pages just add or remove 5 from you pointer and I think it wil work.

Good Luck
them
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Nov 29, 2002 5:29 am

Postby jasonthomasfrance » Fri Jan 03, 2003 5:06 am

How do you mean add 5 to the pointer?
jasonthomasfrance
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jan 03, 2003 3:07 am

Look at jason it's text

Postby them » Fri Jan 03, 2003 5:50 am

Let say you make a point that is standing as possition 0 now image 0 in te array will printed. In your site you say print picture 0 to 4. When you go to the next page you can count from 5 to 9 but whenm you go back you have to minus 10 (not 5 my fault) from the pointer to get back. And it works just like jason say's
them
New php-forum User
New php-forum User
 
Posts: 7
Joined: Fri Nov 29, 2002 5:29 am

Postby jasonthomasfrance » Sat Jan 04, 2003 3:42 am

All righty then. I got it figured out... With some serious help. *grin*

You can see it in action here: http://www.jasonthomasfrance.com/photos/

There's a couple bugs, but nothing too bad.

Here's the code:
Code: Select all
<?php
$dir='thumbs';
if ($handle = opendir($dir))

   $lasttime=0;
   $images=array();
    while (false !== ($file = readdir($handle)))
    {   
      if(is_file($file))$images[$file]=filemtime($file);
    }
}
arsort($images,SORT_NUMERIC);
reset($images);

if($display <= 0)
{
    $display = 5;
}

if(empty($page) || $page == 0)
{
    $offset=0;
    $page = 1;
}
else
    $offset=($page - 1) * $display;

$pagetotal = count($images)?count($images):0;

if($pagetotal < 10)
    $dispay = $pagetotal ;

if($pagetotal > $display)
{
    if($pagetotal > $display)
    {
        $page1=$pagetotal/$display;
        $page2=$pagetotal % $display;

        if($page2 != 0)
            $pagetotal = intval($page1) + 1;
        else
            $pagetotal = (int)$page1;
    }


$myArray = $images;
$start = ($page * $display) - $display;
$news = array_slice($myArray, $start, $display);

foreach($news as $key=>$value)
{
    print("<a href=\"$key\"><img src=\"$dir/$key\" alt=\"$key\" border=\"0\"></a> \n");
}

echo "<br><br>";

    if($page !=1)
    {
        $i = $page - 1;
        print "            <a href=\"$PHP_SELF?page=$i&display=$display\"> << prev</a>";
    }
    print " page $page of <b>$pagetotal</b> ";
    if($page != $pagetotal)
    {
        $i = $page + 1;
        print "            <a href=\"$PHP_SELF?page=$i&display=$display\">next>></a>";
    }

echo "<br><br><a href=\"index.as?page=$page&display=5\">5</a> | <a href=\"index.as?page=$page&display=10\">10</a> | <a href=\"index.as?page=$page&display=15\">15</a> | <a href=\"index.as?page=$page&display=20\">20</a>";

$result = count ($images);
echo "<br><br>this folder contains $result images<br>";
?>
[/code]
jasonthomasfrance
New php-forum User
New php-forum User
 
Posts: 6
Joined: Fri Jan 03, 2003 3:07 am


Return to PHP coding => General

Who is online

Users browsing this forum: Google [Bot] and 7 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.