Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632223
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632236
Share on other sites

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 "";

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632342
Share on other sites

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?

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632348
Share on other sites

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?

 

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632357
Share on other sites

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>

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632366
Share on other sites

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.

Link to comment
https://forums.phpfreaks.com/topic/122438-is-this-possible/#findComment-632423
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

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.

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