How to see the array results of the generated table in php - php

So this is the some part of the code I study:
if ($handle) {
while (($line = fgets($handle)) !== false) {
$exploded_line = explode("::", $line);
$day = date("j", $exploded_line[0]);
$action = trim($exploded_line[4]);
if ($action == LOGIN_ACTION) {
$details[$exploded_line[3]][$day] = isset($details[$exploded_line[3]][$day]) ? $details[$exploded_line[3]][$day] + 1 : 1;
} else if ($action == COMPANY_TICKETS_ACTION || $action == MYTICKETS_ACTION) {
$company_mytickets_action[$day] = isset($company_mytickets_action[$day]) ? $company_mytickets_action[$day] + 1 : 1;
} else if ($action == NEW_TICKET_ACTION) {
$new_ticket_action[$day] = isset($new_ticket_action[$day]) ? $new_ticket_action[$day] + 1 : 1;
} else if ($action == SEARCH_ACTION) {
$search_action[$day] = isset($search_action[$day]) ? $search_action[$day] + 1 : 1;
} else if ($action == PREFERENCES_ACTION) {
$preferences_action[$day] = isset($preferences_action[$day]) ? $preferences_action[$day] + 1 : 1;
}
}
} else {
$data['html'] = ERR_MSG;
}
fclose($handle);
print_r();
foreach ($details as $users => $value) {
$data['html'] .= "<tr><td>$users</td>";
for ($x = 1; $x <= $calendar_days; $x++){
//$details[$users][$x] = empty($details[$users][$x]) ? SPACE : $details[$users][$x];
if(empty($details[$users][$x]))
$details[$users][$x]='';
else
$details[$users][$x];
$data['html'] .= "<td>" . $details[$users][$x] . "</td>";
$unique[$x] = $details[$users][$x] == SPACE ? $unique[$x] : $unique[$x] + 1;
$total[$x] = $details[$users][$x] == SPACE ? $total[$x] : $total[$x] + $details[$users][$x];
}
$data['html'] . "</tr>";
}
Now I want to var_dump the results to array. I try this print_r($details[$exploded_line[3]][$day]); but there is no display in the browser. How can I get the results in array? Thanks

Related

Auto Description From Tags

I'm trying to generate auto description from tags.
The code was working but after updating my site to Laravel 6 in stop working. I need to get it back working.
if( !empty( $request->description ) )
{
$description = Helper::checkTextDb($request->description);
}
else
{
$a_key = explode(",", strtolower($request->tags));
if(count($a_key) == 0)
$description = 'This is a great thing';
else
{
$description_get_keys = '';
foreach ($a_key as &$value)
{
if($value == end($a_key) && count($a_key) != 1)
$description_get_keys = $description_get_keys.' and '.$value.'.';
else if(count($a_key) == 1)
$description_get_keys = $value.'.';
else if (count($a_key) > 1 && $a_key[0] == $value)
$description_get_keys = $value;
else
$description_get_keys = $description_get_keys.', '.$value;
}
$description = 'This is a great thing about '.$description_get_keys;
}
}
I see a couple things that could possibly be an issue, not knowing what came before this code.
I will assume that the $request variable is an instance of Illuminate\Http\Request and that it is available in the function, right?
Try this updated code:
if($request->has('description'))
{
$description = Helper::checkTextDb($request->description);
}
else if ($request->has('tags'))
{
if (strpos($request->tags, ',') === false)
{
$description = 'This is a great thing';
}
else {
$a_key = explode(",", strtolower($request->tags));
$a_count = count($a_key);
$description_get_keys = '';
for ($i = 0; $i < $a_count; $i++)
{
if ($a_count == 1) {
$description_get_keys = "{$a_key[$i]}.";
}
else {
// first
if ($i === 0) {
$description_get_keys = $a_key[0];
}
// last
else if ($i === $a_count - 1) {
$description_get_keys .= " and {$a_key[$i]}.";
}
// middle
else {
$description_get_keys .= ", {$a_key[$i]}";
}
}
}
$description = "This is a great thing about {$description_get_keys}";
}
}
I wrote that quick so hopefully there are no errors.

if output is empty do something else?

This code outputs info from a road. but sometimes there is no info, then I want to gather info from another place. How can I do this?
$title = '1234';
foreach(Feed('$url') as $f ) {
if (strpos($f->title, $title) !== false){
$pos = $f->children('georss', true)->children('gml', true)->children('gml', true);
list($number1, $number2) = explode(' ', $pos);
if ($number1 > 0 && $number1 < 100){
if ($number2 > 1 && $number2 < 10){
echo $f->description. "<br>";
}
}
}
}
how can I make it so if this^^ has no results then do this:
foreach(Feed('$url') as $f ) {
if (strpos($f->title, $title) !== false){
echo $f->description. "<br>";
}
}
I know I can check if a variable is empty with using Len($variable) = 0. but i dont have an variable. how could i get this? then i guess i could do:
if (Len($variable) = 0) {
foreach(Feed('$url') as $g ) {
if (strpos($g->title, $title) !== false){
echo $g->description. "<br>";
}
}
}
I guess it is possible to have foreach inside if?
Any help is greatly appreciated!
You could use a boolean variable to indicate if you found the description the first way, and if not, then find it the second way:
$descriptionFound = false;
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=601') as $f) {
if (strpos($f->title, $title) !== false){
$pos = $f->children('georss', true)->children('gml', true)->children('gml', true);
list($number1, $number2) = explode(' ', $pos);
if (
$number1 > 59.417104 && $number1 < 60.089568
&& $number2 > 5.115812 && $number2 < 7.811784
) {
$descriptionFound = true;
echo $f->description . "<br>";
}
}
}
if (!$descriptionFound) {
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f) {
if (strpos($f->title, $title) !== false){
echo $f->description . "<br>";
}
}
}
Similarly, you could store the description as you're checking various methods, and then output the value at the end:
$description = '';
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=601') as $f) {
if (strpos($f->title, $title) !== false){
$pos = $f->children('georss', true)->children('gml', true)->children('gml', true);
list($number1, $number2) = explode(' ', $pos);
if (
$number1 > 59.417104 && $number1 < 60.089568
&& $number2 > 5.115812 && $number2 < 7.811784
) {
$description .= $f->description . "<br>";
}
}
}
if (!$description) { //this will be falsy if it's an empty string, so no need to do == ''
foreach(Feed('https://www.vegvesen.no/trafikk/xml/savedsearch.rss?id=604') as $f) {
if (strpos($f->title, $title) !== false){
$description .= $f->description . "<br>";
}
}
}
echo $description;
Either would give you the same code flow and end result.

Check and modify Capitalized letters in string

I'm trying to figure out how to make this code work.
I input some text via variable into code:
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
You will see that for the Genome there is a Nn and for the Fenome there is a Ee
Upon this happening I need it to give a 50/50 chance for a compared result to be EITHER Ee OR Nn - The code I have right now can only check them individually (so sometimes I ger Ee AND Nn, other times I will get ee and nn) and I feel there could be a much easier method of achieving this than what I'm trying:
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
if ($gepieces[0] === $fepieces[0]) {
$ss = $gepieces[0];
} else {
$ss = rand(1,2);
if ($ss == 1) {
$ss = $gepieces[0];
} else {
$ss = $fepieces[0];
}
}
if ($gepieces[1] === $fepieces[1]) {
$ee = $gepieces[1];
} else {
$ee = rand(1,2);
if ($ee == 1) {
$ee = $gepieces[1];
} else {
$ee = $fepieces[1];
}
}
if ($gepieces[2] === $fepieces[2]) {
$ff = $gepieces[2];
} else {
$ff = rand(1,2);
if ($ff == 1) {
$ff = $gepieces[2];
} else {
$ff = $fepieces[2];
}
}
if ($gepieces[3] === $fepieces[3]) {
$nn = $gepieces[3];
} else {
$nn = rand(1,2);
if ($nn == 1) {
$nn = $gepieces[3];
} else {
$nn = $fepieces[3];
}
}
if ($gepieces[4] === $fepieces[4]) {
$oo = $gepieces[4];
} else {
$oo = rand(1,2);
if ($oo == 1) {
$oo = $gepieces[4];
} else {
$oo = $fepieces[4];
}
}
echo $cubgender." - ".$ss."/".$ee."/".$ff."/".$nn."/".$oo."<br/>";
}
I'm a colossal idiot!
Figured out my own issue
//Number of Cubs
$cubs = rand(1,4);
//GENDER
for ($x = 0; $x < $cubs; $x++) {
$gender = rand(1,2);
if ($gender == 1) {
$cubgender = "Male";
} elseif ($gender == 2) {
$cubgender = "Female";
}
//COAT COLOR
$genome = "ss/ee/ff/Nn/oo";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo";
$fepieces = explode("/", $fenome);
if ($genome === $fenome) {
$cubgeno = $genome;
} else {
$cubgeno = rand(1,2);
if ($cubgeno == 1) {
$cubgeno = $genome;
} else {
$cubgeno = $fenome;
}
}
echo $cubgender." - ".$cubgeno."<br/>";
$genome = "ss/ee/ff/Nn/oo/Pa/Sr/So";
$gepieces = explode("/", $genome);
$fenome = "ss/Ee/ff/nn/oo/Pa";
$fepieces = explode("/", $fenome);
$sequence = "";
for ($i = 0;$i < count($fepieces); $i++) {
rand(1,2) == 1 ? $sequence .= $gepieces[$i] : $sequence .= $fepieces[$i];
}
echo $sequence;

PHP-Script deletes lines in .dat file

I'm struggling with the following problem since 9 hours and can't find the solution. I'm using a very old script from here (http://www.teralaser.net/invitephp/) to build an invitation-script. I already did all the styling within the past week and it looks great, now.
the script stores the single datasets in a .dat file. Now I realized, that it always keeps just the first 125 lines of the database and deletes the rest. That's a hug he problem, as it is for an invitation with nearly 1000 persons.
After some hours I found out (better I guess), that it's the following line, which destroys it:
case "adminadd":
if (isset($_POST["pwd"])) $pwd = $_POST["pwd"];
else $pwd="";
$pwdchk = 1;
if (isset($_GET["pwd2"])) $pwdchk = strcasecmp(md5( date("z") . $admin_password),$_GET["pwd2"]);
if (strcasecmp($pwd,$admin_password) != 0 && $pwdchk != 0) {
echo "<HTML><HEAD><META HTTP-EQUIV='REFRESH' CONTENT='1; URL=$PHP_SELF?do=admin'></HEAD><BODY bgcolor='#ffffff'><CENTER><H2>Falsches Passwort!</H2>Bitte warten...</CENTER></BODY><HTML>";
exit;
}
// Get all administrator changes
$record = file($data_file);
rsort($record);
$jmlrec = count($record);
// Delete duplicates (the dum way...) and get host email if any
$hostemail = "";
for ($i = 0; $i < $jmlrec; $i++) {
if (isset($record[$i])) {
$r = $record[$i];
$row = explode("|~~|",$r);
$chkmail = $row[2];
if ($row[4] == "Gastgeber") $hostemail = $row[2];
for($j = $i+1; $j < $jmlrec; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail)==0 ) { $record[$j] = ""; }
}
}
}
$updno = 0;
$err = 0;
for ($i = 0; $i < $jmlrec; $i++) { // Geändert von $jmlrec+12
// Changes ?
if (isset($_POST["vemail".$i])) $chkmail = $_POST["vemail".$i]; else $chkmail ="";
$found = -1;
if ($chkmail != "") {
// Find existing record if possible...
$recno = count($record);
for($j = 0; $j < $recno; $j++) {
$r = $record[$j];
$row = explode("|~~|",$r);
if (strcasecmp($row[2],$chkmail) == 0) { $found = $j; break; }
}
$changed = 1;
if ($found >= 0) {
// Changed ?
if ( $row[2] == $_POST["vemail".$i] && $row[3] == $_POST["vname".$i] && $row[4] == $_POST["vreply".$i] && $row[5] == $_POST["vamount".$i] &&
$row[6] == $_POST["vcompany".$i] && $row[7] == $_POST["vcomment".$i] ) $changed = 0;
}
if ( $changed != 0 ) {
$updno++;
$idx = date("YmdHis");
$tgl = date("d.m.Y H:i");
$vemail = str_replace("<","",$_POST["vemail".$i]);
$vemail = str_replace(">","",$vemail);
$vemail = str_replace("~~","--",$vemail);
$vemail = str_replace("\"",""",$vemail);
$vname = str_replace("<","",$_POST["vname".$i]);
$vname = str_replace(">","",$vname);
$vname = str_replace("~","-",$vname);
$vname = str_replace("\"",""",$vname);
$vreply = str_replace("<","",$_POST["vreply".$i]);
$vreply = str_replace(">","",$vreply);
if ($vreply != "ungesehen" && $vreply != "gesehen" && $vreply != "unentschlossen" && $vreply != "abgesagt" && $vreply != "zugesagt" && $vreply != "Gastgeber") $vreply = "ungesehen";
if ($vreply != "Gastgeber" && $vreply != "ungesehen" && $found >= 0 ) $tgl = $tgl . "*"; // admin change
$vamount = str_replace("<","<",$_POST["vamount".$i]);
$vamount = str_replace(">",">",$vamount);
$vamount = str_replace("~","-",$vamount);
$vamount = str_replace("\"",""",$vamount);
$vcomment = str_replace("<","<",$_POST["vcomment".$i]);
$vcomment = str_replace(">",">",$vcomment);
$vcomment = str_replace("~","-",$vcomment);
$vcomment = str_replace("\"",""",$vcomment);
$vcompany = str_replace("<","<",$_POST["vcompany".$i]);
$vcompany = str_replace(">",">",$vcompany);
$vcompany = str_replace("~","-",$vcompany);
$vcompany = str_replace("\"",""",$vcompany);
if (strtoupper($os) == "WIN") {
$vcomment = str_replace("\r\n","<BR>",$_POST["vcomment".$i]);
$vcomment = str_replace("\r","",$vcomment);
$vcomment = str_replace("\n","",$vcomment);
} else {
$vcomment = str_replace("\n","<BR>",$vcomment);
$vcomment = str_replace("\r","",$vcomment);
}
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $vemail)) $validemail = 0;
if ( $vname != "" && $validemail == 0 ) { echo "Email not valid for $vname : $vemail <br>"; $err++; }
if ( $vname != "" && $validemail == 1 ) {
if ($found < 0 ) { $record[] = "\n"; $found = count($record) - 1; }
$newdata = "<?//|~~|$idx|~~|$vemail|~~|$vname|~~|$vreply|~~|$vamount|~~|$vcompany|~~|$vcomment|~~|$tgl|~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[$found] = $newdata;
} else {
if ( $vname != "") { $updno--; $changed = 0; }
}
}
if ( $changed == 0 ) {
if ($found >= 0) $record[$found] = "!".$record[$found];
}
}
}
// Quick add
$quickadd = "";
if (isset($_POST["quickadd"]) && $_POST["quickadd"] != "") $quickadd = stripslashes($_POST["quickadd"]);
if (trim($quickadd) != "") {
$qadd = explode("\"",$quickadd);
$qaddn = count($qadd);
$nametoadd = "";
$companytoadd = "";
for($i = 0; $i < $qaddn; $i++) {
if ($i % 2 == 0) {
$q2 = explode(",",$qadd[$i]);
$q2n = count($q2);
for($j = 0; $j < $q2n; $j++) {
$mailtoadd = trim($q2[$j]);
$mailtoadd = str_replace("<","",$mailtoadd);
$mailtoadd = str_replace(">","",$mailtoadd);
$mailtoadd = str_replace("~~","--",$mailtoadd);
if ($mailtoadd != "") {
$validemail = 1;
if (!preg_match("/([\w\.\-]+)(\#[\w\.\-]+)(\.[a-z]{2,4})+/i", $mailtoadd)) $validemail = 0;
if ($validemail == 0 ) { echo "Fehler bei $mailtoadd (Keine gültige eMail-Addresse)"; $err++; } else {
if ($nametoadd == "") { $nametoaddarr = explode("#",$mailtoadd); $nametoadd = $nametoaddarr[0]; }
$idx = date("YmdHis");
$newdata = "<?//|~~|$idx|~~|$mailtoadd|~~|$nametoadd|~~|ungesehen|~~|1|~~|$companytoadd|~~||~~|?>\n";
$newdata = "!" . stripslashes($newdata);
$record[]= $newdata;
$nametoadd = "";
$companytoadd = "";
}
}
}
}
if ($i % 2 == 1) {
$data = $qadd[$i];
list($nametoadd, $companytoadd) = explode(";", $data);
$nametoadd = str_replace("<","" ,$nametoadd);
$nametoadd = str_replace(">","" ,$nametoadd);
$nametoadd = str_replace("~","-",$nametoadd);
$companytoadd = str_replace("|","" ,$companytoadd);
$companytoadd = str_replace("<","" ,$companytoadd);
$companytoadd = str_replace(">","" ,$companytoadd);
$companytoadd = str_replace("~","-",$companytoadd);
}
}
}
/* Hier liegt der tote Fuchs begraben!!! */
$jmlrec = count($record);
$update_data = fopen($data_file,"w");
if (strtoupper($os) == "UNIX") {
if (flock($update_data,LOCK_EX)) {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
flock($update_data,LOCK_UN);
}
} else {
for ($j=0; $j<$jmlrec; $j++) {
if (strncasecmp($record[$j],"!",1) != 0 ) $updno++;
if ($record[$j] != "" && strncasecmp($record[$j],"!",1) == 0 ) fputs($update_data,substr($record[$j],1));
}
}
fclose($update_data);
I'm looking forward for some ideas. I'll love to invite you for a coffee... ;) I can also send the whole script, if someone is interested.
Best!
Philipp
EDIT: I'm still totally lost in the code, so I want to share the other parts of the code with you and maybe someone finds the problem. ;) Thanks!
$jml_page = intval($jmlrec/$max_entry_per_page);
$sisa = $jmlrec%$max_entry_per_page;
if ($sisa > 0) $jml_page++;
// Set $nomaybe, $nono, $noyes DEBUG
$nomaybe = 0;
$nono = 0;
$noyes = 0;
$countmaybe = 0;
$countno = 0;
$countyes = 0;
$counttotal = 0;
$no = 0;
if ($jmlrec == 0) echo "<tr><td align='center'>Noch keine Einträge.</td></tr>";
$w = 0; //--Color
for ($i=0; $i<$max_entry_per_page; $i++) {
// Find the lowest next possible record.
$no = $jmlrec + 1;
if ($nomaybe < $jmlrec ) $no = $nomaybe + 1;
if ($nono < $no - 1 ) $no = $nono + 1;
if ($noyes < $no - 1 ) $no = $noyes + 1;
// Check this is valid.
do {
while (($no < $jmlrec + 1) && (!isset($record[$no-1]) || $record[$no-1] == "")) { $no++; }
$recno = $no - 1;
$cont = 0;
if ($no < $jmlrec + 1) {
$row = explode("|~~|",$record[$recno]);
$vr = $row[4];
$cont = 0;
if (($vr == "ungesehen" || $vr == "gesehen" || $vr == "unentschlossen") && ($no <= $nomaybe)) $cont = 1;
if (($vr == "abgesagt" ) && ($no <= $nono)) $cont = 1;
if (($vr == "zugesagt" || $vr == "Gastgeber") && ($no <= $noyes)) $cont = 1;
if ($cont == 1) $no++;
}
} while ($cont == 1);
if (isset($record[$recno]) && $record[$recno] != "") {
if ($w==0) {
$warna = $table_content_1a;
$warna2 = $table_content_1b;
$w=1;
} else {
$warna = $table_content_2a;
$warna2 = $table_content_2b;
$w=0;
}
do {
$nomaybe++;
$recno = $nomaybe-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nomaybe <= $jmlrec && $row[4] != "ungesehen" && $row[4] != "gesehen" && $row[4] != "unentschlossen" );
if ($row[4] == "ungesehen" || $row[4] == "gesehen" || $row[4]=="unentschlossen") {
echo "<tr><td>$row[3]</td>";
if (isset($row[2]) && $row[2] != "" ) echo "<td><p>$row[2]</p></td>"; else echo "<td><p></p></td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
$countmaybe++;
}
if ($row[4] == "gesehen") echo "<td><b>gesehen</b></td><td>$row[8]</td>";
if ($row[4] == "unentschlossen") echo "<td><b>unentschlossen</b></td><td>$row[8]</td>";
if ($row[4] == "ungesehen") echo "<td><b>ungesehen</b></td><td></td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && ($row[4]=="gesehen" || $row[4]=="unentschlossen" || $row[4]=="ungesehen" ) && $row[7] != "")
echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
do {
$nono++;
$recno = $nono-1;
$row = explode("|~~|",$record[$recno]);
}
while ($nono <= $jmlrec && $row[4] != "abgesagt" && $row != "gesehen" && $row[5] != "unentschlossen" );
$row = explode("|~~|",$record[$recno]);
if ($row[4] == "abgesagt" ) { echo "<tr><td>$row[3]</td><td><p>$row[6]</p></td><td><b>abgesagt</b></td><td>$row[8]</td>";
echo "<td><span class='date'><b> </b></span></td>";
if (isset($row[7]) && $row[7] != "") echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countno++;
}
do {
$noyes++;
$recno = $noyes-1;
$row = explode("|~~|",$record[$recno]);
} while ($noyes <= $jmlrec && $row[4] != "zugesagt" && $row[4] != "Gastgeber" );
if ($row[4] == "zugesagt" || $row[4] == "Gastgeber" ) {
echo "<tr><td>$row[3]";
if ($row[4] == "Gastgeber") print " (Gastgeber) ";
echo "</td>";
if (isset($row[6]) && $row[6] != "" ) echo "<td><p>$row[6]</p></td>"; else echo "<td><p></p></td>";
echo "<td><b>zugesagt</b></td><td>$row[8]</td>";
if (isset($row[5]) && $row[5] != "") echo "<td><p>$row[5]</p></td>"; else echo "<td><p>1</p></td>";
if (isset($row[7]) && $row[7] != "" ) echo "<td><span class='comment'><i>$row[7]</i></span></td></tr>"; else echo "<td><span class='comment'></span></td></tr>";
$countyes++;
if (isset($row[5]) && $row[5] != "") $counttotal = $counttotal + intval($row[5]);
}
} //--end if
} //--end for
It would appear that updated entries are marked by prepending them with !. This loop appears to only be saving the updates, not every record.
Perhaps there was a typo - fopen with mode "w" will truncate the data file at 0 bytes before writing, if only the updated records are to be written, shouldn't that be mode "a" to append them to the existing file?
It is likely there is also a limit on how large a data file is allowed to grow, make sure you have set it large enough to allow 1000 records.

need help with php loop logic

This code works perfectly except that it doesn't print the last 2 rows of my csv file:
This is the file:
603629,0,ATLV0008,"Vendor1",1942.60,11/04/2010,1942.60,9/1-9/30/10,EFT-JP
603627,2,ATLV0008,"Vendor1",1242.40,11/04/2010,1242.40,7/1-7/31/10,EFT-JP
600023,0,FLD4V0003,"Vendor2",1950.00,06/15/2010,1950.00,6/14/10 Request,EFT-JP
600024,0,FLD4V0003,"Vendor2",1800.00,06/15/2010,1800.00,6/14/10 Request,EFT-JP
603631,0,ATLV5066,"Vendor2",1000.00,11/09/2010,1000.00,11/4/10 Check Request,PM2
603647,0,ATLV5027,"DVendor3",2799.80,11/15/2010,2799.80,10/1-10/31/10 Bishop,PM2
603642,5,ATLV5027,"Vendor3",482.40,11/15/2010,482.40,10/1-10/18/10 Allen,PM2
603653,0,ATLV0403,"Vendor4",931.21,11/17/2010,931.21,9/1-9/30/10,EFT-JP
603661,0,ATLV0105,"Vendor5",26.75,11/19/2010,26.75,093139,PM2
603660,1,ATLV0105,"Vendor5",5.35,11/19/2010,5.35,093472,PM2
Here is the code: (It needs to display the sum of 2 rows with the same vendor before the actual rows)
if (($handle = fopen('upload/ATLANTA.csv', "r")) !== FALSE) {
while (($row_array = fgetcsv($handle, 1000, ","))) {
foreach ($row_array as $key => $val) {
$row_array[$key] = trim(str_replace('"', '', $val));
ob_flush();
}
$complete[] = $row_array;
ob_flush();
}
}
flush();
$prevVendor = '';
$sum = 0;
$venData = '';
if (isset($complete)) {
foreach ($complete as $key => $val) {
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != NULL){
print "<br/>";
print $sum . "<br/>";
print $venData;
$sum = 0; $venData = '';
}
}
foreach ($val as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . '&nbsp';
$prevVendor = $val[3];
}
}
if ($currVendor == $prevVendor){
$sum += $complete[$key][6];
}
}
}
I don't really understand your problem but if you want to get (and save it wherever you want) the sum of each vendors, you should do something like this :
$prevVendor = null;
$venData = '';
$sums = array();
if (isset($complete) && is_array($complete)) {
$lim = count($complete);
for ($key=0;$key<$lim;++$key) { // no need foreach for simple array
$venData .= "<br/>";
$currVendor = $complete[$key][3];
if ($currVendor != $prevVendor){
if ($prevVendor != null){
print "<br/>";
print $venData;
}
$venData = '';
$prevVendor = $currVendor;
$sums[$currVendor] = 0; // set the counter to 0
}
foreach ($complete[$key] as $ikey => $ival){
if ($ikey != 1){
$venData .= $ival . ' '; // is useful for you I guess
}
}
$sums[$currVendor] += $complete[$key][6]; // add amounts
}
}
if ($prevVendor != null){ // you need to do this to display the last record
print "<br/>";
print $venData;
}
var_export($sums); // check results
This can be some syntax errors ...

Categories