1

I have a csv file containing attributes of the stores (id, name, category, featured, etc) to be displayed in a project. For now I need to display an array of featured stores with the condition 'featured'='TRUE'. There are 10 results.

Here's the code to read the file and save the data as an associative array

  function read_all_stores() {
  $file_name = 'csv_files/stores.csv';
  $fp = fopen($file_name, 'r');
  $first = fgetcsv($fp); // get the first row aka headlines of the file
  $stores = [];
  while ($row = fgetcsv($fp)) {
    $i = 0;
    $store = [];
    foreach ($first as $col_name) {
      $store[$col_name] =  $row[$i];
      $i++;
    }
    $stores[] = $store;
  }
  return $stores;
}

sample result of the first 5 stores

Now I want to display only the stores that has attribute featured = 'TRUE'. I tried this code:

function get_store() {
    $stores = read_all_stores();
    $feature = [];
    foreach ($stores as $s) {
      while ($s['featured'] == 'TRUE') {
        $feature[] = $s;
        return $feature;
      }
    }
    return false;
  }

But it only returns one result.

I tried removing the single quotation mark but it seems to only accept the 'TRUE' value as string instead of boolean. How can I fix this foreach loop??

1
  • Is this line causing the problem? $stores[] = $store;. Also, in get_store method you are returning as soon as there is a match. I think you should add all of the matched ones into an array and then return at the end of the method. Commented May 24, 2021 at 3:54

2 Answers 2

1

Your problem is that as soon as you find a matching result: $s['featured'] == 'TRUE', you return it: return $feature;. Instead, you need to process all values in $stores before returning your result. If there are matching stores (count($feature) is non-zero i.e. truthy), return them, otherwise return false.

function get_store() {
    $stores = read_all_stores();
    $feature = [];
    foreach ($stores as $s) {
        if ($s['featured'] == 'TRUE') {
            $feature[] = $s;
        }
    }
    return count($feature) ? $feature : false;
}
Sign up to request clarification or add additional context in comments.

2 Comments

How is this different from the comment added before?
@fiveelements comments are not answers. You had the option to add an answer but chose to make a comment which really didn't provide much detail. I gave OP an answer to their question.
0

Two problems in your code:

  1. In get_store() method you are returning as soon as you find a match. Instead you should add all the matched ones and then return at the end.
  2. For checking a match you should use if instead of while

Here is the modified version of your code:

<?php
function read_all_stores() {
    $file_name = 'stores.csv';
    $fp = fopen($file_name, 'r');
    $first = fgetcsv($fp); // get the first row aka headlines of the file
    $stores = [];
    while ($row = fgetcsv($fp)) {
        $i = 0;
        $store = [];
        foreach ($first as $col_name) {
        $store[$col_name] =  $row[$i];
        $i++;
        }
        $stores[] = $store;
    }
    return $stores;
}
function get_store() {
    $stores = read_all_stores();
    $feature = [];
    foreach ($stores as $s) {
        if ($s['featured'] == 'TRUE') {
        $feature[] = $s;
        }
    }
    return $feature;
}

echo count(get_store());

1 Comment

thank you it works now!! also in which situation should i use while and if?

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.