supermerc Posted May 16, 2009 Share Posted May 16, 2009 Hey, I have a script that goes to a website using CURL and then checks for patterns within a certain area of the page like this: <?php $data = $result2; $pattern = '#src=[\'"][^\'"]+/(.+?\.(?:png|jpe?g|gif))[\'"].+?ONMOUSEOVER=[\'"]itempopup\(event,\'(\d+)\'\)[\'"]#i'; preg_match_all($pattern, $data, $images); $spath = $images[1][0]; $id = $images[2][0]; ?> What I want to do is for each match it finds it checks into database in a table for a match with the selected fields and if theres a match is echos out something. I already have the working code for the check and the echoing but I dont know really how to do the foreach so that it will look for each match it finds. Thanks Link to comment https://forums.phpfreaks.com/topic/158419-foreach-help/ Share on other sites More sharing options...
Ken2k7 Posted May 16, 2009 Share Posted May 16, 2009 Something like this ... foreach ($images as $key => $image) { // some checking here } Use var_dump($image); if you don't know what it contains. But that's just to get you started. Gotta try something yourself since we already provided the regex for you. Link to comment https://forums.phpfreaks.com/topic/158419-foreach-help/#findComment-835473 Share on other sites More sharing options...
supermerc Posted May 16, 2009 Author Share Posted May 16, 2009 The thing is as you know ken, that my script was working before when I was checking for just the image link, but now that Im checking for image link AND id it doesnt work anymore.. This was my previous, working code: <?php f$data = $result2; $pattern = "/src=[\"']?([^\"']?.*(png|jpg|gif))[\"']?/i"; preg_match_all($pattern, $data, $images); $maxItemsPerRow = 20; $i = 1; foreach($images as $array) { foreach($array as $image) { $fun = substr($image, strrpos($image, "/")+1, -1); $qry = "SELECT * from items WHERE spath = '{$fun}'"; $query = mysql_query($qry) or die (mysql_error()); $num = mysql_num_rows($query); if($num > 0) { while($results = mysql_fetch_array($query, MYSQL_ASSOC)) { echo "<td width=\"30\" height=\"30\" align=\"center\" valign=\"middle\" style=\"background-image:url(http://quiver.outwar.com/images/crewup/vault_tile.gif); width:30px; height:30px;padding:3px;\"> <img id=\"v_item_border0\" src=\"http://quiver.outwar.com/images/{$results['spath']}\" style=\"border:0px;margin:0px;width:24px;height:24px;\" ONMOUSEOVER=\"itempopup(event,'9109629')\" ONMOUSEOUT=\"kill()\" onClick=\"checkSelect();kill();\"> <input type=\"checkbox\" name=\"v_selected[]\" id=\"v_selected_0\" value=\"9109629\" style=\"display:none;\"> </td>"; if($i % $maxItemsPerRow == 0) { // Insert row break echo "</tr><tr>"; } $i++; } } } } ?> And now this is my new code where Im looking for link and ID <?php $data = $result2; $pattern = '#src=[\'"][^\'"]+/(.+?\.(?:png|jpe?g|gif))[\'"].+?ONMOUSEOVER=[\'"]itempopup\(event,\'(\d+)\'\)[\'"]#i'; preg_match_all($pattern, $data, $images); $spath = $images[1][0]; $id = $images[2][0]; $maxItemsPerRow = 20; $i = 1; foreach($images as $array) { foreach($array as $image) { $fun = substr($image, strrpos($image, "/")+1, -1); $qry = "SELECT * from items WHERE spath = '{$fun}'"; $query = mysql_query($qry) or die (mysql_error()); $num = mysql_num_rows($query); if($num > 0) { while($results = mysql_fetch_array($query, MYSQL_ASSOC)) { echo "<td width=\"30\" height=\"30\" align=\"center\" valign=\"middle\" style=\"background-image:url(http://quiver.outwar.com/images/crewup/vault_tile.gif); width:30px; height:30px;padding:3px;\"> <img id=\"v_item_border0\" src=\"http://quiver.outwar.com/images/{$results['spath']}\" style=\"border:0px;margin:0px;width:24px;height:24px;\" ONMOUSEOVER=\"itempopup(event,'9109629')\" ONMOUSEOUT=\"kill()\" onClick=\"checkSelect();kill();\"> <input type=\"checkbox\" name=\"v_selected[]\" id=\"v_selected_0\" value=\"9109629\" style=\"display:none;\"> </td>"; if($i % $maxItemsPerRow == 0) { // Insert row break echo "</tr><tr>"; } $i++; } } } } ?> Link to comment https://forums.phpfreaks.com/topic/158419-foreach-help/#findComment-835480 Share on other sites More sharing options...
supermerc Posted May 16, 2009 Author Share Posted May 16, 2009 I forgot to say on the new one it gives the error 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 '8142232')'' at line 1 Link to comment https://forums.phpfreaks.com/topic/158419-foreach-help/#findComment-835483 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.