split a string into two divs, and split inside the string php - 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

Related

How to display text lot of times on HTML and 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
}
?>

How to reverse a PHP while loop including incremental numbers starting with last number of varying length?

I have a working php code which made a numerical loop counting up from 1.
<?php
$i = 0; // define number
while (have_rows('the_rows')) : row(); // begin while loop
$i++; // increase increment with each loop ?>
<div>
<?php echo $i; // outputting the number ?>
</div>
<?php
endwhile; // end while loop
?>
This code will loop through all available entries and stop after there are no more entries. It will count up for each $i output.
For example, it might look like this:
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
There is no pre-set number. It could be any number including 1, 5, 74, or 938 entries. The loop will continue to all entries and stops after all entries are finished.
This works fine if I start at one. But since the number is not fixed, looping backwards is a tad more difficult.
How can I reverse this loop so that first of all, it counts the numbers backwards (e.g. if there are 7 rows, it counts backwards from 7), and also to reverse the loop starting with the last row?
e.g.
<div>7</div>
<div>6</div>
<div>5</div>
<div>4</div>
<div>3</div>
<div>2</div>
<div>1</div>
If you don't know the length of the data, get the length of the data and then print it out, in reverse.
<?php
$i = 0;
while(has_rows('the_rows')):
row();
$i++;
endwhile;
while($i > 0):
?>
<div><?php echo $i--; ?></div>
<?php endwhile; ?>
Without knowing the number of rows in advance and without looping twice, you have several options.
One is to build an array and reverse it:
<?php
$i = 0;
while (have_rows('the_rows')) : row();
$i++;
$html[] = "
<div>
$i
</div>";
endwhile;
echo implode(array_reverse($html));
Or prepend each one onto an array:
<?php
$i = 0;
while (have_rows('the_rows')) : row();
$i++;
$html = "
<div>
$i
</div>";
array_unshift($data, $html);
endwhile;
echo implode($data);
Or prepend the new string to the existing one:
<?php
$i = 0;
$html = "";
while (have_rows('the_rows')) : row();
$i++;
$html = "
<div>
$i
</div>$html";
endwhile;
echo $html;
However this looks like WordPress or something, so I would think there would be a way to get the number of rows before the loop.

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>

weird float to int when moving a var

Here's my code, fetch_data.php returns a float (an exchange rate).
The first echo will return the float that I need (0.65 something) but the second one will return an integer (1)... Why? How do I fix this?
I'm using include because file_get_contents returned PHP for some reason? I don't have a lot of PHP experience and haven't written a line in a month so I'm rusty.
<div class="callout panel">
<p><strong>We sell for:</strong> $<?php
$coin_price = include('fetch_data.php');
$doge = $coin_price;
echo $coin_price;
echo $doge;
?> per 1000</p>
</div>
fetch data
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
echo $val / 1000;
any help appreciated
fetch_data.php
$string = strip_tags($element); //<strong>$484.66</strong>
$string = str_replace('$', '', $string);
$int = $string;
$val = $int * 1.25;
$coin_price = $val / 1000;
other php file
<div class="callout panel">
<p><strong>We sell for:</strong>
$<?php
include('fetch_data.php');
echo $coin_price;
$doge = $coin_price;
echo $doge;
?> per 1000</p>
</div>
I modified the code a bit if you want to use include like you are currently. When a file is included, the code it contains inherits the variable scope of the line on which the include occurs. Any variables available at that line in the calling file will be available within the called file, from that point forward.
You can use
$doge = floatval($coin_price);
Check here : http://www.php.net/manual/en/function.floatval.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; ?>

Categories