Solved

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

Posted on 2004-09-03
7
250 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
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

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

ScreenConnect 6.0 Free Trial

Check out the updates in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI that improves session organization and overall user experience. See the enhancements for yourself!

Question has a verified solution.

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

Suggested Solutions

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 …
This article discusses four methods for overlaying images in a container on a web page
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

777 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