ricky spires Posted March 7, 2012 Share Posted March 7, 2012 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 Quote Link to comment https://forums.phpfreaks.com/topic/258484-how-do-i-pass-a-foreach-loop-through-passing-by-reference/ Share on other sites More sharing options...
scootstah Posted March 7, 2012 Share Posted March 7, 2012 You could append the words to $output. foreach ($word as $words){ $output .= $words->text; } Quote Link to comment https://forums.phpfreaks.com/topic/258484-how-do-i-pass-a-foreach-loop-through-passing-by-reference/#findComment-1324989 Share on other sites More sharing options...
ricky spires Posted March 8, 2012 Author Share Posted March 8, 2012 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 ? Quote Link to comment https://forums.phpfreaks.com/topic/258484-how-do-i-pass-a-foreach-loop-through-passing-by-reference/#findComment-1325351 Share on other sites More sharing options...
scootstah Posted March 9, 2012 Share Posted March 9, 2012 I think you should check out this article on storing hierarchical data. Quote Link to comment https://forums.phpfreaks.com/topic/258484-how-do-i-pass-a-foreach-loop-through-passing-by-reference/#findComment-1325414 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.