Solved

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

Posted on 2004-09-03
7
230 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:
Squinky 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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:Squinky
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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Both Easy and Powerful How easy is PHP? http://lmgtfy.com?q=how+easy+is+php (http://lmgtfy.com?q=how+easy+is+php)  Very easy.  It has been described as "a programming language even my grandmother can use." How powerful is PHP?  http://en.wikiped…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now