0

I'm stuck on this task - to divide the array into two and process it. The situation is as follows:

 $selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);

while ($row = mysqli_fetch_assoc($result)) {

    $this->buttons[] = [[

        "text" => $row["districtName"],

        "callback_data" => strval(0)
    ],[
        "text" => $row["districtName"],

        "callback_data" => strval(0)
    ]];
}`

As you can see, there are two identical arrays, each with 12 records. But I need to make it so that each has 6. Please, please, tell me how it can be done ><

I don't need duplicate arrays. I have list of 12 cities and I need to put them into Telegram inline_button, where should be 2 columns, each has 6 cities

Bipa
  • 271
  • 1
  • 3
  • 10
  • And what entries come into what array? – arkascha Mar 27 '18 at 21:55
  • what do you want your final result to look like? and "As you can see, there are two identical arrays", i cant really see – Ghostff Mar 27 '18 at 21:55
  • Why do you need duplicate entries? – Felippe Duarte Mar 27 '18 at 21:56
  • why you need to make it 6? is for rendering if yes it better to separate the render login from data source which is the array is this case ? – Mohd Alomar Mar 27 '18 at 21:57
  • Sorry, i poorly explained. I don't need duplicate arrays. I have list of 12 cities and I need to put them into Telegram inline_button, where should be 2 columns, each has 6 cities. Now i have 2 columns and each has 12 cities – Bipa Mar 27 '18 at 21:59

1 Answers1

1

You could fetch another time inside the while loop:

$selectCity = "select districtName from telegramCity limit 12 offset 0";
$result = mysqli_query($this->connection, $selectCity);

// 1st fetch
while ($row = mysqli_fetch_assoc($result)) {

    $data1 = [
        "text" => $row["districtName"],
        "callback_data" => strval(0)
    ];

    // prepare an empty array, in case of there is no more results
    $data2 = ['text'=>'', 'callback_data'=> strval(0)] ;

    // 2nd fetch, and put in $data2
    if ($row = mysqli_fetch_assoc($result)) {
      $data2["text"] = $row["districtName"];
    }

    // fill your final array using the 2 arrays
    $this->buttons[] = [$data1, $data2];
}

The array $this->buttons will contains 6 entries, maximum.

Syscall
  • 19,327
  • 10
  • 37
  • 52
  • that's exactly what I need :3 thanx – Bipa Mar 27 '18 at 22:03
  • can you, please, explain me more precisely how it works? I can't understand $data1. At start it has 12 entries, right? – Bipa Mar 27 '18 at 22:13
  • 1
    @Біпач When you fetch a row, you remove an entry from the data which come from the database. So, you fetch one time in the while loop, and another time inside the loop. You fetch 2 rows by loop. After the second fetch, the `$row` variable contains another element from the database. – Syscall Mar 27 '18 at 22:15
  • 1
    @Біпач `$data1` contains the data from the first `mysqli_fetch_assoc()`. `$data2` contains the data from the second `mysqli_fetch_assoc()`. When there are no more data inside the "rows", the loop stops, after 6 loops max. (2 fetchs per iterations). – Syscall Mar 27 '18 at 22:19
  • @Біпач Hope it is more clear. Do you want more details? – Syscall Mar 27 '18 at 22:20
  • Yep, now I understand – Bipa Mar 27 '18 at 22:32