Jump to content

ryanlitwiller

Members
  • Posts

    13
  • Joined

  • Last visited

ryanlitwiller's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Well I will try to prove, This is my main php file and the if statement is toward the bottom indexhelp.php // Select the database to work with $db = mysql_select_db('test',$link); if(!$db) { die('Selected database unavailable: ' . mysql_error()); } // Include the Mail package include("sendMail.php"); // import simple_html_dom.php to give me various methods for website selection and scraping include("simple_html_dom.php"); // get DOM from BPL URL $html0 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?forum=19'); $html1 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=25&forum=19'); $html2 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=50&forum=19'); $html3 = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?offset=75&forum=19'); function makePage($html) { $link_number = -1;//will use this as a new array key number in a foreach $meta_array = array();//making sure there is an array when functions call upon it to eliminate those errors foreach($html->find('td.forum_listing') as $e) { foreach($e->find('p.meta') as $f){//find all meta id's $meta = $f->plaintext; //echo $meta; $explode_meta = explode(" ",$meta);//explode by spaces and make an array if(!ctype_digit($explode_meta[0])){//only if array value content does not have just positive whole numbers $link_number++;//adds 1 to array key number, same as $link_number = $link_number + 1; //build a new array by key number value and name associations $meta_array[$link_number] = array( "date" => $explode_meta['0'], "time" => $explode_meta['1'], "timezone" => str_replace("by","",$explode_meta['2']), "username_first" => $explode_meta['3'], "username_last" => $explode_meta['4'] ); } } } // find all td tags with class=forum_listing $link_number = -1;//reset number foreach($html->find('td.forum_listing') as $tdTagExt){ foreach($tdTagExt->find('a')as $aTagExt){ $link_number++;//add by one again $title = $aTagExt->plaintext; $href = "http://www.backpackinglight.com".$aTagExt->href; //associating the link and title in a new array $meta_array[$link_number]['title'] = $title; $meta_array[$link_number]['href'] = $href; } } //print_r($meta_array)."<br />";//to see how the array looks //access the new links array foreach($meta_array as $link){ echo "<p>"; echo "<a href='".$link['href']."' target='_blank'>".$link['title']."</a><br />"; echo " Date: ".$link['date']." Time: ".$link['time']." ".$link['timezone']."<br />"; //Posted by: ".$link['username_first']." ".$link['username_last']."<br />"; echo "</p>"; //convert array to string for db $link_href = $link['href']; $link_title = $link['title']; $link_date = $link['date'] . " " . $link['time']; $result = mysql_query("select * from bp"); $number_of_rows_before = mysql_num_rows($result); echo "Number of rows fetched are : ". $number_of_rows_before; //populate db mysql_query("insert into `bp` (`url`,`title`,`date`) values ('$link_href','$link_title','$link_date')"); $number_of_rows_after = mysql_num_rows($result); echo "Number of rows fetched are : ". $number_of_rows_after; // send myself email after every new db entry if($number_of_rows_after>$number_of_rows_before) { sendMail(); echo "this my if"; } else{ echo "this is my else"; } } } makePage($html0); makePage($html1); makePage($html2); makePage($html3); // Close the connection mysql_close($link); ?> here is my sendMail.php which has my sendMail function <?php function sendMail() { // Include the Mail package require "Mail.php"; // Identify the sender, recipient, mail subject, and body $sender = "litwi1rm@gmail.com"; $recipient = "litwi1rm@gmail.com"; $subject = "Test mail"; $body = "New Post!"; // Identify the mail server, username, password, and port $server = "ssl://smtp.gmail.com"; $username = "litwi1rm@gmail.com"; $password = "*********"; $port = "465"; // Set up the mail headers $headers = array( "From" => $sender, "To" => $recipient, "Subject" => $subject ); // Configure the mailer mechanism $smtp = Mail::factory("smtp", array( "host" => $server, "username" => $username, "password" => $password, "auth" => true, "port" => 465 ) ); // Send the message $mail = $smtp->send($recipient, $headers, $body); if (PEAR::isError($mail)) { echo ($mail->getMessage()); } } sendMail() ?> My live site is http://php-ryanlitwiller.rhcloud.com/indexhelp.php and even when my else echo is displayed on the screen I still get an email from my sendMail() function
  2. I am trying to call a user defined function in an if statement when the condition is true. if($number_of_rows_after>$number_of_rows_before) { myFunction(); echo "this my if"; } else{ echo "this is my else"; } myFunction() is getting called regardless of whether or not my condition is true or false so i will have "this is my else" echoed and myFuction() runs
  3. one of my columns in my database is unique to prevent duplicate entries, so if a new post has not been published there will not be a new insert. I am not having any problems with my if statement condition but what I am having problems with is that my function gets called regardless of wether or not the if statement is true
  4. Well I am pretty close to having the solution. foreach($meta_array as $link){ echo "<p>"; echo "<a href='".$link['href']."' target='_blank'>".$link['title']."</a><br />"; echo " Date: ".$link['date']." Time: ".$link['time']." ".$link['timezone']."<br />"; //Posted by: ".$link['username_first']." ".$link['username_last']."<br />"; echo "</p>"; //convert array to string for db $link_href = $link['href']; $link_title = $link['title']; $link_date = $link['date'] . " " . $link['time']; $result = mysql_query("select * from bp"); $number_of_rows_before = mysql_num_rows($result); echo "Number of rows fetched are : ". $number_of_rows_before; //populate db mysql_query("insert into `bp` (`url`,`title`,`date`) values ('$link_href','$link_title','$link_date')"); $number_of_rows_after = mysql_num_rows($result); echo "Number of rows fetched are : ". $number_of_rows_after; if($number_of_rows_after>$number_of_rows_before) { sendMail(); } I have echoed the values for testing and the only problem I am having is that my sendMail() function always get called whether the if statement is true or false.
  5. yes I have tired that and it does send me mail, but I do my insert in a foreach loop and I have print my results to the page prior to inserting and when I put my script after it kills my foreach after two records //access the new links array foreach($meta_array as $link){ echo "<p>"; echo "<a href='".$link['href']."' target='_blank'>".$link['title']."</a><br />"; echo " Date: ".$link['date']." Time: ".$link['time']." ".$link['timezone']."<br />"; //Posted by: ".$link['username_first']." ".$link['username_last']."<br />"; echo "</p>"; //convert array to string for db $link_href = $link['href']; $link_title = $link['title']; $link_date = $link['date'] . " " . $link['time']; //populate db mysql_query("insert into `bp` (`url`,`title`,`date`) values ('$link_href','$link_title','$link_date')") //send email sendMail();
  6. With the help of another php freak member I have created a script that scraps the last 50 posts on a forum site it grabs the title, url, and date and populates my database. I also have an auto increment id. I have also made a function using the Mail.php package that successfully sends me an email, I would like to call my email function anytime a new insert is made into my db, although I do not want 50 emails for the first 50 entries, just the new ones, which I intend to set up a cron job to check for a new entries. However I am having a hard time coding the proper sql and php syntax. I figured possibly if does not exist then insert and call mail function. Or use $_Get to grab my column fields in variables and then put it in an if statement greater than 50. I have not been able to come up with anything promising. Any help would be greatly appreciated. I will provide a link to my site and can post my code if needed but it is rather long so let me know. THanks! Ryan http://php-ryanlitwiller.rhcloud.com/indexhelp.php
  7. also So when you set up this array using variables title and href is that comming from simple_html_dom? Are there others? Also any recommendations for literature explaining more on arrays...the book I read "PHP for Absolute Beginners" does not get into much detail
  8. WOW! I cant thank you enough QuickOldCar...so I see that I was using preg_match incorrectly? I see you used if(preg_match("~tent~i", $link['title'])){ Also why is it that you simply used multiple if statements? Is it because you are doing it within a foreach?
  9. Thanks QuickOldCar for all your insight! My reasoning for this project is primary for educational purposes. I'm a senior at a local university graduating with an IT degree that specialized in networking. As I scour the job market I cant help but notice all the software engineer jobs out there and programming was a subject I always swore off in my younger days. So I have an html class and our final project is a refined site, well I was able to talk to him and he gladly allowed me to take on server side scripting, so I tried to find a project where I could utilize server side scripting and have it be something that I could actually use or be interested in. This seemed like a good solution, as I've always got on this site and thought man there are some great deals on here but there has got a to be a better way. Really I just want a project that will give me the ability to challenge myself and to implement and learn as many concepts about php as possible. I thought once I can figure out how to properly populate my db and start running queries then maybe I could even set up email alerts for specific items I'm in need of. Also if I can create something that would make this section more helpful to other users I would gladly hand over my project over. As I read your section about setting up the db it is becoming a little more clear to me of how my concept of the db is wrong. I am having a bit of trouble manipulating all the data from the file_get_html, when I print back the content it seems like a complex structure of arrays, which I'm still trying to understand arrays a little more clearly. Also QuickOldCar I am interested in anything you want to inform me on! Thanks Again!
  10. Great! Thanks, ok great one problem down! Does anybody else have any other suggestions?
  11. Ok I am a complete noob!!! I have done many searches and believe that I have done quite well some what piecing together my script. So I use a forum which has a gear swap section for buying used goods problem is that if you try to search for specific goods then it searches the entire forum. So I found the simple_html_dom class that has the file_get_html method and was able to select only the titles of the listings. I had no problem displaying these listings and then populating a db. Now I want to use if and else-if statements along with regex to grab titles with specific keywords and put them in a corresponding column in my db which I have been unsuccessful at. I'd also like to eventually make my db searchable on my site I'm sure my code could be cleaned up in about every area so if anybody whats to chime in on any part of my code please feel free. It will be much appreciated I have put in many hours and I would like to know that building someone on the right footing lol. you can view my webpage at http://php-ryanlitwiller.rhcloud.com/ - in my page the titles still have their hyperlink but they try to navigate my server...any ideas of how to make them reach the original site? <?php // Open a MySQL connection $link = mysql_connect('127.6.146.130:3306', 'xxxxxxxxxx', 'xxxxxxxxxx'); if(!$link) { die('Connection failed: ' . mysql_error()); } // Select the database to work with $db = mysql_select_db('test'); if(!$db) { die('Selected database unavailable: ' . mysql_error()); } // import simple_html_dom.php to give me various methods for website selection and scraping include('simple_html_dom.php'); // get DOM from BPL URL $html = file_get_html('http://www.backpackinglight.com/cgi-bin/backpackinglight/forums/display_forum.html?forum=19'); // find all td tags with class=forum_listing foreach($html->find('td.forum_listing') as $tdTagExt) //grab just a tags foreach($tdTagExt->find('a')as $aTagExt){ //print selected outertext from previous selectors $refinedTitle = $tdTagExt->outertext; //display nobull listing of goods echo $refinedTitle . '<br>'; //find tent goods using regex to check for the word tent if(preg_match_all('/tent/', $refinedTitle)){ // add matches to corresponding sql coulom $sql = "insert into `bp` (`tent`) values ('$refinedTitle')"; $result = mysql_query($sql); //find sleeping bags using regex to check for the word bag } else if(preg_match_all('/bag/', $refinedTitle)){ $sql1 = "insert into `bp` (`bag`) values ('$refinedTitle')"; $result1 = mysql_query($sql1); //find boots using regex to check for the word boot or shoes } else if(preg_match_all('/boot|shoes/', $aTagExt->innertext)){ $sql2 = "insert into `bp` (`boot`) values ('$aTagExt->innertext')"; $result2 = mysql_query($sql2); //find clothing goods using regex to check for any of the words shirt|pants|parka|shorts|jacket } else if(preg_match_all('/shirt|pants|parka|shorts|jacket/', $aTagExt->innertext)){ $sql3 = "insert into `bp` (`clothing`) values ('$aTagExt->innertext')"; $result3 = mysql_query($sql3); } else { // Create and execute a MySQL query $sql4 = "insert into `bp` (`ahref`) values ('$aTagExt->innertext')"; $result4 = mysql_query($sql4); } } // Close the connection mysql_close($link); ?>
×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.