vb.org Archive

vb.org Archive (https://vborg.vbsupport.ru/index.php)
-   vB3 Programming Discussions (https://vborg.vbsupport.ru/forumdisplay.php?f=15)
-   -   Foreach() problem (https://vborg.vbsupport.ru/showthread.php?t=49735)

matthew tucker 03-06-2003 11:35 PM

Foreach() problem
 
1 Attachment(s)
In admin/user.php I have inserted the following code to extract data from a custom table "memberinfo" and display using the makeinputcode() function of vbulletin.

PHP Code:

maketableheader("Memberinfo details TEST (code in: admin/user.php line 308)");
  
$myquery="SELECT * FROM memberinfo WHERE email='$user[email]'";
$myresult=$DB_site->query_first($myquery);

foreach (
$myresult as $mykey=>$pointer){

    
makeinputcode($mykey,$mykey,$myresult[$mykey]);



Problem is I get two rows for each record. What have I done wrong??

MUG 03-06-2003 11:50 PM

It's the way mysql_fetch_array() works - it returns both a numerical and associative index for each column unless specified otherwise. You can change that block of code to:
PHP Code:

maketableheader("Memberinfo details TEST (code in: admin/user.php line 308)");
$myquery=$DB_site->query("SELECT * FROM memberinfo WHERE email='$user[email]'");
$myresult mysql_fetch_array($myqueryMYSQL_NUM);

mysql_free_result($myquery);

foreach (
$myresult as $mykey=>$pointer){
  
makeinputcode($mykey,$mykey,$myresult[$mykey]);


which should work.

Sebastian 03-06-2003 11:52 PM

add LIMIT 1 to the query.

MUG 03-06-2003 11:53 PM

Quote:

Originally posted by Sebastian
add LIMIT 1 to the query.
What? That wouldn't make any difference. It's only fetching one row in the first place.

Sebastian 03-06-2003 11:55 PM

its adding more results, and he/she only want 1,. not 2,3,4,5...
or did i read the post wrong?

MUG 03-06-2003 11:57 PM

Quote:

Originally posted by Sebastian
its adding more results, and he/she only want 1,. not 2,3,4,5...
or did i read the post wrong?

He's using foreach() on something returned by mysql_fetch_array() - which by default includes each column twice, unless you specify otherwise.

matthew tucker 03-07-2003 01:11 AM

I (he!) want to step through all the fields in the array!

I started to use extract() then hard-code all the makeinputcode bits, then I though no, why not build an interface to the memberinfo table on the fly rather than hard code it.

Foreach() looked a bettersolution than while() ...

I'll try your suggestions.

I was trying to use the vB functions but now I think I'd probably be better off just using plain PHP and mySQL so I understand the code better, no? I'm a bit new to PHP, trying hard ...

Matthew

matthew tucker 03-07-2003 01:16 AM

Nope

I get

Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /[path]/user.php on line 314

matthew tucker 03-07-2003 01:45 AM

All fixed. thanks. I needed the MYSQL_ASSOC value!

Works now, thanks x 10^6

Matthew


All times are GMT. The time now is 07:05 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.01010 seconds
  • Memory Usage 1,735KB
  • 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
  • (2)bbcode_quote_printable
  • (1)footer
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (6)option
  • (1)post_thanks_navbar_search
  • (1)printthread
  • (9)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