Skip to content
Advertisement

How can I make this list working with blank fields showing in it?

Help me please to figure out a logical solution to make such player list working

I have an array filled with data

player_number player_name
-------------|-------
1            | player1
2            | player2
3            | player3
6            | player6
8            | player8
12           | player12
-------------|-------

So as you see some numbers may be missed but max player_number is always 50 so the question is how do I show all 50 fields though missing numbers also have to be shown but filled with “-” on its field

my logical problem is that my_array[$i] has it like 1 to 6(with list shown above) so my for() cycle just cant run it like that my_array[$i] == player_number

i want to have it shown like that on the page

1            | player1
2            | player2
3            | player3
4            | -
5            | -
6            | player6
7            | -
8            | player8
9            | -
10           | -
11           | -
12           | player12
-------------|-------

edit:

Its ok when I use the code provided but when I try to implement it with mine the logic fails

    HowIGrabMyData() {
        $query = $this->db->query($sql); // "SELECT * from `players` etc...

        $output = array();

        foreach ( $query->rows as $result ) {
            $output[] = array(
                'player_number' => $result['player_number'],
                'player_name' => $result['player_nick']
            );

        }

        return $output;
    }

    $players = HowIGrabMyData();

    <?php for($i=1; $i <= 50; $i++){ // loop for 50 times
        $player_name = "-";

        if(isset($players[$i]['player_name'])){ // if the player name is set, use it
            $player_name = $players[$i]['player_name'];
        }
        echo $i. " | ".$player_name."<br>";
    }

It still shows like 1 to 6 rows in one line and not the way I want it with blank ones all over 50 fields I still miss main logic…

Advertisement

Answer

NEW ANSWER.

I have updated my answer with your new array format.

This time we create an array with 50 indexes all set to ‘-‘. we then go over the out put array and put the player names in the correct spot in the array that has the 50 indexes. Finally we go over the 50 indexes and print them to the screen.

Here is the code:

   <?php
$output= array(
    array(
        "player_number" => 1,
        "player_name"=> "player1"
    ),
    array(
        "player_number" => 2,
        "player_name"=> "player2"
    ),
    array(
        "player_number" => 3,
        "player_name"=> "player3"
    ),
    array(
        "player_number" => 6,
        "player_name"=> "player6"
    ),
    array(
        "player_number" => 8,
        "player_name"=> "player8"
    ),
    array(
        "player_number" => 12,
        "player_name"=> "player12"
    ),
);

$player_array = array();

// Create an array with 50 indexes all set to '-'
for($i=1; $i <= 50; $i++){ // loop for 50 times
    $player_array[$i]= "-";

}

// update the player array with names;
foreach($output as $value){
    $player_array[$value['player_number']] = $value['player_name'];
}

// print out numbers and  names
foreach($player_array as $number => $name){

    echo $number. " | ".$name."<br>";

}

OLD ANSWER

You have to create a for loop that runs 50 times, check to see if there is a value for the index in the array and if there is use the name at that index. Otherwise it will just use – as the player name.

Try this:

<?php   
$my_array= array(
    1 => "player1",
    2 => "player2",
    3 => "player3",
    6 => "player6",
    8 => "player8",
    12 => "player12",

);  

for($i=1; $i <= 50; $i++){ // loop for 50 times
        $player_name = "-";
        if(isset($my_array[$i])){ // if the player name is set, use it
            $player_name = $my_array[$i];
        }
        echo $i. " | ".$player_name."<br>";
    }

?>

Check out isset() function.

you could also use array_key_exists() instead of isset(). Like this:

for($i=1; $i <= 50; $i++){
    $player_nick = "-";
    if(array_key_exists($i,$my_array)){ //if value of $i is an index in the array
        $player_nick = $my_array[$i];
    }
    echo $i. " | ".$player_name."<br>";
}

Also have a look at isset() vs array_key_exists(). Handy to know the difference between the two.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement