Hi! On the site I'm working on, there is a new products page. When a new product is added, it is inserted into a table with all of the available products as well as a table with just new items. The rows in the new items table are what show up on the new products page. I'm trying to get rows older than 120 days to automatically drop off of the new items table, so they will no longer show up on the new products page. I have added a column to the new items table titled "date" so I should be able to set the time based on that. I.e. the person goes to the admin page today, puts in all of the information (including date) and it shows up in the new item table. After 120 days, the item the person added will no longer be there.
I added an event, (towards the bottom of the code) which from what I've read should work, and I get the error:
Parse error: syntax error, unexpected T_STRING
Clearly I'm missing something, but I'm lost. Any help would be greatly appreciated!

<?php

$currentpage = $_GET['page'];	
$rowsperpage = 20;

// the offset of the list, based on current page 
$offset = ($currentpage - 1) * $rowsperpage;

//find total results
$sql = "Select * FROM new_item_numbers ORDER BY 'ID' DESC";

$query = mysql_query($sql) or die(mysql_error());
$results = mysql_num_rows($query); //changed result to query

// Print out all new items with limit and offset

$sql2 = "Select *
	FROM new_item_numbers 
	ORDER BY ID DESC
	LIMIT $rowsperpage OFFSET $offset";

$query2 = mysql_query($sql2) or die(mysql_error());
$results2 = mysql_num_rows($query2);

//sets the page range
if (($offset + $rowsperpage) > $results) { 
		$pagerange =  $results;
	}elseif ($offset < $rowsperpage) { 
		$pagerange =  $results2; 
	} else {
		$pagerange =  ($offset + $rowsperpage);
	}

print "<p>Results <b>" . ($offset + 1) . "</b>-<b>" . $pagerange . "</b> of <b>$results</b></p>";

while ($row = mysql_fetch_array($query2)) {
	$Item_Number = $row['Item_Number'];
	$product = getProduct($Item_Number);
	$long_description = getLongDescription($Item_Number);
	$strip = strpos($long_description, ".") ? strpos($long_description, ".") : strpos($long_description, "!");
	$long_description = substr($long_description, 0, $strip + 1); //select first sentence of long description
	$new_item = "<div class='new-product'>";
	$new_item .= "<a href='product_output.php?Item_Number=" . $product['Item_Number'] . "'><img src='images/" . $product['Image'] . "' alt='" . $product['Description'] . "' /></a>";
	$new_item .= "<div class='description'><h3><a href='product_output.php?Item_Number=" . $product['Item_Number'] . "'>" . $product['Description'] . "</a></h3>";
	$new_item .= "<p>$long_description</p></div>";
	$new_item .= "<br class='clear' />";
	$new_item .= "</div>";

print $new_item;

}

//Automatically delete items from new item page after 120 days

CREATE EVENT AutoDeleteNewAfter90
ON SCHEDULE AT CURRENT_TIMESTAMP + INTERVAL 1 DAY 
DO 
DELETE LOW_PRIORITY FROM coas50.new_item_numbers WHERE DATE < DATE_SUB(NOW(), INTERVAL 120 DAY);

//Print Pagination
	$pagination = pagination($results, $rowsperpage, $currentpage, 'http://www.example.com/products/new-products.php?page=%d');
	print "<br />$pagination";

?>
</div>

<?php include($_SERVER['DOCUMENT_ROOT'].'/includes/footer.php'); ?>

    It has to do with the lines following:

    //Automatically delete items from new item page after 120 days 

    I imagine this should be a sql string? I'm not sure, but you have random text not enclosed in a string and PHP doesn't know what's going on (and neither do I).

      Thank you for being so speedy! That is correct, I think it should be a sql string, too. So, next question would be... how do I do that? I'm sorry. I'm new to this sort of thing. Thank you again for your help!

        hhartman1027;11036797 wrote:

        Thank you for being so speedy! That is correct, I think it should be a sql string, too. So, next question would be... how do I do that? I'm sorry. I'm new to this sort of thing. Thank you again for your help!

        These lines:

        //find total results 
        $sql = "Select * FROM new_item_numbers ORDER BY 'ID' DESC"; 
        
        $query = mysql_query($sql) or die(mysql_error()); 
        $results = mysql_num_rows($query); //changed result to query 

        Show a MySQL Query properly enclosed in quotes and assigned to the variable $sql, followed by the execution of the query (which returns a resource identifier to the variable $query) and the assignment of the returned row count to the variable $results.

        So, at the very least, you want to enclose the SQL string in quotes and give it a variable name. You'll probably want to then do something with it.

        Of course, if you don't need that query/SQL string, put a "/" before it and a "/" after it and your syntax error should disappear 😉 😉

          dalecosp;11036799 wrote:

          These lines:

          Show a MySQL Query properly enclosed in quotes and assigned to the variable $sql, followed by the execution of the query (which returns a resource identifier to the variable $query) and the assignment of the returned row count to the variable $results.

          So, at the very least, you want to enclose the SQL string in quotes and give it a variable name. You'll probably want to then do something with it.

          Of course, if you don't need that query/SQL string, put a "/" before it and a "/" after it and your syntax error should disappear 😉 😉

          Ah ha ha! Fixed it! Kind of. No more error. It's not doing what it's supposed to though, so apparently something I did didn't fly. But hey, no errors is something to get excited about!! 😃

            hhartman1027;11036801 wrote:

            Ah ha ha! Fixed it! Kind of. No more error. It's not doing what it's supposed to though, so apparently something I did didn't fly. But hey, no errors is something to get excited about!! 😃

            Heh heh, I understand THAT. 😃

            The SQL statement there is advertised to set an EVENT to happen in the future (deleting products from the "new item page" after a set time [120 days, but the function is called 90days --- oops]). I assume that's supposed to be sent to MySQL as a query also; you can read more about event scheduling in MySQL here.

              dalecosp;11036805 wrote:

              Heh heh, I understand THAT. 😃

              The SQL statement there is advertised to set an EVENT to happen in the future (deleting products from the "new item page" after a set time [120 days, but the function is called 90days --- oops]). I assume that's supposed to be sent to MySQL as a query also; you can read more about event scheduling in MySQL here.

              Woops, we had decided 120 days would be better to cover our butts.. I forgot to change that part. 🙂 I will mess with it based on the link you suggested for a little while and update how it's going later. Thank you very much for your help!

                Possibly a silly/stupid question, but.... instead of setting an event, why not simply combine your date-check logic with the data retrieval query that you already have working? Something like (using pseudocode):
                while ($row = mysql_fetch_array($query2))
                check date on $row
                if date is more than 90 (or 120, or whatever) days old
                execute second query to delete record $row
                else
                load $row into memory and print it, exactly as you have it now

                -- Jon W.

                  JonWoolf;11036875 wrote:

                  Possibly a silly/stupid question, but.... instead of setting an event, why not simply combine your date-check logic with the data retrieval query that you already have working? Something like (using pseudocode):
                  while ($row = mysql_fetch_array($query2))
                  check date on $row
                  if date is more than 90 (or 120, or whatever) days old
                  execute second query to delete record $row
                  else
                  load $row into memory and print it, exactly as you have it now

                  -- Jon W.

                  Honestly, I had no idea how to do it... after lots of Googling, creating an event seemed like the way to go. But, after fighting with it for hours without success, I'm definitely up from trying something else!! Thank you very much for your suggestion... I'll try it out on Monday when I go back to work!

                    Okay, I messed with it some more... no errors, but it still doesn't work. I've added one row as a test item to the table, but made the date for it 2013-05-30, so it is well over the 120 day rule and shouldn't show up on the page. Unless it just doesn't work because I manually added it to the table, rather than filling out the form which adds items to the table? I've tried to do add a made-up item to the table via the form, but it won't let me because the item isn't in our other system (they have to match). Boo. Anyscoot, this is what I came up with:

                    while ($row = mysql_fetch_array($query2)) {
                    	$ldate = $row['Date'];
                    
                    //compare current date to date item was added
                    //if it's older than 120 days, remove from new item table
                    if ($ldate != "0000-00-00") {
                    	$cdate = date('m-d-Y');
                    	$ldate = break_date($ldate); //puts in m-d-y format
                    	$datediff = dateDiff($cdate, $ldate); //calculate days between
                    	$DeleteRow = ($datediff >= -180 && $ldate != "00-00-0000");
                    	$DeleteRow = "DELETE FROM new_item_numbers WHERE date >= Now(), INTERVAL 120 DAY";
                    	mysql_query($DeleteRow);
                    } else {
                    	print $row;
                    
                    }
                    }

                    Can you tell if something just doesn't seem right? Or does it make sense that it's not working because of the item being manually added to the table? I'm not 100% (obviously) on how these things work! Thank you again for being so helpful!

                      First problem I see is that you're executing a SQL query inside of a loop that's processing a separate query's results - that's always a huge red flag. Next problem I see is that the DELETE query you're executing has syntax that doesn't make any sense (perhaps that "," should instead be a "-" since you want to subtract 120 days from now?).

                      So as I understand it (and I'll admit I only did a quick peruse through the thread), there's a few possible courses of action:

                      1. First perform a single DELETE query that deletes all rows from the table that exceed the given interval. This will cause your SQL server to check for (and subsequently delete) these old rows on every page load - rather unscalable and therefore potentially undesirable.

                      2. Utilize some sort of scheduling mechanism (i.e. a cron job) to manually perform the DELETE query once per day.

                      3. Modify your SELECT query such that it only SELECTs dates within the given interval. Older events will still exist, but that's okay, because disk space is (relatively) cheap and you can leave them be for archival purposes or for manual cleanup whenever you feel like it.

                        bradgrafelman;11036919 wrote:

                        First problem I see is that you're executing a SQL query inside of a loop that's processing a separate query's results - that's always a huge red flag. Next problem I see is that the DELETE query you're executing has syntax that doesn't make any sense (perhaps that "," should instead be a "-" since you want to subtract 120 days from now?).

                        So as I understand it (and I'll admit I only did a quick peruse through the thread), there's a few possible courses of action:

                        1. First perform a single DELETE query that deletes all rows from the table that exceed the given interval. This will cause your SQL server to check for (and subsequently delete) these old rows on every page load - rather unscalable and therefore potentially undesirable.

                        2. Utilize some sort of scheduling mechanism (i.e. a cron job) to manually perform the DELETE query once per day.

                        3. Modify your SELECT query such that it only SELECTs dates within the given interval. Older events will still exist, but that's okay, because disk space is (relatively) cheap and you can leave them be for archival purposes or for manual cleanup whenever you feel like it.

                        Thank you for your response! I'm working on switching it to a select statement instead of delete. The " , " was definitely a typo. Thank you for catching it! I was able to run the select statement as a mysql query and it worked fabulously. I'm still working on getting everything right in the PHP file to make it work there. Something is still goofy with the updated code, as the item with the older date still shows up! Hmm. I guess it's a good thing I'll be here for a while longer! 😉

                          Happy news!
                          I was able to change the code and everything is working as it should! 😃 😃 Thank you all for your guidance, it was really appreciated!
                          This is what I added that fixed the issue and got things working the way we wanted:

                          $sql = "DELETE FROM new_item_numbers
                                  WHERE date <= Now() - INTERVAL 120 DAY";
                          $query = mysql_query($sql) or die(mysql_error());

                          Thank you again for your help!

                            Excellent! Please consider using the "Thread Tools" above to mark this thread as "RESOLVED". Thanks!

                              Write a Reply...