I have the following code:
$image_1 = $value->getElementsByTagName("Image1");
$image1 = $image_1->item(0)->nodeValue;
$image_2 = $value->getElementsByTagName("Image2");
$image2 = $image_2->item(0)->nodeValue;
Is there an easier way, to not repeat code If I need $image_3 ?
i.e. how can I refactor this?
Thanks
UPDATE:
I am using the $images_x variables in further code, that also needs refactoring:
UPDATE 2: - My full code:
$image_1 = $value->getElementsByTagName("Image1");
$image1 = $image_1->item(0)->nodeValue;
$image_2 = $value->getElementsByTagName("Image2");
$image2 = $image_2->item(0)->nodeValue;
$image_3 = $value->getElementsByTagName("Image3");
$image3 = $image_3->item(0)->nodeValue;
$filename_1 = basename($image1);
$ch = curl_init ($image1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata_1=curl_exec ($ch);
curl_close ($ch);
$fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_1,'w');
fwrite($fp, $rawdata_1);
fclose($fp);
$filename_2 = basename($image2);
$ch = curl_init ($image2);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata_2=curl_exec ($ch);
curl_close ($ch);
$fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_2,'w');
fwrite($fp, $rawdata_2);
fclose($fp);
$filename_3 = basename($image3);
$ch = curl_init ($image3);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata_3=curl_exec ($ch);
curl_close ($ch);
$fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename_3,'w');
fwrite($fp, $rawdata_3);
fclose($fp);
$product->addImageToMediaGalle开发者_JAVA技巧ry(Mage::getBaseDir('media') . DS . 'import/' . $filename_1, array('image', 'small_image','thumbnail'), false, false);
$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $filename_2, array('image', 'small_image','thumbnail'), false, false);
$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $filename_3, array('image', 'small_image','thumbnail'), false, false);
You could use loops:
$images = array();
for ($i = 1; $i <= 2; $i++) {
$images[] = $value->getElementsByTagName("Image" . $i)->item(0)->nodeValue;
}
// and then you can get an image via $images[0], $images[1] and so on
All code can be rewritten as Brad F Jacobs suggested:
function downloadAndSave($image) {
$filename = basename($image);
$ch = curl_init ($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen(Mage::getBaseDir('media') . DS . 'import/'.$filename,'w');
fwrite($fp, $rawdata);
fclose($fp);
return $filename;
}
// here you should have another loop, suppose foreach ($products as $product) {
for ($i = 1; $i <= 2; $i++) {
$filename = downloadAndSave($value->getElementsByTagName("Image" . $i)->item(0)->nodeValue);
$product->addImageToMediaGallery(Mage::getBaseDir('media') . DS . 'import/' . $filename, array('image', 'small_image','thumbnail'), false, false);
}
// end of foreach }
$images = array();
for ($x = 1; $x <= 3; $x++) {
$images[] = $value->getElementsByTagName("Image$x")->item(0)->nodeValue;
}
You could bundle this into a function:
$image = getImage("Image1");
function getImage($path)
{
$image_raw = $value->getElementsByTagName($path);
return $image_raw->item(0)->nodeValue;
}
Furthermore, you could read the resource names from a file (if this is appropriate in the context you wish to use this in), returning an array of values in a foreach loop:
Pseudocode:
$imgArray = array();
foreach($pathNames as $path)
{
$imgArray[] = getImage($path);
}
Use a loop?
$max_ids = 5; // or whatever
$images = array();
for( $i=1; $i <= $max_ids; $i++ ) {
$images[$i]['tag'] = $value->getElementsByTagName("Image$i");
$images[$i]['value'] = $image[$i]['tag']->item(0)->nodeValue;
}
Also creates a data structure for your images.
Best use arrays. This will give you an array of images:
for ($i = 1; $i < 3; $i++)
{
$imageElement = $value->getElementsByTagName("Image".$i);
$imageElements[] = $imageElement;
$images[] = $imageElement->item(0)->nodeValue;
}
create a function to combine the two lines.
精彩评论