PDA

View Full Version : Querying in functions...


gengar003
11-01-2003, 12:26 PM
Okay, I want to be able to execute this 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,$tex t,$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:

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,$tex t,$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
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).