How to display text lot of times on HTML and PHP - php

i am new in web development , im working now with Html and Php and i wanna know if there is a way i can display a text on a different number of times due to a variable , i have a variable $count , so for example if $count = 1 i want it to display the text one time like this :
<h1> This is a Text 1 </h1>
And if $count = 3 i want it to be like this :
<h1> This is a Text 1 </h1>
<h1> This is a Text 2 </h1>
<h1> This is a Text 3 </h1>
I'll be so glad to have your help please

You can use a loop , like this :
<?php
for ($i = 0; $i <= count; $i++) {
echo "<h1> This is a Text ".$i." </h1>";
}
?>

You can do it as follows:
<?php
$count = 3;
for ($i = 0; $i < $count; $i++) {
?>
<h1>This is a Text</h1>
<?php
}
?>

Related

generating more than one image with a for loop

I am new to the php gd library. I have developed code for generating an image with the gd library and the creation of the image is successful. I have tried to generate more images of same type with a for loop but when i tried to add a button near all the images it didn't fit on one image. It appears like this:
My code
<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><br><br>";
echo "<button>clickme</button>";
}
?>
I want to fit the button in all the 5 images ..Hope you guys can help me out
Thanks in advance..
<?php
$num = 5;
for ($j = 1; $j <= $num; $j++) {
echo "<img src=yeah.php /><button>clickme</button><br><br>";
}
?>
not sure if it is answer worthy..
this is just a basic html output you output a image then 2 linebreaks then your button
and repeated this process 5 times given the loop that is why you have the current final results.

PHP Array Looping & Exploding

I'm not very experienced in using PHP, but I'm having a hard time thinking of a way to do this. I'm reading from a file and exploding on ":" as you can see here.
<?php
$datainfo = file('data.txt');
for($i = 0; $i < count($datainfo); $i++){
$expdata = explode(':', $datainfo[$i]);
}
?>
The issue is that I need to reference specific indexes of the resulted explosion like this.
<p> <?php echo $expdata[1] ?> </p>
I'm getting back an array of the last line inside the data.txt file. I know why It's happening, I just don't know how to get what I want here. (Sorry Very New). Data.txt contain the following.
name:Octopod Juice Stand
balance:20
price:0.5
customers:12
starting:2014-05-26
end: 2014-09-01
juice:15.25
fruit:10
Change your code to
<?php
$datainfo = file('data.txt');
$expdata = array();
for($i = 0; $i < count($datainfo); $i++){
$expdata[] = explode(':', $datainfo[$i]);
}
?>
And then to get the first label.
<p><?php echo $expdata[0][0]; ?></p>
Or the first value
<p><?php echo $expdata[0][1]; ?></p>

split a string into two divs, and split inside the string php

I have a website on wordpress using ACF custom fields.
I have a custom field with around 100 lines of text.
a date and a text like this :
2003-01 PARIS RATP PHILIDOR
2003-01 BORDEAUX TRAMWAY
2003-02 ILE-DE-FRANCE ÉCOLE DES MINES
2003-05 PARIS CITROëN
2003-05 PARIS TRAVERSIÈRE
first of all I wanted to split the field (called "projets") in two separate divs, the first 50 lines in one div and the rest in another div :
here is the PHP code i'm using :
<div class="columns_projets_1">
<p><?php
$string = get_field('projets');
$array = explode("\n", $string);
for($i = 0; $i <50; $i++){
echo $array[$i];
}
?></p>
</div>
<div class="columns_projets_2">
<p><?php
$string = get_field('projets');
$array = explode("\n", $string);
for($i = 50; $i <count($array)-1; $i++){
echo $array[$i];
}
?></p>
</div>
It works perfectly !
but I would like for each line to get the date separatly for the text, to put the date in one div and the text in another one... like this :
<div class="date">2003-01</><div class="text">PARIS RATP PHILIDOR</div>
but I can't find how to code this, to have the first php code, and the next to split inside the line... I heard about substrings, I tried eveything but it's not working...
Can anybody help me ? would be really great !
I hope you can understand me !
thanks a lot
Mattieu
You can echo HTML too.
For instance:
for($i = 0; $i <50; $i++){
$dateAndText= explode(' ', $array[$i], 2);
echo '<div class="date">'.$dateAndText[0].'</div>';
echo '<div class="text">'.$dateAndText[1].'</div>';
}
Although a cleaner way to do this would be something like this:
<?php for($i = 0; $i <50; $i++):
$dateAndText = explode(' ', $array[$i], 2); ?>
<div class="date"><?php echo $dateAndText[0]; ?></div>
<div class="text"><?php echo $dateAndText[1]; ?></div>
<?php endfor; ?>
Just use:
$test = explode(' ', $array[$i], 2);
$test[0] will be with the date, and in $test[1] you will have the rest of line.
For more information:
http://php.net/manual/pl/function.explode.php

new array values on each submit php

Dont know much PHP and wonder how I can get the new values from an array each time I submit, I have a form with 5 fields, the form sends the information to a txt-file then I want to take only the two first items each time the form submits and display it in a list item, but cant seem to figure out how the loop should work? any tips please?
For now I have this and it successfully prints out the fist textblock in the txt-file, but when I submit the form again nothing happens, what is wrong?
$myFile = 'demo.txt';
$content = file_get_contents('demo.txt');
$content_array = explode(";", $content);
<div id="info_php">
<ul id="list_php">
<?php for($i=count($content_array); $i < 1; $i--);
echo '<li>'; echo $content_array[0]; echo'</li>';?>
</ul>
</div>
You have an error in syntax, the for loop is terminated by semicolon, and you are printing only the first element as $content_array[0], instead of using the loop variable. Try this:
<?php
for($i=count($content_array); $i < 1; $i--) {
echo '<li>'; echo $content_array[$i]; echo'</li>';
}
?>
The first thing you need to do is change your loop like this:
<?php
$count = count($content_array); // total rows
$limit = $count > 2 ? $count - 2 : 0; // at most we want two rows
for ($i = $count; $i > limit; $i--) {
echo '<li>' . $content_array[$i - 1] . '</li>';
}
?>
This will get the last two rows of the txt file
This is it!
<?php
$myFile = 'demo.txt';
$content = file_get_contents($myFile);
$content_array2 = explode("\n", $content);
?>
<?php for($i = 0; $i < count($content_array2); $i++):
$values = explode(';', $content_array2[$i]);?>
<li><?php echo $values[0].$values[1]; ?></li>
<?php endfor; ?>

PHP - Tricky... array into columns, but in a specific order

<?php
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$i = 0;
foreach ($combinedArray as $r ){
/*** use modulo to check if the row should end ***/
echo $i++%$num_cols==0 ? '<div style="clear:both;"></div>' : '';
/*** output the array item ***/
?>
<div style="float:left; width:33%;">
<?php
echo $r;
?>
</div>
<?php
}
?>
<div style="clear:both;"></div>
The above code will print out the array like this:
apple --- banana --- watermelon
lemon --- orange --- mango
However, I need it like this:
apple --- watermelon --- orange
banana --- lemon --- mango
Do you know how to convert this? Basically, each value in the array needs to be placed underneath the one above, but it must be based on this same structure of 3 columns, and also an equal amount of fruits per column/row (unless there was like 7 fruits there would be 3 in one column and 2 in the other columns.
Sorry I know it's confusing lol
Thanks everyone for your help... I realized a better way to do it though. Simple put, I have 3 columns floating next to eachother. And in each column, I add a list of the items into it and stop when I hit the max items per row.
working code:
<div style="float:left; width:33%;">
<?php
$combinedArraySizeOf = sizeof($combinedArray);
$num_cols = 3;
$iPerRow = $combinedArraySizeOf / $num_cols;
for ($i=0; $i!=$combinedArraySizeOf; $i++){
if ($i % $iPerRow == 0){
echo '</div><div style="float:left; width:33%;">';
}
echo $combinedArray[$i]."<br />";
}
?>
</div>
<div style='clear:both;'></div>
Don't forget to clear both at the end if necessary :P
Why aren't you doing exactly what you want to do? I mean show them in columns, instead of rows?
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rowCount = ceil(count($combinedArray)/$num_cols);
$i = 1; // in order the modulus to work correctly
?>
<div style="float: left; width:33%"> <!-- this is the first column -->
foreach ($combinedArray as $r ){
?>
<div> <!-- just a div containing $r -->
<?php
echo $r;
?>
</div>
<?php
// this is where the magic happens
// check if we have enough rows and start another column
if ($i % $rowCount == 0) {
?>
</div> <!-- close the previous column and start a new one -->
<div style="float: left; width:33%"> <!-- this is the new column -->
<?php
}
$i++;
}
?>
</div> <!-- closing the last open column -->
<div style="clear:both;"></div>
This should do just the job you wish. Marvin's answer is better if you want to use only tables without divs.
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$step = 2;
$i = 0;
$new_array = array();
foreach ($combinedArray as $r ){
$remainder = ($i % $step);
$new_array[$remainder][] = $r;
$i++;
}
foreach($new_array as $array)
{
echo implode(' --- ', $array)."<br>";
}
Would this work?
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $num_cols; $j++){
echo $combinedArray[(($i+$j) * $rows)-$i];
}
echo "<br />";
}
This would also need to check that the value existed for cases where the number of items wasn't precisely divisible by the number of columns, you could do that with the following change:
$combinedArray = array("apple","banana","watermelon","lemon","orange","mango");
$num_cols = 3;
$rows = ceil(count($combinedArray)/$num_cols);
for($i = 0; $i < $rows; $i++){
for($j = 0; $j < $num_cols; $j++){
$cell = (($i+$j) * $rows)-$i;
if($cell > count($combinedArray)) break;
echo $combinedArray[$cell];
}
echo "<br />";
}
If this order is what you ideally want, but it's not critical that it works in all browsers, perhaps you should look at coloumn layout (still very experimental css3 draft). If you use dispay inline block for the element in each coloumn you'll have the current order as a fallback.
You could also use a table for the layout and use a for loop something like this (pseudo php code, it's been a while since I've coded any php):
maxHeight = Math.ceil(array.length / 3) // meaning length=6 will give 3,
// length=7 will give 4
$x = -1; // horizontal index
for(i = 0; i < array.length(); i++){
$y = i % maxHeight; // vertical index
if($y == 0){
$x++;
}
addToTable($x,$y, array[i]);
}

Categories