...

View Full Version : I need a PHP script that can do this or a method to help me create it?



westmatrix99
05-29-2007, 05:52 PM
Ok I know what I need a PHP script to do!
I just don't know how to do it???

Ok I have a site to work with date(s) and time(s),
Basically this is the layout/idea:

You register=> get email then you Activate=> IF (a big IF)
IF you don't activate within 3 days (72 hours) your account that you created must be deleted.

IF your activation was succesful then:

You submit an image=> get email then you Activate=> IF (a big IF again)
IF you don't activate within 7 days your image that you submitted must be deleted.

I hope that this makes sense.

I need a PHP script that can do this or a method to help me create it?

rafiki
05-29-2007, 05:57 PM
add a field to database like reg <= make it a time
and imgadded <= time aswell
make a cron run a php script which checks if the user has activated if !activated && now => time + 60*60*24*3 (72 hours change for image) delete the account and info

westmatrix99
05-29-2007, 06:10 PM
That looks like it could work, except that I have no CRON available and wont change the host as it is the cheapest.

westmatrix99
05-29-2007, 06:12 PM
Sorry forgot to add how do I get the script to run without the cron job?

rafiki
05-29-2007, 06:31 PM
visit the page which holds the script :)

westmatrix99
05-29-2007, 07:07 PM
Ok that would mean that it is exactly like object orientated programming:
CLICK THEN DO.

Is there no way to make it do it automatically by date or something without a CLICK THEN DO situation, probably CRON? right?

rafiki
05-29-2007, 07:13 PM
you just create the normal php script which the cron would run, then www.yoursite.com/folder/page.php in ur browser will run the same as if a cron was doing it
no OOP required

westmatrix99
05-29-2007, 07:21 PM
Cool I fully understand that part but I physically have to go to that page:
UNLESS :D
I make sure that the user has to pass that page before or after logging in.

Ok will try it and get back to you as I am sure that I will need some help with coding it properly as you understand this time thing quite well.

westmatrix99
05-29-2007, 07:35 PM
Ok this is what I got:

<?php
$activated = $row_rstime['active'];
if (!$activated && time + 60*60*24*3){
echo "You are ok";
}
else{
echo "You are not ok";
}
?>How do I add 7 days to this?
Just change 3 to 7 (60*60*24*3)?

westmatrix99
05-29-2007, 07:43 PM
I have tried this way, but it keeps saying: You are ok

<?php
$time = $row_rstime['DateJoined'];
$activated = $row_rstime['active'];
if (!$activated && $time + 60*60*24*3){
echo "You not are ok";
}
else{
echo "You are ok";
}?>

It does not make sense as the DateJoined is: "2007-05-24 20:50:53"
And the account is not active: = "N"

rafiki
05-29-2007, 07:47 PM
yes change the 3 to 7
$activated will always return true you need to check the value of it with ==
and
time + 60*60*24*3
what is time? maybe you mean $time or time() (http://uk.php.net/time)


$activated = $row['active'];
$when = $row['timedate']
if ($activated == yes || $when !> time() +(60*60*24*7)) {
//something like that sorry you wanted an or sign there instead of and
}else{
echo 'activate';
}

westmatrix99
05-29-2007, 07:53 PM
Parse error: parse error, unexpected '!' in line 44

Line 44 =
if ($activated == 'Y' && $when !> time()+(60*60*24*7)){

aedrin
05-29-2007, 08:00 PM
A cheap alternative to cron is to have the check/update process be fired upon each page visit, or every X visits if you have a busier site.

It's not as reliable, but it works.

rafiki
05-29-2007, 08:05 PM
Parse error: parse error, unexpected '!' in line 44

Line 44 =
if ($activated == 'Y' && $when !> time()+(60*60*24*7)){
sorry use the less than operator
< instead of !>
and change the && to ||

westmatrix99
05-29-2007, 08:06 PM
Can do that but still can't execute this:

if ($activated == 'Y' && $when !> time()+(60*60*24*7)){
Changed it to:

if ($activated == 'Y' && $when != time()+(60*60*24*7)){
It worked but is that the trick?

westmatrix99
05-29-2007, 08:08 PM
Changed it to this

sorry use the less than operator
< instead of !>
and change the && to ||
Same error on the "!"

rafiki
05-29-2007, 08:09 PM
Can do that but still can't execute this:

if ($activated == 'Y' && $when !> time()+(60*60*24*7)){
Changed it to:

if ($activated == 'Y' && $when != time()+(60*60*24*7)){
It worked but is that the trick?
i editted my last post
you should change && to || because you want to execute the code when either they are still in the week before deletion or are activated not both conditions

westmatrix99
05-29-2007, 08:10 PM
Changing it around works! THAT's ODD

if ($activated == 'Y' && $when <! time()+(60*60*24*7)){
WORKS:
<!

NO WORKING? ODD
!<

rafiki
05-29-2007, 08:10 PM
you should have deleted !> and replaced with just < there should be 0 !'s in your code at this time

westmatrix99
05-29-2007, 08:15 PM
Ok got this:

<?php
$activated = $row_rstime['active'];
$when = $row_rstime['DateJoined']; // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || $when < time()+(60*60*24*7)){
echo 'You are ok';
}else{
echo 'activate';
}
?>It still says 'You are ok' :Now I am more lost?

westmatrix99
05-29-2007, 08:29 PM
Tried this and difference is "5" does this make sense?

$digest_date = "2007-05-24";
$date_diff = date("d",strtotime(date('y-m-d'))) - date("d",strtotime($digest_date));
echo $date_diff;
As it would make sense because it's less "<" than 7 so it shouldn't say 'You are ok' if I change it to 3 but it still says 'You are ok'???
No Idea man?

westmatrix99
05-29-2007, 08:36 PM
If I change it " &&" then it works but it makes no sense to me?
What is the difference between "||" and "&&" ?

IT always works but makes no sense to me why.

rafiki
05-29-2007, 08:36 PM
2007-05-24 is less than 7 days ago so it will say you are ok
try


<?php
$activated = $row_rstime['active'];
$when = $row_rstime['DateJoined']; // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || $when < time()+(60*60*24*3)){
echo 'You are ok';
}else{
echo 'activate';
}
?>
because 2007-05-24 20:50:53 is more than 3 days ago it should echo activate
make sense?

rafiki
05-29-2007, 08:39 PM
the difference between && and || is....
|| means or and
&& means and
so if


if ($a && $b) {
echo '$a returned true and $b returned true';
}
if($a || $b) {
echo '$a or $b returned true';
}

you see?

westmatrix99
05-29-2007, 08:40 PM
GOT IT COOL Thank you!!!

Still says 'You are ok'

rafiki
05-29-2007, 08:43 PM
ahhh i see
time() is the time now
so you need to change the order of the time() and $when


if ($activated == 'Y' || time() < ($when+60*60*24*3)) {

westmatrix99
05-29-2007, 08:52 PM
Ok

<?php
$activated = $row_rstime['active'];
$when = $row_rstime['DateJoined']; // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo 'You are ok';
}else{
echo 'activate';
}
?>

Even if I change the date to 2007-05-29 20:50:53 it still says 'activate'
Oh wait I see it!!!
Because there is an OR in the statement F@#% classic!!!

THE || = OR RIGHT GOT IT, It's DRILLED IN F@#% COOL!!!

Thank you.

Now I need to delete the data! more sh1t.

aedrin
05-29-2007, 08:53 PM
$when = $row_rstime['DateJoined'];

You are trying to add time to a string?

Your query needs to have this:


UNIX_TIMESTAMP(DateJoined) AS DateJoined

Then you can add time to the value of DateJoined.

westmatrix99
05-29-2007, 08:54 PM
I would have to base it now on the userID or username in order to delete the proper record(s).

What happens if I want to check the entire DB?

westmatrix99
05-29-2007, 08:56 PM
How to check entire DB?

<?php
$activated = $row_rstime['active'];
$when = $row_rstime['DateJoined']; // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo KEEP DATA VERY HAPPY!!!;
//no code here
}else{
echo DELETE DELETE DELETE;
//some delete code here
}
?>

rafiki
05-29-2007, 09:03 PM
sql = "SELECT * from users";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['DateJoined']); // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo KEEP DATA VERY HAPPY!!!;
//no code here
}else{
echo DELETE DELETE DELETE;
//some delete code here
}
}

something like this?

westmatrix99
05-29-2007, 09:08 PM
You are trying to add time to a string?
Your query needs to have this:
Then you can add time to the value of DateJoined.


Not sure what you mean by this:

Then you can add time to the value of DateJoined.
Also still need to check entire field of DB for more invalid DATA as such.

aedrin
05-29-2007, 09:08 PM
You'd use a query like this:



SELECT * FROM users WHERE activated = 'N' AND DateJoined < DATE_SUB(SYSDATE(), INTERVAL 3 DAY)


This will get you all non-activated users that registered 3 days or earlier.

westmatrix99
05-29-2007, 09:15 PM
THANK YOU "rafiki"

Man how do you know all this?
F@$% that is classic!!!
Thank you again!

Onto learning some new code from "aedrin"

aedrin
05-29-2007, 09:15 PM
something like this?

No.

Your table has a date column. This column gets formatted as a string when you use SELECT().

When you use DateJoined to add seconds to it, you are adding a string and a number. What is the result of this? "01/02/03 08:00" + 100. What should PHP decide to do with this? It's not smart enough to think that it is a date and you want 100 seconds added to it. You need 2 similar units to add. That is why you use UNIX_TIMESTAMP() on DateJoined in your query. It is the number of seconds since 1970. Now they're both numbers (seconds to be exact) and you can add them.

strtotime() is for a completely different purpose. It is only meant to take a user typed string and change that into a unix timestamp. Using it your manner is unreliable.

westmatrix99
05-29-2007, 09:20 PM
I must be honest "aedrin" it works for my PC and should work on the host.
Do I have to change it to fit the UNIX system?

aedrin
05-29-2007, 09:28 PM
Do not be confused by the name of the Unix Timestamp. It works the same way on all systems. It is a big number, the number of seconds since a certain date in 1970 (also known as the Unix epoch).

It will work, correct. But strtotime() is a function intended to read human language. It has to make assumptions sometimes to get to a number.

This may not always be correct. You should not add unnecessary conversion which could break your script. Using strtotime() you're doing Integer > String > Integer. Using UNIX_TIMESTAMP() you are are going directly from 1 Integer to another. Not to mention it is easier to read with less functions in your code.

The problem with Rafiki's example is that now you are doing this conversion for every single user out there.

10 users. That's no problem.

10.000 users? Now your script has to do ask PHP 10000 times to read a date/time and convert it to what it thinks is the right time. Not to mention asking PHP to load ALL data for every single user out there. If you just store a username and a date, that is fine. But I'm sure there are more fields in your table.

Still sounds good? I would hope not.

Use my query, it is the most efficient way to get a list of all users to be deleted.

In programming, there are hundreds of ways to get to your solution. But only a few of those are efficient, reliable and clean (to read).

westmatrix99
05-29-2007, 09:36 PM
Ok will try it but still need to add the delete data part.

I know it is similar to:

DELETE FROM tblogin WHERE userid=$row_rstime['userid'];
OR

DELETE FROM tblogin WHERE userid=$row_rstime['userid'] AND $when;
OR

DELETE FROM tblogin WHERE userid=$row_rstime['userid'] AND $activated == 'Y';
Not sure which one or which way with this one?

rafiki
05-29-2007, 09:42 PM
DELETE FROM tblogin WHERE userid={$row_rstime['userid']} AND $activated == 'N';

because you want to delete the users that have not activated within that period
and i think you need to wrap array values in {}'s not too sure though

westmatrix99
05-29-2007, 09:51 PM
Like such?


$sql2 = "DELETE FROM tblogin WHERE userid=$row_rstime['userid'] AND $activated == 'N'";
$delete = mysql_query($sql2);
echo 'DELETED';

iLLin
05-29-2007, 09:54 PM
Hmm, what are you trying to do? Delete all non-activated users after a certain time?



$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= $certain_date";

iLLin
05-29-2007, 09:56 PM
Like such?


$sql2 = "DELETE FROM tblogin WHERE userid=$row_rstime['userid'] AND $activated == 'N'";
$delete = mysql_query($sql2);
echo 'DELETED';


You don't use == in queries. AND whats up with the variable $activated ?

westmatrix99
05-29-2007, 10:01 PM
Entire script:

<?php
$sql = "SELECT * FROM tblogin";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['DateJoined']); // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo 'KEEP DATA VERY HAPPY!!!<br>';
//no code here
}else{
//echo 'DELETE DELETE DELETE';
$user = $row_rstime['userid']
//$sql2 = "DELETE FROM tblogin WHERE userid= $user AND $activated == 'N'";
$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= $when";
$delete = mysql_query($query);
echo 'DELETED';
}
?>
Error on line: Parse error: parse error, unexpected T_VARIABLE in


$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= $when";

iLLin
05-29-2007, 10:16 PM
You don't need to do a select * and loop through your results. Just do ONE query to delete out all the old records after the time you specify, or before the time you specify. You basically just delete ALL records that are not activated AND are atleast 3 days old OR 7 days old... whatever you want. NO need to select them then delete.

westmatrix99
05-29-2007, 10:19 PM
This does not actually delete the data; even though it returns that it has deleted and one is kept.

<?php
$sql = "SELECT * FROM tblogin";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['DateJoined']); // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo 'KEEP DATA<br>';
//no code here
}else{
$DateJoined = $row_rstime['DateJoined'];
$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $DateJoined <= $when";
$delete = mysql_query($query);
echo 'DELETED<br>';
}
}
?>

iLLin
05-29-2007, 10:30 PM
$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $DateJoined <= $when";


Did I use variables in my query for field names? And your $when is a timstamp where as your DatJoined field is not, so one or the other will need to be formatted to match

westmatrix99
05-29-2007, 10:34 PM
Ok how do I format the time() to a variable?
I am not exactly sure what you mean?

westmatrix99
05-29-2007, 10:40 PM
Did I use variables in my query for field names?
You used both:

$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= $certain_date";

westmatrix99
05-29-2007, 10:46 PM
Just to explain this part of the query:

The date the user signed up is what I have to play with and the current time.
How do get the currrent time into a variable to delete the data?

I think that makes sense?

iLLin
05-29-2007, 10:46 PM
You used both:

$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= $certain_date";

Not as "field names" the $certain_date is a value. Ok lets say for instance you want to delete ALL records that are not activated since yesterday


$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= '2007-05-27 00:00:00'";

westmatrix99
05-29-2007, 10:50 PM
Ok that makes sense but how do I get that into my code below?

$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= '2007-05-27 00:00:00'";
<?php
$sql = "SELECT * FROM tblogin";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['DateJoined']); // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo 'KEEP DATA<br>';
//no code here
}else{
$DateJoined = $row_rstime['DateJoined'];
//$datetest = time()
$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $DateJoined <= $when";
$delete = mysql_query($query);
echo 'DELETED<br>';
}
}
?>

westmatrix99
05-29-2007, 10:59 PM
Besides all these in and outs this query does not work:

$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $DateJoined <= $when";
I mean it does not delete anything?
If I change the code to remove the datejoined or activated still nothing?

iLLin
05-29-2007, 11:00 PM
You wouldn't it would replace all your code with that one query. Obviously you will put in some code to get whatever date you want to delete from but thats it.

westmatrix99
05-29-2007, 11:02 PM
You wouldn't it would replace all your code with that one query. Obviously you will put in some code to get whatever date you want to delete from but thats it.

That is exactly the point isn't it?
How do I get the date to a variable in order to delete the correct data?

iLLin
05-29-2007, 11:03 PM
What is the date your looking for>?

westmatrix99
05-29-2007, 11:08 PM
Today's date as the code will have to check today's date against the date already in the database.

iLLin
05-29-2007, 11:18 PM
So your deleting all rows that are not activated from today?

westmatrix99
05-29-2007, 11:23 PM
Yes:
After 3 days if your account is not active then your account gets deleted.

westmatrix99
05-29-2007, 11:30 PM
Tried this; NO LUCK?

$today = date("Y-m-d");
$DateJoined = $row_rstime['DateJoined'];
//$datetest = time()
$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $today <= $when";

iLLin
05-29-2007, 11:43 PM
You'd use a query like this:


SELECT * FROM users WHERE activated = 'N' AND DateJoined < DATE_SUB(SYSDATE(), INTERVAL 3 DAY)

This will get you all non-activated users that registered 3 days or earlier.

Only you would do a DELETE FROM instead of SELECT *

westmatrix99
05-29-2007, 11:52 PM
That doesn't do anything?

$query = "DELETE * FROM tblogin WHERE activated = 'N' AND DateJoined < DATE_SUB(SYSDATE(), INTERVAL 3 DAY)";

Am I not supposed to have variables or something in here?

westmatrix99
05-29-2007, 11:56 PM
Could you perhaps repost this entire code with your change that you suggest?

<?php
$sql = "SELECT * FROM tblogin";
$result = mysql_query($sql);
while($row_rstime = mysql_fetch_array($result)){
$activated = $row_rstime['active'];
$when = strtotime($row_rstime['DateJoined']); // currently at "2007-05-24 20:50:53"
if ($activated == 'Y' || time() < ($when+60*60*24*3)) {
echo 'KEEP DATA<br>';
//no code here
}else{
$DateJoined = $row_rstime['DateJoined'];
$query = "DELETE FROM tblogin WHERE $activated = 'N' AND $DateJoined <= $when";
$delete = mysql_query($query);
echo 'DELETED<br>';
}
}
?>

iLLin
05-30-2007, 12:09 AM
Sure this I know will work.



//Get the date and hour, 72 hours ago
$minus = strtotime("-72 hours");
$date = date("Y-m-d H:i:s", $minus);

$query = "DELETE FROM tblogin WHERE activated = 'N' AND DateJoined <= '".$date."'";

westmatrix99
05-30-2007, 07:31 AM
Sorry to have to inform you iLLin that none of the code that you have posted has worked.
The echo stills displays

DELETED
KEEP DATA
Nothing actually gets deleted?
The objective when this question was asked was to delete the data not display whether or not the data needs to be deleted.

Hope that makes sense?

westmatrix99
05-30-2007, 07:47 AM
Forget what I just posted the FIELD NAME OF activated was supposed to be activate!!! Man that was just stupid!:D

Thank you to rafiki TOP DOG! 1:thumbsup:
For helping me figure this out!!!
Thank you to iLLin TOP DOG! 2:thumbsup:
For helping me complete this!!!

Thank you to aedrin for the unix timestamp as a second option if the database gets toooooooooooo BIG!!!:cool:

I do appreciate all the help!

aedrin
05-30-2007, 03:18 PM
This is a serious suggestion: Buy a PHP book or read some good beginner's tutorials.

You use code in odd ways, and I don't think you have a grasp on the language yet.


The objective when this question was asked was to delete the data not display whether or not the data needs to be deleted.

You also misunderstand the purpose of this forum. When you post a problem, and people suggest code, you do not blindly copy and paste it into your code and expect it to work. We only try to show you what the basic method is of achieving your end result. It is up to you to create the actual code.

We showed you how to get the data you needed in multiple ways. And you copied that code directly. And it didn't do what you wanted. That is why.


if the database gets toooooooooooo BIG!!!

When you are programming, if a small change enables you to create a much more stable/scalable product, then you should. You only hurt yourself if you don't.

westmatrix99
05-30-2007, 03:34 PM
I think you weren't following from the beginning.
If you did then you would have noticed that I wrote my own code after the suggestion made by rafiki, the problem that I had was a field name not a copy and paste issue.

Read a tutorial, there aren't any good ones; well not until I have finished them.
I have no idea why you are upset about this when you yourself copied your own code in?

Who copied who's code?
Cheers

aedrin
05-30-2007, 04:09 PM
I've read the whole thing.


I have no idea why you are upset about this when you yourself copied your own code in?

I'm not upset. I'm just recommending to you that you get some more experience.

And I'm not sure what you're trying to say by that I copied my own code? You're the one being helped here, I'm one of the people trying to help you. I wrote my own code. I'm not sure where you got the idea from that I'm copying it from somewhere?


Read a tutorial, there aren't any good ones; well not until I have finished them.

Then get a book. Either buy it - they're cheap - or go to a library and borrow one.

I had given you all you needed to know in one or two of my first posts. But it took what, 3 more pages for you to understand how it works?

Again, it's just a suggestion. I'm not saying you are required to. But it would make your and our lives easier.

westmatrix99
05-30-2007, 04:32 PM
Ok, I get it thank you.

Also I did not mean that you "copied your own code"
I meant that you copied and pasted your own piece of code into the forum whether you wrote it or copied from your pc.

Yes it took 5 pages but that is the way the forum is setup.(I think you have a preference, I think, not sure also not insterested in the layout as it's not my layout.)Also there were odd questions or answers that expanded the whole thread.:D

Sorry that it took that long to solve this thread but I have another one.
This one wont take that many pages in the thread:
http://www.codingforums.com/showthread.php?t=115209
I PROMISE!:thumbsup:



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum