Jump to content

Recommended Posts

Okay, i'm a newb to PHP, so please bare with me....

 

I have made a function which will set a variable depending on other variables and database data.

 

I'm not sure whether in PHP this should even work, so if i'm TOTALLY wrong then just tell me..

 

okay, here's my original code that worked...

// Allow the front page to display 15 recent news entries
$NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15';

$Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error());
$Menu = mysql_fetch_array($Menu_Query);

$Cat = explode(",", $Menu['category1']);
if ($Cat[0] == "true") {
if ($Cat[2] == "true") {
	$fieldset_class = "menu gold";
} else {
	$fieldset_class = "menu";
}
$MENUS .= '
	<fieldset class="' . $fieldset_class . '">
		<legend>' . $Cat[1] . '</legend>
		<ul>
';
$link = explode("^", $Menu['links1']);
$url = explode("^", $Menu['urls1']);
for ($i = 0; $i < count($link); $i++) {
	if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") {
		continue;
	}
	$MENUS .= '
		<li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li>
	';
}
$MENUS .= '
	</ul>
	</fieldset>
';
}

 

Now here's when i tried to put it into a function, so i could use it later on.. (This doesn't work, it just doesn't work for some reason? :S)

// Allow the front page to display 15 recent news entries
$NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15';

$Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error());
$Menu = mysql_fetch_array($Menu_Query);

function NewCat($category, $links, $urls) {
$Cat = explode(",", $Menu[$category]);
if ($Cat[0] == "true") {
	if ($Cat[2] == "true") {
		$fieldset_class = "menu gold";
	} else {
		$fieldset_class = "menu";
	}
	$MENUS .= '
		<fieldset class="' . $fieldset_class . '">
			<legend>' . $Cat[1] . '</legend>
			<ul>
	';
	$link = explode("^", $Menu[$links]);
	$url = explode("^", $Menu[$urls]);
	for ($i = 0; $i < count($link); $i++) {
		if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") {
			continue;
		}
		$MENUS .= '
			<li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li>
		';
	}
	$MENUS .= '
		</ul>
		</fieldset>
	';
}
}

 

Just to be clear... the database queries that appear at the very top of the code DO WORK.

 

Anyone got any idea why my function don't work? Thanks alot! :)

Link to comment
https://forums.phpfreaks.com/topic/178411-solved-functions-something-isnt-right/
Share on other sites

Your function uses the $Menu variable from $Menu = mysql_fetch_array($Menu_Query);

But its hasn't been passed to the function,

also your function isn't being called or returning anything

 

here is a totally untested example (but may work)

<?php
// Allow the front page to display 15 recent news entries
$NEWS_QUERY = 'SELECT * FROM `Sites_News` WHERE `site`=\'' . $SITENAME . '\' ORDER BY `ID` DESC LIMIT 15';

$Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error());
$Menu = mysql_fetch_array($Menu_Query);
$MENUS= NewCat($Menu, $category, $links, $urls); //call function and set returned value to $MENUS


function NewCat($Menu, $category, $links, $urls) { //also passed $Menu
   $Cat = explode(",", $Menu[$category]);
   $MENUS = "";
   if ($Cat[0] == "true") {
      if ($Cat[2] == "true") {
         $fieldset_class = "menu gold";
      } else {
         $fieldset_class = "menu";
      }
      $MENUS .= '
         <fieldset class="' . $fieldset_class . '">
            <legend>' . $Cat[1] . '</legend>
            <ul>
      ';
      $link = explode("^", $Menu[$links]);
      $url = explode("^", $Menu[$urls]);
      for ($i = 0; $i < count($link); $i++) {
         if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") {
            continue;
         }
         $MENUS .= '
            <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li>
         ';
      }
      $MENUS .= '
         </ul>
         </fieldset>
      ';
   }
   return $MENUS; //return $MENUS
}
?>

 

EDIT: added some comments

Oh, i understand now.. I'm just so used to Java, so i'm guessing PHP functions are equivelant to String methods in Java, you have to return a value.

 

So to call that function, i would do:

$MENUS .= NewCat('categories1', 'links1', 'urls1');

 

(Since the variable $MENUS is in the main file which is why i was trying to use it, because i was thinking about it as if it were the same as a void in Java.

 

So that's how i call the function, if i understand correctly?

 

EDIT: Thanks alot, i now understand. Thanks buddy :)

Still seems to not work?

(I changed the variable which the function returns to not confuse myself, but it doesn't work still?)

 

<?php
$Menu_Query = mysql_query("SELECT * FROM `Sites_Menus` WHERE `site`='" . $SITENAME . "'") or die(mysql_error());
$Menu = mysql_fetch_array($Menu_Query);

function NewCat($Menu, $category, $links, $urls) { //also passed $Menu
   $Cat = explode(",", $Menu[$category]);
   $string = "";
   if ($Cat[0] == "true") {
      if ($Cat[2] == "true") {
         $fieldset_class = "menu gold";
      } else {
         $fieldset_class = "menu";
      }
      $string .= '
         <fieldset class="' . $fieldset_class . '">
            <legend>' . $Cat[1] . '</legend>
            <ul>
      ';
      $link = explode("^", $Menu[$links]);
      $url = explode("^", $Menu[$urls]);
      for ($i = 0; $i < count($link); $i++) {
         if ($link[$i] == null || empty($link[$i]) || $link[$i] == " " || $url[$i] == null || empty($url[$i]) || $url[$i]==" ") {
            continue;
         }
         $string .= '
            <li><a href="' . $url[$i] . '">' . $link[$i] . '</a></li>
         ';
      }
      $string .= '
         </ul>
         </fieldset>
      ';
   }
   return $string;
}
$MENUS .= NewCat($Menu, 'categories1', 'links1', 'urls1');
?>

Quick note/tip,

Try not to put your functions in the middle of your code.. it makes it a nightmare to read later

 

also you could get problems calling them if you loss them in a block

 

ie

<?php
test();
if(false){
function test(){
	echo "World";
}
}
?>

Fatal error:  Call to undefined function test()
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.