Jump to content

how do i pass a foreach loop through "Passing by Reference"


ricky spires

Recommended Posts

hello

 

how do i pass a foreach loop through "Passing by Reference"

 

say i have a foreach loop in a function like this:

 


function findText(&$output){
  $word = Text::find_all();  		
  foreach ($word as $words){
  $output = $words->text;
  }
}

 

then one the page i put

 


findText($output);
echo $output;

 

that will just give me the last word in the database.

 

so how do i get it to echo out the array on the page ?

 

thanks

 

 

no. that did not seem to work.

 

maybe if i show you what im actually trying to do.

 

im trying to get my navigation from a database but put most of the code in a function so i can use it for different navigation.

 

 

 

in my database i have:

there could be any number of these. i.e there could be 10 titles.

 

ID  -  TITLE      -      PARENT ID

1  -  title1          -      0

2  -  title2          -      0

3  -  link1          -      1

4  -  link2            -      2

5  -  subtitle1      -      1

6  -  subtitle2      -      2

7  -  sublink1      -      5

8  -  sublink2      -      6

 

so the structure would be

 

title1

- link1

  - subtitle1

  - sublink1

 

title2

- link2

  - subtitle2

  - sublink2

 

 

in my function i have.

im using "Passing by Reference" to pass the value to the page where its needed


function nav(&$nav_id, &$nav_Pnt, &$nav_txt){

  $nav = Nav::find_all();  		
  foreach ($nav as $navs){
  $nav_id .= $navs->id;
  $nav_pnt .= $navs->parent_id;
  $nav_txt .= $navs->title;
  }

}

 

 

 

so on the page i have this

 

 

 

<?PHP require_once("includes/initialize.php"); ?> //FIND FUNCTION
<?PHP

echo'
<div class="arrowlistmenu">';
    

//GET FUNCTION
nav($nav_id, $nav_Pnt, $nav_txt);


// I NEED TO GET ALL FROM DB HERE. 
// AT THE MOMENT I ONLY GET 1 1TITLE RETURNED


//IF PARENT ID IS "0" IT MUST BE A TITLE
if($nav_Pnt == "0"){
echo'
<h3 class="menuheader expandable">TITLE='.$nav_txt.'</h3>';
}

echo'
<ul cl/ass="categoryitems">';

        //LINKS
        //IF PARENT ID == ID OF THE ONE ABOVE IT MUST BE ITS CHILD
        if($nav_pnt == $nav_id){
echo'	
<li><a href="">LINK='.$nav_txt.'</a></li>';
}

        //SUBTITLE
        if($nav_pnt == $nav_id){
echo'
<li><a href="" class="subexpandable">SUBTITLE=</a>';
}

        if($nav_pnt == $nav_id){
        echo'
<ul class="subcategoryitems" style="margin-left: 15px">';

        if($nav_pnt == $nav_id){
        echo'
<li><a href="">SUBLINK=</a></li>';
}

       echo'
      </ul>';
}
echo'</li></ul></div>';
}
?>

 

 

 

 

any thoughts ?

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.