Solved

how to store the query in the db and execute it at a certain time php/mysql

Posted on 2004-09-03
7
257 Views
Last Modified: 2008-02-20
Hi

check this http://www.esvon.com/pg/products/p_classifieds/mod_notify/

It would have 2 multiple select boxes to select 1 or more cities and 1 or more property types, user would type first name, last name and his/her email in and subscribe.
Can you please help me i'm confused - how to get that criteria from boxes, get it compared to the data in the db , how to store that query and get it sent every day around midnight.

Any help would be appreciated
Grazias
0
Comment
Question by:mimalune
  • 4
  • 2
7 Comments
 
LVL 20

Expert Comment

by:Muhammad Wasif
ID: 11979149
It seems to me a promotion of your porducts :-P
0
 
LVL 25

Accepted Solution

by:
Marcus Bointon earned 500 total points
ID: 11979757
I think that's someone else's site that he wants to emulate.

Add each multiple select box like this:

<select name="city[]" multiple="multiple">
<option value="london">London</option>
<option value="newyork">New York</option>
<option value="paris">Paris</option>
</select>

<select name="propertytype[]" multiple="multiple">
<option value="flat">Flat</option>
<option value="studio">Studio</option>
<option value="house">House</option>
</select>

then normal text input boxes for the other properties:

<input type="text" name="firstname" />
<input type="text" name="lastname" />
<input type="text" name="email" />

Storing this in a database with appropriate columns:

$query = "INSERT INTO notifications SET
firstname = '{$_REQUEST['firstname']}',
lastname = '{$_REQUEST['lastname']}',
email = '{$_REQUEST['email']}',
cities = '".implode(',', $_REQUEST['city']})."',
propertytypes = '".implode(',', $_REQUEST['propertytype']})."'";
mysql_query($query);

In a script run from a cron job, look for matching ads for each registered person:

$query = "SELECT * FROM notifications";
$result = mysql_query($query);
while ($data = mysql_fetch_array($result)) {
$query = "SELECT * FROM ads WHERE city IN({$data['cities']}) AND propertytype IN({$data['propertytypes']})";
$properties = mysql_query($query);
if (mysql_num_rows($properties) > 0) {
$details = '';
while ($property = mysql_fetch_array($properties)) {
$details .= "{$property['id'} {$property['name']} {$property['description']} {$property['price']}\n";
}
$mail = new PHPMailer;
$mail->AddAddress($data['email'], "{$data['firstname']} {$data['lastname']}");
$mail->From = 'notifications@mypropertystore.com';
$mail->Subject = "Matching properties found";
$mail->Body = "$details";
$mail->send();
}
}

That should cover the basics. I've just written this off the top of my head, so it's not tested or anything. You need to look up how to set up a cron job (plenty of examples in EE), and you need to set up the database appropriately and connect to it. Some error checking and validation would be a good idea (using $_REQUEST elements directly in SQL is not a good habit). It's using PHPMailer, which you'll need to download from here: http://phpmailer.sourceforge.net/

Hope that gets you going in the right direction.
0
 

Author Comment

by:mimalune
ID: 11983108
Thanks a bunch..this was really great.
.i have small prob with this line
$query = "SELECT * FROM ads WHERE city IN({$data['cities']}) AND propertytype IN({$data['propertytypes']})";

this is how it looks in mine code

$q1 = "SELECT * FROM re2_notifications";
$p1 = mysql_query($q1) or die(mysql_error());
while($a1 = mysql_fetch_array($p1))
{
$q2 = "SELECT * FROM re2_listings WHERE city IN('$a1[sub_cities]') AND PropertyType IN('$a1[sub_types]')";
$properties = mysql_query($q2) or die(mysql_error());
$lrows = mysql_num_rows($properties);
if ($lrows > 0) {
$details = '';
while($property = mysql_fetch_array($properties)) {
$details .= "$property[ListingID] $property[Price]\n";
}

echo "$a1[sub_email], $a1[first_name] $a1[last_name]";
echo "$details";
}
}

It works only when there is a single city or single property type - with multiple - nope
This is how the table looks like...
 sub_ID  first_name  last_name  sub_email                      sub_cities                  sub_types  
  5           John     Wayne2   contact@mine-it.com          TORONTO                     Residential  
 4           John     Wayne1   john@mine-it.com     BRAMPTON,TORONTO             Residential  

is there some other way to query it or it has to be using IN, or if i missed some quotes (putting {} doesn't change anything)??
Thank you
0
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

Author Comment

by:mimalune
ID: 11983151
OK i realized that cities have to have qoutes in order to get things working
'BRAMPTON', 'TORONTO'
i'm struggling to get them inserted into DB in that format now..
this is the insertions part
            $query = "INSERT INTO re2_notifications SET
      first_name = '$_POST[f_name]',
      last_name = '$_POST[l_name]',
      sub_email = '$_POST[semail]',
      sub_cities = '".implode(',', $_POST[s_city])."',
      sub_types = '".implode(',', $_POST[s_pt])."'";
    mysql_query($query);

0
 

Author Comment

by:mimalune
ID: 11983156
got it working..:)))) thanks a bunch once more....
appreciate your help greatly
0
 
LVL 25

Expert Comment

by:Marcus Bointon
ID: 11987900
Glad it's working. One small thing - you must quote indices of associative arrays, otherwise you'll find your error log filling up with warnings. Instead of saying this:

$_POST[f_name]

say this:

$_POST['f_name']

this can occasionally cause conflicts with quoting within SQL strings, so use {} syntax to remove ambiguity:

          $query = "INSERT INTO re2_notifications SET
     first_name = '{$_POST['f_name']}',
     last_name = '{$_POST['l_name']}',
     sub_email = '{$_POST['semail']}',
     sub_cities = '".implode(',', $_POST['s_city'])."',
     sub_types = '".implode(',', $_POST['s_pt'])."'";
0
 

Author Comment

by:mimalune
ID: 11992978
k..thanks for the tip..appreciate very much :)
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Help in good tutorials for PHP, HTML and CSS 6 43
IP 10.0.1.2 / 255.0.0.0 61 56
issue with DB import 1 20
Add a loading gif while php runs server side 15 37
Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question