al3x8730 Posted September 2, 2008 Share Posted September 2, 2008 If I have an ordered list, is there any function or somehow that I can make something that will say like There are X Whatever the list is? Like something that will say the # of items on the list total. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/ Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 Is this a all html ordered list or do you make the list via PHP? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632195 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 Is this a all html ordered list or do you make the list via PHP? Via php. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632201 Share on other sites More sharing options...
discomatt Posted September 2, 2008 Share Posted September 2, 2008 How are you storing the data? In an array? <?php $arr = array( 'Item 1', 'Item 2', 'Item 3', 'Item 4' ); echo 'There are '. count( $arr ) .' items in the array'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632215 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 How are you storing the data? In an array? <?php $arr = array( 'Item 1', 'Item 2', 'Item 3', 'Item 4' ); echo 'There are '. count( $arr ) .' items in the array'; ?> It's just a listing of the files within a directory. I need something that will display the number of total files. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632223 Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 Well if it's done via PHP then you are using a loop right? So you should know how many times you've been through the loop? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632230 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 Well if it's done via PHP then you are using a loop right? So you should know how many times you've been through the loop? <?php echo "<ol>"; foreach(glob("*.php") as $val){ $file = basename($val, '.php'); echo "<li>$file</li>"; } echo "</ol>"; ?> That's the code I'm using to list the files, I want something that will say, There are X Files. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632236 Share on other sites More sharing options...
revraz Posted September 2, 2008 Share Posted September 2, 2008 Put a variable in that foreach loop, increment it, then echo it when you are done. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632262 Share on other sites More sharing options...
al3x8730 Posted September 2, 2008 Author Share Posted September 2, 2008 Put a variable in that foreach loop, increment it, then echo it when you are done. I know the foreach function, but how would I define "foreach loop"? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632338 Share on other sites More sharing options...
Zane Posted September 2, 2008 Share Posted September 2, 2008 I know the foreach function' date=' but how would I define "foreach loop"?[/quote'] the foreach loop is the foreach function. It is a loop not a function revraz is simple telling you to add a variable to your....for the sake of non-vocabulary......foreach function this variable would act as a counter. echo ""; $i = 0; //It begins at zero foreach(glob("*.php") as $val){ $file = basename($val, '.php'); echo "$file"; $i++; //At the end of each loop, this counter is incremented. } //Since you don't want to echo the number of results EVERY time.......put it outside the "loop" echo "$i results"; echo ""; Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632342 Share on other sites More sharing options...
al3x8730 Posted September 3, 2008 Author Share Posted September 3, 2008 I know the foreach function' date=' but how would I define "foreach loop"?[/quote'] the foreach loop is the foreach function. It is a loop not a function revraz is simple telling you to add a variable to your....for the sake of non-vocabulary......foreach function this variable would act as a counter. echo "<ol>"; $i = 0; //It begins at zero foreach(glob("*.php") as $val){ $file = basename($val, '.php'); echo "<li>$file</li>"; $i++; //At the end of each loop, this counter is incremented. } //Since you don't want to echo the number of results EVERY time.......put it outside the "loop" echo "<li>$i results</li>"; echo "</ol>"; What if I want to display the number above the other files? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632348 Share on other sites More sharing options...
DeanWhitehouse Posted September 3, 2008 Share Posted September 3, 2008 You would probably have to rethink your code logic, or change the html. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632353 Share on other sites More sharing options...
al3x8730 Posted September 3, 2008 Author Share Posted September 3, 2008 You would probably have to rethink your code logic, or change the html. Okay, thanks :| Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632354 Share on other sites More sharing options...
DeanWhitehouse Posted September 3, 2008 Share Posted September 3, 2008 If you show ure HTML i maybe able to help. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632356 Share on other sites More sharing options...
al3x8730 Posted September 3, 2008 Author Share Posted September 3, 2008 If you show ure HTML i maybe able to help. <?php echo "<br />"; echo "<ol>"; $i = 0; //It begins at zero foreach(glob("*.php") as $val){ $file = basename($val, '.php'); echo "<li>$file</li>"; $i++; //At the end of each loop, this counter is incremented. } //Since you don't want to echo the number of results EVERY time.......put it outside the "loop" echo "</ol>"; echo "There are ".$i." users in the database."; $i = $x; ?> That's it, just a simple file. I was thinking... Sense I'm measuring the number of files inside the directory, is there a way to just get the number of files inside the directory? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632357 Share on other sites More sharing options...
DeanWhitehouse Posted September 3, 2008 Share Posted September 3, 2008 Is the that the whole page code? Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632358 Share on other sites More sharing options...
al3x8730 Posted September 3, 2008 Author Share Posted September 3, 2008 Is the that the whole page code? Yea, I didn't even apply a template to this site yet, I'm just building it. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632360 Share on other sites More sharing options...
DeanWhitehouse Posted September 3, 2008 Share Posted September 3, 2008 ahh ok, well its not w3c complaint then, but the only way i can think of it would be using JS to move enter the number in a tag after. e.g. <?php echo "<span id='tag'></span> foreach(glob("*.php") as $val){ $file = basename($val, '.php'); echo "<li>$file</li>"; $i++; //At the end of each loop, this counter is incremented. } //Since you don't want to echo the number of results EVERY time.......put it outside the "loop" echo "</ol>"; ?> <script> var i = <?php echo "There are ".$i." users in the database."; ?> document.getElementById("tag").innerHTML = i; </script> Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632366 Share on other sites More sharing options...
Zane Posted September 3, 2008 Share Posted September 3, 2008 if you want the number results on top just put the li's in a variable instead echo ""; $i = 0; foreach(glob("*.php") as $val){ $file = basename($val, '.php'); $list .= "$file\n"; $i++; //At the end of each loop, this counter is incremented. } //then echo it afterwards as well echo "By god, there are $i results!...and here they are\n"; echo $list; echo ""; the only way i can think of it would be using JS There's no need for javascript. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632423 Share on other sites More sharing options...
DeanWhitehouse Posted September 3, 2008 Share Posted September 3, 2008 ahh ,well he has 2 ways now. Quote Link to comment https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632443 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.