Jump to content

[SOLVED] Call function within another function


programguru

Recommended Posts

FYI if you want the answer, for what was going on, your test script worked exactly as expected.  PHP is somewhat odd, in that what the global keyword does is say -- reference the "global variable of this name" in the function rather than make a new local variable.  So you set the variable in the global scope at the top, in in the one function you declare the global and echo it.  It displays of course.  In the other you don't and you get no echo. 

 

This is exactly what global does, and why it's a bad idea and a mess that produces spaghetti.  Once you start using it, you might as well have one big gigantic script with no functions at all.

Link to comment
Share on other sites

  • Replies 56
  • Created
  • Last Reply

Top Posters In This Topic

Top Posters In This Topic

FYI if you want the answer, for what was going on, your test script worked exactly as expected.  PHP is somewhat odd, in that what the global keyword does is say -- reference the "global variable of this name" in the function rather than make a new local variable.  So you set the variable in the global scope at the top, in in the one function you declare the global and echo it.  It displays of course.  In the other you don't and you get no echo. 

 

This is exactly what global does, and why it's a bad idea and a mess that produces spaghetti.  Once you start using it, you might as well have one big gigantic script with no functions at all.

 

I hate globasl! But I added a & (reference) to the front of that the var that was not outputting and now it is:

 

function globalEcho() {

  echo "&$table_name";

}

Link to comment
Share on other sites

The problem is the constants function was not working either.

 

Because you where still refering to the non-existant $connection variable within viewSpecialsUI(). mysql_query() does not need a resource passed to it, it is an optional parameter and if not passed it will simply use the last opended connection.

 

But I'm just on a relentless battle now to figure out something that works, so I don't have to have a redundant $table_name in all of my functions when I could just call one dbConnect() function that holds it.

 

Are you telling us your only going to use one table for your entire application? Seems odd.

 

Anyway...

 

This will work. Just change 'foo' to whatever you need.

 

<?php

define('DBNAME', 'foo');
define('DBHOST', 'foo');
define('DBUSER', 'foo');
define('DBPASS', 'foo');
define('DBTABLE', 'foo');

function dbConnect() {
  mysql_connect(DBHOST, DBUSER, DBPASS) or die(mysql_error());
  mysql_select_db(DBNAME) or die(mysql_error());
}

function viewSpecialsUi() {
  dbConnect();
  $bd_string = "<ul>";
  $sql = "SELECT id, date, date_added, title, image FROM " . DBTABLE . " ORDER by date_added";
  $result = mysql_query($sql) or die(mysql_error());
  if (!mysql_num_rows($result)) {
    echo "<p>There are no records to display.</p>";
   }
}

viewSpecialsUi();

?>

 

You can then modify viewSpecialsUi() to display whatever you want. Though really, functions are most usefull when they simply return result and not echo them.

Link to comment
Share on other sites

FYI if you want the answer, for what was going on, your test script worked exactly as expected.  PHP is somewhat odd, in that what the global keyword does is say -- reference the "global variable of this name" in the function rather than make a new local variable.  So you set the variable in the global scope at the top, in in the one function you declare the global and echo it.  It displays of course.  In the other you don't and you get no echo. 

 

This is exactly what global does, and why it's a bad idea and a mess that produces spaghetti.  Once you start using it, you might as well have one big gigantic script with no functions at all.

 

I hate globasl! But I added a & (reference) to the front of that the var that was not outputting and now it is:

 

function globalEcho() {

   echo "&$table_name";

}

 

Globals are bad. I don't think you get it.

Link to comment
Share on other sites

thorpe,

 

that did work!

 

and you are right. it does not make much sense as i am normalizing my db data.

 

so maybe i went all this way for really no good reason!

 

however, i just realized i can just define all my tables in the costants, so i can still keep it all in one place.

 

im not sure how this applies to viewSpecialsUi():

 

"Though really, functions are most useful when they simply return result and not echo them."

 

do you mean the function output should be returned as opposed to echoed like: echo "<p>There are no records to display.</p>";???

 

 

Link to comment
Share on other sites

FYI if you want the answer, for what was going on, your test script worked exactly as expected.  PHP is somewhat odd, in that what the global keyword does is say -- reference the "global variable of this name" in the function rather than make a new local variable.  So you set the variable in the global scope at the top, in in the one function you declare the global and echo it.  It displays of course.  In the other you don't and you get no echo. 

 

This is exactly what global does, and why it's a bad idea and a mess that produces spaghetti.  Once you start using it, you might as well have one big gigantic script with no functions at all.

 

I hate globasl! But I added a & (reference) to the front of that the var that was not outputting and now it is:

 

function globalEcho() {

   echo "&$table_name";

}

 

Globals are bad. I don't think you get it.

 

i don't get a lot of things

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.