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

 

 

Link to comment
Share on other sites

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 ?

Link to comment
Share on other sites

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.