Advertisement
Virajsinh

Get Sub Dist List MySQL Query PhP Script

Oct 28th, 2024 (edited)
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 6.56 KB | Source Code | 0 0
  1. <?php
  2.  
  3. // $main_link = "https://www.viewvillage.in/district/ahmadabad-438"; $subDistId = 1; // Starting sub-district ID
  4. // $main_link = "https://www.viewvillage.in/district/amreli-439"; $subDistId = 18; // Starting sub-district ID
  5. // $main_link = "https://www.viewvillage.in/district/anand-440"; $subDistId = 29; // Starting sub-district ID
  6. // $main_link = "https://www.viewvillage.in/district/arvalli-672"; $subDistId = 38; // Starting sub-district ID
  7. // $main_link = "https://www.viewvillage.in/district/banas-kantha-441"; $subDistId = 44; // Starting sub-district ID
  8. // $main_link = "https://www.viewvillage.in/district/bharuch-442"; $subDistId = 58; // Starting sub-district ID
  9. // $main_link = "https://www.viewvillage.in/district/bhavnagar-443"; $subDistId = 67; // Starting sub-district ID
  10. // $main_link = "https://www.viewvillage.in/district/botad-676"; $subDistId = 77; // Starting sub-district ID
  11. // $main_link = "https://www.viewvillage.in/district/chhotaudepur-668"; $subDistId = 81; // Starting sub-district ID
  12. // $main_link = "https://www.viewvillage.in/district/dohad-445"; $subDistId = 87; // Starting sub-district ID
  13. // $main_link = "https://www.viewvillage.in/district/dang-444"; $subDistId = 96; // Starting sub-district ID
  14. // $main_link = "https://www.viewvillage.in/district/devbhumi-dwarka-674"; $subDistId = 99; // Starting sub-district ID
  15. // $main_link = "https://www.viewvillage.in/district/gandhinagar-446"; $subDistId = 103; // Starting sub-district ID
  16. // $main_link = "https://www.viewvillage.in/district/gir-somnath-675"; $subDistId = 107; // Starting sub-district ID
  17. // $main_link = "https://www.viewvillage.in/district/jamnagar-447"; $subDistId = 113; // Starting sub-district ID
  18. // $main_link = "https://www.viewvillage.in/district/junagadh-448"; $subDistId = 120; // Starting sub-district ID
  19. // $main_link = "https://www.viewvillage.in/district/kachchh-449"; $subDistId = 130; // Starting sub-district ID
  20. // $main_link = "https://www.viewvillage.in/district/kheda-450"; $subDistId = 140; // Starting sub-district ID
  21. // $main_link = "https://www.viewvillage.in/district/mahisagar-669"; $subDistId = 151; // Starting sub-district ID
  22. // $main_link = "https://www.viewvillage.in/district/mahesana-451"; $subDistId = 157; // Starting sub-district ID
  23. // $main_link = "https://www.viewvillage.in/district/morbi-673"; $subDistId = 167; // Starting sub-district ID
  24. // $main_link = "https://www.viewvillage.in/district/narmada-452"; $subDistId = 172; // Starting sub-district ID :: Please Check Database Have 7 Sub District And in Link 5 Sub Dist
  25. // $main_link = "https://www.viewvillage.in/district/navsari-453"; $subDistId = 179; // Starting sub-district ID
  26. // $main_link = "https://www.viewvillage.in/district/panch-mahals-454"; $subDistId = 185; // Starting sub-district ID :: Please Check Database Have 8 Sub District And in Link 7 Sub Dist
  27. // $main_link = "https://www.viewvillage.in/district/patan-455"; $subDistId = 193; // Starting sub-district ID
  28. // $main_link = "https://www.viewvillage.in/district/porbandar-456"; $subDistId = 202; // Starting sub-district ID
  29. // $main_link = "https://www.viewvillage.in/district/rajkot-457"; $subDistId = 205; // Starting sub-district ID
  30. // $main_link = "https://www.viewvillage.in/district/sabar-kantha-458"; $subDistId = 219; // Starting sub-district ID :: Please Check Database Have 9 Sub District And in Link 8 Sub Dist
  31. // $main_link = "https://www.viewvillage.in/district/surat-459"; $subDistId = 228; // Starting sub-district ID :: Please Check Database Have 15 Sub District And in Link 14 Sub Dist
  32. // $main_link = "https://www.viewvillage.in/district/surendranagar-460"; $subDistId = 243; // Starting sub-district ID :: Please Check Database Have 11 Sub District And in Link 10 Sub Dist
  33. // $main_link = "https://www.viewvillage.in/district/tapi-641"; $subDistId = 254; // Starting sub-district ID :: Please Check Database Have 8 Sub District And in Link 7 Sub Dist
  34. // $main_link = "https://www.viewvillage.in/district/vadodara-461"; $subDistId = 262;
  35. $main_link = "https://www.viewvillage.in/district/valsad-462"; $subDistId = 274;
  36.  
  37. // Fetch the content of the main link
  38. $response = @file_get_contents($main_link);
  39. if ($response === FALSE) {
  40.     echo "Error fetching data from: $main_link\n";
  41.     exit; // Exit if there is an error
  42. }
  43.  
  44. // Create a DOM parser for the main link
  45. $dom = new DOMDocument();
  46. @$dom->loadHTML($response); // Suppress warnings due to invalid HTML
  47.  
  48. // Use XPath to extract village list links
  49. $xpath = new DOMXPath($dom);
  50. $villageNodes = $xpath->query('//tbody/tr/td[2]/a[@href]'); // Adjust the selector to find the links
  51.  
  52. $villageLinks = []; // Array to hold the village links
  53.  
  54. foreach ($villageNodes as $node) {
  55.     $villageLinks[] = $node->getAttribute('href'); // Get the village link
  56. }
  57.  
  58. foreach ($villageLinks as $link) {
  59.     // Fetch the content of the village list page
  60.     $response = @file_get_contents($link);
  61.     if ($response === FALSE) {
  62.         echo "Error fetching data from: $link\n";
  63.         continue; // Skip this link if there is an error
  64.     }
  65.  
  66.     // Create a DOM parser for the village link
  67.     $dom = new DOMDocument();
  68.     @$dom->loadHTML($response); // Suppress warnings due to invalid HTML
  69.  
  70.     // Use XPath to extract village names
  71.     $xpath = new DOMXPath($dom);
  72.     $villageNodes = $xpath->query('//td'); // Adjust this selector as needed
  73.  
  74.     // Check if village names were found
  75.     if ($villageNodes->length === 0) {
  76.         echo "No village names found for: $link\n";
  77.         continue; // Skip this link if no village names are found
  78.     }
  79.  
  80.     $values = []; // Initialize an array to hold values for the current link
  81.  
  82.     foreach ($villageNodes as $node) {
  83.         // Only take the second <td> (the village name)
  84.         if ($node->parentNode->childNodes->length > 1) {
  85.             $villageName = trim($node->textContent); // Get the village name
  86.             // Prepare the value for insertion
  87.             $values[] = "(NULL, '$subDistId', '$villageName', NULL, NULL, 'active', '0', '0', current_timestamp(), NULL)";
  88.         }
  89.     }
  90.  
  91.     // Create the SQL query for the current link if there are values
  92.     if (!empty($values)) {
  93.         $sqlQueries = "INSERT INTO `tbl_village` (`village_id`, `sub_dist_id`, `village_name`, `village_name_guj`, `village_name_hin`, `status`, `created_by`, `updated_by`, `created_at`, `updated_at`) VALUES\n" . implode(",\n", $values) . ";\n";
  94.         echo "<pre>$sqlQueries</pre><br>"; // Output the SQL query
  95.     } else {
  96.         echo "No village names found for: $link\n";
  97.     }
  98.  
  99.     $subDistId++; // Increment sub-district ID for the next link
  100. }
  101. ?>
  102.  
Tags: sql php mysql
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement