M.O.S. Studios Posted August 31, 2009 Share Posted August 31, 2009 Hey, I'm working on a code that will take information from a db and turn it into the constant [menu] and [men2]. here is what i have so far, im starting to think that useing a class system might be more effective. <?php //PRE LOADED //***************** function mysql_fullarray($resource) { while($array=mysql_fetch_assoc($resource)) { $return_array[]=$array; } return $return_array; } *****************// function replacesym($item, $links, $name, $global, $catnum, $itemnum=NULL) { $item = str_replace("[#]",$global,$item); $item = str_replace("[cat#]",$catnum,$item); $item = str_replace("[item#]",$itemnum,$item); $item = str_replace("[url]",$links,$item); $item = str_replace("[name]",$name,$item); $item = str_replace("[nl]","\n",$item); return $item; } function additems($index, $catnum, $global, $item) { $itemnum=0; $headlist=mysql_query("SELECT * FROM `headers` WHERE `act` = '1' AND `header` = '".$index."' ORDER BY `sort` DESC "); while(list($number, $name, $internal, ,$exlink)= mysql_fetch_array($headlist)) { $global++; if($internal==1){ $links="index.php?page=custom&file=".$number;}else{$links=$exlink;} $menitem .=replacesym($item, $links, $name, $global, $catnum, $itemnum++); } return $menitem; } function replacemenu($number, $name, $internal, $inlink, $exlink, $catnum, $item) { $global=$GLOBALS['global']; if($item==1){$final=MENU_CATBEG;$end=MENU_CATEND;} if($item==2){$final=MENU_CATBEG2;$end=MENU_CATEND2;} if($item==1){ if($name=='STOREMENU'){$name=MENU_TITLE;} if(CATAGORIES==1){ if($internal==1){ $links="index.php?page=custom&file=".$number;}else{$links=$exlink;} $final=$final.MENU_CATITEM; } if(PRODUCTS==1){ $final .= additems($number,$catnum, $global, MENU_PROD); } } if($item==2){ if($name=='STOREMENU'){$name=MENU_TITLE2;} if(CATAGORIES2==1){ if($internal==1) {$links="index.php?page=custom&file=".$number;}else{$links=$exlink;} $final .=MENU_CATITEM2; } if(PRODUCTS2==1){ $final .= additems($number,$catnum, $global, MENU_PROD2); } } $final =replacesym($final.$end, $links, $name, $global, $catnum, $itemnum++); return $final; } session_start(); //pulling header info $headlist=mysql_query("SELECT * FROM `headers` WHERE `act` = '1' AND `header` = '0' ORDER BY `sort` ASC"); $headsort=mysql_fullarray($headlist); $global=-1; foreach($headsort as $catnum => $catvalue) { $global++; $menus .=replacemenu($catvalue['index'], $catvalue['name'], $catvalue['internal'], $catvalue['inlink'], $catvalue['exlink'], $catnum, 1); $menus2 .=replacemenu($catvalue['index'], $catvalue['name'], $catvalue['internal'], $catvalue['inlink'], $catvalue['exlink'], $catnum, 2); $newglobal=mysql_query("SELECT count(*) FROM `headers` WHERE `act` = '1' AND `header` = '".$catvalue['index']."' ORDER BY `sort` DESC "); list($newglobal) = mysql_fetch_array($newglobal); } cdefine(MENUS,$menus,TRUE); cdefine(MENUS2,$menus2,TRUE); ?> Quote Link to comment https://forums.phpfreaks.com/topic/172608-solved-not-sure-how-to-make-this-less-complicated/ Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 you COULD make this a class if you wanted to, and would probably be a good idea, but does the script as it stands work? it doesn't seem too complicated to me Quote Link to comment https://forums.phpfreaks.com/topic/172608-solved-not-sure-how-to-make-this-less-complicated/#findComment-909864 Share on other sites More sharing options...
M.O.S. Studios Posted August 31, 2009 Author Share Posted August 31, 2009 as it stands it works, but i want to make this as light as possible. I also dont understand the class system entirly.... I was wondering if I could make it smaller Quote Link to comment https://forums.phpfreaks.com/topic/172608-solved-not-sure-how-to-make-this-less-complicated/#findComment-909867 Share on other sites More sharing options...
mikesta707 Posted August 31, 2009 Share Posted August 31, 2009 The cool thing about classes is data encapsulation, so if you make a class var, named, say, $name, that var is accessible in every member function of the class. so for example class foo { private $word="Hello World!"; function bar(){ echo $this->word; } } $foo = new foo(); $foo->bar(); //output Hello World! This does a few things. Namely, it allows you to access variables in functions without having to pass them. so lets take one of your functions, and see how it would be in a class class foo{ private $item, $links, $name, $global, $catnum; //lets assume these are set before we call the function function replacesym($itemnum=NULL) { $item = str_replace("[#]",$this->global,$this->item); $item = str_replace("[cat#]",$this->catnum,$this->item); $item = str_replace("[item#]",$itemnum,$this->item); $item = str_replace("[url]",$this->links,$this->item); $item = str_replace("[name]",$this->name,$this->item); $item = str_replace("[nl]","\n",$this->item); // return $item; we could do this, or we could set the class variable like so $this->item =$item; } } Since the data is already set, and is already inside the class, that function only really needs one parameter, as opposed to 4-6 which could make it kind of clunky. This explanation is very basic though, and there is a lot more to classes than just this. Hope this helps Quote Link to comment https://forums.phpfreaks.com/topic/172608-solved-not-sure-how-to-make-this-less-complicated/#findComment-909869 Share on other sites More sharing options...
M.O.S. Studios Posted August 31, 2009 Author Share Posted August 31, 2009 Awesome, That seems like it will work for me Quote Link to comment https://forums.phpfreaks.com/topic/172608-solved-not-sure-how-to-make-this-less-complicated/#findComment-909875 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.