...

View Full Version : Display last 5 Modified Pages



kochier
07-23-2008, 01:40 AM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map Limit 5") or die(mysql_error());

while($row = mysql_fetch_array($result)){

$list = $row['Pathname'];

echo " - " .date("m d Y", filemtime($_SERVER['"/home/kochier/www/absurdity981.com/"$list']));
echo '<br />';
}

include 'closedb.php';
?>

This is what I have so far, but all it displays is
- 12 31 1969
- 12 31 1969
- 12 31 1969
- 12 31 1969
- 12 31 1969

instead of the dates they were last modified. Any help would be nice as I'm stuck on this right now.

oesxyl
07-23-2008, 01:48 AM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map Limit 5") or die(mysql_error());

while($row = mysql_fetch_array($result)){

$list = $row['Pathname'];

echo " - " .date("m d Y", filemtime($_SERVER["/home/kochier/www/absurdity981.com/" . $list]));
echo '<br />';
}

include 'closedb.php';
?>
remove ' inside $_SERVER and use '.' to add $list.

regards

kochier
07-23-2008, 06:26 AM
Alright I've tried that and it still displays 12 31 1969 as the modified date.

Fou-Lu
07-23-2008, 07:48 AM
Why is this: $_SERVER["/home/kochier/www/absurdity981.com/" . $list] within you're $_SERVER superglobal? I'm taking a stab here, but I'm guessing that you don't actual add this index to the server superglobal, and if you do I'd suggest that you remove it. If anything, you may be wanting the PATH_TRANSLATED option, minus the SCRIPT_NAME part of it, so you can add you're own.

oesxyl
07-23-2008, 08:03 AM
while($row = mysql_fetch_array($result)){

$list = $row['Pathname'];

you use mysql_fetch_array and try to retrive 'Pathname'. Use mysql_fetch_assoc instead.

and check $list to be correct else filemtime will return 0
regards

kochier
07-24-2008, 05:09 PM
Thank you for your help, it's working now. Now is it possible to store this info in a table or array so I can sort it by the modified date?


<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());

while($row = mysql_fetch_assoc($result)){

$list = $row['Pathname'];
echo $list;

echo " - " .date( filemtime("/home/kochier/www/absurdity981.com/" . $list));
echo '<br />';
}

include 'closedb.php';
?>

oesxyl
07-24-2008, 05:28 PM
Thank you for your help, it's working now. Now is it possible to store this info in a table or array so I can sort it by the modified date?
yes. filemtime return a unix timestamp so you can store and sort easy.
it in a table as timestamp, maybe same table 'map', or fill an array with filemtime as values and $row['Pathname'] as key.


<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
$flist = array();
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
echo $list;
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$flist[$list] = $fmtime;
echo " - " .date("your format", $fmtime);
echo '<br />';
}
include 'closedb.php';
?>


regards

kochier
07-24-2008, 07:30 PM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$flist[$list] = $fmtime;
$insert = ("INSERT INTO map (Modified)
VALUES ('$fmtime'") or die('Failure to Insert data');
mysql_query($insert) or die(mysql_error());
echo " - " .date("d m Y", $fmtime);
echo ' ';
echo $list;
echo '<br />';
}
include 'closedb.php';
?>

Is what I have right now, but how do I ensure it makes the modified path with the right pathname? Like if $pathname = index modified = timestamp(for index). Or am I better off trying to figure out how to sort the timestamp in the array and not worry about the database?

oesxyl
07-24-2008, 07:43 PM
in my opinion, the insert must contain info about pathname:


$insert = "insert into map (Modified, Pathname) values (".$fmtime.", '".$list."')";


regards

kochier
07-24-2008, 08:03 PM
Problem with that is Pathname is a primary key, so I get a duplicate error when I try this out. I need to simply have it modify the modified column for the selected pathname I believe. I was thinking something like

mysql_query("ALTER TABLE map MODIFY Modified $fmtime") or die (mysql_error());

Though I think I'm using it fairly wrong at this point.

oesxyl
07-24-2008, 08:11 PM
Problem with that is Pathname is a primary key, so I get a duplicate error when I try this out. I need to simply have it modify the modified column for the selected pathname I believe.
I don't know what is your case, but a solution could be to use something else as primary key, for example define a unsigned int field 'id' and use auto_increment for it

regards

kochier
07-24-2008, 08:20 PM
$update = "UPDATE map SET Modified = '$fmtime' WHERE Pathname = '$list'" or die(mysql_error());
mysql_query($update) or die(mysql_error());

Though I still get the duplicate entry problem, even though I'm not trying to overwrite the Pathname entry.

Ok giving up on mysql, I just want to be able to sort the array by the timestamp

$arry = array($fmtime);
sort($arry);
foreach ($arry as $key => $val) {
echo " - " .date("d m Y", $val);
}

but it still isn't sorting it any differently.

oesxyl
07-24-2008, 09:15 PM
$update = "UPDATE map SET Modified = '$fmtime' WHERE Pathname = '$list'" or die(mysql_error());
mysql_query($update) or die(mysql_error());

Though I still get the duplicate entry problem, even though I'm not trying to overwrite the Pathname entry.
I'm not sure but I think that this will remain, once you have insert some rows in the table, untill you remove the duplicates.


Ok giving up on mysql, I just want to be able to sort the array by the timestamp

$arry = array($fmtime);
sort($arry);
foreach ($arry as $key => $val) {
echo " - " .date("d m Y", $val);
}

but it still isn't sorting it any differently.



$arry = array();
// insert items using something like:
$arry[$list] = $fmtime;
// use asort instead of sort to preserve the relation key values
asort($arry);
foreach ($arry as $key => $val) {
echo $key. " - " . date("d m Y", $val);
}


regards

kochier
07-24-2008, 09:29 PM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$arry = array();
$arry[$list] = $fmtime;
asort($arry);
foreach ($arry as $key => $val) {
echo $key. " - " . date("d m Y", $val);
echo '<br />';
}
}
include 'closedb.php';
?>

It still shows the entries as how they were inserted into the database, not by when they were modified.

oesxyl
07-24-2008, 09:45 PM
try this:

<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
$arry = array();
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$arry[$list] = $fmtime;
}
asort($arry);
foreach ($arry as $key => $val) {
echo $key. " - " . date("d m Y", $val);
echo '<br />';
}
include 'closedb.php';
?>

regards

kochier
07-24-2008, 10:10 PM
oops extra post

kochier
07-24-2008, 10:21 PM
Yes! Thanks so much for all your help. Only thing I changed was asort to arsort becuase it was display the oldest on top instead of the bottom. Now all I need to do is limit the loop to five loops somehow. I'm thinking an if statement that will count a string, and if the string is less than 6 run, but if equals to six simply die().

If (count == 6)
{
die();
}
else
{
"runs code"
count++;
}

oesxyl
07-24-2008, 10:22 PM
Yes! That works, thank you so much. Only thing is it was showing it with the oldest at the top instead of the bottom so I switched the asort to arsort. Now all I need to do is limit the loop to five.

try this:

<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
$arry = array();
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$arry[$list] = $fmtime;
// this will break the loop
if(count($arry) == 5){
break;
}
}
arsort($arry);
foreach ($arry as $key => $val) {
echo $key. " - " . date("d m Y", $val);
echo '<br />';
}
include 'closedb.php';
?>

regards

kochier
07-24-2008, 10:30 PM
Yes! Perfect, thank you so much again! Just wondering what's the difference between break and die? (never used break before).

oesxyl
07-24-2008, 10:49 PM
Yes! Perfect, thank you so much again! Just wondering what's the difference between break and die? (never used break before).
die/exit stop execution at that line.
break will get out of the current loop only and continue with the first line after it.



foreach($something as $somethingelse){
if($somethingelse == $thisone){
break;
}
// anything from here until } will be skiped on break
}
// <- here after the break




foreach($something as $somethingelse){
while(condition){
if(other_condition){
break;
}
}
// <- here after the break
}
// <- here after a 'break 2' instead of break


usualy if break is used is a sign that the design could be improved, :)
In this case using limit 5 and order by in query with proper setup of the table is a better aproach.

regards

kochier
07-24-2008, 10:51 PM
Alright, that makes sense. The more you know. Thanks again :)

oesxyl
07-24-2008, 10:53 PM
Alright, that makes sense. The more you know. Thanks again :)
you are welcome, :)

best regards

kochier
07-24-2008, 11:16 PM
Actually upon further testing, this method will sadly not work, as when it comes time for the array to echo all of the values, it only has 5 values to work with and echoes them, so I'm getting the last 5 values inserted into the database, not the last 5 modified files. I've tried making the whole array a loop, but that wasn't leading anywhere.

I'm thinking something in here:


foreach ($arry as $key => $val) {

Can be changed so it only displays five results?

oesxyl
07-24-2008, 11:25 PM
Actually upon further testing, this method will sadly not work, as when it comes time for the array to echo all of the values, it only has 5 values to work with and echoes them, so I'm getting the last 5 values inserted into the database, not the last 5 modified files. I've tried making the whole array a loop, but that wasn't leading anywhere.
correct, is my fault. using break in while fetch only first 5 rows.

one solution is to modify the query:


select * from map order by modified desc limit 0,5


I guess you can get rid of break, :)

second is to extract all the rows from the tables and put the break in foreach. That's a bad solution but work if first don't work from some reason.

regards

kochier
07-24-2008, 11:33 PM
Problem with that is there is no database entry for modified. I was trying a different solution, only problem is $list is all the same, the dates may change but $list stays the same.


<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
$arry = array();
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$arry[$list] = $fmtime;
// this will break the loop
}
arsort($arry);
echo date("d m Y", current($arry));
echo $list;
echo '<br />';
echo date("d m Y", next($arry));
echo $list;
echo '<br />';
echo date("d m Y", next($arry));
echo $list;
echo '<br />';
echo date("d m Y", next($arry));
echo $list;
echo '<br />';
echo date("d m Y", next($arry));
echo $list;
echo '<br />';
include 'closedb.php';
?>

And I can't figure out where to put the break in the foreach, I'm thinking I have to change the string I'm using since $arry would of run it's course and wouldn't add up to 5 ever again, however no variable seems to be appropriate.

oesxyl
07-24-2008, 11:44 PM
Problem with that is there is no database entry for modified. I was trying a different solution, only problem is $list is all the same, the dates may change but $list stays the same.
this is a two step process:
1. update database with modified timestamp
2. retrive/sort/display the items

to achive first you must do a update like in one of the previous posts and to avoid duplicates warnings must be sure that in column Pathname have only uniq values or to change primary key to another column( see my sugestion with id from a previous post).

you can try what I said in previous post?

regards

kochier
07-25-2008, 12:00 AM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$flist[$list] = $fmtime;
$update = "UPDATE map SET Modified = '$fmtime' WHERE Pathname = '$list'" or die(mysql_error());
mysql_query($update) or die(mysql_error());
echo " - " .date("d m Y", $fmtime);
echo ' ';
echo $list;
echo '<br />';
}
include 'closedb.php';
?>


Is what I have right now, no dupliacate errors, however when I view my database, the modified times are all 0000-00-00, as if the timestamp didn't get updated.

I have checked and $fmtime is a UNIX timestamp, I've tried it in other formats as well but it doesn't have any affect.

kochier
07-25-2008, 12:59 AM
<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$flist[$list] = $fmtime;
$input = date("Y-m-d", "$fmtime");
$update = "UPDATE map SET Modified = '$input' WHERE Pathname = '$list'" or die(mysql_error());
mysql_query($update) or die(mysql_error());
}
$result2 = mysql_query("SELECT * FROM map ORDER BY Modified DESC LIMIT 5") or die(mysql_error());
while($row = mysql_fetch_assoc($result2)){
$mdate = $row['Modified'];
$mdate2 = strtotime("$mdate");
echo date("m d Y", "$mdate2");
echo ' ';
$pathname = $row['Pathname'];
echo $pathname;
echo '<br />';
}
include 'closedb.php';
?>


Got it working perfectly.

oesxyl
07-25-2008, 03:10 AM
field Modified in map table is timestamp?

make it timestamp and try this:


<?php
include 'opendb.php';
$result = mysql_query("SELECT * FROM map") or die(mysql_error());
while($row = mysql_fetch_assoc($result)){
$list = $row['Pathname'];
$fmtime = filemtime("/home/kochier/www/absurdity981.com/" . $list);
$flist[$list] = $fmtime;
// $input = date("Y-m-d", "$fmtime");
$update = "UPDATE map SET Modified = ".$fmtime." WHERE Pathname = '".$list."'";
mysql_query($update) or die(mysql_error());
}
$result2 = mysql_query("SELECT * FROM map ORDER BY Modified DESC LIMIT 5") or die(mysql_error());
while($row = mysql_fetch_assoc($result2)){
// $mdate = $row['Modified'];
// $mdate2 = strtotime("$mdate");
echo date("m d Y", $row['Modified']);
echo ' ';
echo $row['Pathname'];
echo '<br />';
}
include 'closedb.php';
?>


regards

kochier
07-25-2008, 05:11 AM
If I do that I'll get the values "12 -31 - 1969 again, I find I need to input it in the "Y-m-d" format because when I created the table I declared modified as a date.

oesxyl
07-25-2008, 06:07 AM
If I do that I'll get the values "12 -31 - 1969 again, I find I need to input it in the "Y-m-d" format because when I created the table I declared modified as a date.
you can use alter to change the column from date to timestamp. I'm not sure, but date I guess don't store time information.

regards

oesxyl
07-25-2008, 06:08 AM
If I do that I'll get the values "12 -31 - 1969 again, I find I need to input it in the "Y-m-d" format because when I created the table I declared modified as a date.
you can use alter to change the column from date to timestamp. I'm not sure, but date I guess don't store time information.

regards

oesxyl
07-25-2008, 06:08 AM
If I do that I'll get the values "12 -31 - 1969 again, I find I need to input it in the "Y-m-d" format because when I created the table I declared modified as a date.
you can use alter to change the column from date to timestamp. I'm not sure, but date I guess don't store time information.

regards



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum