-2

I am trying to use to a loop to pull all rows from a table, and change every row to a string then pass to an array. Here is the script I am currently working on.

PHP:

function toggleLayers(){
    $toggleArray = array($toggle);
    for($i=0;$i<$group_layer_row;$i++){
        $toggle=mb_convert_encoding(mssql_result ($rs_group_layer, $i, 0),"UTF-8","SJIS")."_".mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1),"UTF-8","SJIS");
        return $toggleArray($toggle);
    }
}    

Right now it only returns a string without passing to the array. Been looking and can't seem to find anywhere or anyone that can explain this to me in plain english.

Hope you can help. Thanks

5
  • There's no sane syntax like that. Commented Dec 6, 2011 at 0:23
  • 2
    The PHP documentation site is in plain English. Check for example this page on how to use an array: php.net/manual/en/language.types.array.php Commented Dec 6, 2011 at 0:25
  • @Jon I wonder what a for loop will evaluate to Commented Dec 6, 2011 at 0:27
  • @TheNail: A loop is a statement, not an expression -- so it doesn't evaluate to anything. Commented Dec 6, 2011 at 0:32
  • first line inside the function - $toggle isn't set. When you initialise the for loop $group_layer_row isn't set. $rs_group_layer isn't defined anywhere yet you try to use it in your db query. You have the return statement inside the loop so on the first pass you exit the entire function. $toggleArray($toggle) would attempt to run a function with the name stored inside $toggleArray (not what you want) see uk3.php.net/manual/en/functions.variable-functions.php . Commented Dec 6, 2011 at 0:46

2 Answers 2

1

I have no idea what vars are what in your example, but if you wanted to loop through an array and change its contents, here's how i'd do it:

$myArray = array( 'thing', 'thing2' );
// the ampersand will pass by reference, i.e.
// the _Actual_ element in the array
foreach( $myArray as &$thing ){
  $thing .= " - wat?!";
}

print_r( $myArray );

will give you

[0] =>
  'thing - wat?!'
[1] =>
  'thing2 - wat?!'
0

I think you will gonna change your code to something like this:

$toggleArray = array();
for ($i = 0; $i < $group_layer_row; $i++) {
   // push your string onto the array
   $toggleArray[] = mb_convert_encoding(mssql_result($rs_group_layer, $i, 0), "UTF-8", "SJIS") . "_" . mb_convert_encoding(mssql_result ($rs_group_layer, $i, 1), "UTF-8", "SJIS");
}
return $toggleArray;
4
  • where does $group_layer_row initially come from? Commented Dec 6, 2011 at 0:37
  • @DeanMarshall $group_layer_row is a count of the rows in the table. $rs_group_layer=mssql_query($sql, $con); $group_layer_row=mssql_num_rows($rs_group_layer); Commented Dec 6, 2011 at 0:43
  • @Yus any variable that you want to use in a function must either be a parameter or you have to declare it global (or is a superglobal, static member or constant, but that is not likely here) Commented Dec 6, 2011 at 0:45
  • @KristerAndersson Thanks your answer was correct, I also realized I did not include the php file... Commented Dec 6, 2011 at 1:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.