I have a webshop database from which I need to extract image file names. An article number can have multiple filenames. The path of my code is as follows: I have to start from The SKU numbers, get the ID, if it is a child product, then parent ID, media ID, and image file name.
I have a code to get file names for a SKU that only contains numbers, but if there are letters, they don’t.
I only get the last item on the list, that’s right.
I need a list in this format:
SKU; image name 1, image name 2, image name 3, etc
Thanks for helping
My code:
$content=file_get_contents("sku_list.txt");
$sku_row=explode("\n",$content);
foreach ($sku_sor as $key => $value) {
$sku=$value;
$sku=str_replace(" ","",$sku);
$sql = "SELECT product_id FROM sc_ws_products_variant WHERE productnumber='".$sku."' ";
$result = mysqli_query($conn, $sql);
while ($row = $result->fetch_assoc()) {
$product_id = $row['product_id'];
$sql2 = "SELECT media_id FROM sc_media_reference WHERE table_name2_id=".$product_id."";
$result2 = mysqli_query($conn, $sql2);
if ($result2->num_rows > 0) {
while ($row2 = $result2->fetch_assoc()) {
$media_id = $row2['media_id'];
$sql3 = "SELECT name FROM sc_media WHERE id=".$media_id."";
$result3 = mysqli_query($conn, $sql3);
if ($result3->num_rows > 0) {
while ($row3 = $result3->fetch_assoc()) {
$name = $row3['name'];
$data.=$sku.";".$media_id."\r";
file_put_contents('image_and_sku.csv', $data);
}
}
else {print "not found: ".$media_id."<br>";}
}
}
else{
$sql4 = "SELECT parent FROM sc_ws_products WHERE pid=".$product_id."";
$result4 = mysqli_query($conn, $sql4);
while ($row4 = $result4->fetch_assoc()) {
$parent=$row4['parent'];
$sql5 = "SELECT media_id FROM sc_media_reference WHERE table_name2_id=".$parent."";
$result5 = mysqli_query($conn, $sql5);
while ($row5 = $result5->fetch_assoc()) {
$media_id = $row5['media_id'];
$sql6="SELECT name FROM sc_media WHERE id=".$media_id."";
$result6 = mysqli_query($conn, $sql6);
while ($row6 = $result6->fetch_assoc()) {
$name= $row6['name'].",";
$data.=$sku.";".$name."\r";
file_put_contents('image_and_sku.csv', $data);
}
}
}
}
}
}