General Programming Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
 
User Name:
Password:
Remember me
 



Go Back   Dev Articles Community ForumsProgrammingGeneral Programming Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Display Modes
 
Unread Dev Articles Community Forums Sponsor:
  #1  
Old June 10th, 2003, 10:36 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Calling a user made functionfunction

I have alot of db queries and alot of them happen regulary. So I decided to create a class with those functions. Since I never really created my functions or class I decided to go with one of the easiest of functions just to test out if i am doing it right. As it turns out I am doing it wrong. The page is giving me
Quote:
Fatal error: Call to undefined function: viewnewshome() in /home/virtual/site4/fst/var/www/html/test/suanhacky/index.php on line 8


Here is the code for botht he class file and the page tryng to czll the function

PHP Code:
<?php
    
require("class.top.php");
    require(
"class.news.php");
?>
    <table>
<?php
    viewnewshome
();
?>
    </table>
<?php
     
require("class.footer.php");
?>


That was the page callign the function and here is the other file
PHP Code:
<?php
  
require("class.dbcon.php");
  class 
news
   
{
    function 
viewnewshome()
     {
      
$newsloop=4;
      
$newsquery=mysqlquery("SELECT title,type,date,news FROM news ORDER BY date DESC LIMIT 4") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
      
$news=mysql_fetch_array($newsquery) or die ("Could not create Array. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
      while(
$newsloop>=1)
       {
        require(
"class.typenews.php");
        
$posted=date("m/d/Y"strtotime($news['date']));
        
printf("<th bgcolor=#400000><center>%s - %s - %s</center></th><tr><td>%s</td></tr>",$news['title'],$typenews,$posted,$news['news']);
        
$newsloop--;
       }
     }
   }
?>
__________________
CHornJr
"One day I'll know what I am doing"
My Blog
Suanhacky Lodge #49
Rebel Squadrons

Reply With Quote
  #2  
Old June 11th, 2003, 01:39 AM
ApathyBoy ApathyBoy is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 3 ApathyBoy User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Send a message via ICQ to ApathyBoy
Functions contained within a class are actually called methods. In order to access these methods you have to create a new "instance" of the class and store it in a variable. Then to access a method you use the "->" operator. In your case the code would look something like :

PHP Code:
<?php
    
require("class.top.php");
    require(
"class.news.php");
?>
    <table>
<?php
    $news 
=& new news();
    
$news->viewnewshome();
?>
    </table>
<?php
     
require("class.footer.php");
?>


I hope that helps out!

Reply With Quote
  #3  
Old June 11th, 2003, 02:46 AM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
hmm, to use functions you dont need to use classes!

just use:

PHP Code:
function name($arg1 ''$arg2 '') {

// stuff here

}

name('data'$test); 



Reply With Quote
  #4  
Old June 12th, 2003, 12:57 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
I now got that working but I have another question. In my file I have a number of functions now. Can I get one function to call another function? If I can, can I call function 2 into function 1. For instance:
PHP Code:
 Function_1()
 {
   
//some code
   
function_2();
   
//some more code
 
}
Function_2()
 {
   
//some code
   //return some value
 


Reply With Quote
  #5  
Old June 12th, 2003, 01:12 AM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
yes, that code would work!

Reply With Quote
  #6  
Old June 12th, 2003, 03:15 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
in the first function I originally had it include a seperate file and it worked fine but i added the contents of that file to my function file as it's own function but it is not working and i am not sure why it isn't.

Here are the two functions
PHP Code:
function viewnewshome()
   {    
    
$recent=4;
    
$newsquery=mysql_query("SELECT rownum FROM news ORDER BY rownum DESC LIMIT 4") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
    
$news=mysql_fetch_array($newsquery);
    
$newsrow=$news['rownum'];
    while(
$recent >= 1)
     {
      
$newsquery=mysql_query("SELECT*FROM news WHERE rownum='$newsrow'") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
      
$news=mysql_fetch_array($newsquery); 
      
newstype();
      
$posted=date("m/d/Y"strtotime($news['date']));
      
printf('<th bgcolor=#400000><center>%s - %s - %s</center></th><tr><td>%s</td></tr>',$news['title'],$newstype,$posted,$news['news']);
      
$newsrow--;
      
$recent--;
     }
   }
  function 
newstype()
   {
    if(
$news['type']==1)
     {
      
$newstype='General OA(National, Regional, Section) News';
     }
    elseif(
$news['type']==2)
     {
      
$newstype='Lodge News';
     }
    elseif(
$news['type']==3)
     {
      
$newstype='Suanhacky.org News';
     }
    elseif(
$news['type']==4)
     {
      
$newstype='Catawba News';
     }
    elseif(
$news['type']==5)
     {
      
$newstype='Matinecock News';
     }
    else
     {
      
$newstype='Wandoweknock News';
     }
    return 
$newstype;
   } 


Any ideas as to what I am doing wrong?

Reply With Quote
  #7  
Old June 12th, 2003, 05:25 PM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
what error messages are you getting, and which line??

Reply With Quote
  #8  
Old June 12th, 2003, 05:35 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
On a hunch... try calling your second function like so:

$this->newsType();

See if that works...
__________________
____________________________________________
Developer Shed Weekly Writer | DevArticles Forum Moderator
Build Your Own KlipFolio Klip With PHP
FrankManno.com - Under Construction
Design Interactive Group - Under Construction

Reply With Quote
  #9  
Old June 12th, 2003, 07:16 PM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
After a second look i know what the problem is now!

firstly frankie, $this->newsType(); is used in a class. You would use this when your making a refrence to a function, which is called inside another class funcion.

CHornJr, there is a few problems with your code.

Firstly a function cant use variables that are not declaired inside the same function. which means

PHP Code:
 $var "Hello World";

function 
Test() {

echo 
$var;




this would result in an error, cos the var $var has not been defined in the function, there are a couple of work arounds here, make the $var a global with

global $var;

inside the function, before the $var is called, or pass the $var as a argument

function Test ($var) {...

and when you call the function

Test($var);

so in your case you would need to declair the $news as a global

your function newstype() actually returns a value, meaning, if you where to echo the function, you would echo a value. you need to assign the function to a $variable

$var = newstype();

Reply With Quote
  #10  
Old June 12th, 2003, 11:26 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Ok I am little confused here cause I thought I tried what you said but apparently I didn't do it correctly.

In function viewnewshome I have after I declare $news

PHP Code:
global $news;
$newtype=newstype(); 

Reply With Quote
  #11  
Old June 12th, 2003, 11:31 PM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
you will need to declair the global in the newstype function

function NewsType() {

global $news;

....


then the next change is where you have

newstype();

in the first function. Calling this function without assigning it to a var, or string doesnt actually do anything, because the variable

$newstype

has been declaired inside that function, however you return it, so if you change the line

newstype();


$newstype = newstype();

it will cause your function to work.

if you make those 2 changes your code should then work

Reply With Quote
  #12  
Old June 12th, 2003, 11:35 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
Ok it is jumping straight to the else statmement in the second function

Reply With Quote
  #13  
Old June 12th, 2003, 11:43 PM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
try checking your database, also please post what code you have so far

Reply With Quote
  #14  
Old June 12th, 2003, 11:46 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
The databse is showing different types

here is the code I have so far

PHP Code:
function viewnewshome()
   {    
    
$recent=4;
    
$newsquery=mysql_query("SELECT rownum FROM news ORDER BY rownum DESC LIMIT 4") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
    
$news=mysql_fetch_array($newsquery);
    
$newsrow=$news['rownum'];
    while(
$recent >= 1)
     {
      
$newsquery=mysql_query("SELECT*FROM news WHERE rownum='$newsrow'") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
      
$news=mysql_fetch_array($newsquery);
      
$newstype=newstype();
      
$posted=date("m/d/Y"strtotime($news['date']));
      
printf('<th bgcolor=#400000><center>%s - %s - %s</center></th><tr><td>%s</td></tr>',$news['title'],$newstype,$posted,$news['news']);
      
$newsrow--;
      
$recent--;
     }
   }
  function 
newstype()
   {
    global 
$news;
    if(
$news['type']==1)
     {
      
$newstype='General OA(National, Regional, Section) News';
     }
    elseif(
$news['type']==2)
     {
      
$newstype='Lodge News';
     }
    elseif(
$news['type']==3)
     {
      
$newstype='Suanhacky.org News';
     }
    elseif(
$news['type']==4)
     {
      
$newstype='Catawba News';
     }
    elseif(
$news['type']==5)
     {
      
$newstype='Matinecock News';
     }
    else
     {
      
$newstype='Wandoweknock News';
     }
    return 
$newstype;
   } 

Reply With Quote
  #15  
Old June 13th, 2003, 12:04 AM
ben rowe ben rowe is offline
Registered User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Jun 2003
Posts: 24 ben rowe User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Heres how the code should look:

PHP Code:
function viewnewshome() {    

    
$recent=4;
    
$newsquery=mysql_query("SELECT rownum FROM news ORDER BY rownum DESC LIMIT 4") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());

    
$news=mysql_fetch_array($newsquery);

    
$newsrow=$news['rownum'];

    while(
$recent >= 1) {
      
$newsquery=mysql_query("SELECT*FROM news WHERE rownum='$newsrow'") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());

      
$news=mysql_fetch_array($newsquery);

      
$newstype=newstype();

      
$posted=date("m/d/Y"strtotime($news['date']));

      
printf('<th bgcolor=#400000><center>%s - %s - %s</center></th><tr><td>%s</td></tr> ',$news['title'],$newstype,$posted,$news['news']);
      
$newsrow--;
      
$recent--;
     }
   }

  function 
newstype()
   {
    global 
$news;

$news $news['type'];

//for testing uncomment the following line
//echo $news;

switch($news) {

case 
"1":
$newstype='General OA(National, Regional, Section) News';
break;

case 
"2":
$newstype='Lodge News';
break;

case 
"3":
$newstype='Suanhacky.org News';
break;

case 
"4":
$newstype='Catawba News';
break;

case 
"5":
$newstype='Matinecock News';
break;

default:
$newstype='Wandoweknock News';
break;

}
      
    return 
$newstype;
   } 

Reply With Quote
  #16  
Old June 13th, 2003, 02:25 PM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
It still showed only the default. I un-commented the echo for testing and it isn't showing anything.

PHP Code:
<?php
  
require("class.dbcon.php");  
  function 
viewnewshome()
   {    
    
$recent=4;
    
$newsquery=mysql_query("SELECT rownum FROM news ORDER BY rownum DESC LIMIT 4") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
    
$news=mysql_fetch_array($newsquery);
    
$newsrow=$news['rownum'];
    while(
$recent >= 1)
     {
      
$newsquery=mysql_query("SELECT*FROM news WHERE rownum='$newsrow'") or die ("Could not query the table NEWS. Please inform the <a href='mailto:webmaster@suanhacky.org'>Webmaster</a> immedietly. ".mysql_error());
      
$news=mysql_fetch_array($newsquery);
      
$newstype=newstype();
      
$posted=date("m/d/Y"strtotime($news['date']));
      
printf('<th bgcolor=#400000><center>%s - %s - %s</center></th><tr><td>%s</td></tr>',$news['title'],$newstype,$posted,$news['news']);
      
$newsrow--;
      
$recent--;
     }
   }
  function 
newstype()
   {
    global 
$news;
    
$news=$news['type'];
    
//for testing uncomment the following line
    //echo $news;
    
switch($news)
     {
      case 
"1":
      
$newstype='General OA(National, Regional, Section) News';
      break;
      case 
"2":
      
$newstype='Lodge News';
      break;
      case 
"3":
      
$newstype='Suanhacky.org News';
      break;
      case 
"4":
      
$newstype='Cataeba News';
      break;
      case 
"5":
      
$newstype='Matinecock News';
      break;
      default:
      
$newstype='Wandoweknock News';
      break;
     }
    return 
$newstype;
   }   
?>

Reply With Quote
  #17  
Old June 13th, 2003, 05:37 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,240 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 17
Send a message via ICQ to FrankieShakes Send a message via MSN to FrankieShakes
Ben,

Since $news is an array, would you not have to refer to it when globalizing it as:

global $news[];

or am I mistaken?

CHornJr,

If when echoing the value of news displays nothing, there's a problem with your statement that deals with assigning a value to $news within your viewnewshome() function...

Try echoing it out right after this statement:

Code:
$news=mysql_fetch_array($newsquery);
echo($news['type']);


See if that displays anything!

Reply With Quote
  #18  
Old June 14th, 2003, 12:05 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
When I echoed out $news['type'] in the first function it gave the proper types so it is not the query that is causing the problem

Reply With Quote
  #19  
Old June 14th, 2003, 12:09 AM
CHornJr's Avatar
CHornJr CHornJr is offline
Contributing User
Dev Articles Newbie (0 - 499 posts)
 
Join Date: Nov 2002
Location: New York City
Posts: 236 CHornJr User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 Day 1 h 50 m 36 sec
Reputation Power: 16
Send a message via AIM to CHornJr Send a message via MSN to CHornJr Send a message via Yahoo to CHornJr
I also tried makign the last case case "6" instead of deafult and it gave me nothing which means it isn't grabbing the variable properly

Reply With Quote
Reply

Viewing: Dev Articles Community ForumsProgrammingGeneral Programming Help > Calling a user made functionfunction


Developer Shed Advertisers and Affiliates


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.

© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap