0

I am reading some rows from mysql to create a group of radio buttons. I need to write the name of the player two times, first as a value attribute and second the second time as a text. I don't understand why the same code ($players[$x]) produces two different values in the output html. This is the php code:

$players = readSqlFromMySql("select name from players");        
for($x = 0; $x < count($players); $x++) {                     
    echo('<input type="radio" name="radio-choice-v-2" value="' . 
    $players[$x] . '" id="name-choice-' . chr(66 + $x) . '" ><label 
    for="name-choice-' . chr(66 + $x) . '">' . $players[$x] . '</label>');
} 

This is the rendered output html:

<input type="radio" name="radio-choice-v-2" value="<td style='width:150px;border:1px solid black;'>Amie</td>" id="name-choice-B">

So the first time, the variable's output is "td style=...", wheareas the second time it shows the expected value ("Amie"). What's going on? Is it php? jquery? jquerymobile? I am using XAMPP.

Thanks

10
  • try printing the players variable before the loop Commented Nov 21, 2017 at 4:39
  • I have done it, and it shows the expected value. Commented Nov 21, 2017 at 4:40
  • can you post the printed value..your code looks fine.. Commented Nov 21, 2017 at 4:41
  • echo($players[$x]) prints "Amie" Commented Nov 21, 2017 at 4:42
  • no..do like this.. give print_r($players); Commented Nov 21, 2017 at 4:44

2 Answers 2

2

The value of $players[$x] is <td style='width:150px;border:1px solid black;'>Amie</td> and thats what is getting printed. The second $players[$x] is within the label tag that probably you did not notice.

You probably tried printing $player[$x] on the browser thats why you did not see the td tag around it. Please check the source of your browser output and see whats in there.

Also <td style='width:150px;border:1px solid black;'>Amie</td> is exactly 57 char long; which matches your print_r output.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your help, but I still don't understand why the label tag makes a different.
It did not make any difference. The output that you showed is for the first $player[$x] the second one is missing from the output you showed. You are assuming you are getting the <td part from the first $player[$x] and Amie from the second. But basically the whole thing comes from the first one. Does that make sense ?
0

Original Answer

Warning: this answer is wrong on account of a recent comments. Regardless, this answer explains a way in which the described phenomenon could happen.


What you describe is possible if $players is an object instead of an array.

If $players is an object of a class that implements Countable then count($players) is equivalent to calling the method $players->Count`.

Futhermore, if the class also implements ArrayAccess you can access it like an array. That is $players[$x] will be equivalent to $players->offsetGet($x). And the implementation of that method could do whatever, including returning different values on each call.


Try the following ot see if it is in fact some custom class:

echo get_class($players);

Also try the following:

var_dump($players);

You can debate the merits of print_r vs var_dump another day.


New Answer

In comment you say:

$players is an Array, $players[$x] is a String, both expected. However, var_dump returns "string(57) Amie". String(57) is the length of the unexpected code I got.

If you go back to your code, you placed $players[$x] inside of the attribute value. And what we found inside of value in the rendered out put is: <td style='width:150px;border:1px solid black;'>Amie</td>. That seems to be what you get from $players[$x].

If I put in my code:

<?php var_dump("<td style='width:150px;border:1px solid black;'>Amie</td>");

This is what I see in the browser:

string(57) "Amie"

But if I look at the source code of the page in the browser, it reads:

string(57) "<td style='width:150px;border:1px solid black;'>Amie</td>"

That is what is happening there. The value of Amie is actually "<td style='width:150px;border:1px solid black;'>Amie</td>" but you do not see in the browser.

4 Comments

$players is an Array, $players[$x] is a String, both expected. However, var_dump returns "string(57) Amie". String(57) is the length of the unexpected code I got.
I am using jquery mobile, so the code within the echo() statement is not exactly the same as the output html
@PabloMedina jquery mobile is client side, PHP is server side, how could jquery mobile affect PHP? It does not. Now, if you have some code in javascript (jquery mobile or not) that is modifying the page that is another thing. You need to view the source code of the page in the browser.
Right, lazy me, I was just looking at the output and chrome inspect code, but the source code is clear. What comes from mysql is the long string. Thanks!

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.