1
kaotik
Populate dropdown
  • 2004/10/6 21:57

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


I've been reading through the forums tring to understand how to populate a dropdown with values taken from the db. But my code simply does not work.

$results $xoopsDB->query('
   SELECT account
   FROM ' 
$xoopsDB->prefix('wmfx_popsettings') . '
   WHERE id = $id
'
);
$select_str "";
while (
$opt $xoopsDB->fetchArray($results)) {
$sel $opt['account'];
$select_str .= "$seln";
}


What am I doing wrong?

2
Dave_L
Re: Populate dropdown
  • 2004/10/6 22:42

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Add some diagnostic output to troubleshoot the problem:

[color=ff0000]error_reporting(E_ALL);#*#DEBUG#[/color]
$results $xoopsDB->query('
   SELECT account
   FROM ' 
$xoopsDB->prefix('wmfx_popsettings') . '
   WHERE id = $id
'
);
$select_str "";
while (
$opt $xoopsDB->fetchArray($results)) {
[
color=ff0000]var_dump('opt'$opt);#*#DEBUG#[/color]
   
$sel $opt['account'];
   
$select_str .= "$seln";
}
[
color=ff0000]var_dump('select_str'$select_str);#*#DEBUG#[/color]


(I thought I suggested this in another topic.)

3
kaotik
Re:Populate dropdown
  • 2004/10/6 23:05

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


Sucess!! Thanks Dave_L for your excelent sugestions (you had already sugested this to me but I didn't think of aplling to so extensivly). I finally figured out where it was getting screwed up.
It was on this line:
while ($opt $xoopsDB->fetchArray($results)) {

Which I replaced with:
while ($row=mysql_fetch_array($result)) {


For anyone else that might be looking for this, here's my complete code; I commented out the lines I used for troubleshooting:
//error_reporting(E_ALL);#*#DEBUG#
$results $xoopsDB->query('
   SELECT account
   FROM ' 
$xoopsDB->prefix('wmfx_popsettings') . '
   WHERE id = $id
'
);
$select_str "";
while (
$row=mysql_fetch_array($result)) {
//var_dump('row', $row);#*#DEBUG#
$sel $row['account'];
$select_str .= "$seln";
}
//var_dump('select_str', $select_str);#*#DEBUG#


Once again, thanks Dave_L!

4
Dave_L
Re:Populate dropdown
  • 2004/10/7 15:14

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Hmmm ...

I don't see any reason why mysql_fetch_array would work and $xoopsDB->fetchArray wouldn't, unless your PHP version is < 4.0.3.

$xoopsDB->fetchArray simply calls mysql_fetch_assoc, which is equivalent to calling mysql_fetch_array with MYSQL_ASSOC for the optional second parameter. Since you're only accessing the return value as an associative array, that should work ok.

mysql_fetch_assoc requires PHP >= 4.0.3, but that's a pretty old version.

Another possibility, I guess, is that the variable $opt was already being used, and your overwriting it messed that up.

5
kaotik
Re:Populate dropdown
  • 2004/10/9 3:20

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


I'm running version 4.3.3
I tried replacing $opt with $row, and it still doesn't work.
For some reason that I don't understand, $xoopsDB->fetchArray doesn't work and mysql_fetch_array does.

6
Dave_L
Re:Populate dropdown
  • 2004/10/9 4:49

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


I don't know.

I just noticed that you're mixing $result and $results in the code above, but maybe that's just a typo in the posts here, and not in the actual code you're using.

Other than that, the only way of figuring out why $xoopsDB->fetchArray didn't work for you is more debugging. But you may not want to go to the trouble, unless this is a module you're planning on publishing.

7
kaotik
Re:Populate dropdown
  • 2004/10/9 16:21

  • kaotik

  • Just can't stay away

  • Posts: 861

  • Since: 2004/2/19


As usual your right. My sloppy and newbie coding abilities are to blame. The $result/$results was in the code and since $result was already being used, it was getting called from there. Now $xoopsDB->fetchArray does work.
I'm now having another problem.
This works ok:
$query "select * FROM ".$xoopsDB->prefix("wmfx_popsettings")." where uid = $userid";
$results=$xoopsDB->query($query,$options[0],0);


But this doesn't:
$results $xoopsDB->query('
   SELECT *
   FROM ' 
$xoopsDB->prefix('wmfx_popsettings') . '
   WHERE uid = $userid
'
);


Aren't these suppose to be the same?

8
Dave_L
Re:Populate dropdown
  • 2004/10/9 17:45

  • Dave_L

  • XOOPS is my life!

  • Posts: 2277

  • Since: 2003/11/7


Quote:
As usual your right.


That's only because I've made the same mistakes myself 1000 times, so I recognize them.

You need to use double quotes so that the variable $userid will be interpolated:

$results $xoopsDB->query("
   SELECT *
   FROM " 
$xoopsDB->prefix('wmfx_popsettings') . "
   WHERE uid = 
$userid
"
);


(I missed that earlier.)

9
script_fu
Re:Populate dropdown

I wonder where this code was used and what for? I would love to be able to populate dropdowns...

I was hopeing the new formulize_1.5rc.zip from mod dev would have dropdowns. Thats looking very nice....

10
script_fu
Re:Populate dropdown

bump

Login

Who's Online

275 user(s) are online (216 user(s) are browsing Support Forums)


Members: 0


Guests: 275


more...

Donat-O-Meter

Stats
Goal: $100.00
Due Date: Nov 30
Gross Amount: $0.00
Net Balance: $0.00
Left to go: $100.00
Make donations with PayPal!

Latest GitHub Commits