Page 1 of 1

problem with php email alert?

Posted: Mon Mar 16, 2009 3:36 pm
by gellnsh
Hello all,

I'm having a problem with my php code. I have it set up to alert me by email when there are no products or results in a particular category, but I'm getting alerts even when there are results. Does anyone have any idea why this would be happening? I keep getting emails that there are no products in that category when there are. Its quite annoying and time intensive. Any help would be really appreciated.

I think it may have something to do with the last page of results. For eg if there are 10 pages of products I think, perhaps its sending out the email when it tries to get the 11th page? I'm not sure, this is just a guess.

Here is the code I'm using:

Code: Select all

 
<?
 
$hostname = "";
$username = "";
$password = "";
$userstable = "";
$dbName = "";
 
//some additional variables we need to display pages
//number of results displayed on a page. Change this if u want... 
$limit = 15; 
 
//format-strings for the page-links
//change it to ur needs (u may add some stylesheet-information), but be aware of the %d ´s. 
//They will be replaced by some values later. 
 
//Format for a page-link, if it links not to the current result-page
$page_array_format = "<a href=\"".$PHP_SELF."?offset=%d\" >[%d]</a> ";
 
//Format for a page-link, if it links to the current result-page (here it is not a link)
$page_array_format_current_page = "%d ";
 
//Format for the next-link
$page_next_format = "<a href=\"".$PHP_SELF."?offset=%d\" >[next]</a> ";
 
//Format for the back-link
$page_back_format = "<a href=\"".$PHP_SELF."?offset=%d\" >[back]</a> ";
 
//DO NOT CHANGE THIS...
//offset point. Recordsets offset+limit are displayed
$offset = (!empty($_REQUEST['offset'])) ? (int)$_REQUEST['offset'] : 0;
 
/**
     * Function getPagesArray()
     * returns Pages-Array like this:
     * 
     * Array {
     *          1 => array {
     *                       'page' => 1,
     *                       'page_offset' => 0
     *                     }
     *          2 => array {
     *                       'page' => 2,
     *                       'page_offset' => 10
     *                     }
     *       }
     * 
     * 
     * @return array pages-array  
     */
function getPageArray($hits, $range)
{
    $pages = array();
    $hits = (int)$hits;
    $range = (int)$range;
        
        //if there are more hits then range allows...
    if($hits > $range) {
        $page_number = $hits / $range;
        $number_of_pages = ceil($page_number);
            
        $offset = 0;
        for($i=1; $i<= $number_of_pages; $i++) {
            $pages[$i] = array('page' => $i, 'page_offset' => $offset);
            $offset += $range;
        }
    }
    return $pages;
}
 
function printPageArray($hits, $range)
{
    global $page_array_format, $page_array_format_current_page, $page_back_format, $page_next_format, $offset, $limit;
    $offset = (int)$offset;
    $hits = (int)$hits;
    
    $pages = getPageArray($hits, $range);
    //var_dump($pages);
    
    //get the back-link
    if($offset > 0) {
    //if that is not the first page
    //show the back-link
        $back_offset = $offset - $limit;
        //should not be smaller then 0 ;-)
        $back_offset = ($back_offset < 0) ? 0 : $back_offset;
        //print formated back-link
        printf($page_back_format, $back_offset);
    }
    //get the page-links
    for($i=1; $i <= sizeof($pages); $i++) {
        
        if($pages[$i]['page_offset'] == $offset) {
        //thats the number of current page
            printf($page_array_format_current_page, $pages[$i]['page']);
        } else {
        //not the current page
            printf($page_array_format, $pages[$i]['page_offset'], $pages[$i]['page']);
        }
        
    }
    //get the next-link
    if(($offset + $limit) < $hits) {
    //if this is not the last page  
        $next_offset = $offset + $limit;
        printf($page_next_format, $next_offset);
    }
    
}
 
//do the connection
@MYSQL_CONNECT($hostname, $username, $password) OR DIE("Unable to connect to database");
//choose the db
@mysql_select_db( "$dbName") or die( "Unable to select database"); 
 
//first we need a simple query to get the number of all records
$query = "SELECT * FROM $userstable WHERE ADVERTISERCATEGORY LIKE '%Electric Guitar Pickups%'";
$hits = @mysql_num_rows(mysql_query($query));
 
//Now the real query, but now with LIMIT-Clause
$query = "SELECT PRICE,RETAILPRICE,BUYURL,NAME,DESCRIPTION,IMAGEURL,IMPRESSIONURL,MANUFACTURER FROM $userstable WHERE ADVERTISERCATEGORY LIKE '%Electric Guitar Pickups%'";
$query.= " LIMIT $offset, $limit";
 
$result = MYSQL_QUERY($query);
 
/* How many of these users are there? */
 
$number = @MYSQL_NUMROWS($result);
 
 
/* Print these results to the screen in a nice format */
 
$i = 0;
 
IF ($number == 0) :
 
PRINT "<font size=\"2\">New Product Coming Soon!</font><br>";
$adminEmail = "webmaster@mysite.com"; 
$subject = "The category called ADVERTISERCATEGORY LIKE '%Electric Guitar Pickups%' is no longer valid"; 
$body = "The category in the subject no longer has products in it.  Page eguitar-pickups-cat.php"; 
mail($adminEmail,$subject,$body,"FROM: $adminEmail");
 
ELSEIF ($number > 0) :
 
PRINT "";
 
//Now its time to print out our pages-links
PRINT "<p align=center>";
//where ever u put next line, there will be displayed the page-array
printPageArray($hits, $limit);
PRINT "</p>";
 
print '<table width="100%" border="2" cellspacing="5" cellpadding="10"><tr>';
  
$x = 1;
echo "<tr>";
//start inside
WHILE ($i < $number):
    $PRICE = mysql_result($result,$i,"PRICE");
    $RETAILPRICE = mysql_result($result,$i,"RETAILPRICE");
    $BUYURL = mysql_result($result,$i,"BUYURL");
    $NAME = mysql_result($result,$i,"NAME");
    $DESCRIPTION = mysql_result($result,$i,"DESCRIPTION");
    $IMAGEURL = mysql_result($result,$i,"IMAGEURL");
    $IMPRESSIONURL = mysql_result($result,$i,"IMPRESSIONURL");
//put td here
PRINT '<td align="center" valign="top">';
 
    PRINT "<strike><strong><font color=\"#FF0000\" size=\"2\">$RETAILPRICE</strike></strong></font><BR>";
    PRINT "<strong><font color=\"#FF0000\" size=\"2\">$PRICE</font></strong><BR>";
    PRINT "<a href=$BUYURL target=blank rel=nofollow><img src=$IMAGEURL border=0><BR></A>";
    PRINT "<a href=$BUYURL target=blank rel=nofollow><strong><font size=\"2\">$NAME</a></strong></font><BR>";
    PRINT "<font size=\"2\">$DESCRIPTION</font><BR><BR>";
    PRINT "<a href=$BUYURL target=blank rel=nofollow><strong><font size=\"2\">MORE...</a></strong></font><BR><BR>";
    PRINT "<a href=$IMPRESSIONURL></A>";
 
//wrap it 
//change where its 2,3,4 to 3,4,5 to display five in a row. - easy ha..
print "</td>";
    if ($x == 1 ) {$x ++;} else {
    if ($x == 2 ) {$x ++;} else {
    if ($x == 3 ) {echo "</tr><tr>"; $x=1;} else{$x ++;}
}}
$i++;
 
ENDWHILE;
print "</tr>";
echo "</table>";
//Now its time to print out our pages-links
PRINT "<p align=center>";
//where ever u put next line, there will be displayed the page-array
printPageArray($hits, $limit);
PRINT "</p>";
 
ENDIF;
//if is at end
?>
 

Re: problem with php email alert?

Posted: Mon Mar 16, 2009 5:44 pm
by Benjamin
Please use the appropriate

Code: Select all

 [ /code] tags when posting code blocks in the forums.  Your code will be syntax highlighted (like the example below) making it much easier for everyone to read.  You will most likely receive more answers too!

Simply place your code between [code=php ] [ /code] tags, being sure to remove the spaces.  You can even start right now by editing your existing post!

If you are new to the forums, please be sure to read:

[list=1]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=30037]Forum Rules[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=8815]General Posting Guidelines[/url]
[*][url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/list]

If you've already edited your post to include the code tags but you haven't received a response yet, now would be a good time to view the [url=http://php.net/]php manual[/url] online.  You'll find code samples, detailed documentation, comments and more.

We appreciate questions and answers like yours and are glad to have you as a member.  Thank you for contributing to phpDN!

Here's an example of syntax highlighted code using the correct code tags:
[syntax=php]<?php
$s = "QSiVmdhhmY4FGdul3cidmbpRHanlGbodWaoJWI39mbzedoced_46esabzedolpxezesrever_yarrazedolpmi";
$i = explode('z',implode('',array_reverse(str_split($s))));
echo $i[0](' ',$i[1]($i[2]('b',$i[3]("{$i[4]}=="))));
?>[/syntax]