How to echo a position of a .txt? - php

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";
}

Related

i want add multiple product using php session

I want to print both
<?php
//in file A
$_SESSION['cart']['prices'] = array('1000');
$_SESSION['cart']['services'] = array('game');
//In File B
$_SESSION['cart']['prices'] = array('2000');
$_SESSION['cart']['services'] = array('game2');
//in file C
foreach ($_SESSION['cart']['services'] as $key => $service) {
echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}
?>
Better use this :
$_SESSION['cart']['prices'][] = array('1000');
$_SESSION['cart']['services'][] = array('game');
//In File B
$_SESSION['cart']['prices'][] = array('2000');
$_SESSION['cart']['services'][] = array('game2');
According to the current data foreach loop will execute two times. It will print Array as $service is array array('1000') and array('2000') same as for $_SESSION['cart']['prices'][$key]
foreach ($_SESSION['cart']['services'] as $key => $service) {
echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}
Try this :
$array1 = array('1000','2000');
$array2 = array('game1','game2');
foreach($array1 as $index=>$key)
{
$_SESSION['cart']['prices'][] = $key;
$_SESSION['cart']['services'][] = $array2[$index];
}
foreach ($_SESSION['cart']['services'] as $key => $service) {
echo $service . ' = ' . $_SESSION['cart']['prices'][$key] . '<br />';
}

Parsing Filenames and outputing sorted list in HTML using PHP

Say I have a directory with the following files, named by date (month, day and year).
030313.pdf
030513.pdf
040113.pdf
052013.pdf
I know it is possible to break about the file names using explode() and I'm assuming I would need to save the data somehow to a multidimensional array like this:
$files = array
(
array("030313.pdf", 03,03,13),
array("030513.pdf", 03,05,13),
array("040113.pdf", 04,01,13),
array("052013.pdf", 05,20,13)
);
Is it possible to then take that information, group by month, sort by day and then echo that out as a bullet list of links?
Something like:
March 2013:
03/03/2013 (linked to actual file)
03/05/2013 (linked to actual file)
April 2013:
04/01/2013 (linked to actual file)
May 2013:
05/20/2013 (linked to actual file)
If this is possible, say there were 100-1000's of files, would loading this page cause any server performance issues?
Thanks
Brett
Here you go, just updated it to order them (I missed this when i first read your post.)
<?php
$files = array
(
array("030313.pdf", 03,03,13),
array("030513.pdf", 03,05,13),
array("040113.pdf", 04,01,13),
array("052013.pdf", 05,20,13)
);
$newArray = array();
foreach($files AS $file => $val){
$newArray[$date = date('Ym', strtotime($val[1] . '/' . $val[2] . '/' . $val[3]))][] = $val[0];
}
ksort($newArray);
$list = '<ul>';
foreach($newArray AS $key => $val){
$list .= '<li>' . date('F Y', strtotime('01-' . substr($key, 4, 2) . '-' . substr($key, 0, 4)));
if(is_array($val)){
$list .= '<ul>';
foreach($val AS $file => $filename){
$list .= '<li>Download ' . $filename . '</li>';
}
$list .= '</ul>';
}
$list .= '</li>';
}
$list .= '</ul>';
echo $list;
?>
The solution is very simple:
$files = array
(
array("030313.pdf", 03,03,13),
array("030513.pdf", 03,05,13),
array("040113.pdf", 04,01,13),
array("052013.pdf", 05,20,13)
);
$sortedData = array();
foreach ($files as $file) {
$dt = mktime(0, 0, 0, $file[1], 1, 2000);
$key = date('F', $dt) . ' ' . $file[3];
if (array_key_exists($key, $sortedData)) {
$sortedData[$key] []= $file;
} else {
$sortedData[$key] = array($file);
}
}
function sortFunc($a, $b) {
if ($a[2] > $b[2]) {
return 1;
} else if ($a[2] < $b[2]) {
return -1;
}
return 0;
}
foreach ($sortedData as &$sd) {
usort($sd, sortFunc);
}
foreach ($sortedData as $key => $data) {
echo "<h1>".$key."</h1>\n";
foreach ($data as $d) {
echo $d[0]."\n";
}
}
Just reformat the output.
Performance depends mostly on filesystem, used on server. Reformating the array of 1000 elemens usually is nothing.

Printing only once IF statement

Below is a php file used within my e-commerce website (prototype) to write the item(s) selected by a customer and storing their choices in a flat file database. The logic works fine although the echo "Order Submitted!; is printed for every item selected e.g. if 4 items are selected this line is printed 4 times, I only require it to be printed once. Any idea how this could be accomplished?
<body>
<table>
<?php
if (!($data = file('items.txt'))) {
echo 'ERROR: Failed to open file! </body></html>';
exit;
} else {
echo "<h1>Transaction Completed!</h1>";
}
date_default_timezone_set('Europe/London');
$now = date(' d/m/y H:i:s ');
$visitor = $_POST['visitor'];
foreach ($_POST as $varname => $varvalue) {
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
if ($partno == $varname) {
$myFile = "purchases.txt";
$fh = fopen($myFile, 'a') or die("ERROR: Failed to open purchases file!\n");
$content = $now . "|" . $visitor . "|" . $partno . "|" . $name . "|" . $price . "\n";
if (!(fwrite($fh, $content))) {
echo "<p>ERROR: Cannot write to file($myFile)\n</p>";
exit;
} else {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
}
}
}
?>
</table>
<input type="button" onClick="parent.location='home.php'" value="Return Home">
<input type="button" onClick="parent.location='items.php'" value="New Purchase">
If 2 items are selected the output is:
Transaction Completed
Order Submitted!
Order Submitted!
Keep track of if there's an error and move the else outside the loop.
$error=false;
foreach ($_POST as $varname => $varvalue) {
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
if ($partno == $varname) {
$myFile = "purchases.txt";
$fh = fopen($myFile, 'a') or die("ERROR: Failed to open purchases file!\n");
$content = $now . "|" . $visitor . "|" . $partno . "|" . $name . "|" . $price . "\n";
if (!(fwrite($fh, $content))) {
echo "<p>ERROR: Cannot write to file($myFile)\n</p>";
$error=true;
exit;
}
}
}
}
if(!$error) {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
Although, the way you have it written, you don't even need a conditional surrounding "Order submitted" because it will never execute if there's an error. (Due to the exit.)
Also you can move $myfile out of the loop if it doesn't change.
Second version:
$myFile = "purchases.txt";
foreach ($_POST as $varname => $varvalue) {
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
if ($partno == $varname) {
$fh = fopen($myFile, 'a') or die("ERROR: Failed to open purchases file!\n");
$content = $now . "|" . $visitor . "|" . $partno . "|" . $name . "|" . $price . "\n";
if (!(fwrite($fh, $content))) {
echo "<p>ERROR: Cannot write to file($myFile)\n</p>";
exit;
}
}
}
}
echo "<p>Order Submitted!</p>";
fclose($fh);
before foreach loop:
$printed=false;
then instead of
else {
echo "<p>Order Submitted!</p>";
fclose($fh);
}
use
else {
if(!$printed)
{
echo "<p>Order Submitted!</p>";
$printed=true;
}
fclose($fh);
}
You should remove the echo from the else branch and put it right after the first foreach closing bracket
You can use a counter and see if more the one was submitted
like:
$count=0;
foreach ($_POST as $varname => $varvalue) {
foreach ($data as $thedata) {
list($partno, $name, $description, $price, $image) = explode('|', $thedata);
if ($partno == $varname) {
$myFile = "purchases.txt";
$fh = fopen($myFile, 'a') or die("ERROR: Failed to open purchases file!\n");
$content = $now . "|" . $visitor . "|" . $partno . "|" . $name . "|" . $price . "\n";
if (!(fwrite($fh, $content))) {
echo "<p>ERROR: Cannot write to file($myFile)\n</p>";
exit;
} else {
$count++;
fclose($fh);
}
}
}
}
if ($count>0)
echo "<p>Order Submitted! $count Times</p>";

PHP For Each Loop? There has to be a better way

<?php
header("Content-type: text/plain");
$GLOBALS["db_name"] = "fggff_highscores";
$GLOBALS["table_name"] = "testing";
$GLOBALS["view_user"] = "fgfggg_players";
$GLOBALS["view_pass"] = "removed";
$username = strip_tags($_GET["player"]);
$fileContents = #file_get_contents("http://hiscore.runescape.com/index_lite.ws?player=" . $username);
echo $fileContents;
if ($fileContents == FALSE) {
die("PLAYER NOT FOUND");
}
//$content = array();
$data = explode("\n", $fileContents);
/*
foreach ($data as $word) {
$index = $skills[$index];
$new_data = explode (",", $word);
$content[$index]["rank"] = $new_data[0];
$content[$index]["level"] = $new_data[1];
$content[$index]["experience"] = $new_data[2];
$index++;
}
*/
$stats0 = explode(",", $data[0]);
echo "\n";
echo "\n";
echo "Overall Rank: " .number_format($stats0[0]);
echo "\n";
echo "Total Level: " .number_format($stats0[1]);
echo "\n";
echo "Overall Total XP: " .number_format($stats0[2]);
$stats1 = explode(",", $data[1]);
echo "\n";
echo "\n";
echo "Attack Rank: " .number_format($stats1[0]);
echo "\n";
echo "Attack Level: " .number_format($stats1[1]);
echo "\n";
echo "Attack XP: " .number_format($stats1[2]);
$stats2 = explode(",", $data[2]);
echo "\n";
echo "\n";
echo "Defence Rank: " .number_format($stats2[0]);
echo "\n";
echo "Defence Level: " .number_format($stats2[1]);
echo "\n";
echo "Defence XP: " .number_format($stats2[2]);
?>
Example above should be working when ran you can use this player to see output -- .php?player=zezima
The output of each $data[0] is something like--------53,2496,1661657944
53-----------------------number_format($stats0[0])
2,496--------------------number_format($stats0[1])
1,661,657,944------------number_format($stats0[2])
--
Then I'm trying to break up each $data[] index into 3 pieces, the way I have it above works but I want to be able to do something like a for each loop to go through each $data[] indexes and break each up using explode(",", $data[0]); explode(",", $data[1]); explode(",", $data[2]); but it would have the index increment each time. What I need in the end is each value to be in a variable that I can use. Example:
$apple = number_format($stats0[0]);
echo $apple;
This is what I've tried something with:
$content = array();
foreach ($data as $word) {
$index = $skills[$index];
$new_data = explode (",", $word);
$content[$index]["rank"] = $new_data[0];
$content[$index]["level"] = $new_data[1];
$content[$index]["experience"] = $new_data[2];
$index++;
}
instead of repeating yourself again and again, create a function like this one:
function print($data, $i){
$stats0 = explode(",", $data[$i]);
echo "\n\nOverall Rank: " .number_format($stats0[0]) . "\n";
echo "Total Level: " .number_format($stats0[1]) . "\n";
echo "Overall Total XP: " .number_format($stats0[2]);
}
and call it in a loop.

Show first letter of a list once?

I have a list of directory name and need to get the first letter from each name and just display it once before the start of that lettered group ie;
what I have:
1
2
3
4
5
Aberdeen
Arundel
Aberyswith
Bath
Bristol
Brighton
Cardiff
coventry
what I would like:
#
1
2
3
4
5
A
Aberdeen
Arundel
Aberyswith
B
Bath
Bristol
Brighton
C
Cardiff
coventry
function htmlDirList($subdirs) {
global $z_self, $z_img_play, $z_img_lofi, $z_img_more, $z_admin,
$z_img_down, $z_img_new, $zc;
$now = time();
$diff = $zc['new_time']*60*60*24;
$num = 0;
$dir_list_len = $zc['dir_list_len'];
if ($zc['low']) { $dir_list_len -= 2; }
$html = "";
$checkbox = ($z_admin || ($zc['playlists'] && $zc['session_pls']));
/**/
$row = 0;
$items = sizeof($subdirs);
$cat_cols = "2";
$rows_in_col = ceil($items/$cat_cols);
if ($rows_in_col < $cat_cols) { $cat_cols = ceil($items/$rows_in_col); }
$col_width = round(100 / $cat_cols);
$html = "<table width='600'><tr>";
$i = 0;
/**/
foreach ($subdirs as $subdir => $opts) {
if ($row == 0) {
$class = ($cat_cols != ++$i) ? ' class="z_artistcols"' : '';
$html .= "<td $class valign='top' nowrap='nowrap' width='$col_width%'>";
}
/*$currentleter = substr($opts, 0 , 1);
if($lastletter != $currentleter){
echo $currentleter;
$lastletter = $currentleter;
}*/
if($alphabet != substr($opts,0,1)) {
echo strtoupper(substr($opts,0,1)); // add your html formatting too.
$alphabet = substr($opts,0,1);
}
$dir_len = $dir_list_len;
$dir = false;
$image = $opts['image'];
$new_beg = $new_end = "";
if (substr($subdir, -1) == "/") {
$dir = true;
$subdir = substr($subdir, 0, -1);
}
$path_raw = getURLencodedPath($subdir);
$href = "<a href='$path_raw";
if (!$dir) {
if ($zc['download'] && $zc['cmp_sel']) { $html .= "$href/.lp&l=8&m=9&c=0'>$z_img_down</a> "; }
if ($zc['play']) { $html .= "$href&l=8&m=0'>$z_img_play</a> "; }
if ($zc['low'] && ($zc['resample'] || $opts['lofi'])) { $html .= "$href&l=8&m=0&lf=true'>$z_img_lofi</a> "; }
if ($checkbox) { $html .= "<input type='checkbox' name='mp3s[]' value='$path_raw/.lp'/> "; }
$num++;
if ($zc['new_highlight'] && isset($opts['mtime']) && ($now - $opts['mtime'] < $diff)) {
$dir_len -= 5;
if ($z_img_new) {
$new_end = $z_img_new;
} else {
$new_beg = $zc['new_beg'];
$new_end = $zc['new_end'];
}
}
}
$title = formatTitle(basename($subdir));
if (strlen($title) > $dir_len) {
$ht = " title=\"$title.\"";
$title = substr($title,0,$dir_len).$opts['mtime']."...";
} else {
$ht = "";
}
if ($zc['dir_list_year']) {
$di = getDirInfo($subdir);
if (!empty($di['year'])) $title .= " (".$di['year'].")";
}
$html .= "$href'$ht>$new_beg$title$new_end</a><br />";
$row = ++$row % $rows_in_col;
if ($row == 0) { $html .= "</td>"; }
}
if ($row != 0) $html .= "</td>";
$html .= "</tr></table>";
$arr['num'] = $num;
$arr['list'] = $html;
return $arr;
}
I need help to get work.
The following will display the list of directories, beginning each group with a first letter as beginning of the group (see codepad for proof):
(this assumes $dirs is array containing the names)
$cur_let = null;
foreach ($dirs as $dir) {
if ($cur_let !== strtoupper(substr($dir,0,1))){
$cur_let = strtoupper(substr($dir,0,1));
echo $cur_let."\n";
}
echo $dir . "\n";
}
You just need to add some formatting on your own, suited to your needs.
Edit:
Version grouping under # sign entries that begin with a number, can look like that:
$cur_let = null;
foreach ($dirs as $dir) {
$first_let = (is_numeric(strtoupper(substr($dir,0,1))) ? '#' : strtoupper(substr($dir,0,1)));
if ($cur_let !== $first_let){
$cur_let = $first_let;
echo $cur_let."\n";
}
echo $dir . "\n";
}
Please see codepad as a proof.
Is this what you are looking for?
<?php
$places = array(
'Aberdeen',
'Arundel',
'Aberyswith',
'Bath',
'Bristol',
'Brighton',
'Cardiff',
'coventry'
);
$first_letter = $places[0][0];
foreach($places as $p)
{
if(strtolower($p[0])!=$first_letter)
{
echo "<b>" . strtoupper($p[0]) . "</b><br/>";
$first_letter = strtolower($p[0]);
}
echo $p . "<br/>";
}
?>
Prints:
A
Aberdeen
Arundel
Aberyswith
B
Bath
Bristol
Brighton
C
Cardiff
coventry
My approach would be to generate a second array that associates the first letter to the array of names that begin with that letter.
$dirs; // assumed this contains your array of names
$groupedDirs = array();
foreach ($dirs as $dir) {
$firstLetter = strtoupper($dir[0]);
$groupedDirs[$firstLetter][] = $dir;
}
Then, you can iterate on $groupedDirs to print out the list.
<?php foreach ($groupedDirs as $group => $dirs): ?>
<?php echo $group; ?>
<?php foreach ($dirs as $dir): ?>
<?php echo $dir; ?>
<?php endforeach; ?>
<?php endforeach; ?>
This allows for a clean separation between two separate tasks: figuring out what the groups are and, secondly, displaying the grouped list. By keeping these tasks separate, not only is the code for each one clearer, but you can reuse either part for different circumstances.
Use something like this, change it to output the HTML the way you want thouugh:
sort($subdirs);
$count = count($subdirs);
$lastLetter = '';
foreach($subdirs as $subdir => $opts){
if(substr($subdir,0,1) !== $lastLetter){
$lastLetter = substr($subdir,0,1);
echo '<br /><div style="font-weight: bold;">'.strtoupper($lastLetter).'</div>';
}
echo '<div>'.$subdir.'</div>';
}
EDIT
Just realized $subdir is associative, made the change above:

Categories