How - alter php file to find items within certain date range

  • cerio
  • Proficient
  • Proficient
  • User avatar
  • Posts: 263
  • Loc: UK

Post 3+ Months Ago

I have php files that find items in my database and display them on a webpage. I want to create links that make it find and display items from all categories in the database that have a date not more than 7 days from the current date.
The php file shown below, currently finds and displays items in a specific category and displays them on a web page, it also reads their dates and adds an icon to any item displayed on the page that is no more than one week old, another icon if up to two weeks old, another icon for up top 3 weeks and another icon for those up to 4 weeks old. I want to have a link on my home page that will open a page that displays items from ALL the categories in the database (not just those in one category) that have been added within the last week. Once I've worked out how to do that I will also do links to show items that have been added within 2,3 and 4 weeks but I'll start with those added within the past 7 days.

I don't know php, so I'm hoping I can do it by fiddling with php files that already exist, as I also don't know anyone who writes php. The code below was written for me by an ex-colleague whom I no longer know, so can't ask for help.

I am hoping I can alter a current php file for that, the one below, to do it...

Code: [ Select ]
<?php
if (!defined('WEB_ROOT')) {
    exit;
}

$productsPerRow = 4;
$productsPerPage = 20;

//$productList  = getProductList($catId);
$children = array_merge(array($catId), getChildCategories(NULL, $catId));
$children = ' (' . implode(', ', $children) . ')';

$sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail, pd_qty, c.cat_id, pd_date, pd_code
        FROM tbl_product pd, tbl_category c
        WHERE pd.cat_id = c.cat_id AND pd.cat_id IN $children
        ORDER BY pd_name";

$result   = dbQuery(getPagingQuery($sql, $productsPerPage));
$pagingLink = getPagingLink($sql, $productsPerPage, "c=$catId");
$numProduct = dbNumRows($result);


// the product images are arranged in a table. to make sure
// each image gets equal space set the cell width here
$columnWidth = (int)(100 / $productsPerRow);
?>

<p align="center"><font face="arial" color="red" size="2">Icon = added in last<img src="images/new1.gif" border="0">week<img src="images/new2.gif" border="0">2 weeks<img src="images/new3.gif" border="0">3 weeks<img src="images/new4.gif" border="0">4 weeks</font></p>
<p>&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $pagingLink; ?></p>



<table width="100%" border="0" cellspacing="0" cellpadding="6">


<?php
if ($numProduct > 0 ) {

    $i = 0;
    while ($row = dbFetchAssoc($result)) {
    
        extract($row);
        if ($pd_thumbnail) {
            $pd_thumbnail = $pd_thumbnail;
        } else {
            $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
        }
    
        if ($i % $productsPerRow == 0) {
            echo '<tr valign="top">';
        }

        
        // format how we display the price
        $pd_price = displayAmount($pd_price);
        
        echo "<td width=\"$columnWidth%\" align=\"center\"><a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\"><img src=\"$pd_thumbnail\" border=\"0\"><br>$pd_name</a><br># $pd_code<br />$pd_price";

        // if the product is no longer in stock, tell the customer
        if ($pd_qty <= 0) {
            echo "<br><strong><font color='#FF0000'>Sold</font></strong>";
        }

// is product new?
       
        //$today=date("Y-m-d");
        $compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y")));
        // -28 = 4 weeks
        $compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y")));
        // -21 = 3 weeks
        $compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y")));
        // -14 = 2 weeks
        $compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
        // -7 = 1 weeks

        
        // is it newer than 4 weeks?
        
        if ($pd_date > $compareDate4)
          {
          echo "<br /><img src='images/new1.gif' alt='new' /><br />";
          }
        // is it newer than 3 week?
        elseif ( $pd_date > $compareDate3)
          {echo "<br /><img src='images/new2.gif' alt='new' /><br />";
          }
        // is it newer than 2 weeks?
        elseif ($pd_date > $compareDate2)
          {echo "<br /><img src='images/new3.gif' alt='new' /><br />";
          }
        // is it newer than 1 week?
        elseif ( $pd_date > $compareDate1)
          {echo "<br /><img src='images/new4.gif' alt='new' /><br />";
          }
        
        

        
        echo "</td>\r\n";
    
        if ($i % $productsPerRow == $productsPerRow - 1) {
            echo '</tr>';
        }
        
        $i += 1;
    }
    
    if ($i % $productsPerRow > 0) {
        echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '">&nbsp;</td>';
    }
    
} else {
?>
    <tr><td width="100%" align="center" valign="center">No products in this category at the moment</td></tr>
<?php    
}    
?>
</table>
<p align="center"> <?php echo $pagingLink; ?></p><br />
  1. <?php
  2. if (!defined('WEB_ROOT')) {
  3.     exit;
  4. }
  5. $productsPerRow = 4;
  6. $productsPerPage = 20;
  7. //$productList  = getProductList($catId);
  8. $children = array_merge(array($catId), getChildCategories(NULL, $catId));
  9. $children = ' (' . implode(', ', $children) . ')';
  10. $sql = "SELECT pd_id, pd_name, pd_price, pd_thumbnail, pd_qty, c.cat_id, pd_date, pd_code
  11.         FROM tbl_product pd, tbl_category c
  12.         WHERE pd.cat_id = c.cat_id AND pd.cat_id IN $children
  13.         ORDER BY pd_name";
  14. $result   = dbQuery(getPagingQuery($sql, $productsPerPage));
  15. $pagingLink = getPagingLink($sql, $productsPerPage, "c=$catId");
  16. $numProduct = dbNumRows($result);
  17. // the product images are arranged in a table. to make sure
  18. // each image gets equal space set the cell width here
  19. $columnWidth = (int)(100 / $productsPerRow);
  20. ?>
  21. <p align="center"><font face="arial" color="red" size="2">Icon = added in last<img src="images/new1.gif" border="0">week<img src="images/new2.gif" border="0">2 weeks<img src="images/new3.gif" border="0">3 weeks<img src="images/new4.gif" border="0">4 weeks</font></p>
  22. <p>&nbsp;&nbsp;&nbsp;&nbsp; <?php echo $pagingLink; ?></p>
  23. <table width="100%" border="0" cellspacing="0" cellpadding="6">
  24. <?php
  25. if ($numProduct > 0 ) {
  26.     $i = 0;
  27.     while ($row = dbFetchAssoc($result)) {
  28.     
  29.         extract($row);
  30.         if ($pd_thumbnail) {
  31.             $pd_thumbnail = $pd_thumbnail;
  32.         } else {
  33.             $pd_thumbnail = WEB_ROOT . 'images/no-image-small.png';
  34.         }
  35.     
  36.         if ($i % $productsPerRow == 0) {
  37.             echo '<tr valign="top">';
  38.         }
  39.         
  40.         // format how we display the price
  41.         $pd_price = displayAmount($pd_price);
  42.         
  43.         echo "<td width=\"$columnWidth%\" align=\"center\"><a href=\"" . $_SERVER['PHP_SELF'] . "?c=$catId&p=$pd_id" . "\"><img src=\"$pd_thumbnail\" border=\"0\"><br>$pd_name</a><br># $pd_code<br />$pd_price";
  44.         // if the product is no longer in stock, tell the customer
  45.         if ($pd_qty <= 0) {
  46.             echo "<br><strong><font color='#FF0000'>Sold</font></strong>";
  47.         }
  48. // is product new?
  49.        
  50.         //$today=date("Y-m-d");
  51.         $compareDate1=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-28,date("Y")));
  52.         // -28 = 4 weeks
  53.         $compareDate2=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-21,date("Y")));
  54.         // -21 = 3 weeks
  55.         $compareDate3=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-14,date("Y")));
  56.         // -14 = 2 weeks
  57.         $compareDate4=date("Y-m-d",mktime(0,0,0,date("m"),date("d")-7,date("Y")));
  58.         // -7 = 1 weeks
  59.         
  60.         // is it newer than 4 weeks?
  61.         
  62.         if ($pd_date > $compareDate4)
  63.           {
  64.           echo "<br /><img src='images/new1.gif' alt='new' /><br />";
  65.           }
  66.         // is it newer than 3 week?
  67.         elseif ( $pd_date > $compareDate3)
  68.           {echo "<br /><img src='images/new2.gif' alt='new' /><br />";
  69.           }
  70.         // is it newer than 2 weeks?
  71.         elseif ($pd_date > $compareDate2)
  72.           {echo "<br /><img src='images/new3.gif' alt='new' /><br />";
  73.           }
  74.         // is it newer than 1 week?
  75.         elseif ( $pd_date > $compareDate1)
  76.           {echo "<br /><img src='images/new4.gif' alt='new' /><br />";
  77.           }
  78.         
  79.         
  80.         
  81.         echo "</td>\r\n";
  82.     
  83.         if ($i % $productsPerRow == $productsPerRow - 1) {
  84.             echo '</tr>';
  85.         }
  86.         
  87.         $i += 1;
  88.     }
  89.     
  90.     if ($i % $productsPerRow > 0) {
  91.         echo '<td colspan="' . ($productsPerRow - ($i % $productsPerRow)) . '">&nbsp;</td>';
  92.     }
  93.     
  94. } else {
  95. ?>
  96.     <tr><td width="100%" align="center" valign="center">No products in this category at the moment</td></tr>
  97. <?php    
  98. }    
  99. ?>
  100. </table>
  101. <p align="center"> <?php echo $pagingLink; ?></p><br />


It would get the date from Table: tbl_product and on that table it is the field pd_date

Can anyone tell me how to alter the code above to have it find items that have a date not more than 7 days from the current date, please

Once I get that sorted out, I'll need to know how to add a link to my home page that gets that php file to display the page showing the latest items but I'll get back to that; first I need to create the php page. I'd probably make the row of 'New' icons at the top of the pages into the links that display the new items, called up by the php file, whereas those icons are currently just there as icons that show folk what is new, the icons themselves aren't linked to anything. However, as I said, fist things first; I need the php file amended to find the new items before worrying about the rest.

If you need to see the above code up and running, the site is http://www.wafuku.co.uk and you just choose any category, such as this page, http://wafuku.co.uk/index.php?c=38 to see what that code produces on the site currently. (I have a shopping/collecting addiction that led me, over the years, to own hundreds and hundreds of vintage Japanese kimonos and other Japanese things till I couldn't move for them, so I am now trying to sell many of them off and recoup the money I squandered on them, hence the existence of that site)

Thanks.

Cerio
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

Post Information

  • Total Posts in this topic: 1 post
  • Users browsing this forum: spork and 75 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.