Advertisement
Guest User

Untitled

a guest
May 30th, 2017
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.50 KB | None | 0 0
  1. <html>
  2.  
  3. <head>
  4. <title>Image Association Script</title>
  5.  
  6. </head>
  7.  
  8. <body>
  9.  
  10. <?
  11. //This script is designed to associate a single image with multiple products in Magento
  12. //The image filenames must be placed in the "value" of the catalog_product_entity_varchar table
  13. //under records where the product's ID matches the entity_id column and attribute_id is
  14. //493 - large image
  15. //109 - small image
  16. //106 - thumbnail
  17. //
  18. //Additionally the images must be linked to the products in the catalog_product_entity_media_gallery
  19. //table, or when the product is edited
  20.  
  21.  
  22. //Configure Server Information here
  23. $server='localhost';
  24. $user='1user';
  25. $pass='letmein';
  26. $dbname='coffee_magento';
  27.  
  28. //Establish connection to server
  29. $link = mysql_connect($server, $user, $pass);
  30. if (!$link) {
  31.     die('Connection Failed: ' . mysql_error());
  32. }
  33.  
  34. //Select database on server
  35. mysql_select_db($dbname) or die ("DB Selection Failed :: ".mysql_error());
  36.  
  37.  
  38. //Get product and image from URL
  39. $product = $_GET['product'];
  40. $img = $_GET['img'];
  41.  
  42. //Select relevant tables in catalog_product_entity_varchar table
  43. $query = "SELECT * from catalog_product_entity_varchar WHERE entity_id in (".$product.") AND attribute_id in (493, 109, 106)";
  44. $result = mysql_query($query);
  45.  
  46. //Printout code
  47. while($pri=mysql_fetch_array($result)){
  48.     echo "$pri[attribute_id] $pri[entity_id] $pri[value]<br>";
  49. }
  50.  
  51. //If URL passed in new image filename, update it here.
  52. if (!$img) {
  53.     echo "OK I'm done";
  54. }
  55.  
  56. //Arbitrarily assigns the thumbnail as URL argument + _t.jpg
  57. //Small image is URL argument + _.jpg
  58. //Large image is URL argument + .jpg
  59. //URL argument &img=product would produce
  60. //Large Image product.jpg
  61. //Small Image product_.jpg
  62. //Thumbnail   product_t.jpg
  63. else
  64. {
  65.     echo "<br>^^ That's what we used to have. Let's run a query!!<br><br>";
  66.  
  67.     $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img.".jpg\" WHERE entity_id in (".$product.") and attribute_id = 493";
  68.     echo $query1;
  69.     $result=mysql_query($query1);
  70.     echo "<br>";
  71.  
  72.     $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img."_.jpg\" WHERE entity_id in (".$product.") and attribute_id = 109";
  73.     echo $query1;
  74.     $result=mysql_query($query1);
  75.     echo "<br>";
  76.  
  77. $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img."_t.jpg\" WHERE entity_id in (".$product.") and attribute_id = 106";
  78.     echo $query1;
  79.     $result=mysql_query($query1);
  80.     echo "<br>";
  81.  
  82.  
  83.     echo "<br><br>Now we've turned that into<br>";
  84.     $result=mysql_query($query);
  85.     while($pri=mysql_fetch_array($result)){
  86.         echo "$pri[attribute_id] $pri[entity_id] $pri[value]<br>";
  87.     }
  88. }
  89.  
  90. echo "<br><br>That was fun wasn't it?? Let's not stop there!!<br>Here's relevant rows from media table!<br><br>";
  91.  
  92. //Now select existing relevant rows from catalog_product_entity_media_gallery table
  93. $query = "SELECT * from catalog_product_entity_media_gallery WHERE entity_id in (".$product.") ORDER BY entity_id ASC";
  94. $result = mysql_query($query);
  95.  
  96. //Print existing relevant rows.
  97. while($pri=mysql_fetch_array($result)){
  98.     echo "$pri[entity_id] $pri[value]<br>";
  99. }
  100.  
  101. if (!$img) {
  102.     echo "<br>Isn't that swell?<br><br>";
  103. }
  104.  
  105. else {
  106.     echo "<br><br>Oh noes! That's going to have to change!<br><br>";
  107.     $query1 = "DELETE FROM catalog_product_entity_media_gallery WHERE entity_id in (".$product.")";    
  108.     $result = mysql_query($query1);
  109. }
  110.  
  111. $result = mysql_query($query);
  112.  
  113. while($pri=mysql_fetch_array($result)){
  114.     echo "$pri[entity_id] $pri[value]<br>";
  115. }
  116.  
  117. mysql_close($link);
  118. ?>
  119.  
  120.  
  121. </body>
  122. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement