FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


PHP page from arrays displays nothing, what's going on?





Mosquito.Tyler
I would like to make a table. A simple table to display a yes or no response in each cell, based on information in the top and left headers. I've stored all my information into three separate arrays, and I wrote a function that i hoped would output the table I want. As it happened, I got my table...well parts of it. Take a look:

Like I said I have my data stored in 3 arrays. Two are numeric and one is associative. They are fairly large so I'm only displaying portions of them.

The Admin Array...
Code:

$admin[0] = "krqembsoacpwzxyilftdgjRvVQCABDEFGHIJKLMNIPTUWXYSZp";
$admin[1] = "kmbsoacpwilftdgjvVQCAFGIKMTUWYSZ";
$admin[2] = "kmbsoacpwldjvVQCAIKMY";
$admin[3] = "ksoaplgQIK";

and the main array...
Code:

$main = array("k","r","q","e","m","b","s","o","a","c","p","w","z","x","y",...);

and the associative array to hold string values.
Code:

$def["k"] = "kick players";
$def["r"] = "execute rcon commands via ma_rcon";
$def["q"] = "execute rcon commands from the menu";
$def["e"] = "run the adminexplode command";
$def["m"] = "slay players";
$def["b"] = "ban players";
$def["s"] = "run ma_say";
$def["o"] = "run ma_chat";
$def["a"] = "run ma_psay";
...

And this is the function that was supposed to build the table.
Code:

function drawTable()
{
echo '<table><tr><th>Ability</th><th>Admin 1</th><th>Admin 2</th><th>Admin 3</th><th>Admin 4</th></tr>';
foreach($main as $letter){
   echo '<tr><th>' . $def[$letter] . '</th>';
   foreach($admin as $val){
      echo '<td>';
      if (substr_count($val, $letter))
         echo '<img src="" alt="Yes" />';         
      else
         echo '<img src="" alt="No" />';
      echo '</td>';
      }
   echo '</tr>';
}
echo '</table>';
}

of course right after that I called the drawTable() function.

The source output I get from this is...
Code:

<table><tr><th>Ability</th><th>Admin 1</th><th>Admin 2</th><th>Admin 3</th><th>Admin 4</th></tr></table>


So, obviously there's something happening that isn't allowing the function to go through my arrays...but I can't figure it out. I'm pretty sure it's a syntax error but I can't find what's wrong, as far as I know (which isn't very far) it should work!

Help of any kind (including unsure suggestions) would be greatly appreciated.
Mosquito.Tyler
Sigh...this is the second time this has happened to me. I make a post asking for help, then go back to it, change one little thing and it works perfectly.

For those who care, all I had to do was add arguments to the function that carried the array variables into the function itself.

Thanks anyway those who look at this!
hexkid
Problem is that variables defined outside a function aren't available inside it.
Code:
function foo1() {
  echo $x; // $x is not defined inside the function
}
function foo2($param) {
  echo $param; // ok
}
function foo3() {
  $x = 4;
  echo $x; // ok, prints 4; the $x outside the function is not changed
}
function foo4(&$param) {
  echo $param; // ok
  $param = 42; // changes the parameter!!
}

$x = 1;
foo1();
foo2($x);
foo3();
echo $x; // prints 1
foo4($x);
echo $x; // prints 42!!


For your specific example I'd pass the arrays to the function, like this
Code:
$admin[0] = "krqembsoacpwzxyilftdgjRvVQCABDEFGHIJKLMNIPTUWXYSZp";
// ...
$main = array("k", "r", ...);
$def["k"] = "kick players";
// ...
drawTable($admin, $main, $def);

function drawTable($admin, $main, $def) {
  // ...
}
Mosquito.Tyler
Aye, I was just cooking and thinking "what if i wanted to use the arrays in other functions?".

Thanks hexkid.
Related topics
How To : Secure Your PHP Website
Tutorial: PHP Installed Modules Dynamic Reference Tool
[php]putting script fragments together?
PHP Page
MORE smf help needed!!!
PHP script trouble
help - download pop up on php page
how do i make a window/iframe in a table within php page..??
php page flickering, maybe caused by flash item
Authentication Module
Send POST variables from HTML
PHP Whos Online Code OR Page refresh..
How to make a php page act differently
php not on?
Reply to topic    Frihost Forum Index -> Scripting -> Php and MySQL

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.