Jump to content

If statement and count


gsencan
 Share

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
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';
}

 

Link to comment
Share on other sites

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++;}

?>

Link to comment
Share on other sites

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?)

Link to comment
Share on other sites

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>';
}
?>

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

This thread is more than a year old.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • 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.