Problem Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Nosferatu
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 6:56 pm

Problem Script

Post by Nosferatu »

Hey, I've been doing PHP a couple of days now, and programmed a fully working MessageBoard yesterday.

This has nothing to do with my problem, I just illustrating that I'm a newcomer to PHP, not coding ;)

So the problem script is one I'm making for a friend who is constructing a Member Awards for another Forum. Currently, the script only asks you to type in the name of your vote into a text field. We both agree that a Select Box would be more appropriate. So, since neither of us realy want to write a Select Box with the usernames of every member in HTML, I decided that creating an array of the usernames and using a For loop to add them to the box would be the best way.

It's a VERY simple script, but I can't get it to output the array.
If you ask it to print out any name in the array, it will have no problems, but in a For loop, they just don't exist.

I thought it might just be that the array is too long, so I put in a While loop to stop the prossessing untill the array had been populated, but that didn't work.

Here's the script:

Code: Select all

<?
$User=array(&quote;User1&quote;,&quote;User2&quote;,&quote;User3&quote;);

function select($name, $size){
print &quote;<SELECT NAME='&quote;.$name.&quote;' SIZE='&quote;.$size.&quote;'>\n&quote;;
for ($a=0; $a<count($User); $a++){
print &quote;<OPTION VALUE='&quote;.$User&#1111;$a].&quote;'>&quote;.$User&#1111;$a].&quote;</OPTION>\n&quote;;
}
print &quote;</SELECT>&quote;;
}

select(sel1, 5);
?>
[See it in "action"]

I realy am at a loss here, if anyone can spot where I'm going wrong, I'd be most appreiciative.

For the record, I've tried replacing "$a<count($User)" with "$a<3". No joy...
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

php has a very nice function called foreach() that loops over arrays.

do this:

Code: Select all

$bob = array('bob','jim','kyle','burrito');
$sel = "<select name=\"name\">";
foreach($bob as $name){
   $sel .= "<option value=\"".$name."\">".$name."</option>";
}
$sel .= "</select>";
Nosferatu
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 6:56 pm

Post by Nosferatu »

Thanks, that will help even if it wasn't the problem ;)

It seems to work fine if the Array is set within the function, not before. Bit strange, but ok! :p
User avatar
shoebappa
Forum Contributor
Posts: 158
Joined: Mon Jul 11, 2005 9:14 pm
Location: Norfolk, VA

Post by shoebappa »

foreach is good, but your problem is that functions have a local scope, meaning that you pass it variable and it returns a value. So any variables used in the function, that aren't passed and aren't returned don't effect variables in the global scope and are destroyed when the function ends. This is important because in larger projects, particularly with multiple developers, you don't want to have to worry about variable conflicts when using a shared function.

Code: Select all

<?php

function increment() {
  $i = $i + 1;
  return $i;
}

$i = 1;
echo increment();

?>
Would output 1 because the global $i doesn't get passed to the function.

Code: Select all

<?php

function increment($i) {
  $i = $i + 1;
  return $i;
}

$i = 1;
echo increment();

?>
This would output 2 BUT $i would still be 1

Your code would need to pass the $User array to the function. Just to avoid confusion, I would use a different name in the function scope.

Code: Select all

<?php
$User=array("User1","User2","User3");

function select($name, $size, $tempuser){
  print "<SELECT NAME='".$name."' SIZE='".$size."'>\n";
  for ($a=0; $a<count($tempuser); $a++){
    print "<OPTION VALUE='".$tempuser[$a]."'>".$tempuser[$a]."</OPTION>\n";
  }
  print "</SELECT>";
}

select("sel1", 5, $User);
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

damn it if I didn't even notice it was in a function! 8O

there's really no reason for it to be in a function, just use the code I gave out (outside of a function) and you're good to go.
Nosferatu
Forum Newbie
Posts: 3
Joined: Mon Jul 11, 2005 6:56 pm

Post by Nosferatu »

It's in a Function because it will be called multiple times.

As I said, it's to be used for a Member Awards system, so for each question (Most Helpfull, Worst Spammer Etc.) It will be displayed.

It just makes it a little easier to just write select(mosthelpfull, 5) for each different element of the form, then change the name every time.

Anyways, thanks for the help guys, most aprieciated :)
Post Reply