homepage Welcome to WebmasterWorld Guest from 54.83.133.189
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
looping help needed
roelp




msg:3909243
 8:55 am on May 8, 2009 (gmt 0)

Hi webmasters!

I've been messing around with some piece of PHP-code for some time. I got it partially working, but I just can't seem to fix the error :(

The script is intended to store imagemagick-output into a database. When there's one image it works (the bottom part of the code), however when I try it with multiple images, it just won't work.

I tried to make a loop, but the complexity of the code is kinda overwhelming for me. I tried looking up the functions in the php-manual, but when I try to insert a loop, it just doesn't work. I'll continue trying and maybe someone here could give me a hand in the right direction.

All help is appreciated!

the code:

function checkFile($dir,$fileName,$num,$paper_num_id)
{
if(file_exists($dir.$fileName.".$num") )
{
$expFN = explode(".",$fileName);
$fn = $expFN[0] . "$num.jpg";

//renaming files here
rename("$dir$fileName.$num","$dir$fn");
$num += 1;

//INSERTING RECORD
if( mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD) )
{
mysql_select_db(DB_DBNAME);

$sql = mysql_query("INSERT INTO article_pages(article_no_id, file_name) VALUES ($article_num_id, '$dir$fn')");
if( $sql )
{
$ins = 1;
}
else
{
$ins = 0;
}

}
checkFile($dir,$fileName,$num,$article_num_id);
}
else //STORES A SINGLE FILE
{
$fn = "";
$expFN = explode(".",$fileName);
$fn = rand(1,9) . ".jpg";

//renaming files here
rename("$dir$fileName.$num","$dir$fn");

//INSERTING RECORD
if( mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD) )
{
mysql_select_db(DB_DBNAME);
$sql = mysql_query("INSERT INTO article_pages(article_no_id, file_name) VALUES ($article_num_id, '$dir$fn')");
if( $sql )
{
$ins = 1;
}
else
{
$ins = 0;
}
}
}
return $ins;
}

 

le_gber




msg:3909533
 3:38 pm on May 8, 2009 (gmt 0)

ok I am not the greatest PHP dev around but have you tried adding a foreach loop to split your file array when you have more than one file?

roelp




msg:3909584
 4:45 pm on May 8, 2009 (gmt 0)

Hi le_gber,

that was exactly the point. I'm also not a allround php-dev, I can read the code and manipulate it somewhat.

A foreach seemed to me as the best solution as well, but I didn't know where to place it in the code :(

I'll take some more time this weekend to check how to use a foreach properly and just mess around with the code.

roelp




msg:3910936
 12:15 pm on May 11, 2009 (gmt 0)

I got it solved.

For the ones interested. Here is it:

else //RUNS IF THERE IS A SINGLE FILE
{
$fn = "";
$expFN = explode(".",$fileName);

$count = count($expFN);

for($i = 0; $i < $count; ++$i)
{
$fn[] = "$expFN[0]-$i.jpg";
}

//renaming files here
rename("$dir$fileName.$num","$dir$fn");
$num += 1;

foreach ($fn as $key) {

//INSERTING RECORD
if( mysql_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD) )
{
mysql_select_db(DB_DBNAME);
$sql = mysql_query("INSERT INTO article_pages(article_no_id, file_name) VALUES ($article_num_id, '$dir$key')");
if( $sql )
{
$ins = 1;
}
else
{
$ins = 0;
}
}
}
}
return $ins;
}

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved