?
Solved

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

Posted on 2004-09-03
7
Medium Priority
?
274 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

Question has a verified solution.

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

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.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses

764 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