Reply to Re: wandering in dis- array

Your name:

Reply:


Posted by strawberry on 11/06/06 16:38

Captain Paralytic wrote:
> strawberry wrote:
> > strawberry wrote:
> > > Captain Paralytic wrote:
> > > > strawberry wrote:
>
> > > > The reason for getting the first INSERT that has a field called "name"
> > > > is that the original code will output an INSERT statement for every
> > > > array element that is processed, since the echo is outside of the "if"
> > > > statement that identifies a table "name" field.
>
> > >
> > > Thanks a lot for the pointer CP. I'll take a look at it right away.
> >
> > Yep, excellent. That seems to have sorted out that problem.
> >
> > function array_to_mysql($array) //define the function
> > {
> > static $table = null;
> >
> > etc, etc...
> >
> > 1 down, maybe 9 to go?
> >
> > Anyway, thanks again.
>
> Surely nearly 2 down, when you look at (and fix) the the "if" statement
> that I mentioned ;-)

You're quite right, of course! Call me a dummie if you like but the
only way I could think of resolving that was to ask again whether or
not $key = 'name'
This doesn't look very elegant but it looks like it might work. I'll
turn the echoes into real mysql statements and see what happens. Maybe
I don't have to bother with the implosion
I described. I guess it just means more INSERT statements - but so
what, right?

Cheers.

function array_to_mysql($array) //define the function
{
static $table = null;
if(is_array($array)){ //if $array is an array
foreach($array as $key=>$value){//foreach key/value pair within the
array
if(is_array($value)){
//either the current value is itself an array
array_to_mysql($value); //(so call this function again to
process that array)
} else { //or it isn't
if($key == 'name'){ //(in which case, if the key paired with that
value = 'name'
$table = $value; //then assign that value to $table)
} elseif //or else
($key == 'ID'){ //(if the key paired with that value = 'ID'
$parent = $value;} //then asign that value to $parent
//and also issue the following statement
if($key != 'name'){echo "INSERT INTO $table ($key) VALUES ('$value')
UPDATE ON DUPLICATE KEY<BR>\n";}
if(!is_null($parent)){ //Finally, if $parent is assigned issue
the following
echo "INSERT INTO $table (ID) VALUES ('$parent')<BR>\n";
}
}
}
}
}

[Back to original message]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация