Jump to content

Is this possible?


al3x8730

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

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.