Here is my code..
<?php
$files = scandir('audio');
$files = array_slice($files, 2);
$files = array_combine(range(1, count($files)), $files);
foreach ($files as $file) {
$count =0;
echo ++$count . " ";
echo rtrim($file, ".mp3 ");
{ ?>
<br><audio src="audio/<?php echo rtrim($file, " "); ?>" controls="controls"></audio><br>
<?php }
echo "<hr>";
}
?>
Part of that code is
$count =0;
echo ++$count . " ";
As I currently have 4 files in my directory, I was expecting this to echo 1,2,3,4 but instead I got 1,1,1,1
I have also tried the other way round with $count++ (for post/pre) but this gives me 0,0,0,0 what am I doing wrong, please?
Move your count variable out of the for loop:
$count =0;
foreach ($files as $file) {
echo ++$count . " ";
echo rtrim($file, ".mp3 ");
}
Related
I'm doing a code based on two .txt, one with names and the other with birthdays. I'm reading them and when the day date coincides with the date.txt, the name in the coincident line of the names.txt will be shown, but when I do the comparison, only the last line of the names.txt appears.
That's the code:
<?php
$nome = fopen("nome.txt", "r");
while(!feof($nome)){
$pessoa = fgets($nome);
} fclose($nome);
$current = date("d-m");
$content = fopen("data.txt", "r");
while (!feof($content)){
$linha = fgets($content);
if (strtotime($linha) == strtotime($current)) {
echo $pessoa;
echo '<br>';
}
} fclose($content);
?>
Content of the .txt:
nome.txt:
teste
teste1
teste2
teste3
data.txt:
12-12
18-12
12-12
12-12
You can process line by line from both files at the same time
<?php
$nome = fopen("nome.txt", "r");
$content = fopen("data.txt", "r");
$current = date("d-m");
while(!feof($nome) && !feof($content)){
$pessoa = fgets($nome);
$linha = fgets($content);
if (trim($current) == trim($linha)) {
echo $pessoa;
echo '<br>';
}
}
fclose($content);
fclose($nome);
?>
Or you can read entire file into an array using file function but this may be slower
<?php
$nome = file("nome.txt");
$content = file("data.txt");
$current = date("d-m");
foreach($content as $key => $linha)
if (trim($current) == trim($linha)) {
echo $nome[$key];
echo '<br>';
}
?>
You can open the files and load them in to arrays and use foreach with $key to output them in sync.
$names = explode(PHP_EOL,file_get_contents("none.txt"));
$dates = explode(PHP_EOL,file_get_contents("data.txt"));
Foreach($names as $key => $name){
Echo $name . " " . $dates[$key] . "\n";
}
https://3v4l.org/59ao6
Another way is to combine the two arrays in to one.
This however has a flaw, you can not have two people with the same name.
$days = array_combine($names, $dates);
// Days is now an associate array with name as key and date as birthday.
Foreach($days as $name => $date){
Echo $name . " " . $date . "\n";
}
I have 2 arrays: post_titles and posts. How do I print them one after another with foreach?
When I use 1 array, it works fine:
<?php foreach ($titles as $row) { ?>
<?php echo $row['post_title'] ?> <br>
<?php } ?>
I want data to be printed like this:
Title
Post
<br>
Title
Post
<br>
etc.
If each item in the titles and post correspond to each other (eg titles[1] and posts[1], titles[2] and posts[2]), you could use a for loop.
eg.
for($i = 0; $i < count($titles); $i++) {
echo $titles[$i];
echo $posts[$i];
echo "<br>";
}
or
foreach ($titles as $i => $value ){
echo $value ." <br>" . $posts[$i] . " <br>";
}
If the array have the same index key you can use this
<?php
foreach($titles as $key=> $value) {
echo $value . ' - ' $post[$key] . '<br>';
}
?>
If the 2 arrays are in sync i.e. the same length and arr1(0) equates to arr2(0) then its easy
<?php
foreach ($post_titles as $i => $title) {
echo $title . '<br>' . $posts[$i] . '<br>';
}
?>
Please have a look at: http://codepad.org/uNlMsvwj for the json.
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
$i = 0;
foreach($json->response->results as $item){
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " "; echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/"; echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
$i++;
}
The problem is that only the first elemet is printed. If i change $i manually with 1 i get the second.
I've been looking for 3 hours now and can't find a solution. Please forgive me if it is a beginner mistake.
Thanks
Update:
thank you all for your fast help
Did you mean to iterate over the results like so?
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
Also, you can simplify your echo statements:
foreach($json->response->results as $item) {
for ($i = 0; $i < count($item->entries); $i++) {
echo $item->entries[$i]->displayname.'<br>';
echo $item->entries[$i]->location->street.' ';
echo $item->entries[$i]->location->streetnumber.'<br>';
echo $item->entries[$i]->location->zipcode.' ';
echo $item->entries[$i]->location->city.'<br><br>';
echo $item->entries[$i]->phonenumbers[0]->area.'/';
echo $item->entries[$i]->phonenumbers[0]->number.'<br>';
}
}
Try this one,it works I tested it:
foreach($json->response->results[0]->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}
You should echo like this:
echo $item->entries[$i]->displayname . "<br>";
the . concatenates strings. Also echo doesn't use any ().
Change
foreach($json->response->results as $item){
to
foreach($json->response->results->entries as $item){
and these lines
echo($item->entries[$i]->displayname);echo "<br>";
to
echo $item->displayname . "<br>";
So your code will look like:
foreach($json->response->results->entries as $item){
echo "{$item->displayname}<br />";
echo "{$item->location->street} {$item->location->streetnumber}<br />";
echo "{$item->zipcode}<br />";
echo "{$item->location->city}<br /><br />";
echo "{$item->phonenumbers[0]->area}/{$item->phonenumbers[0]->number}<br />";
}
Try either for-loop instead of foreach:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results->entries as $item){
echo($item->displayname);echo "<br>";
echo($item->location->street);echo " ";
echo($item->location->streetnumber);echo "<br>";
echo($item->location->zipcode);echo " ";
echo($item->location->city);echo "<br>";echo "<br>";
echo($item->phonenumbers[0]->area);echo "/"; echo($item->phonenumbers[0]->number);echo "<br>";
}
...or include a for-loop inside the foreach:
$json = json_decode($raw_json);
//print_r($json);
$count = count($json->response->results);
foreach($json->response->results as $item){
for ($i = 0; $i < count($item->entries); $i++) {
echo($item->entries[$i]->displayname);echo "<br>";
echo($item->entries[$i]->location->street);echo " ";
echo($item->entries[$i]->location->streetnumber);echo "<br>";
echo($item->entries[$i]->location->zipcode);echo " ";
echo($item->entries[$i]->location->city);echo "<br>";echo "<br>";
echo($item->entries[$i]->phonenumbers[0]->area);echo "/";
echo($item->entries[$i]->phonenumbers[0]->number);echo "<br>";
}
}
I am using PDO to get result from database and then using foreach loop to display data.
my code is
$result1=$objPage->getGallery($id);
foreach($result1 as $row)
{
echo "pic1=" . $row['pic'];
echo "pic2=" . $row['pic'];
echo "pic3=" . $row['pic'];
echo "<br>";
}
Actually, I want to display three pictures names in one line and then next 3 names.
But now its printing one name 3 times.
Do something like this:
$result1=$objPage->getGallery($id);
$count = 0;
foreach($result1 as $row)
{
echo "pic" . $count + 1 . "=" . $row['pic'];
if( $count % 3 == 2 )
{
echo "<br>";
}
$count++;
}
Information
Modulo %
first, prepare your data
$data = $objPage->getGallery($id);
$data = array_chunk($data,3);
then output it
<table>
<? foreach($data as $chunk): ?>
<tr>
<? foreach($chunk as $row): ?>
<td><img src="<?=$row['pic']?>"></td>
<? endforeach ?>
</tr>
<? endforeach ?>
</table>
You can use an external flag, like this:
$flag = 0;
$result1=$objPage->getGallery($id);
foreach($result1 as $row) {
echo "pic" . ($flag + 1) . "=" . $row['pic'];
if( $flag % 3 == 0 )
echo "<br>";
$flag++;
}
You could use modulo as suggested by others, but how about this more compact method?
$result1 = $objPage->getGallery($id);
$separators = array('', '', '<br/>');
foreach ($result1 as $index => $row) {
echo 'pic' . ($index % 3) . '=' . $row['pic'] . $separators[$index % 3];
}
This is part of the working code that shows the entire array;
$files = filelist("./",1,1); // call the function
shuffle($files);
foreach ($files as $list) {//print array
echo "<h4> " . $list['name'] . " </h4>";
// echo "Directory: " . $list['dir'] . " => Level: " . $list['level'] . " => Name: " . $list['name'] . " => Path: " . $list['path'] ."<br>";
How do I modify it so that it only displays 10 or 15 list instead of all?
Use a counter to limit the number of iterations:
$counter = 0;
foreach ($files as $list) {//print array
// your loop code here...
$counter++;
if ($counter > 10) break;
}
If you know the keys or indexes of the array you can do what KingCrunch is doing a lot faster by simple for loop
for($i=0; $i<=14; $i++) {
// echo $file[$i];
}
There is a function for it
foreach(array_slice($files, 0, 15) as $file) {
/* your code here */
}
http://php.net/array-slice
Another solution is to use array_rand() instead of shuffle() and array_chunk()
foreach (array_rand($files, 15) as $key) {
$file = $files[$key];
// Your code here
}
http://php.net/array-rand
Note, that this keeps the order of the keys (see salathes comment below).