vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   while loop in a function (https://vborg.vbsupport.ru/showthread.php?t=100693)

harmor19 11-13-2005 06:52 AM

while loop in a function
 
I am doing the hook system in my own scripts but I have a small problem.
It only returns the first hook that matches $hook_name.

How do I have it return all the rows where the hook_names match?

PHP Code:

function fetch_hook($hook_name)
{
  
$gethook mysql_query("SELECT * FROM hooks WHERE hook_name = '$hook_name' ");

  while(
$hook mysql_fetch_array($gethook))
  { 
      return 
$hook['phpcode'];   



Marco van Herwaarden 11-13-2005 06:03 PM

A 'return' statement leaves the function and returns control to the calling script.

harmor19 11-13-2005 06:48 PM

So I'll need to check if I got every field where hook_name = $hook_name?

I don't know how to do this offhand, hopefully I'll find it easily.

Marco van Herwaarden 11-13-2005 07:52 PM

No, you should not return there. Instead you would probably want to evaluate the code you have just read from the database.

harmor19 11-13-2005 08:12 PM

It occured to me that I was using return in the while loop so I took it out of the while loop and now it works.
That's another lesson learned.

Thanks for the help though.

Marco van Herwaarden 11-13-2005 08:20 PM

Well that was my first answer. ;)

harmor19 11-13-2005 08:21 PM

Quote:

Originally Posted by MarcoH64
Well that was my first answer. ;)

I didn't interpret it like that.

The Geek 11-13-2005 08:45 PM

Think you want something like this:

PHP Code:

function fetch_hook($hook_name)
{
  
$gethook mysql_query("SELECT * FROM hooks WHERE hook_name = '$hook_name' ");
  
$return = array();
  while(
$hook mysql_fetch_array($gethook))
  {
      
$return[]= $hook['phpcode'];
  }
  return 
$return;


for a more vb3.5 approach (with cleansing, etc...) try this:

PHP Code:

 
function fetch_hook($hook_name)
{
  global 
$db;
  
$hooks $db->query_read("SELECT phpcode FROM hooks WHERE hook_name = '" $db->escape_string($hook_name) . "' ");
  
$return = array();
  while(
$hook $db->fetch_array($hooks))
  {
      
$return[]= $hook['phpcode'];
  }
  
$db->free_result($hooks);
  return 
$return;


Havent tested either - but they should work fine for you.

nJoy


All times are GMT. The time now is 04:43 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.01019 seconds
  • Memory Usage 1,734KB
  • 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
  • (3)bbcode_php_printable
  • (1)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (8)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