incremented included php pages one by one - php

As you see I use this code to increment photos in a file, one by one :
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . '0' . $i . '.jpg"/></a></div>';
}
else
{
echo '<div class="photos"><a target="_blank"><img src="../photosfile/' . $nom . $i . '.jpg"/></a></div>';
}
}
?>
I would like to do the same but with php pages reading them from a specific directory like : ../phppages/
Any idea?
I tried but I missed something I guess and I would like to do it same way but with .php instead of .jpg.
Thank you very much.

Try this:
<?php
for ($i = $photonumber; $i >= 1; $i--) {
if($i < 10){
$i = "0".$i;
}
include("../phppages/page".$i.".php");
}
?>
Update: output by 3
<?php
$photonumber = 12;
for ($i = 0; $i < $photonumber; $i++) {
$j = $photonumber - $i;
if($j < 10){
$j = "0".$j;
}
if ($i % 3 === 0)
{
if ($i !== 0)
{
echo '</div>';
}
echo '<div>';
}
include("../phppages/page". $j.".php");
}
echo '</div>';
Update 2
To find the amount of files, that look like "page".$j.".php" in "../phppages" directory try this:
$files = glob("../phppages/page*.php");
$photonumber = count($files);

That would just be this:
for ($i = 1; $i <= $photonumber; $i++) {
include ('php-file-' . $i . '.php');
}

About having it 3 by 3, to be more precise, i want to create a fore each 3 .php file. Like
<?php
for ($i = 1; $i <= $photonumber; $i++) {
if($i < 10){
echo '<div>';
include("../phppages/page".$i.".php") x3;
echo '</div>';
}
?>
Then it become something like this :
div : 12.php + 11.php + 10.php , div : 09.php + 08.php + 07.php , div
:
06.php + 05.php + 04.php , div : 03.php + 02.php + 01.php

Related

Write a PHP code to print following number pattern

Write a PHP code to print following number pattern:
147
258
369
I am trying like this but its shows me shows below. how to convert column to row pattern.
<?php
$num = "";
for($i=1;$i<=9;$i++) {
$num .= $i;
if($i%3==0){
$num .="<br />";
}
}
echo $num;
?>
please help me
You need to use for loop inside for loop to achieve this. Below is the code
$num = "";
for( $i = 1; $i <= 3; $i++ ) {
for( $j = 0; $j <= 2; $j++) {
$k = $i + ( $j * 3 );
$num .= $k;
}
$num .= "<br />";
}
echo $num;
Here is another way to get this output.
for($i = 1; $i <= 3; $i++) {
$print = $i;
for($j = 1; $j <= 3; $j++) {
echo $print;
$print = $print + 3;
}
echo "<br />";
}

Create unlimited, nested list in PHP

I know, the headline is not very specific, but I dont't know how to title it.
I want to create a list from 1 to 10. All those numbers should be clickable and open a sublist. Again with the numbers from 1-10. Like this:
1
2
1
2
3
and so on...
Here is some code, I've got:
<?php
$num = $_GET['zahl'];
$zahlen = array();
while($num != 0){
$part = $num % 10;
array_push($zahlen, $part);
$num = floor($num/10);
}
foreach ($zahlen as $key => $value) {
$runs = $value + 1;
for ($i=1; $i < $runs ; $i++) {
if ($i == 1) {
echo $ulon . "\n";
}
echo $lion . "\n";
echo $a . "ordner.php?zahl=" . $i . $amiddle . $i . $aoff . "\n";
echo $lioff . "\n";
}
for ($i=1; $i < 11 ; $i++) {
if ($i == 1) {
echo $ulon;
}
echo $lion . "\n";
echo $a . "ordner.php?zahl=" . $zahlen[0] . $i . $amiddle . $i . $aoff;
echo $lioff . "\n";
}
echo $uloff;
echo "\n" . "</body>";
?>
What you wish to achieve should be fairly straigtforward with Javascript rather than PHP - the following uses PHP only to generate the initial list and subsequent sub-lists are generated using javascript.
<?php
echo "<ul id='infinite-menu'>";
for( $i=1; $i < 11; $i++ ){
echo "<li>$i</li>";
}
echo "</ul>";
?>
<script>
var children=10;
function newnodes(e){
if( e.target.childNodes.length==1 ){
var ul=document.createElement('ul');
e.target.appendChild(ul);
for( i=1; i < children+1; i++ ){
var li=document.createElement('li');
li.innerHTML=i;
ul.appendChild( li );
}
}
}
var col=document.querySelectorAll('ul#infinite-menu li');
if( col )for( n in col )if( col[n].nodeType==1){
col[n].addEventListener('click',newnodes,false);
}
</script>
This is my solution based on php. Thanks for your comments!
<body>
<?php
$open = isset($_GET["open"]) ? $_GET["open"] : "";
$openArr = explode(",",$open);
function liste($openArr, $link) {
$firstValue = array_shift($openArr);
echo "<ul>";
for ($i = 0; $i < 10; $i++) {
echo "<li>";
echo "<a href='rekursion.php?open=".$link.$i."'>$i</a>";
if (isset($firstValue) && $firstValue == $i) {
liste($openArr, $link.$i.",");
}
echo "</li>";
}
echo "</ul>";
}
liste($openArr,"");
?>
</body>

how to print this pattern in php?

I run this program, but I need the output within table. So would you please solve this?
<?php
$s="*";
for($b=1; $b<=5; $b++) {
for($c=5; $c>=$b-1; $c--) {
if($c>=$b) {
echo $s;
}
else if($b != 1) {
echo " ";
}
}
for($d=5; $d>=$b; $d--) {
echo $s;
}
echo "<br/>";
}
?>
If you're fine regardless of how the code was written (only results matter), you can use this:
print('<table>');
for ($i = 0; $i < 5; $i++)
{
print('<tr>');
for ($j = 1; $j <= 5; $j++)
{
print('<td>');
(5 - $i >= $j) ? print('*') : '';
print('</td>');
}
for ($j2 = 1; $j2 <= 5; $j2++)
{
print('<td>');
(1 + $i <= $j2) ? print('*') : '';
print('</td>');
}
print('</tr>');
}
print('</table>');
What I did there is I sliced table in half vertically and used 2 for loops to fill left and right halfs. You will get something like this:

Hackerrank draw a staircase of length N in php

Draw a staircase of height N like this:
#
##
###
####
#####
######
Staircase of height 6, note the last line should have zero spaces.
My solution does not work correctly
function draw($size)
{
for ($i = 1; $i <=$size ; $i++)
{
$spaces = $size-$i;
while ($spaces)
{
echo " ";
$spaces--;
}
$stairs = 0;
while ($stairs < $i)
{
echo "#";
$stairs++;
}
echo "<br/>";
}
}
draw(6);
//output
#
##
###
####
#####
######
It is not printing the spaces, I tried \n, PHP.EOL still it didn't work. Any suggestions?
Although other solutions are all good , here is my code as well.
$max=5;
for ( $i =1 ; $i<=$max;$i++) {
for ( $space = 1; $space <= ($max-$i);$space++) {
echo " ";
}
for ( $hash = 1; $hash <= $i;$hash ++ ) {
echo "#";
}
echo "\n";
}
//PHP
$n = 6; // Number of rows.
for($i=1;$i<=$n;$i++){
echo str_repeat(' ', $n-$i) . str_repeat('#', $i);
echo '\n';
}
for(var i = 0; i < n; i++)
{
var s = "";
for(var j = 0; j < n; j++)
{
if(n - i - 2 < j)
{
s += "#";
}
else
{
s += " ";
}
}
console.log(s);
}
for ($i=0; $i<$n; $i++){
for ($j=0; $j<$n; $j++){
if($i+$j>$n-2){
echo "#";
} else {
echo " ";
}
if($j==$n-1 && $i+$j<$n*2-2){ //The second part is to dont break the last line
echo "\n";
}
}
}
Here's another solution:
$int = 7;
for($i = 1; $i<=$int; $i++){
printf('%1$s%2$s%3$s',str_repeat(" ",$int-$i),str_repeat("#",$i),"\n");
}
From official PHP documentation:
str_repeat
$n = 6;
for ($i = 0; $i < $n; $i++) {
$pad = 1;
for ($space = 0; $space < $n-$i-1; $space++) {
$pad++;
}
echo str_pad('#', $pad,' ',STR_PAD_LEFT);
for ($j = 0; $j < $i; $j++) {
echo '#';
}
echo '<br>';
}
Took me a while but finally I manage to do it following OFC (A. Sharma) Example.
<?php
$handle = fopen("php://stdin","r");
$n = intval(fgets($handle));
for ($rows = 0; $rows < $n; $rows++) {
for ($columns = 0; $columns < $n - $rows - 1; $columns++) {
echo " ";
}
for ($columns = 0; $columns < $rows + 1; $columns++) {
echo "#";
}
echo "\n";
}
?>
Use PHP functions range() and str_repeat() for an elegant solution:
function staircase($n){
foreach (range(1, $n) as $i)
print( str_repeat(' ',$n-$i).str_repeat('#',$i)."\n");
}
demo
Check if n is between 0 and 101 ( 0 < n <= 100)
Loop through each row
2.1 print spaces according to the last item position
2.2 print the items
Separate rows
The code below explains everything...
function staircase($n) {
// check if n is between 0 and 101 (0 < n <=100)
if( 0 < $n && $n > 100 ) {
} else {
// Loop through each row
for($i = 1; $i <= $n; $i++) {
// print spaces according to the last item position
$si = 1;
while( $si <= ($n - $i)){
print(" ");
$si++;
}
// print the items
for($j = 1; $j <= $i; $j++) {
print("#");
}
// separate rows
print("\n");
}
}
}
Output: For n = 6
#
##
###
####
#####
######
After playing around with the code and trying/failing couple of times I finally got it right. Notice how in order to print the space and new line I am using "\n". Previous "<br/>" and " " for space didn't work.
Break line will come out of the loop every row number. So if we have $n=4 then every 4 spaces after break line will be echoed.
I have made 2 loops to fill in all fields in the staircase.
The tricky part here is to have them right aligned. This is where if statement comes in place.
Reference link:
Hackerrank Challenge
// Complete the staircase function below.
function staircase($n) {
for($i=1; $i<=$n; $i++){
for($j=1; $j <= $n; $j++){
if( ($n - $i) < $j ){
echo "#";
}else{
echo " ";
}
}
echo "\n";
}
}
Try This
$n=6;
for($i=1;$i<=$n;$i++){
for($spaces=1;$spaces<=($n-$i);$spaces++){
echo " ";
}
for($staires=0;$staires<$i;$staires++){
echo "#";
}
echo "\n";
}
This worked for me
$n = 6;
function staircase($n) {
for($i=1; $i <= $n; $i++){
for($j=1; $j <= $n; $j++){
if($j > $n-$i){
echo "#";
}else{
echo " ";
}
}
echo "\n";
}
}
Use print(' '), if you want to go to next line put print(' ')."\n"
JavaScript:
Solution:
function StairCase(n){
let x = [];
for(let i = 0; i<n; i++){
while(x.length < n){
x.push(" ");
}
x.shift();
x.push("#");
console.log(x.join(''));
} } //StairCase(6)

Each 3rd Increment Loop PHP

Hi im looking for a way to breakout of a loop every 3 increments to echo a static string.this script echos a "test" after each increment. i want a test after each 3rd increment. any ideas ?
my loop:
<?php
$i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i+3) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}
?>
try below Code
<?php
for($i=1;$i<=100;$i++)
{
if($i%3==0)
{
echo $i."test"."<br>";
}
}
?>
use % operator in if loop,
i = 0;
while (++$i < 100){
$x = $i - 3;
if ($i%3 == 0) {echo $i . "<br>TEST<br>";}
else{ echo $i . "<br>";}
}

Categories