Jump to content

Updating database after foreach


graham23s

Recommended Posts

Hi Guys,

 

I have a folder full of product images, like:

 

shoes.jpg

jacket.jpg etc

 

in my database i have a field called product names that are structured like

 

shoes

jacket

 

with this code:

 

<?php

include("inc-dbconnection.php");

$q = "SELECT * FROM `fhp_products`";
$r = mysql_query($q);

$dir = 'images/'; 
$files = scandir($dir);

foreach ($files as $file) 
{
   if ($file!='.' && $file!='..' )
   {
      // Take away the extension of the image files to leave just the filename
      $image = explode(".", $file);
      
      // List of images in directory
      echo $image[0].'<br>';
   }

}

  echo "<hr>";

while ($a = mysql_fetch_array($r))
{

$ID = $a['id'];
$genericName = $a['product'];

$strReplace = str_replace(" ", "-", $genericName); 

// This print all the products
echo $strReplace.'<br>';

}

?>

 

i manage to do some string manipulation, i have:

 

1) cut the extension of the images

2) replaced spaces the hyphens

 

what this does is makes the shoes.jpg into shoes (exactly as it appears in the database)

 

what i need to do is: UPDATE `fhp_products` SET `image`='NAMEOFTHEIMAGE' WHERE `id`='$ID';

 

basically so:

 

if ($image[0] == $strReplace)

{

// If this is shoes then update the database accordingly etc

UPDATE `fhp_products` SET `image`='NAMEOFTHEIMAGE' WHERE `id`='$ID';

}

 

I hope i explained it properly lol

 

thanks for any help guys

 

Graham

 

 

Link to comment
https://forums.phpfreaks.com/topic/184069-updating-database-after-foreach/
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.