I am using flat file db, not mysql so I can't use $limit

I need to limit the number of records to 1, if more than 1 then echos something else:

$result = $db->getall(month);
$i = 0;
foreach($result as $item){
     show_record($item);
     if($i>=2) break;
}

I tried to use 'break' when got to 2 records, but it does not work.

Any ideas?
Thanks

    That's because you initialize $i to 0 but never change it, so it's always going to be 0.

      Actually the opposite, it is listing all records, and what I want is to list only 1 record.

        I never said anything about records.

        You initialize a variable called $i to 0. You're checking it inside the loop, but you never alter its value, so it's always going to remain at 0.

        EDIT: Also, if you only want the first value, then why do you even have a loop in the first place?

          bradgrafelman;10984279 wrote:

          I never said anything about records.

          You initialize a variable called $i to 0. You're checking it inside the loop, but you never alter its value, so it's always going to remain at 0.

          EDIT: Also, if you only want the first value, then why do you even have a loop in the first place?

          For the past day I tried all possible combinations, but NONE worked, the code I posted here is incomplete, but I was hoping that someone with a little more knowledge using flat file db would complete it in a way that would work.

          I am using flat file to create landing pages, it is working perfectly until it gets to the option of showing the landing page. The user can create N amount of landings and each one created can be set to on or off, if only one is ON the result is OK, BUT if the user let multiple landings ON then I want to grab 1 with the closest expiration date from today - and it is not letting me do so.

          I really appreciate your input!

            As bradgrafelman says, just

            $result = $db->getall(month);
            show_record($item);
            

            wouldn't works?

              anoopmail;10984301 wrote:

              As bradgrafelman says, just

              $result = $db->getall(month);
              show_record($item);
              

              wouldn't works?

              it does BUT it is listing all records and not limiting to 1.

              I want it to look for a record that matches the if statement and stops the loop once it is satisfied. But I tried to use break; and I got a blank page. I also tried ($item[0]) and got the 1st record, and tried (end($item)) and got the last record.

              Nothing that I tried would give 1 record based on the if statement.

              After trying for the past two days and not getting anywhere, at this point I would be happy if I can echo an error message if the result is greater than 1.
              😕

                cchapoval wrote:

                I am using flat file db, not mysql so I can't use $limit

                Consider the use of SQLite instead.

                  I would suggest to upgrade to a database system. Menawhile you try,
                  replace

                    if($i>=2) break;
                  

                  with

                    if($i>=2) break;
                       $i++;
                  

                    @

                    anoopmail;10984364 wrote:

                    I would suggest to upgrade to a database system. Menawhile you try,
                    replace

                      if($i>=2) break;
                    

                    with

                      if($i>=2) break;
                         $i++;
                    

                    Still doesn't work, and I know why, but don't know how to solve, perhaps you know the solution:

                     $result = $db->getall(lp_month);
                     $i = 0;
                     foreach ($result as $item){
                          show_record($item);
                     if ($i >= 2) 
                     break;
                    $i++;
                     }

                    This works except for the break; - I need to explode the flat file to read each record individually, then add to the foreach(). Since this is flat file, when calling the function getall() the file comes flat as a single file, no matter how many records on it, records = 1 or records = 100 is the same at this point. Therefore break; does not work because all records come as single record - when breaking still show all.

                    What I want to do is explode the records, count them, and based on my if statement post:
                    if $i == 0 echo "content1";
                    if $i == 1 echo "content2";
                    if $i > 1 echo "content 3";

                    But here is the problem I can not call $file to read, it does not allow, I would need to use one of the functions included in the ffdb.inc.php (attached), but I literally tired them all, all I get is ARRAY and not the file itself.

                    I am so behing on my work, that at this point would be a lot work to convert to mysql, besides I have a few limitations would prevent to use...so I need to continue using FFDB, can you recommend anyone that could help with this? I just need to solve this to publish it, almost ready, getting frustrared.

                    Thanks a lot!!! 😉

                    I got held here for the past 3 days...exhausted all solutions.

                      @ - see if you can help me somehow, I am signning off now and will be back tomorrow morning (US time). I hope you can help me 🙂

                        I ddin't look into the file in detail. But I belive the problem is pulling out the data from file. So try,

                        Change

                        $result = $db->getall(lp_month);
                        

                        to

                        $result = file(lp_month);
                        

                          SOLUTION

                          Print-r did the trick, I was using echo and echo was not printing the array but the file.

                          print_r (show_record($row));

                          Here is the final code:

                          $result = $db->getall(lp_month,lp_year);
                          $i = 0;
                          foreach ($result as $row){
                          print_r (show_record($row));
                          if ($i >= 1)
                          break;
                          $i++;
                          }

                          Thanks

                            Write a Reply...