Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <html>
- <head>
- <title>Image Association Script</title>
- </head>
- <body>
- <?
- //This script is designed to associate a single image with multiple products in Magento
- //The image filenames must be placed in the "value" of the catalog_product_entity_varchar table
- //under records where the product's ID matches the entity_id column and attribute_id is
- //493 - large image
- //109 - small image
- //106 - thumbnail
- //
- //Additionally the images must be linked to the products in the catalog_product_entity_media_gallery
- //table, or when the product is edited
- //Configure Server Information here
- $server='localhost';
- $user='1user';
- $pass='letmein';
- $dbname='coffee_magento';
- //Establish connection to server
- $link = mysql_connect($server, $user, $pass);
- if (!$link) {
- die('Connection Failed: ' . mysql_error());
- }
- //Select database on server
- mysql_select_db($dbname) or die ("DB Selection Failed :: ".mysql_error());
- //Get product and image from URL
- $product = $_GET['product'];
- $img = $_GET['img'];
- //Select relevant tables in catalog_product_entity_varchar table
- $query = "SELECT * from catalog_product_entity_varchar WHERE entity_id in (".$product.") AND attribute_id in (493, 109, 106)";
- $result = mysql_query($query);
- //Printout code
- while($pri=mysql_fetch_array($result)){
- echo "$pri[attribute_id] $pri[entity_id] $pri[value]<br>";
- }
- //If URL passed in new image filename, update it here.
- if (!$img) {
- echo "OK I'm done";
- }
- //Arbitrarily assigns the thumbnail as URL argument + _t.jpg
- //Small image is URL argument + _.jpg
- //Large image is URL argument + .jpg
- //URL argument &img=product would produce
- //Large Image product.jpg
- //Small Image product_.jpg
- //Thumbnail product_t.jpg
- else
- {
- echo "<br>^^ That's what we used to have. Let's run a query!!<br><br>";
- $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img.".jpg\" WHERE entity_id in (".$product.") and attribute_id = 493";
- echo $query1;
- $result=mysql_query($query1);
- echo "<br>";
- $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img."_.jpg\" WHERE entity_id in (".$product.") and attribute_id = 109";
- echo $query1;
- $result=mysql_query($query1);
- echo "<br>";
- $query1 = "UPDATE catalog_product_entity_varchar SET value=\"/images/".$img."_t.jpg\" WHERE entity_id in (".$product.") and attribute_id = 106";
- echo $query1;
- $result=mysql_query($query1);
- echo "<br>";
- echo "<br><br>Now we've turned that into<br>";
- $result=mysql_query($query);
- while($pri=mysql_fetch_array($result)){
- echo "$pri[attribute_id] $pri[entity_id] $pri[value]<br>";
- }
- }
- echo "<br><br>That was fun wasn't it?? Let's not stop there!!<br>Here's relevant rows from media table!<br><br>";
- //Now select existing relevant rows from catalog_product_entity_media_gallery table
- $query = "SELECT * from catalog_product_entity_media_gallery WHERE entity_id in (".$product.") ORDER BY entity_id ASC";
- $result = mysql_query($query);
- //Print existing relevant rows.
- while($pri=mysql_fetch_array($result)){
- echo "$pri[entity_id] $pri[value]<br>";
- }
- if (!$img) {
- echo "<br>Isn't that swell?<br><br>";
- }
- else {
- echo "<br><br>Oh noes! That's going to have to change!<br><br>";
- $query1 = "DELETE FROM catalog_product_entity_media_gallery WHERE entity_id in (".$product.")";
- $result = mysql_query($query1);
- }
- $result = mysql_query($query);
- while($pri=mysql_fetch_array($result)){
- echo "$pri[entity_id] $pri[value]<br>";
- }
- mysql_close($link);
- ?>
- </body>
- </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement