vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Querying in functions... (https://vborg.vbsupport.ru/showthread.php?t=58335)

gengar003 11-01-2003 12:26 PM

Querying in functions...
 
Okay, I want to be able to execute this code:

PHP Code:


/////////////// get news vars

$nid=$HTTP_GET_VARS['nid'];
$newsget=$DB_SITE->query("SELECT * FROM news WHERE nid='$nid'");
$author=$newsget['author'];
$title=$newsget['title'];
$text=$newsget['text'];
$date=$newsget['date'];
$number=0;

$idfind=$DB_SITE->query_plain("SELECT `number` FROM `comments` WHERE cnid='$nid' ORDER BY `number`");

while(
$idloop=$DB_SITE->fetch_array($idfind))
{
$number++;

}
$ncomments=$number;

//////////////
////////////// show the news item
maketableheader("1","black","500","","white","","1","1");
newsdisplay_content($nid,$date,$author,$title,$text,$ncomments);
maketablefooter();
spacing("p");
/////////////get comment vars and show comments.
$commentfetch $DB_SITE->query_plain("SELECT * FROM comments WHERE cnid='$nid' ORDER BY number DESC");
if (!
$commentfetch){
echo 
"NO comments.<p>";
} else {
while(
$commentloop $DB_SITE->fetch_array($commentfetch))
{
$name=$commentloop['author'];
$email=$commentloop['email'];
$www=$commentloop['www'];
$comment=$commentloop['text'];
commentdisplay($name,$email,$www,$comment);
}






And when I place it in the script by itself, it works fine. However, for the purposes of my script, It would be much better were it in a function. (This large amt of code is used more than once. Also, functions allow me to place it more accurately on the page) So, to make a funciton, I do this:

PHP Code:

function makeviewcommentpage(){
/////////////// get news vars
global $DB_SITE,$loggedinuser,$session;
$nid=$HTTP_GET_VARS['nid'];
$newsget=$DB_SITE->query("SELECT * FROM news WHERE nid='$nid'");
$author=$newsget['author'];
$title=$newsget['title'];
$text=$newsget['text'];
$date=$newsget['date'];
$number=0;

$idfind=$DB_SITE->query_plain("SELECT `number` FROM `comments` WHERE cnid='$nid' ORDER BY `number`");

while(
$idloop=$DB_SITE->fetch_array($idfind))
{
$number++;

}
$ncomments=$number;

//////////////
////////////// show the news item
maketableheader("1","black","500","","white","","1","1");
newsdisplay_content($nid,$date,$author,$title,$text,$ncomments);
maketablefooter();
spacing("p");
/////////////get comment vars and show comments.
$commentfetch $DB_SITE->query_plain("SELECT * FROM comments WHERE cnid='$nid' ORDER BY number DESC");
if (!
$commentfetch){
echo 
"NO comments.<p>";
} else {
while(
$commentloop $DB_SITE->fetch_array($commentfetch))
{
$name=$commentloop['author'];
$email=$commentloop['email'];
$www=$commentloop['www'];
$comment=$commentloop['text'];
commentdisplay($name,$email,$www,$comment);
}




}


Adding only the "Function name (){", "Global...", and "}" lines.

And all the queries cease to work. Anyone know why?

Thanks in advance.

Dean C 11-01-2003 02:42 PM

global $DB_site (I believe case makes a big difference :))

assassingod 11-01-2003 02:55 PM

Quote:

Originally Posted by Mist
global $DB_site (I believe case makes a big difference :))

He has

filburt1 11-01-2003 03:31 PM

No offense, but the lack of indenting is stopping me from even trying to diagnose the problem.

assassingod 11-01-2003 04:51 PM

Btw, can you use functions within functions? You are using maketableheader as a function already

Dean C 11-01-2003 06:43 PM

Yes Steve :) Also he's using $DB_SITE not $DB_site (there's a difference in case there).


All times are GMT. The time now is 05:54 AM.

Powered by vBulletin® Version 3.8.12 by vBS
Copyright ©2000 - 2025, vBulletin Solutions Inc.

X vBulletin 3.8.12 by vBS Debug Information
  • Page Generation 0.01129 seconds
  • Memory Usage 1,750KB
  • Queries Executed 10 (?)
More Information
Template Usage:
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_header_end
  • (1)ad_header_logo
  • (1)ad_navbar_below
  • (2)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (6)printthreadbit
  • (1)spacer_close
  • (1)spacer_open 

Phrase Groups Available:
  • global
  • postbit
  • showthread
Included Files:
  • ./printthread.php
  • ./global.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/class_hook.php
  • ./includes/modsystem_functions.php
  • ./includes/class_bbcode_alt.php
  • ./includes/class_bbcode.php
  • ./includes/functions_bigthree.php 

Hooks Called:
  • init_startup
  • init_startup_session_setup_start
  • init_startup_session_setup_complete
  • cache_permissions
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • style_fetch
  • cache_templates
  • global_start
  • parse_templates
  • global_setup_complete
  • printthread_start
  • bbcode_fetch_tags
  • bbcode_create
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • printthread_post
  • printthread_complete