Colin Brazier
asked on
Understanding array_walk_recursive and htmlspecialchars
Hi experts, I am trying to prepare my data for display using my function html, as below.
I am trying to do it all in one go, but the array_walk_recursive doesn't seem to be working as expected - basically, O'Flanagan is displaying as O in my form. It's the first time I have used it. Or maybe the problem is that I don't understand how htmlspecialchars works, ie it doesn't actually change the parameter given to it.
and then eg. echo $arr_fobg_teams[1]['ts_man agername'] shows O
if I say echo html($arr_fobg_teams[1]['t s_managern ame']) it's fine.... shows O'Flanagan
Where am I going wrong?
I am trying to do it all in one go, but the array_walk_recursive doesn't seem to be working as expected - basically, O'Flanagan is displaying as O in my form. It's the first time I have used it. Or maybe the problem is that I don't understand how htmlspecialchars works, ie it doesn't actually change the parameter given to it.
function html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
$sql = "SELECT ts_team_code, ts_team_name, ts_managername, ts_manager_details, ts_instructions
FROM teams_seasons t";
$arr_fobg_teams = $dbi->db_select($sql,'Error occurred with FOBG team');
array_walk_recursive($arr_fobg_teams,'html');
and then eg. echo $arr_fobg_teams[1]['ts_man
if I say echo html($arr_fobg_teams[1]['t
Where am I going wrong?
Actually your code working fine.You pass a parameter in the html() function.So when you echoed the
html($arr_fobg_teams[1]['ts_managername'])
the parameter is the
$arr_fobg_teams[1]['ts_man.agername']
ASKER
Thanks for replying. I'll need a little while to sort things out, I simplified my code for the example!
ASKER
I'm actually using array_walk for a one dimensional array (a single row of results).
ASKER
OK here's the actual code
but later when I want to display $arr_existing_fixture['fr_ alt_venue' ] in the form I see only St Jude
// Ensure we get one row at the most by LIMIT 1.
$sql = sprintf("SELECT id AS fr_fixture_id, fr_season_lit, fr_season_start, fr_match_date, fr_team_code, fr_compdiv,
fr_othercomp, fr_opposition, fr_otheroppo, fr_home_away, fr_venue, fr_alt_venue, fr_mapref, fr_kickoff, fr_meet,
fr_referee, fr_assistants, fr_double_banker, fr_postponed_flag, fr_postponed_reason, fr_instructions, fr_manager_contact,
comp_code, comp_competition, comp_division, G.postcode
FROM fixtures_results
LEFT JOIN competitions ON fr_compdiv = comp_code AND fr_team_code = comp_fobg_team_code
LEFT JOIN grounds G ON fr_venue = G.name
WHERE fr_team_code = '%s'
AND fr_match_date = '%s' AND fr_double_banker <> 2
ORDER BY fr_team_code, id LIMIT 1 ", $side,$_GET['day']);
$arr_existing_fixture = $dbi->db_select_single_row($sql,'Error occurred with existing fixture');
if ($arr_existing_fixture)
{
echo "<br/>Before...".$arr_existing_fixture['fr_alt_venue']; // Shows St Jude's
array_walk($arr_existing_fixture, 'html');
echo "...and then after...".$arr_existing_fixture['fr_alt_venue']; // Also shows St Jude's
$exist_comp_code = $arr_existing_fixture['comp_code'];
$exist_comp_competition = $arr_existing_fixture['comp_competition'];
$exist_comp_division = $arr_existing_fixture['comp_division'];
}
else
{
// no record found...
}
but later when I want to display $arr_existing_fixture['fr_
ASKER
Using my function, but doing it individually, works OK:
echo "<br/>Before...".$arr_existing_fixture['fr_alt_venue'];
$arr_existing_fixture['fr_alt_venue'] = html($arr_existing_fixture['fr_alt_venue']);
echo "...and then after...".$arr_existing_fixture['fr_alt_venue'];
Could you post the :
print_r($arr_existing_fixture);
ASKER
Array
(
[fr_fixture_id] => 6940
[fr_season_lit] => 2017-18
[fr_season_start] => 2017
[fr_match_date] => 2017-09-18
[fr_team_code] => 3
[fr_compdiv] => Other
[fr_othercomp] => Beer Cup
[fr_opposition] => Other
[fr_otheroppo] => St Trinian's
[fr_home_away] => A
[fr_venue] => Other
[fr_alt_venue] => St Jude's
[fr_mapref] =>
[fr_kickoff] => 10:00am
[fr_meet] => 9:00am
[fr_referee] =>
[fr_assistants] =>
[fr_double_banker] => 0
[fr_postponed_flag] => 0
[fr_postponed_reason] => 0
[fr_instructions] => ALL PLAYERS TO CONFIRM BY TEXT BY FRIDAY.
[fr_manager_contact] => Joe Bloggs
[comp_code] => Other
[comp_competition] => Other
[comp_division] =>
[postcode] =>
)
(
[fr_fixture_id] => 6940
[fr_season_lit] => 2017-18
[fr_season_start] => 2017
[fr_match_date] => 2017-09-18
[fr_team_code] => 3
[fr_compdiv] => Other
[fr_othercomp] => Beer Cup
[fr_opposition] => Other
[fr_otheroppo] => St Trinian's
[fr_home_away] => A
[fr_venue] => Other
[fr_alt_venue] => St Jude's
[fr_mapref] =>
[fr_kickoff] => 10:00am
[fr_meet] => 9:00am
[fr_referee] =>
[fr_assistants] =>
[fr_double_banker] => 0
[fr_postponed_flag] => 0
[fr_postponed_reason] => 0
[fr_instructions] => ALL PLAYERS TO CONFIRM BY TEXT BY FRIDAY.
[fr_manager_contact] => Joe Bloggs
[comp_code] => Other
[comp_competition] => Other
[comp_division] =>
[postcode] =>
)
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Isn't that your code? Here's mine:
<input id='otherVenue<?=$side?>' name='otherVenue<?=$side?> ' type='text' value='<?=$arr_existing_fi xture['fr_ alt_venue' ]?>'/>
<input id='otherVenue<?=$side?>' name='otherVenue<?=$side?>
ASKER
This is what I am thinking...from the manual, my html function needs another parameter, not just the array, but I still don't know how to code it.
Description ¶
bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )
Applies the user-defined callback function to each element of the array array.
array_walk() is not affected by the internal array pointer of array. array_walk() will walk through the entire array regardless of pointer position.
Parameters ¶
array
The input array.
callback
Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.
Note:
If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.
Description ¶
bool array_walk ( array &$array , callable $callback [, mixed $userdata = NULL ] )
Applies the user-defined callback function to each element of the array array.
array_walk() is not affected by the internal array pointer of array. array_walk() will walk through the entire array regardless of pointer position.
Parameters ¶
array
The input array.
callback
Typically, callback takes on two parameters. The array parameter's value being the first, and the key/index second.
Note:
If callback needs to be working with the actual values of the array, specify the first parameter of callback as a reference. Then, any changes made to those elements will be made in the original array itself.
Change to :
<input id="otherVenue<?=$side?>" name="otherVenue<?=$side?>" type="text" value=" <?=$arr_existing_fixture['fr_alt_venue']?> "/>
ASKER
My apologies. So where was it tripping up exactly?
ASKER
Brilliant, I appreciate your time on this. Thank you.
Check your HTML code and specially the input elements.At the value attr you must have double quotes to echo the php variables.
ASKER
Perfect, I see now.
Open in new window