-1

I have an array containing attributes of each store (this is just the first few kv pairs, there're 100 in total)

Array
(
    [0] => Array
        (
            [id] => 1
            [name] => 21st Century Fox 3
            [category_id] => 1
            [created_time] => 2018-06-03 11:01:54Z
            [featured] => FALSE
        )

    [1] => Array
        (
            [id] => 2
            [name] => 21st Century Fox 5
            [category_id] => 1
            [created_time] => 2021-05-02 11:22:04Z
            [featured] => FALSE
        )

    [2] => Array
        (
            [id] => 3
            [name] => Carrefour 3
            [category_id] => 1
            [created_time] => 2018-10-20 22:28:48Z
            [featured] => FALSE
        )

    [3] => Array
        (
            [id] => 4
            [name] => Coca-Cola Company 2
            [category_id] => 1
            [created_time] => 2020-09-19 19:14:29Z
            [featured] => FALSE
        )
    [4] => Array
        (
            [id] => 5
            [name] => Erickson
            [category_id] => 1
            [created_time] => 2021-01-23 00:58:07Z
            [featured] => FALSE
        )

    [5] => Array
        (
            [id] => 6
            [name] => Boeing 1
            [category_id] => 2
            [created_time] => 2019-11-26 11:17:17Z
            [featured] => FALSE
        )

and a select box (attribute name="letter-start") with each option as a character in the alphabet.

The goal is when I choose from the select box, it will display only the stores whose names are similar to that letter from the select option. For example, I choose "A" and it will only display the stores with [name] starting with A:

Array
(
    [0] => Array
        (
            [id] => 12
            [name] => AECOM 1
            [category_id] => 3
            [created_time] => 2020-12-29 12:57:25Z
            [featured] => FALSE
        )

    [1] => Array
        (
            [id] => 20
            [name] => Areon Impex 1
            [category_id] => 3
            [created_time] => 2019-11-09 22:43:32Z
            [featured] => TRUE
        )

    [2] => Array
        (
            [id] => 21
            [name] => Amazon.com 2
            [category_id] => 4
            [created_time] => 2019-10-19 21:30:39Z
            [featured] => FALSE
        )

Here's what I tried:

echo "<div class=\"flex-container\">";
$selected = $_GET['first-letter'];
foreach ($stores as $s) {
  if ($selected === $s['name'][0]) {
  $id = $s['id'];
  $name = $s['name'];
  //display the stores
  echo "<div class=\"item\"><a href=\"store-home.php?id=$id\"><div class=\"image\"><img src=\"images/store.png\" alt=\"a store\"></div><h3 class=\"name\">$name</h3></a></div>";
  }
}
echo "</div>";

But it doesn't receive the $_GET or $_POST value. You can refer to the full store list here and the function to read and store all data into array $stores here.

Edit: here's the select box

<form action="browse-store-1.php">
 <label for="letter-start">Store names start with letter </label>
 <select id="letter-start" name="letter-start">
  <option id="all">All</option>
  <option id="#" value="#">#</option>
  <option id="a" value="A">A</option>
  <option id="b" value="B">B</option>
  <option id="c" value="C">C</option>
  <option id="d" value="D">D</option>
  <option id="e" value="E">E</option>
  <option id="f" value="F">F</option>
  <option id="g" value="G">G</option>
  <option id="h" value="H">H</option>
  <option id="i" value="I">I</option>
  <option id="j" value="J">J</option>
  <option id="k" value="K">K</option>
  <option id="l" value="L">L</option>
  <option id="m" value="M">M</option>
  <option id="n" value="N">N</option>
  <option id="o" value="O">O</option>
  <option id="p" value="P">P</option>
  <option id="q" value="Q">Q</option>
  <option id="r" value="R">R</option>
  <option id="s" value="S">S</option>
  <option id="t" value="T">T</option>
  <option id="u" value="U">U</option>
  <option id="v" value="V">V</option>
  <option id="w" value="W">W</option>
  <option id="x" value="X">X</option>
  <option id="y" value="Y">Y</option>
  <option id="z" value="Z">Z</option>
 </select>
</form>
9
  • what exactly do you mean when you say: "But it doesn't receive the $_GET or $_POST value." ? Commented May 31, 2021 at 17:51
  • @berend I tried to echo a sentence when $_GET['first-letter'] receives a value yet nothing appeared. so I assumed that the selection did not provide any value Commented May 31, 2021 at 17:57
  • when I choose from the select box, it will display How do you fetch the data ?. Share that code Commented May 31, 2021 at 18:03
  • @IndraKumarS I'm quite new to php, besides setting method="get" and name="first-letter" I don't understand much what to do next with the form Commented May 31, 2021 at 18:09
  • Share that form Please Commented May 31, 2021 at 18:10

1 Answer 1

1

You need to submit your form for it to get processed...

elements of type submit are rendered as buttons. When the click event occurs (typically because the user clicked the button), the user agent attempts to submit the form to the server. source: MDN

Form with a submit button:

<form method="get" action="browse-store-1.php">
 <label for="letter-start">Store names start with letter </label>
 <select id="letter-start" name="letter-start">
  <option id="all">All</option>
  <option id="#" value="#">#</option>
  <option id="a" value="A">A</option>
  <option id="b" value="B">B</option>
  <option id="c" value="C">C</option>
...
  <option id="z" value="Z">Z</option>
 </select>
 <input type="submit" name="submit">
</form>

Form processing in browse-store-1.php:

<?php
if(isset($_GET['submit'])) {
    echo "<div class=\"flex-container\">";
    $selected = $_GET['letter-start'];
    foreach ($stores as $s) {
        if ($selected === $s['name'][0]) {
            $id = $s['id'];
            $name = $s['name'];
            //display the stores
            echo "<div class=\"item\"><a href=\"store-home.php?id=$id\"><div class=\"image\"><img src=\"images/store.png\" alt=\"a store\"></div><h3 class=\"name\">$name</h3></a></div>";
        }
    }
}
Sign up to request clarification or add additional context in comments.

Comments

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.