1

Case :

Hi, Stackoverflow community.

Actually i have looping (while) inside while. That's what i call sub while. I want to return some value from while to sub while same, but it looks like, value can't be read in a sub while.

Experiment :

  1. Fetch code working normally. All fine, except this $x;
  2. There is no other error found, but global $x, can't be printed inside sub while

I try to use global function to declare the while function value. Still, global function can't be read in sub while. The code is barely like this :

while (fetch){
$x = value; // multi value, like A, B, C, D, E for example
global $x;
echo $x; // echo print still ok

while (fetch){
echo "<select>". // i use select attribute in this case, it's plus a (.) dot
// so code inside this sub while actually working but $x can't be printed
echo $x; // not print anything
echo "<option" . (($x==$throw[0]) ? 'selected="selected"': '') ."value=".$throw[0].">".$throw[0]."</option>'";
// i use this option attribute, and want $x to be inside the option attribute basically, because it needs to be compared
"</select>";

} // end sub while

} // end while

Desired Output :

I want value in $x return as is it, like in while loop. Because the multi value inside $x will be compared with $throw[0] which has value shown properly while $x value isn't even shown.

FYI, $x contain several value like for example : A, B, C, D, E.

Do you have any idea why this is happen ? What should i do or modify ?

Thank You Stackoverflow Community.

1
  • There should not be a (.) dot after select. echo "<select>"; Commented Jul 29, 2016 at 17:52

1 Answer 1

1

Try This

while (fetch){
$x = value; // multi value, like A, B, C, D, E for example
global $x;
echo $x; // echo print still ok

while (fetch){
echo "<select>".$x; // CHANGED
echo "<option" . (($x==$throw[0]) ? 'selected="selected"': '') ."value=".$throw[0].">".$throw[0]."</option>'";
// i use this option attribute, and want $x to be inside the option attribute basically, because it needs to be compared
"</select>";

} // end sub while

} // end while
Sign up to request clarification or add additional context in comments.

6 Comments

in changed code : echo "<select>".$x; // CHANGED. actually it prints $x, but inside echo <option>, it is not, i still wonder why
Can you show me the output that you desire to achieve?
link like that, if $x work, it should set default value on select attribute. but turned out, the category isnt selected, that means $x probably can't be read there. i use this code for reference link
@KeshiaAngelina Can you send me the working code with some dummy data for fetch with your desired output it should provide?
im not sure why but suddenly it's working. selected value won't work because the listed category isnt same like in select category value. thank youuu
|

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.