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, 11: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: 7
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, 02:39 AM
ApathyBoy ApathyBoy is offline
Junior Member
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, 03:46 AM
ben rowe ben rowe is offline
Junior Member
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, 01: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: 7
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, 02:12 AM
ben rowe ben rowe is offline
Junior Member
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, 04: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: 7
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, 06:25 PM
ben rowe ben rowe is offline
Junior Member
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, 06:35 PM
FrankieShakes FrankieShakes is offline
Frank The Tank!
Dev Articles Beginner (1000 - 1499 posts)
 
Join Date: Jun 2002
Location: Toronto, Canada
Posts: 1,246 FrankieShakes User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 8
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, 08:16 PM
ben rowe ben rowe is offline
Junior Member
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 13th, 2003, 12:26 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: 7
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 13th, 2003, 12:31 AM
ben rowe ben rowe is offline
Junior Member
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 13th, 2003, 12:35 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: 7
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 13th, 2003, 12:43 AM
ben rowe ben rowe is offline
Junior Member
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