Jump to content

Recommended Posts

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);
?>

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.