Need to explode and create an unordered list inside a function

This is part of adding a shortcode for WordPress. In my query, the ‘players’ field will have 3 to 4 names in it. I know how to explode it and re-create the list as I want it, but not within this function for some reason.

It has to use Return.

function team_players( $atts, $content ) {

$a = shortcode_atts( array(
‘team’ => ‘’,
), $atts );
if ( empty ( $a[‘team’] )) {
return ‘Empty’;
}

$team = $a[‘team’];

		$query = "SELECT * FROM a_game_preview_players
		where teamID = '" . $team . "'";
		 
		$results = mysqli_query($con,$query);
		echo mysqli_error($con);
		while($row = mysqli_fetch_assoc($results)) {
		 
			return '<b>' . $row['school'] . '</b><br/>'
				. $row['players'];
			}

}
add_shortcode(‘players’, ‘team_players’);

I’ve tried several things, including using echo instead of return.

This code does not make sense. Also, you are not clear what you wish to create here.
Do you mean you wish to create an “ARRAY” of players? If so, in your query you would need to alter your WHILE loop to create the list. Let’s say you are using < UL > tags? And, each list item would then need to be inside of a < LI > tag. If that is what you are asking for, you could do it something loosely like this:

$ul_list = '<ul>';
while($row = mysqli_fetch_assoc($results)) {
      $ul_list .= '<li><b>' . $row['school'] . '</b></li><br/>' . $row['players'] . '</li>';
}
$ul_list .= '</ul>';

This would place all of your schools and players into the < UL > tag and then you can just echo it on the page where it is needed. Not sure this is really what you are asking about. The code you displayed is a jumble of things that do not explain your code or what you want. Let us know what you need. Good luck.

Sponsor our Newsletter | Privacy Policy | Terms of Service