Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

better way to list all of these options?

Status
Not open for further replies.

dkemas

Programmer
Mar 22, 2012
70
GB
I have made two functions to insert and update data to a database from a rather large form. My code below works fine, but I was wondering whether it was possible to make it much simpler through iteration or similar?

Here's my functions

Code:
function insertCF($id,$cf1_score,$cf1_isseen,$sc1_cf_id,$cf1_pdp,$cf1_redflag,$cf2_score,$cf2_isseen,$sc2_cf_id,$cf2pdp,$cf2_redflag,$cf3_score,$cf3_isseen,$sc3_cf_id,$cf3_pdp,$cf3_redflag,$cf4_score,$cf4_isseen,$sc4_cf_id,$cf4_pdp,$cf4_redflag,$cf5_score,$cf5_isseen,$sc5_cf_id,$cf5_pdp,$cf5_redflag,$cf6_score,$cf6_isseen,$sc6_cf_id,$cf6_pdp,$cf6_redflag,$cf7_score,$cf7_isseen,$sc7_cf_id,$cf7_pdp,$cf7_redflag,$cf8_score,$cf8_isseen,$sc8_cf_id,$cf8_pdp,$cf8_redflag,$cf9_score,$cf9_isseen,$sc9_cf_id,$cf9_pdp,$cf9_redflag,$cf10_score,$cf10_isseen,$sc10_cf_id,$cf10_pdp,$cf10_redflag,$cf11_score,$cf11_isseen,$sc11_cf_id,$cf11_pdp,$cf11_redflag,$cf12_score,$cf12_isseen,$sc12_cf_id,$cf12pdp,$cf12_redflag,$cf13_score,$cf13_isseen,$sc13_cf_id,$cf13_pdp,$cf13_redflag,$cf14_score,$cf14_isseen,$sc14_cf_id,$cf14_pdp,$cf14_redflag,$cf15_score,$cf15_isseen,$sc15_cf_id,$cf15_pdp,$cf15_redflag,$cf16_score,$cf16_isseen,$sc16_cf_id,$cf16_pdp,$cf16_redflag,$cf17_score,$cf17_isseen,$sc17_cf_id,$cf17_pdp,$cf17_redflag,$cf18_score,$cf18_isseen,$sc18_cf_id,$cf18_pdp,$cf18_redflag,$cf19_score,$cf19_isseen,$sc19_cf_id,$cf19_pdp,$cf19_redflag,$cf20_score,$cf20_isseen,$sc20_cf_id,$cf20_pdp,$cf20_redflag,$cfcomments,$redflagnotify)
    {
        $result = $this->Query("insert into mytable values ('','$cf1_score','$cf1_isseen','$sc1_cf_id','$cf1_pdp','$cf1_redflag','$cf2_score','$cf2_isseen','$sc2_cf_id','$cf2_pdp','$cf2_redflag','$cf3_score','$cf3_isseen','$sc3_cf_id','$cf3_pdp','$cf3_redflag','$cf4_score','$cf4_isseen','$sc4_cf_id','$cf4_pdp','$cf4_redflag','$cf5_score','$cf5_isseen','$sc5_cf_id','$cf5_pdp','$cf5_redflag','$cf6_score','$cf6_isseen','$sc6_cf_id','$cf6_pdp','$cf6_redflag','$cf7_score','$cf7_isseen','$sc7_cf_id','$cf7_pdp','$cf7_redflag','$cf8_score','$cf8_isseen','$sc8_cf_id','$cf8_pdp','$cf8_redflag','$cf9_score','$cf9_isseen','$sc9_cf_id','$cf9_pdp','$cf9_redflag','$cf10_score','$cf10_isseen','$sc10_cf_id','$cf10_pdp','$cf10_redflag','$cf11_score','$cf11_isseen','$sc11_cf_id','$cf11_pdp','$cf11_redflag','$cf12_score','$cf12_isseen','$sc12_cf_id','$cf12_pdp','$cf12_redflag','$cf13_score','$cf13_isseen','$sc13_cf_id','$cf13_pdp','$cf13_redflag','$cf14_score','$cf14_isseen','$sc14_cf_id','$cf14_pdp','$cf14_redflag','$cf15_score','$cf15_isseen','$sc15_cf_id','$cf15_pdp','$cf15_redflag','$cf16_score','$cf16_isseen','$sc16_cf_id','$cf16_pdp','$cf16_redflag','$cf17_score','$cf17_isseen','$sc17_cf_id','$cf17_pdp','$cf17_redflag','$cf18_score','$cf18_isseen','$sc18_cf_id','$cf18_pdp','$cf18_redflag','$cf19_score','$cf19_isseen','$sc19_cf_id','$cf19_pdp','$cf19_redflag','$cf20_score','$cf20_isseen','$sc20_cf_id','$cf20_pdp','$cf20_redflag','$cfcomments','$redflagnotify')");
    }
	
	function updateCF($cf1_score,$cf1_isseen,$sc1_cf_id,$cf1_pdp,$cf1_redflag,$cf2_score,$cf2_isseen,$sc2_cf_id,$cf2_pdp,$cf2_redflag,$cf3_score,$cf3_isseen,$sc3_cf_id,$cf3_pdp,$cf3_redflag,$cf4_score,$cf4_isseen,$sc4_cf_id,$cf4_pdp,$cf4_redflag,$cf5_score,$cf5_isseen,$sc5_cf_id,$cf5_pdp,$cf5_redflag,$cf6_score,$cf6_isseen,$sc6_cf_id,$cf6_pdp,$cf6_redflag,$cf7_score,$cf7_isseen,$sc7_cf_id,$cf7_pdp,$cf7_redflag,$cf8_score,$cf8_isseen,$sc8_cf_id,$cf8_pdp,$cf8_redflag,$cf9_score,$cf9_isseen,$sc9_cf_id,$cf9_pdp,$cf9_redflag,$cf10_score,$cf10_isseen,$sc10_cf_id,$cf10_pdp,$cf10_redflag,$cf11_score,$cf11_isseen,$sc11_cf_id,$cf11_pdp,$cf11_redflag,$cf12_score,$cf12_isseen,$sc12_cf_id,$cf12_pdp,$cf12_redflag,$cf13_score,$cf13_isseen,$sc13_cf_id,$cf13_pdp,$cf13_redflag,$cf14_score,$cf14_isseen,$sc14_cf_id,$cf14_pdp,$cf14_redflag,$cf15_score,$cf15_isseen,$sc15_cf_id,$cf15_pdp,$cf15_redflag,$cf16_score,$cf16_isseen,$sc16_cf_id,$cf16_pdp,$cf16_redflag,$cf17_score,$cf17_isseen,$sc17_cf_id,$cf17_pdp,$cf17_redflag,$cf18_score,$cf18_isseen,$sc18_cf_id,$cf18_pdp,$cf18_redflag,$cf19_score,$cf19_isseen,$sc19_cf_id,$cf19_pdp,$cf19_redflag,$cf20_score,$cf20_isseen,$sc20_cf_id,$cf20_pdp,$cf20_redflag,$cfcomments,$redflagnotify,$id,)
    {
        $result = $this->Query("update mytable set 
		cf1_score='$cf1_score', 
		cf1_isseen='$cf1_isseen', 
		sc1_cf_id='$sc1_cf_id', 
		cf1_pdp='$cf1_pdp', 
		cf1_redflag='$cf1_redflag',
		cf2_score='$cf2_score', 
		cf2_isseen='$cf2_isseen', 
		sc2_cf_id='$sc2_cf_id', 
		cf2_pdp='$cf2_pdp', 
		cf2_redflag='$cf2_redflag',
		cf3_score='$cf3_score', 
		cf3_isseen='$cf3_isseen', 
		sc3_cf_id='$sc3_cf_id', 
		cf3_pdp='$cf3_pdp', 
		cf3_redflag='$cf3_redflag',
		cf4_score='$cf4_score', 
		cf4_isseen='$cf4_isseen', 
		sc4_cf_id='$sc4_cf_id', 
		cf4_pdp='$cf4_pdp', 
		cf4_redflag='$cf4_redflag',
		cf5_score='$cf5_score', 
		cf5_isseen='$cf5_isseen', 
		sc5_cf_id='$sc5_cf_id', 
		cf5_pdp='$cf5_pdp', 
		cf5_redflag='$cf5_redflag',
		cf6_score='$cf6_score', 
		cf6_isseen='$cf6_isseen', 
		sc6_cf_id='$sc6_cf_id', 
		cf6_pdp='$cf6_pdp', 
		cf6_redflag='$cf6_redflag',
		cf7_score='$cf7_score', 
		cf7_isseen='$cf7_isseen', 
		sc7_cf_id='$sc7_cf_id', 
		cf7_pdp='$cf7_pdp', 
		cf7_redflag='$cf7_redflag',
		cf8_score='$cf8_score', 
		cf8_isseen='$cf8_isseen', 
		sc8_cf_id='$sc8_cf_id', 
		cf8_pdp='$cf8_pdp', 
		cf8_redflag='$cf8_redflag',
		cf9_score='$cf9_score', 
		cf9_isseen='$cf9_isseen', 
		sc9_cf_id='$sc9_cf_id', 
		cf9_pdp='$cf9_pdp', 
		cf9_redflag='$cf9_redflag',
		cf10_score='$cf10_score', 
		cf10_isseen='$cf10_isseen', 
		sc10_cf_id='$sc10_cf_id', 
		cf10_pdp='$cf10_pdp', 
		cf10_redflag='$cf10_redflag',
		cf11_score='$cf11_score', 
		cf11_isseen='$cf11_isseen', 
		sc11_cf_id='$sc11_cf_id', 
		cf11_pdp='$cf11_pdp', 
		cf11_redflag='$cf11_redflag',
		cf12_score='$cf12_score', 
		cf12_isseen='$cf12_isseen', 
		sc12_cf_id='$sc12_cf_id', 
		cf12_pdp='$cf12_pdp', 
		cf12_redflag='$cf12_redflag',
		cf13_score='$cf13_score', 
		cf13_isseen='$cf13_isseen', 
		sc13_cf_id='$sc13_cf_id', 
		cf13_pdp='$cf13_pdp', 
		cf13_redflag='$cf13_redflag',
		cf14_score='$cf14_score', 
		cf14_isseen='$cf14_isseen', 
		sc14_cf_id='$sc14_cf_id', 
		cf14_pdp='$cf14_pdp', 
		cf14_redflag='$cf14_redflag',
		cf15_score='$cf15_score', 
		cf15_isseen='$cf15_isseen', 
		sc15_cf_id='$sc15_cf_id', 
		cf15_pdp='$cf15_pdp', 
		cf15_redflag='$cf15_redflag',
		cf16_score='$cf16_score', 
		cf16_isseen='$cf16_isseen', 
		sc16_cf_id='$sc16_cf_id', 
		cf16_pdp='$cf16_pdp', 
		cf16_redflag='$cf16_redflag',
		cf17_score='$cf17_score', 
		cf17_isseen='$cf17_isseen', 
		sc17_cf_id='$sc17_cf_id', 
		cf17_pdp='$cf17_pdp', 
		cf17_redflag='$cf17_redflag',
		cf18_score='$cf18_score', 
		cf18_isseen='$cf18_isseen', 
		sc18_cf_id='$sc18_cf_id', 
		cf18_pdp='$cf18_pdp', 
		cf18_redflag='$cf18_redflag',
		cf19_score='$cf19_score', 
		cf19_isseen='$cf19_isseen', 
		sc19_cf_id='$sc19_cf_id', 
		cf19_pdp='$cf19_pdp', 
		cf19_redflag='$cf19_redflag',
		cf20_score='$cf20_score', 
		cf20_isseen='$cf20_isseen', 
		sc20_cf_id='$sc20_cf_id', 
		cf20_pdp='$cf20_pdp', 
		cf20_redflag='$cf20_redflag',
		cfcomments='$cfcomments',
		redflagnotify='$redflagnotify'
		where id='$id'");

which are called with

Code:
$result = $dbStuff->insertCF('',$_POST['1score'],$_POST['1isseen'],$_POST['1scenario'],$_POST['1pdp'],$_POST['1redflag'],$_POST['2score'],$_POST['2isseen'],$_POST['2scenario'],$_POST['2pdp'],$_POST['2redflag'],$_POST['3score'],$_POST['3isseen'],$_POST['3scenario'],$_POST['3pdp'],$_POST['3redflag'],$_POST['4score'],$_POST['4isseen'],$_POST['4scenario'],$_POST['4pdp'],$_POST['4redflag'],$_POST['5score'],$_POST['5isseen'],$_POST['5scenario'],$_POST['5pdp'],$_POST['5redflag'],$_POST['6score'],$_POST['6isseen'],$_POST['6scenario'],$_POST['6pdp'],$_POST['6redflag'],$_POST['7score'],$_POST['7isseen'],$_POST['7scenario'],$_POST['7pdp'],$_POST['7redflag'],$_POST['8score'],$_POST['8isseen'],$_POST['8scenario'],$_POST['8pdp'],$_POST['8redflag'],$_POST['9score'],$_POST['9isseen'],$_POST['9scenario'],$_POST['9pdp'],$_POST['9redflag'],$_POST['10score'],$_POST['10isseen'],$_POST['10scenario'],$_POST['10pdp'],$_POST['10redflag'],$_POST['11score'],$_POST['11isseen'],$_POST['11scenario'],$_POST['11pdp'],$_POST['11redflag'],$_POST['12score'],$_POST['12isseen'],$_POST['12scenario'],$_POST['12pdp'],$_POST['12redflag'],$_POST['13score'],$_POST['13isseen'],$_POST['13scenario'],$_POST['13pdp'],$_POST['13redflag'],$_POST['14score'],$_POST['14isseen'],$_POST['14scenario'],$_POST['14pdp'],$_POST['14redflag'],$_POST['15score'],$_POST['15isseen'],$_POST['15scenario'],$_POST['15pdp'],$_POST['15redflag'],$_POST['16score'],$_POST['16isseen'],$_POST['16scenario'],$_POST['16pdp'],$_POST['16redflag'],$_POST['17score'],$_POST['17isseen'],$_POST['17scenario'],$_POST['17pdp'],$_POST['17redflag'],$_POST['18score'],$_POST['18isseen'],$_POST['18scenario'],$_POST['18pdp'],$_POST['18redflag'],$_POST['19score'],$_POST['19isseen'],$_POST['19scenario'],$_POST['19pdp'],$_POST['19redflag'],$_POST['20score'],$_POST['20isseen'],$_POST['20scenario'],$_POST['20pdp'],$_POST['20redflag'],$_POST['comments'],$_POST['redflagnotify']);

$result = $dbStuff->updateCF($_POST['1score'], $_POST['1isseen'], $_POST['1scenario'], $_POST['1pdp'], $_POST['1redflag'],$_POST['2score'], $_POST['2isseen'], $_POST['2scenario'], $_POST['2pdp'], $_POST['2redflag'],$_POST['3score'], $_POST['3isseen'], $_POST['3scenario'], $_POST['3pdp'], $_POST['3redflag'],$_POST['4score'], $_POST['4isseen'], $_POST['4scenario'], $_POST['4pdp'], $_POST['4redflag'],$_POST['5score'], $_POST['5isseen'], $_POST['5scenario'], $_POST['5pdp'], $_POST['5redflag'],$_POST['6score'], $_POST['6isseen'], $_POST['6scenario'], $_POST['6pdp'], $_POST['6redflag'],$_POST['7score'], $_POST['7isseen'], $_POST['7scenario'], $_POST['7pdp'], $_POST['7redflag'],$_POST['8score'], $_POST['8isseen'], $_POST['8scenario'], $_POST['8pdp'], $_POST['8redflag'],$_POST['9score'], $_POST['9isseen'], $_POST['9scenario'], $_POST['9pdp'], $_POST['9redflag'],$_POST['10score'], $_POST['10isseen'], $_POST['10scenario'], $_POST['10pdp'], $_POST['10redflag'], $_POST['11score'], $_POST['11isseen'], $_POST['11scenario'], $_POST['11pdp'], $_POST['11redflag'],$_POST['12score'], $_POST['12isseen'], $_POST['12scenario'], $_POST['12pdp'], $_POST['12redflag'],$_POST['13score'], $_POST['13isseen'], $_POST['13scenario'], $_POST['13pdp'], $_POST['13redflag'],$_POST['14score'], $_POST['14isseen'], $_POST['14scenario'], $_POST['14pdp'], $_POST['14redflag'],$_POST['15score'], $_POST['15isseen'], $_POST['15scenario'], $_POST['15pdp'], $_POST['15redflag'],$_POST['16score'], $_POST['16isseen'], $_POST['16scenario'], $_POST['16pdp'], $_POST['16redflag'],$_POST['17score'], $_POST['17isseen'], $_POST['17scenario'], $_POST['17pdp'], $_POST['17redflag'],$_POST['18score'], $_POST['18isseen'], $_POST['18scenario'], $_POST['18pdp'], $_POST['18redflag'],$_POST['19score'], $_POST['19isseen'], $_POST['19scenario'], $_POST['19pdp'], $_POST['19redflag'],$_POST['20score'], $_POST['20isseen'], $_POST['20scenario'], $_POST['20pdp'], $_POST['20redflag'], $_POST['comments'],$_POST['redflagnotify'],$_POST['id']);
 
yup. there's a much easier way. check back in ten minutes and i'll post it.
 
Hi

Personally I would prefer something like this :
PHP:
[teal]<?php[/teal]
[gray]// generic preprocessing[/gray]
[navy]$data[/navy][teal]=[/teal][b]array[/b][teal]();[/teal]
[b]for[/b] [teal]([/teal][navy]$i[/navy][teal]=[/teal][purple]1[/purple][teal];[/teal][navy]$i[/navy][teal]<=[/teal][purple]20[/purple][teal];[/teal][navy]$i[/navy][teal]++)[/teal] [teal]{[/teal]
  [navy]$data[/navy][teal][[/teal][green][i]"cf${i}_score"[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]"${i}score"[/i][/green][teal]];[/teal]
  [navy]$data[/navy][teal][[/teal][green][i]"cf${i}_isseen"[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]"${i}isseen"[/i][/green][teal]];[/teal]
  [navy]$data[/navy][teal][[/teal][green][i]"sc${i}_cf_id"[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]"${i}scenario"[/i][/green][teal]];[/teal]
  [navy]$data[/navy][teal][[/teal][green][i]"cf${i}_pdp"[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]"${i}pdp"[/i][/green][teal]];[/teal]
  [navy]$data[/navy][teal][[/teal][green][i]"cf${i}_redflag"[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]"${i}redflag"[/i][/green][teal]];[/teal]
[teal]}[/teal]
[navy]$data[/navy][teal][[/teal][green][i]'cfcomments'[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]'comments'[/i][/green][teal]];[/teal]
[navy]$data[/navy][teal][[/teal][green][i]'redflagnotify'[/i][/green][teal]]=[/teal][navy]$_POST[/navy][teal][[/teal][green][i]'redflagnotify'[/i][/green][teal]];[/teal]

[gray]// escaping[/gray]
[b]foreach[/b] [teal]([/teal][navy]$data[/navy] [b]as[/b] [navy]$field[/navy][teal]=>[/teal][navy]$value[/navy][teal])[/teal]
  [navy]$data[/navy][teal][[/teal][navy]$field[/navy][teal]]=[/teal][green][i]"'"[/i][/green][teal].[/teal][COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$value[/navy][teal]).[/teal][green][i]"'"[/i][/green][teal];[/teal]

[gray]// insert[/gray]
[navy]$result[/navy] [teal]=[/teal] [navy]$this[/navy][teal]->[/teal][COLOR=darkgoldenrod]Query[/color][teal]([/teal][green][i]"insert into mytable values ('',"[/i][/green][teal].[/teal][COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]','[/i][/green][teal],[/teal][COLOR=darkgoldenrod]array_values[/color][teal]([/teal][navy]$data[/navy][teal])).[/teal][green][i]')'[/i][/green][teal]);[/teal]

[gray]// update[/gray]
[navy]$pair[/navy][teal]=[/teal][b]array[/b][teal]();[/teal]
[b]foreach[/b] [teal]([/teal][navy]$data[/navy] [b]as[/b] [navy]$field[/navy][teal]=>[/teal][navy]$value[/navy][teal])[/teal]
  [navy]$pair[/navy][teal][]=[/teal][green][i]"$field=$value"[/i][/green][teal];[/teal]

[navy]$result[/navy] [teal]=[/teal] [navy]$this[/navy][teal]->[/teal][COLOR=darkgoldenrod]Query[/color][teal]([/teal][green][i]'update mytable set '[/i][/green][teal].[/teal][COLOR=darkgoldenrod]implode[/color][teal]([/teal][green][i]','[/i][/green][teal],[/teal][navy]$pair[/navy][teal]).[/teal][green][i]" where id='"[/i][/green][teal].[/teal][COLOR=darkgoldenrod]mysql_real_escape_string[/color][teal]([/teal][navy]$_POST[/navy][teal][[/teal][green][i]'id'[/i][/green][teal]]).[/teal][green][i]"'"[/i][/green][teal]);[/teal]
[small][maroon]Warning[/maroon] The above code was not tested[/small]

Feherke.
 
this is the code

note that the base class is generic. you can easily add in other methods to it and reuse it across other objects.

Code:
[COLOR=#990000 ]<?php[/color] 
[b][COLOR=#0000FF ]class[/color][/b] base [COLOR=#FF0000 ]{[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]__construct[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]foreach[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]fields [b][COLOR=#0000FF ]as[/color][/b] [COLOR=#009900 ]$field[/color][COLOR=#990000 ]):[/color]
[tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ](![/color][b][COLOR=#0000FF ]isset[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]))[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color] [COLOR=#990000 ]=[/color] NULL[COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]endforeach[/color][/b][COLOR=#990000 ];[/color]
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]getPrimary[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#990000 ]![/color][b][COLOR=#0000FF ]empty[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]primary[COLOR=#FF0000 ]}[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]?[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]primary[COLOR=#FF0000 ]}[/color] [COLOR=#990000 ]:[/color] false[COLOR=#990000 ];[/color]
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]save[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]if[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]getPrimary[/color][/b][COLOR=#990000 ]()[/color] [COLOR=#990000 ]===[/color] false[COLOR=#990000 ]):[/color] 
[tab][tab][tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]insert[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][b][COLOR=#0000FF ]else[/color][/b][COLOR=#990000 ]:[/color]
[tab][tab][tab][COLOR=#009900 ]$return[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]update[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#009900 ]$return[/color][COLOR=#990000 ];[/color]
[tab][tab]
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]insert[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]global[/color][/b] [COLOR=#009900 ]$dbObj[/color][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#009900 ]$params[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][COLOR=#009900 ]$sql[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"INSERT into {$this->table} SET "[/color][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]foreach[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]fields [b][COLOR=#0000FF ]as[/color][/b] [COLOR=#009900 ]$key[/color][COLOR=#990000 ]=>[/color][COLOR=#009900 ]$field[/color][COLOR=#990000 ]):[/color]
[tab][tab][tab][COLOR=#009900 ]$fields[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"`$field` = '%s'"[/color][COLOR=#990000 ];[/color]
[tab][tab][tab][COLOR=#009900 ]$params[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]is_array[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]?[/color] [b][COLOR=#000000 ]mysql_real_escape_string[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]serialize[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]))[/color] [COLOR=#990000 ]:[/color] [b][COLOR=#000000 ]mysql_real_escape_string[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]);[/color] 
[tab][tab][b][COLOR=#0000FF ]endforeach[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#009900 ]$query[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$sql[/color] [COLOR=#990000 ].[/color] [b][COLOR=#000000 ]implode[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]','[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$fields[/color][COLOR=#990000 ]);[/color]
[tab][tab][b][COLOR=#000000 ]mysql_last_insert_id[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab]
[tab][tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#009900 ]$dbObj[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]query[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]vsprintf[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$query[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$params[/color][COLOR=#990000 ]));[/color]
[tab][tab][i][COLOR=#9A1900 ]/* alternative code [/color][/i]
[i][COLOR=#9A1900 ][tab][tab]mysql_query(vsprintf($query, $params)) or die(mysql_error());[/color][/i]
[i][COLOR=#9A1900 ][tab][tab]return mysql_insert_id();[/color][/i]
[i][COLOR=#9A1900 ][tab][tab]*/[/color][/i]
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]update[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]global[/color][/b] [COLOR=#009900 ]$dbObj[/color][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#009900 ]$params[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][COLOR=#009900 ]$sql[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"UPDATE {$this->table} SET "[/color][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]foreach[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]fields [b][COLOR=#0000FF ]as[/color][/b] [COLOR=#009900 ]$key[/color][COLOR=#990000 ]=>[/color][COLOR=#009900 ]$field[/color][COLOR=#990000 ]):[/color]
[tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$field[/color] [COLOR=#990000 ]===[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]primary[COLOR=#990000 ])[/color] [b][COLOR=#0000FF ]continue[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][tab][COLOR=#009900 ]$fields[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]"`$field` = '%s'"[/color][COLOR=#990000 ];[/color]
[tab][tab][tab][COLOR=#009900 ]$params[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [b][COLOR=#000000 ]is_array[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]?[/color] [b][COLOR=#000000 ]mysql_real_escape_string[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]serialize[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]))[/color] [COLOR=#990000 ]:[/color] [b][COLOR=#000000 ]mysql_real_escape_string[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]);[/color] 
[tab][tab][b][COLOR=#0000FF ]endforeach[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#009900 ]$query[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$sql[/color] [COLOR=#990000 ].[/color] [b][COLOR=#000000 ]implode[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]','[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$fields[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ].[/color] [COLOR=#FF0000 ]" WHERE {$this->primary} = '%s'"[/color][COLOR=#990000 ];[/color]
[tab][tab][COLOR=#009900 ]$params[/color][COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]getPrimary[/color][/b][COLOR=#990000 ]();[/color]
[tab][tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#009900 ]$dbObj[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]Query[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]vsprintf[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$query[/color][COLOR=#990000 ],[/color] [COLOR=#009900 ]$params[/color][COLOR=#990000 ]));[/color]
[tab][tab][i][COLOR=#9A1900 ]/* alternative code[/color][/i]
[i][COLOR=#9A1900 ][tab][tab]mysql_query(vsprintf($query, $params)) or die (mysql_error());[/color][/i]
[i][COLOR=#9A1900 ][tab][tab]return mysql_affected_rows();[/color][/i]
[i][COLOR=#9A1900 ][tab][tab]*/[/color][/i]
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]load[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#000000 ]is_array[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]))[/color] [COLOR=#009900 ]$row[/color] [COLOR=#990000 ]=[/color] [COLOR=#990000 ]([/color]object[COLOR=#990000 ])[/color] [COLOR=#009900 ]$row[/color][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]foreach[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]fields [b][COLOR=#0000FF ]as[/color][/b] [COLOR=#009900 ]$field[/color][COLOR=#990000 ]):[/color]
[tab][tab][tab][b][COLOR=#0000FF ]if[/color][/b][COLOR=#990000 ]([/color][b][COLOR=#0000FF ]isset[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])):[/color]
[tab][tab][tab][tab][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]is_serialized[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]?[/color] [b][COLOR=#000000 ]unserialize[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]:[/color] [b][COLOR=#000000 ]trim[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$row[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color][COLOR=#990000 ]);[/color]
[tab][tab][tab][b][COLOR=#0000FF ]else[/color][/b][COLOR=#990000 ]:[/color]
[tab][tab][tab][tab][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color][COLOR=#FF0000 ]{[/color][COLOR=#009900 ]$field[/color][COLOR=#FF0000 ]}[/color] [COLOR=#990000 ]=[/color] null[COLOR=#990000 ];[/color]
[tab][tab][tab][b][COLOR=#0000FF ]endif[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]endforeach[/color][/b][COLOR=#990000 ];[/color] 
[tab][COLOR=#FF0000 ]}[/color]
[tab]
[tab]private [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]isSerialized[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$string[/color][COLOR=#990000 ])[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][b][COLOR=#0000FF ]return[/color][/b] [COLOR=#990000 ]([/color][COLOR=#009900 ]@unserialize[/color][COLOR=#990000 ]([/color][COLOR=#009900 ]$string[/color][COLOR=#990000 ])[/color] [COLOR=#990000 ]!==[/color] FALSE[COLOR=#990000 ]);[/color]
[tab][COLOR=#FF0000 ]}[/color]
[COLOR=#FF0000 ]}[/color]

[b][COLOR=#0000FF ]class[/color][/b] cf [b][COLOR=#0000FF ]extends[/color][/b] base [COLOR=#FF0000 ]{[/color]
[tab]public [COLOR=#009900 ]$fields[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'id'[/color][COLOR=#990000 ],[/color][COLOR=#FF0000 ]'cfcomments'[/color][COLOR=#990000 ],[/color][COLOR=#FF0000 ]'redflagnotify'[/color] [COLOR=#990000 ]);[/color]
[tab]public [COLOR=#009900 ]$primary[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]'id'[/color][COLOR=#990000 ];[/color]
[tab]public [COLOR=#009900 ]$table[/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]'mytable'[/color][COLOR=#990000 ];[/color]
[tab]
[tab]public [b][COLOR=#0000FF ]function[/color][/b] [b][COLOR=#000000 ]__construct[/color][/b][COLOR=#990000 ]()[/color][COLOR=#FF0000 ]{[/color]
[tab][tab][COLOR=#009900 ]$a[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]array[/color][/b][COLOR=#990000 ]([/color][COLOR=#FF0000 ]'score'[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'isseen'[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'cf_id'[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'pdp'[/color][COLOR=#990000 ],[/color] [COLOR=#FF0000 ]'redflag'[/color][COLOR=#990000 ]);[/color]
[tab][tab][b][COLOR=#0000FF ]for[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$i[/color][COLOR=#990000 ]=[/color][COLOR=#993399 ]1[/color][COLOR=#990000 ];[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]<=[/color][COLOR=#993399 ]20[/color][COLOR=#990000 ];[/color] [COLOR=#009900 ]$i[/color][COLOR=#990000 ]++):[/color]
[tab][tab][tab][b][COLOR=#0000FF ]foreach[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$a[/color] [b][COLOR=#0000FF ]as[/color][/b] [COLOR=#009900 ]$item[/color][COLOR=#990000 ]):[/color]
[tab][tab][tab][tab][COLOR=#009900 ]$this[/color][COLOR=#990000 ]->[/color]fields[COLOR=#990000 ][][/color] [COLOR=#990000 ]=[/color] [COLOR=#FF0000 ]'cf'[/color] [COLOR=#990000 ].[/color] [COLOR=#009900 ]$i[/color] [COLOR=#990000 ].[/color][COLOR=#FF0000 ]'_'[/color] [COLOR=#990000 ].[/color] [COLOR=#009900 ]$item[/color][COLOR=#990000 ];[/color]
[tab][tab][tab][b][COLOR=#0000FF ]endforeach[/color][/b][COLOR=#990000 ];[/color]
[tab][tab][b][COLOR=#0000FF ]endfor[/color][/b][COLOR=#990000 ];[/color]
[tab][tab]parent[COLOR=#990000 ]::[/color][b][COLOR=#000000 ]__construct[/color][/b][COLOR=#990000 ]();[/color]
[tab][COLOR=#FF0000 ]}[/color]
[COLOR=#FF0000 ]}[/color]
[COLOR=#990000 ]?>[/color]

use it like this
Code:
[i][COLOR=#9A1900 ]//to insert OR update[/color][/i]
[COLOR=#009900 ]$cf[/color] [COLOR=#990000 ]=[/color] [b][COLOR=#0000FF ]new[/color][/b] CF[COLOR=#990000 ];[/color]
[COLOR=#009900 ]$cf[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]load[/color][/b][COLOR=#990000 ]([/color][COLOR=#009900 ]$_POST[/color][COLOR=#990000 ]);[/color] [i][COLOR=#9A1900 ]//this is safe because the object will only take on board its own fields[/color][/i]
[COLOR=#009900 ]$result[/color] [COLOR=#990000 ]=[/color] [COLOR=#009900 ]$cf[/color][COLOR=#990000 ]->[/color][b][COLOR=#000000 ]save[/color][/b][COLOR=#990000 ]();[/color] [i][COLOR=#9A1900 ]//the code determines whether to update or insert[/color][/i]

note that this assumes you have a database class handling the querying and that the dbclass object is called $dbObj. the above can be easily rewritten to avoid this. the alternative code is in the base class already, just commented out.

Note also that this performs no checks on your values to ensure that they are valid before database insertion.

and ... having that many repetitive columns is suggestive of poor database normalisation.

and lastly ... the cf class could easily be extended to provide a nice loop to create the html forms for you on the fly.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top