Reputation: 89
I have the following code which is almost the same all the time (about 50 times):
if ($product=='product1' || $product=='product1 + extra')
{
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO downloads (key, file, expires)VALUES ('{$strKey}', 'test.zip', '".(time()+(60*60*24*7))."')");
}
else if ($product=='product2' || $product=='product2 + extra')
{
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO downloads (key, file, expires)VALUES ('{$strKey}', 'test2.zip', '".(time()+(60*60*24*7))."')");
}
//else if () {}
The only things that are changing are the product and the file name (product1, product2, ecc.. + test.zip, test2.zip,ecc...).
So I was thinking about making two arrays with the product names and the file names and then looping through these arrays, but I have no idea if it's possible and how to change the code properly.
Does anybody know a good solution?
Thanks for helping and sorry for my English, it isn't perfect.
EDIT
product1, product2, ecc, test1.zip, test2.zip, ecc. are only placeholders for the real names which are changing and they are always different.
product1, product2 come from a $_POST which is selected from the user by some options.
Upvotes: 1
Views: 97
Reputation: 89
I've found my own solution which is working:
$files = array(
'product1' => 'test.zip',
'product2' => 'test2.zip'
//other products and files
);
After creating the array it's sufficient to create a loop like
foreach($files as $file => $name){
if($product == $file || $product == $file . ' + extra' ){
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO downloads (key, file, expires) VALUES ('{$strKey}', '$name', '".(time()+(60*60*24*7))."')");
}
};
Upvotes: 1
Reputation: 13759
Roughly you want something like the following...
$count = 50;//This should be set using something such as count() on an object.
$i = 0;
while ($i<$count)
{
if ($product=='product'.$i || $product=='product'.$i.' + extra')
{
$filename1 = strrev($product);
$file = explode('.',$filename,2);
$file_name = mysqli_real_escape_string($GLOBALS["___mysqli_ston"],strrev($file[1]));
$file_ext = mysqli_real_escape_string($GLOBALS["___mysqli_ston"],strrev($file[0]));
mysqli_query($GLOBALS["___mysqli_ston"], "INSERT INTO downloads (key, file, expires) VALUES ('{$strKey}', $file_name.$i.'.'.$file_ext, '".(time()+(60*60*24*7))."')");
}
$i++;
}
You'll have to make sure that you assign the $count
to something that determines how many items you'll be dealing with. Also make sure the while
loop iterates for as many items as you have (some people end up wondering why the last item isn't processed. If you provided more code I'd be able to give you a more spot-on example though I think you'll be able to handle it from here. Make sure that you are escaping data in those INSERT
queries, again you've only provided a limited amount of code so it is possible you that you are escaping though it's important to make sure your code is secure. Lastly you may need to change $i
to 1 instead of 0 subjective to your numbering scheme.
Upvotes: 2