gsencan Posted August 25, 2011 Share Posted August 25, 2011 $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. Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/ Share on other sites More sharing options...
WebStyles Posted August 25, 2011 Share Posted August 25, 2011 what exactly is in the variable $date? I'm assuming it's not a *real* date. Just a number? Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261875 Share on other sites More sharing options...
gsencan Posted August 25, 2011 Author Share Posted August 25, 2011 yes only numbers. Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261879 Share on other sites More sharing options...
WebStyles Posted August 25, 2011 Share Posted August 25, 2011 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'; } Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261880 Share on other sites More sharing options...
gsencan Posted August 25, 2011 Author Share Posted August 25, 2011 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++;} ?> Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261883 Share on other sites More sharing options...
WebStyles Posted August 25, 2011 Share Posted August 25, 2011 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?) Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261885 Share on other sites More sharing options...
gsencan Posted August 25, 2011 Author Share Posted August 25, 2011 Yes true i want to show 20 results in a page but with using $date as a key so i can choose it between => 20 and 40 or 40 and 60 Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261887 Share on other sites More sharing options...
WebStyles Posted August 25, 2011 Share Posted August 25, 2011 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>'; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261896 Share on other sites More sharing options...
gsencan Posted August 25, 2011 Author Share Posted August 25, 2011 Last code is a bit advanced for me but ty i will try both. Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261920 Share on other sites More sharing options...
WebStyles Posted August 25, 2011 Share Posted August 25, 2011 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) Quote Link to comment https://forums.phpfreaks.com/topic/245686-if-statement-and-count/#findComment-1261922 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.