Jump to content

Recommended Posts

hello.

 

im having a little trouble with a function

 

say i have a function that gets the nav title and header. how do i echo the results out on the page

ok, this is an example:

 

function nav($title, $link){ 

    $title = "this is the title";
    $link = "this is the link";
}

 

this is the page:

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

//FIND THE FUNCTION
<?PHP 
nav($title, $link); 
?>

<h1><?php echo $title; ?></h1>
<ul>
<li><?php echo $link; ?></li>
</ul>;

 

THE ABOVE DOES NOT WORK

 

 

i also tried this but it does not work .

 

function nav($title, $link){ 

    $title = "this is the title";
    $link = "this is the link";

return $title;
return $link;
}

 

THANKS

Link to comment
https://forums.phpfreaks.com/topic/258387-how-do-i-get-values-from-a-function/
Share on other sites

If you _have_ to do everything in one function, then just use pass by reference.

 

http://php.net/manual/en/language.references.pass.php

 

Eh, using reference side effects in lieu of actual return values is kinda awkward in this context.  It's easier to return an array:

 

function nav($title, $link) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

If you _have_ to do everything in one function, then just use pass by reference.

 

http://php.net/manual/en/language.references.pass.php

 

Eh, using reference side effects in lieu of actual return values is kinda awkward in this context.  It's easier to return an array:

 

function nav($title, $link) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

What side-effects would those be?  I come from the C/C++ and Java world, so I'm aware of their limitations.

Yeah, I used the wrong term.

 

Generally speaking, using an array, like I did, as opposed to using references is the preferred idiom in PHP.  References appear so rarely in normal use, it's just convention to return multiple values via an array.

To be honest, I'd still use pass-by-reference.  The reason being is that this is usually how things are done in other languages, especially if they share a syntax similar to PHP (C/C++, Java, C#, etc.) and will make it easier to understand the flow of the code for others, most notably if those individuals have a background in those languages.

 

Returning an array is just... weird when you take into account the points of view of more developers.

 

[edit]

 

And if you ever need to, it's easier to 'translate' the code to other languages, since making such arrays on the fly is not as easy in other languages.

hello,

 

how do i echo this out on the page?

 

this is in function.php

function nav($title, $link) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

 

i tried this but i didn't work..

 

first i tried changing the function to - function nav($ret); like this:

function nav($ret) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

 

then on the page that i want to display the code i tried:

 

nav($ret);
   echo $ret;

 

then i tried

 

$a = nav($ret);
foreach ($a as $b){
   echo $b->title;
   echo $b->link;
}

 

then i tried

 

nav($ret);
print_r($ret);

 

 

so anyway..  i don't know ???

 

 

 

hello,

 

how do i echo this out on the page?

 

this is in function.php

function nav($title, $link) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

 

i tried this but i didn't work..

"didn't work"?  What happened?  Error message?

first i tried changing the function to - function nav($ret); like this:

function nav($ret) { 
   $ret = array();

   $ret['title'] = "this is the title";
   $ret['link'] = "this is the link";

   return $ret;
}

 

then on the page that i want to display the code i tried:

 

nav($ret);
   echo $ret;

You're returning an array, to print that out in php, do print_r($ret);

then i tried

 

$a = nav($ret);
foreach ($a as $b){
   echo $b->title;
   echo $b->link;
}

 

then i tried

 

nav($ret);
print_r($ret);

 

 

so anyway..  i don't know ???

What happened exactly?  Can you provide an error message?

the thing is .. i get nothing..

 

i can echo

 

echo nav($ret);

 

and that gives me "Array"

 

 

also...

 

i tried doing some "Passing by Reference"

i went to http://php.net/manual/en/language.references.pass.php

then copied the code below and saved it as test.php but it also returned nothing ???

why is it not echoing out any results ?

 

<?php
function foo(&$var)
{
    $var++;
}

$a=5;
foo($a);
// $a is 6 here
?> 

When you did nav($ret), did you store the output of that method in a variable (say $return_value) and then did print_r($return_value)?

 

Could you post all of your code?  You're giving bits and pieces and I'm having a hard time to visualize how stuff fits together.

 

I'll try to replicate your issues and will give you some more feedback.

Here is how you have an array returned with your two inputs:

<?php
$returner = abrakadabra("Hello!", "World!");

print_r($returner);

function abrakadabra($val1, $val2)
{
    $ret_array = array();

    $ret_array['val1'] = $val1;
    $ret_array['val2'] = $val2;

    return $ret_array;
}
?>

 

Here is how you pass stuff by reference:

<?php
$yummy = "Cake!";
$delicious = "Cookies!";

echo "Yummy: " . $yummy . "<br>";
echo "Delicious: " . $delicious . "<br><br>";

modifier_fun($yummy, $delicious);

echo "New Yummy: " . $yummy . "<br>";
echo "New Delicious: " . $delicious . "<br>";

function modifier_fun(&$yummy, &$delicious)
{
    $yummy = "Carrots!";
    $delicious = "Tilapia!";
}
?>

 

I hope this answers some of your questions and clears up the confusion :) .

hi all

 

i took "yoursurrogategod" post and jigged it and im getting what i need to get the job done. i think :)

 

 

its all a bit complex for me really but i think ill work it out..

 

see it you can rap your heads around this.

 

im pulling a dynamic navigation from the database.

 

im making it so that you can create any number of navigations in my admin panel.

the navigation list can be a page, an external link or have no link. they can also use the set page name or have a custom name

 

please remember im new to oop and a beginner in php :)

 

 

so this gets the list number and order

this part is in functions.php

	$PHL = PhLists::by_listNo($PHGlno); 
	foreach ($PHL as $PHLs){
	$PHLlno = $PHLs->phList_No;	
	$PHLorb = $PHLs->orderBy;
	$PHLord = $PHLs->orderDir;
	}

 

this is in the navigation.php file

this gets the navigation (list) details

 

$nH = PhLists::navOrder($PHLlno, $PHLorb, $PHLord);
foreach ($nH as $nHs){
$nav_id = $nHs->id;
$nav_typ = $nHs->type;
$nav_lvl = $nHs->levels;
$nav_pnt = $nHs->parent_id;
$nav_pLnk = $nHs->pageLink_id;
$nav_eLnk = $nHs->extLink_id;
$nav_tType = $nHs->titleType;
$nav_ctBid = $nHs->custTxtBridge_id;

 

 

now the next bit of code is why i wanted to use functions...

 

so if i have a this structure:

 

<h1>title</h1>

<ul>

<li>list</li>

    <li>nested sub title</li>

    <li>list</li>

</ul>

 

the header looks for only items with a parent id of 0

if($nav_pnt == "0"){

 

the other levels look for the id of the level above

if($nav_pnt == $nav_id){

 

 

so now it looks like this:

 

<?PHP require_once("includes/initialize.php"); ?>
<?PHP
//FIND PAGES 
$nH = PhLists::navOrder($PHLlno, $PHLorb, $PHLord);
foreach ($nH as $nHs){
$nav_id = $nHs->id;
$nav_typ = $nHs->type;
$nav_lvl = $nHs->levels;
$nav_pnt = $nHs->parent_id;
$nav_pLnk = $nHs->pageLink_id;
$nav_eLnk = $nHs->extLink_id;
$nav_tType = $nHs->titleType;
$nav_ctBid = $nHs->custTxtBridge_id;


echo'<div id="Vnav">
<div class="arrowlistmenu">';
  
if($nav_pnt == "0"){
	echo'
	<h3 class="menuheader expandable">'.$navLink.'</h3>';
}
echo'
		<ul class="categoryitems">';

			if($nav_pnt == $nav_id){
				echo'	
				<li><a href="">'.$navLink.'</a></li>';
			}
			if($nav_pnt == $nav_id){
			echo'
			<li><a href="" class="subexpandable">subTitle='.$navLink.'</a>';
			}
				if($nav_pnt == $nav_id){
				echo'
				<ul class="subcategoryitems" style="margin-left: 15px">';
					if($nav_pnt == $nav_id){
					echo'
					<li><a href="">subLink='.$navLink.'</a></li>';
					}
				echo'
				</ul>';
				}
			echo'
			</li>';
		echo'
		</ul>';
echo'</div></div>';
}
?>

 

 

so is where its getting complicated.

 

first i need to get the title:

 

$langBS = basicSettings::find_by_id(1);
$langID = $langBS->language_id; 

 //INTERNAL LINKS
if($nav_typ == "Internal"){

	//USE SET TITLE
	if($nav_tType == "SetTitle"){

		$navPage = Pages::findNav($nav_pLnk);	
		foreach ($navPage as $navPages){
		$NPbid = $navPages->PCbridge_id;
		}
		$STBhead = PCbridge::find_setTitle($NPbid, $langID);
		foreach ($STBhead as $STBheads){
		$text = $STBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navTexts->title;

		}


	//USE CUSTOM TITLE
	}else if($nav_tType == "Custom"){

		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
}

//EXTERNAL LINKS
if($nav_typ == "External"){

	//USE SET TITLE
	if($nav_tType == "SetTitle"){

		$navExt = ExtLinks::findNav($nav_eLnk);	
		foreach ($navExt as $navExts){
		$navLink = $navExts->name;
		$PHGurl = $navExts->url;
		$PHGtarget = $navExts->target;
		echo $navLink;
		}


	//USE CUSTOM TITLE
	}else if($nav_tType == "Custom"){

		$navExt = ExtLinks::findNav($nav_eLnk);	
		foreach ($navExt as $navExts){
		$PHGurl = $navExts->url;
		$PHGtarget = $navExts->target;
		}
		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
}


//NO LINK
if($nav_typ == "Non"){

	//USE CUSTOM TITLE
	if($nav_tType == "Custom"){

		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
} 
}

 

 

if i dont put that in to the function it looks like this

 

<?PHP require_once("includes/initialize.php"); ?>
<?PHP
//FIND PAGES 
$nH = PhLists::navOrder($PHLlno, $PHLorb, $PHLord);
foreach ($nH as $nHs){
$nav_id = $nHs->id;
$nav_typ = $nHs->type;
$nav_lvl = $nHs->levels;
$nav_pnt = $nHs->parent_id;
$nav_pLnk = $nHs->pageLink_id;
$nav_eLnk = $nHs->extLink_id;
$nav_tType = $nHs->titleType;
$nav_ctBid = $nHs->custTxtBridge_id;


 $langBS = basicSettings::find_by_id(1);
$langID = $langBS->language_id; 

 //INTERNAL LINKS
if($nav_typ == "Internal"){

	//USE SET TITLE
	if($nav_tType == "SetTitle"){

		$navPage = Pages::findNav($nav_pLnk);	
		foreach ($navPage as $navPages){
		$NPbid = $navPages->PCbridge_id;
		}
		$STBhead = PCbridge::find_setTitle($NPbid, $langID);
		foreach ($STBhead as $STBheads){
		$text = $STBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navTexts->title;

		}


	//USE CUSTOM TITLE
	}else if($nav_tType == "Custom"){

		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
}

//EXTERNAL LINKS
if($nav_typ == "External"){

	//USE SET TITLE
	if($nav_tType == "SetTitle"){

		$navExt = ExtLinks::findNav($nav_eLnk);	
		foreach ($navExt as $navExts){
		$navLink = $navExts->name;
		$PHGurl = $navExts->url;
		$PHGtarget = $navExts->target;
		echo $navLink;
		}


	//USE CUSTOM TITLE
	}else if($nav_tType == "Custom"){

		$navExt = ExtLinks::findNav($nav_eLnk);	
		foreach ($navExt as $navExts){
		$PHGurl = $navExts->url;
		$PHGtarget = $navExts->target;
		}
		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
}


//NO LINK
if($nav_typ == "Non"){

	//USE CUSTOM TITLE
	if($nav_tType == "Custom"){

		$CTBhead = PCbridge::find_customTitle($nav_ctBid, $langID);
		foreach ($CTBhead as $CTBheads){
		$text = $CTBheads->pageContent_id;
		}
		$navText = PageContent::find_text($text);  		
		foreach ($navText as $navTexts){
		$navLink = $navTexts->title;
		echo $navLink;
		}
	}
} 

echo'<div id="Vnav">
<div class="arrowlistmenu">';
  
if($nav_pnt == "0"){
	echo'
	<h3 class="menuheader expandable">'.$navLink.'</h3>';
}
echo'
		<ul class="categoryitems">';

			if($nav_pnt == $nav_id){
				echo'	
				<li><a href="">'.$navLink.'</a></li>';
			}
			if($nav_pnt == $nav_id){
			echo'
			<li><a href="" class="subexpandable">subTitle='.$navLink.'</a>';
			}
				if($nav_pnt == $nav_id){
				echo'
				<ul class="subcategoryitems" style="margin-left: 15px">';
					if($nav_pnt == $nav_id){
					echo'
					<li><a href="">subLink='.$navLink.'</a></li>';
					}
				echo'
				</ul>';
				}
			echo'
			</li>';
		echo'
		</ul>';
echo'</div></div>';
}
?>

 

 

as you can see, its getting huge.

 

 

 

 

that all gives me back

 

SUBLINK = View subLink2

 

LINK = View link2

 

LINK = View link2

 

SUB TITLE = View subTitle2

 

TITLE = View title2

TITLE = View title2

 

EXT LINK = go to Facebook

 

EXT LINK = twitter

 

EXT LINK = go to YouTube

 

EXT LINK = Go to DJs on Rotation

EXT LINK = Go to DJs on Rotation

 

 

which makes no sence to you im sure but well. its getting there just a bit messed up and the code looks way to big :)

That's one heck of a post and I'll need time to digest, but a quick glance at this won't give you a nested sub-title, at least not from an HTML point of view:

<h1>title</h1>
<ul>
<li>list</li>
     <li>nested sub title</li>
     <li>list</li>
</ul>

Look up a tutorial online about lists and nesting them.

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.