Jump to content

cyberkiller

Members
  • Posts

    21
  • Joined

  • Last visited

    Never

Profile Information

  • Gender
    Not Telling

cyberkiller's Achievements

Newbie

Newbie (1/5)

0

Reputation

  1. Found the fix on google $productdescription[1] = mysql_real_escape_string($productdescription[1]);
  2. I am trying to insert product descriptions into a mysql database, however they are failing because of apostrophes in the text. How do I fix this? Everything is working fine except $productdescription $ProductDescription contains, "Record In Stereo Sound, Then Play Back Your Videos Instantly On You Computer. The Sx130 Is Is All About Creativity, With Fun New Scene Modes Like Miniature Effect For Movies, And Fisheye Effect For Artistically Distorted Stills. While You're Having Fun Creating, The Camera's Smart Flash Exposure And Advanced Smart Auto Systems Are Ensuring That Every Image Is The Best It Can Be. Add The Digic 4 Image Processor, 12.1 Megapixels And Canon's Optical Image Stabilizer, And You've Got The Ideal Camera For Making The Good Times Last." SQL Error on insert, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 're Having Fun Creating, The Camera's Smart Flash Exposure And Advanced Smart Aut' at line 1 " mysql_select_db("testdb") or die(mysql_error()); mysql_query ("INSERT INTO product (merchantname, producttitle, productdescription, gtin, availability, price) VALUES ('$merchantname[1]','$producttitle[1]','$productdescription[1]','$gtin[1]',''$availability[1]','$price[1]')");
  3. I need to pull data out of google's merchant API. The resulting array I am getting is confusing me though and I am not sure how to pull the data I need from it. How would I get all the "name" fields out of the array or say product titles? Either to just print to screen or save to another variable? I'm currently pulling the data in the json format. Google offers both json and atom. I figured json would be easier? I thought I could just do something like $result['name'] but that doesn't seem to be working. Here's my code, and a snippet of the output. <?php $result = json_decode(file_get_contents('https://www.googleapis.com/shopping/search/v1/public/products?key=key&country=US&q=digital+camera&alt=json'), true); echo "<pre>"; var_dump($result); echo "</pre>"; ?> [code] array(10) { ["kind"]=> string(17) "shopping#products" ["etag"]=> string(57) ""q_Cz93YRTLuwHgwpLYFnNN97DNM/Yat9tcRJVFxzoZmXA_0H3Qp-HuI"" ["id"]=> string(37) "tag:google.com,2010:shopping/products" ["selfLink"]=> string(125) "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=json&startIndex=1&maxResults=25" ["nextLink"]=> string(126) "https://www.googleapis.com/shopping/search/v1/public/products?country=US&q=digital+camera&alt=json&startIndex=26&maxResults=25" ["totalItems"]=> int(833935) ["startIndex"]=> int(1) ["itemsPerPage"]=> int(25) ["currentItemCount"]=> int(25) ["items"]=> array(25) { [0]=> array(4) { ["kind"]=> string(16) "shopping#product" ["id"]=> string(66) "tag:google.com,2010:shopping/products/1113342/17031817732574057954" ["selfLink"]=> string(103) "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/17031817732574057954?alt=json" ["product"]=> array(15) { ["googleId"]=> string(20) "17031817732574057954" ["author"]=> array(2) { ["name"]=> string(7) "Walmart" ["accountId"]=> string(7) "1113342" } ["creationTime"]=> string(24) "2011-07-07T18:51:04.000Z" ["modificationTime"]=> string(24) "2011-10-16T21:59:30.000Z" ["country"]=> string(2) "US" ["language"]=> string(2) "en" ["title"]=> string(66) "Canon Powershot Sx30-is Black 14.1mp Digital Camera W/ 35x Optical" ["description"]=> string(187) "Canon PowerShot SX30-IS 14MP Black Digital Camera:14.1 megapixel resolutionDelivers excellent picture quality24-840mm wide-angle zoom lensLets you get up close with the 35x optical zoom2." ["link"]=> string(117) "http://www.walmart.com/ip/Canon-14.1-MP-SX30/14972590?sourceid=1500000000000003142050&ci_src=14110944&ci_sku=14972590" ["brand"]=> string(5) "Canon" ["condition"]=> string(3) "new" ["gtin"]=> string(14) "00013803127348" ["gtins"]=> array(1) { [0]=> string(14) "00013803127348" } ["inventories"]=> array(1) { [0]=> array(5) { ["channel"]=> string(6) "online" ["availability"]=> string(7) "inStock" ["price"]=> float(349.99) ["shipping"]=> float(2.97) ["currency"]=> string(3) "USD" } } ["images"]=> array(1) { [0]=> array(1) { ["link"]=> string(71) "http://i.walmartimages.com/i/p/00/01/38/03/12/0001380312734_500X500.jpg" } } } } [1]=> array(4) { ["kind"]=> string(16) "shopping#product" ["id"]=> string(65) "tag:google.com,2010:shopping/products/3622330/1688520904457034451" ["selfLink"]=> string(102) "https://www.googleapis.com/shopping/search/v1/public/products/3622330/gid/1688520904457034451?alt=json" ["product"]=> array(15) { ["googleId"]=> string(19) "1688520904457034451" ["author"]=> array(2) { ["name"]=> string(11) "Apple Store" ["accountId"]=> string(7) "3622330" } ["creationTime"]=> string(24) "2010-10-21T20:31:02.000Z" ["modificationTime"]=> string(24) "2011-10-14T21:10:06.000Z" ["country"]=> string(2) "US" ["language"]=> string(2) "en" ["title"]=> string(39) "Canon PowerShot SX130 IS Digital Camera" ["description"]=> string(532) "Record In Stereo Sound, Then Play Back Your Videos Instantly On You Computer. The Sx130 Is Is All About Creativity, With Fun New Scene Modes Like Miniature Effect For Movies, And Fisheye Effect For Artistically Distorted Stills. While You're Having Fun Creating, The Camera's Smart Flash Exposure And Advanced Smart Auto Systems Are Ensuring That Every Image Is The Best It Can Be. Add The Digic 4 Image Processor, 12.1 Megapixels And Canon's Optical Image Stabilizer, And You've Got The Ideal Camera For Making The Good Times Last." ["link"]=> string(112) "http://store.apple.com/us/go/product/H2807ZM/A?aosid=p228&affiliateID=10262&cid=AOS-US-SHOPIP-Google&site=Google" ["brand"]=> string(5) "Canon" ["condition"]=> string(3) "new" ["gtin"]=> string(14) "00013803127386" ["gtins"]=> array(1) { [0]=> string(14) "00013803127386" } ["inventories"]=> array(1) { [0]=> array(4) { ["channel"]=> string(6) "online" ["availability"]=> string(7) "inStock" ["price"]=> float(229.95) ["currency"]=> string(3) "USD" } } ["images"]=> array(1) { [0]=> array(1) { ["link"]=> string(177) "http://storeimages.apple.com/1819/as-images.apple.com/is/image/AppleInc/H2807?wid=600&hei=600&fmt=jpeg&qlt=95&op_sharpen=0&resMode=bicub&op_usm=0.5,0.5,0,0&iccEmbed=0&layer=comp" } } } } [2]=> array(4) { ["kind"]=> string(16) "shopping#product" ["id"]=> string(66) "tag:google.com,2010:shopping/products/1113342/13850367466326274615" ["selfLink"]=> string(103) "https://www.googleapis.com/shopping/search/v1/public/products/1113342/gid/13850367466326274615?alt=json" ["product"]=> array(15) { ["googleId"]=> string(20) "13850367466326274615" ["author"]=> array(2) { ["name"]=> string(7) "Walmart" ["accountId"]=> string(7) "1113342" } ["creationTime"]=> string(24) "2011-07-04T19:48:28.000Z" ["modificationTime"]=> string(24) "2011-10-16T21:59:30.000Z" ["country"]=> string(2) "US" ["language"]=> string(2) "en" ["title"]=> string(67) "Canon Powershot Sx130-is Black 12.1mp Digital Camera W/ 12x Optical" ["description"]=> string(599) "Canon PowerShot SX130-IS 12.1MP Digital Camera:12.1-megapixel resolutionDelivers excellent picture qualityWide-angle zoom lensLets you get up close with the 12x optical zoom3.0" widescreen LCD displayEnjoy easy focus selection and photo viewingShoot HD videoRecord movies in 720p HD format on the Canon black 12.1MP digital cameraSmart Flash Exposure PowerShot SX130-IS determines the correct flashUseful new scene modesMiniature Effect for movies and Fisheye for capturing imagesSmart AutoSelects the proper settings based on 28 predefined shooting situationsMulti-format memory slotCanon black 12." ["link"]=> string(219) "http://www.walmart.com/ip/Kodak-Playsport-Video-Camera-Value-Bundle-w-Bonus-4GB-Memory-Card-Camera-Yellow-Float-Strap-Rechargeable-Battery-Charger/14972582?sourceid=1500000000000003142050&ci_src=14110944&ci_sku=14972582" ["brand"]=> string(5) "Canon" ["condition"]=> string(3) "new" ["gtin"]=> string(14) "00013803127386" ["gtins"]=> array(1) { [0]=> string(14) "00013803127386" } ["inventories"]=> array(1) { [0]=> array(5) { ["channel"]=> string(6) "online" ["availability"]=> string(7) "inStock" ["price"]=> float(179.99) ["shipping"]=> float(2.97) ["currency"]=> string(3) "USD" }
  4. What you posted works and is what I am looking for however I was going to also display the image_url with the brands. End Result would be to display the list of brands with their associated image_url, but with no duplicate brands.
  5. Is there anyway to remove the duplicate brands and maintain the rest of the data associated with the brands? After filtering 'brands' can I rejoin it back with the other data? img url, product name etc....
  6. Doing an API call and want to remove duplicates so I can display the products by brand. Problem I am having now is when I try to display the brands, I am getting duplicate brands. When I display the brands I am getting Nike, Nike, Reebook. When I just want, Nike, Reebok. My Code <?php $results = array(); //array from API call $results2 = $results['data']; function multi_unique($array) { foreach ($array as $k=>$na) $new[$k] = serialize($na); $uniq = array_unique($new); foreach($uniq as $k=>$ser) $new1[$k] = unserialize($ser); return ($new1); } $results3 = multi_unique($results2); foreach ($results3 as $var) { echo $var['brand']; ?> <br /> <?php } ?> The array { "errors": [ ], "warnings": [ ], "data": [ { "url": "http://[...]", "image_url": "[...]", "image_thumb_url": "[...]", "keyword": "Red Running Shoes", "description": "Red Running Shoes - Perfect for long jogs!", "category": "Shoes", "price": "59.99", "price_sale": null, "currency": "USD", "merchant": "", "brand": "Nike", "upc": "0944588778", "isbn": null, "sales": 23 }, { "url": "http://[...]", "image_url": "http://[...]", "image_thumb_url": null, "keyword": "Blue Running Shoes", "description": "Blue Running Shoes - Perfect for long jogs!", "category": "Shoes", "price": "59.99", "price_sale": "49.99", "currency": "USD", "merchant": "", "brand": "Nike", "upc": "0944588779", "isbn": null, "sales": 12 } { "url": "http://[...]", "image_url": "http://[...]", "image_thumb_url": null, "keyword": "Black Running Shoes", "description": "Black Running Shoes - Perfect for long jogs!", "category": "Shoes", "price": "59.99", "price_sale": "49.99", "currency": "USD", "merchant": "", "brand": "Reebok", "upc": "0944554779", "isbn": null, "sales": 12 } ], "coupons": [ ], "countryCode": "US", "page": 1, "limit": 10, "totalRecords": 3 }
  7. $results['info'] is loaded from a fetch to an API. The problem with processing it before hand is the API we are dealing with only allows 10 results at a time, so we loop it several hundred times to get the data out. I'm thinking I should either just load it all into a mysql table instead of a text file and work with it there or just load the text file with the duplicates and filter them out after.
  8. Tried both your examples, same exact problem with both still. Two duplicates then none. I just don't understand why the second one gets through, file is empty so it inserts it, second time though it should read the file and flag it as duplicate?
  9. Updated the code with your example, but I am still getting duplicate entries. If I feed the script the same duplicate data say, 6 times. It will write it twice to the file, before recognizing it as being duplicate. $myFile = "complete.txt"; foreach($results['info'] as $fields) { // Make the desired row from $row $output = $fields['keyword']."|".$fields['affiliate_url']."|".$fields['image_url']."|".$fields['description']; $dataArray = file($myFile, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES); // Check if that row already exists in the file if(!in_array($output, $dataArray)) { // Doesn't exist, write the line and add a line break at the end. // FILE_APPEND will append the new line, so no need to write everything again. file_put_contents($myFile, $output."\n", FILE_APPEND); } }
  10. Sorry the code I posted had some typo's in it, here is the fixed version. $results['info'] is the result of an API call to the back end to retrieve the data. And then firstname, lastname, address, number are the values in the array. I added the | in $output to pipe delimit the file. And basically the API data is full of duplicates, so I put in the inarray so we can clean out the duplicates as we write the text file. The whole purpose of this script is to grab data from the API and put it into a text file that can be used in excel, etc. Dumping the API data was the simple part, but I was hoping to remove the duplicates as we dumped it, that's why it is reading the file every time inside the loop to keep comparing the data we just inserted to what we're about to write again. The other problem is the API has result limits, so we have to call it multiple times to get all the data out. I tried converting my code to your example, however I was having problems with $dataArray and again inarray always being false. The code below works, however it doesn't seem to catch duplicates until their is already one. If I feed the exact same data into it, say 6 times. It'll insert it into the text file twice, then start reading the duplicates and skipping them. I need to get it so it's only inserting the same data in once and not twice. $myfile = "complete.txt"; foreach ($results['info'] as $fields) { $output=$fields['firstname']."|".$fields['lastname']."|".$fields['address']."|".$fields['number']."\n"; $handle = fopen($myfile, "r"); $contents = fread($handle, filesize($myfile)); fclose($handle); $list = array(); $list = explode("\n",$contents); $list = array_map("trim", $list); $current = $output; $current = trim($current); if (in_array($current,$list)) { } else { if($file=fopen($myfile, "a")) { //open file for writing fwrite($file, $output); //write to file } } }
  11. ok, that definitely helped. However I am still getting duplicates in the text file. It seems to allow one duplicate before it starts detecting them. There must be an error in my loops somewhere? My updated code, $myfile = "complete.txt"; foreach ($results['info'] as $data) { $output=$fields['firstname']."|".$record['lastname']."|".$record['address']."|".$record['number']."\n"; $handle = fopen($myfile, "r"); $contents = fread($handle, filesize($myfile)); fclose($handle); $list = array(); $list = explode("\n",$contents); $list = array_map("trim", $list); $current = $output; $current = trim($current); echo in_array($current,$list) ? $current.' exists' : $current.' does not exist'; if (in_array($current,$list)) { print "duplicate"; } else { if($file=fopen($myfile, "a")) { //open file for writing fwrite($file, $output); //write to file } } }
  12. I am trying to take all the information from $results and input it into a text file, sometimes we get duplicates in $results so I want to check the text file for the entry before writing it in so we don't get duplicates in the text file. However inarray keeps returning false, even when I echo out the two arrays and they match? $myfile = "complete.txt"; foreach ($results['info'] as $data) { $output=$fields['firstname']."|".$record['lastname']."|".$record['address']."|".$record['number']."\n"; $handle = fopen($myfile, "r"); $contents = fread($handle, filesize($myfile)); fclose($handle); $list = array(); $list = explode("\n",$contents); $list = array_map("trim", $list); $current = $output; echo in_array($current,$list) ? $current.' exists' : $current.' does not exist'; if (in_array($current,$list)) { print "duplicate"; } else { if($file=fopen($myfile, "a")) { //open file for writing fwrite($file, $output); //write to file } } }
  13. I have a variable coming in with a state abbreviation which I want to change to the full name of the state. I am pretty new to php and have no clue how I should code it or where to start. I need to do it for all 50 states. I included some pseudo code below. $variable1 $variable2 if variable = la variable2 = los angeles if variable = ca variable2 = california etc.. for all 50 states.
  14. Is their a more efficient way to do this query? $query="SELECT domain_id,domain FROM domain ORDER BY rand() Limit 1"; Expected output is one random domain_id, domain from the table. Now I did read the sticky, http://www.phpfreaks.com/forums/index.php/topic,125759.0.html However to me the queries in that post looked more strainful than my current one.
  15. I wrote a small script that will output information to an rss file. It worked fine, however I modified it to output to a randomly named file. Upon running the script it outputs two random files instead of just one. I've narrowed it down and for some reason its the while loop causing the problem. Not sure why though. The table im using just has three fields, id title date where id is auto incrementing. <?php $link = mysql_connect('server', 'user', 'pw') or die('Could not connect: ' . mysql_error()); echo 'Connected successfully'; mysql_select_db('database') or die('Could not select database'); $xmldate = date("r"); $query="SELECT * FROM rss order by 'id' desc limit 0,100"; $result=mysql_query($query); $body="<?xml version=\"1.0\" encoding=\"UTF-8\"?> <rss version=\"2.0\"> <channel> <title>My Site</title> <link>http://www.mydomain.com</link> <description>My Description</description> <copyright>(c)</copyright>"; $i=0; while ($i < 100) { $title=mysql_result($result,$i,"title"); $body .=" <item> <title>$title</title> <link>http://www.mysite.com</link> <description><![CDATA[ my description ]]></description> <pubDate>$xmldate</pubDate> </item>"; $i++; } $body .=" </channel> </rss>"; echo $body; $ran=rand(); $path=$ran.".xml"; $filenum=fopen($path,"w"); fwrite($filenum,$body); fclose($filenum); 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.