PHP Array Looping & Exploding - php

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>

Related

Output MySQL variables in PHP array

I have MySQL variables, which are echoing but I want to use them in an array and randomly display the results - it's for a competition - the random is working great but I can't get the variable to show correctly when they're in the array().
I have written the following:
<?php
$answer = array('$this->item->correct_answer','$this->item->false_answer1','$this->item->false_answer2');
shuffle($answer);
?>
<ol class="answers">
<?php
for( $i = 0; $i < 3; $i++)
echo "<li>$answer[$i]</li>";
?>
</ol>
This is only outputting:
$this->item->correct_answer$this->item->false_answer2$this->item->false_answer1
That's because you've got ' for the array inputs. That means you're setting a string. Revise it to:
$answer = array($this->item->correct_answer,$this->item->false_answer1,$this->item->false_answer2);
As demonstrated On codepad.org

Echo of array doesn't do paragraphs on the right places

I know this isn’t a very useful question for most people but I have dedicated a lot of time to solve a small problem and I didn’t get the results that I wanted, so I need your help:
<?php
$lines = file('temphum.txt');
$result = array_reverse($lines);
for($i=0; $i<count($result);$i=$i+4){
$cenas= $result[$i]."ºC";
$contentsite= nl2br($cenas);
echo $contentsite ;
}
?>
This code prints this:
25ºC24
ºC23
ºC22
ºC21
ºC
Instead of this:
25ºC
24ºC
23ºC
22ºC
21ºC
What is the problem?
You append the °C sign after the newline at the end of the string.
<?php
$lines = file('temphum.txt', FILE_IGNORE_NEW_LINES);
$result = array_reverse($lines);
for($i=0; $i<count($result);$i=$i+4){
echo $result[$i]."ºC"."<br>";
}

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

PHP leading zero beginning with 01 not 00

I am using the following PHP in a wordpress theme for comment count...
<?php $i = 0; foreach ($comments as $comment) : ?>
<?php $ii = sprintf("%02d", $i);?>
<?php $i++;echo $ii; echo '. ';?>
(Echo the comment number beginning with 01.)
The problem is that the first comment starts with 00. instead of 01.
Example:
00. John Smith says...
01. Patrick Smith says...
02. Etc..
Does anyone have any suggestions?
You obviously started at 0:
<?php $i = 0; foreach ...
If you wanted it to start at 1, you should code it to do so:
<?php $i = 1; foreach ...
One key to writing good code is understanding exactly what it is you're asking the computer to do.
change your code to have $i start from one.
<?php $i = 1; foreach ($comments as $comment) : ?>
<?php $ii = sprintf("%02d", $i);?>
<?php $i++;echo $ii; echo '. ';?>

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