Jump to content

If statement and count


gsencan

Recommended Posts

              $count = 0;

foreach($myArray as $date => $values){

if($count >= 20) break;

echo 'blablabla' ;

              $count++;}

 

i am using $date as a key,

and i want to start counter if $date is bigger then 20

php operates in order so i guess i have to put something before $count command so

it must be something like this :

 

              if ($date >=20) {$count = 0;}

foreach($myArray as $date => $values){

if($count >= 20) break;

echo 'blablabla' ;

              $count++;}

all suggestions are welcome.

Link to comment
https://forums.phpfreaks.com/topic/245686-if-statement-and-count/
Share on other sites

man, I can think of 200 names you could give that variable, none of which is $date...

 

why not sort the array by key ($date), and use a for loop with the range of numbers you want? (you're not extracting anything from the array, so I don't really understand what your final goal is though.)

 

$count = 20;
$startNumber = 21;
for($i=$startNumber;$i<=($startNumber+$count;$i++){
echo 'blablabla';
}

 

Well yes i should name it as $ID

 

 

  <?php

$file = file_get_contents('TEXT.txt');

$lines = explode("\n",$file);

$myArray = array();

foreach($lines as $line){

$fields = explode(",",$line);

              $date = array_pop($fields);

$myArray[$date] = $fields;}

ksort($myArray);

for ($count = 0; $count >= 20;) ; <= Something like this?

              foreach($myArray as $date => $values){

if($count >= 20) break;

                            echo 'blablabla';

$count++;}

?>

for ($count = 0; $count >= 20;) ;  <= Something like this?

 

well no. Something like the example I posted. Your original question was that you wanted to skip anything with and ID bellow 20.. right?

 

Tell me exactly what your final goal is. (reading a file and then displaying 20 lines at a time?)

ok... a few things to consider:

 

file will do the same as file_get_contents + explode (reads file directly into an array)

 

when doing things this way, you're reading the entire file every time, and only displaying 20 lines at a time. A bit of a waste of resources, though nothing serious. Depending on the size of the file, you could just read it into a $_SESSION variable (so you only read it once and it will be available on every page) or you should consider a database if you're expecting large amounts of data. this example uses a session, so I'm assuming the file will have less that 1000 lines (?)

 

(just typed this off the top of my head, it's untested so there might be a typo or two)

<?php
// start session
session_start();
// load file into session if it does not exist:
if(!isset($_SESSION['fileContents'])){
$_SESSION['fileContents'] = file('filename.txt');
}
// define start
$start = isset($_GET['start']) ? $_GET['start'] : 0;
// choose number of records per page
$itemsPerPage = 20;
// show items
for($i=$start;$i<=($start+$itemsPerPage);$i++){
echo $_SESSION['fileContents'][$i].'<br />';
}
// show next & previous buttons:
if($start > 0){
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.($start-$itemsPerPage).'"><prev</a>  ';
}
if(($start+$itemsPerPage) < sizeof($_SESSION['fileContens'])){
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.($start+$itemsPerPage).'">next></a>';
}
?>

just found a typo in my last code. (last 3 lines) where it reads:

 

if(($start+$itemsPerPage) < sizeof($_SESSION['fileContens'])){
echo '<a href="'.$_SERVER['PHP_SELF'].'?start='.($start+$itemsPerPage).'">next></a>';
}

 

$_SESSION['fileContens'] should be $_SESSION['fileContents'] (contents was misspelled)

 

this code I gave you should read your file, 20 lines at a time, and also create links to go to previous and next page (if there are no more typos)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.