Schlo_50 Posted June 10, 2008 Share Posted June 10, 2008 Hi there, I have a .DAT file and I need some help in obtaining a value thats in the last row of it. the value I need is $canid, but I don't know how to only get the last rows value instead of an array of values. $file = file("./admin/data/candidates.DAT"); $file = array_reverse($file); foreach($file as $i => $val){ $data[$i] = explode("|", $file[$i]); if($data[$i][5] == "uploaded"){ list($name, $email, $tel, $cv, $canid, $uploaded) = explode("|", $file[$i]); } else { list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $file[$i]); } print $canid; } Could someone please advise? Thanks Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 All I need to do is find the last row in my .DAT file. I think I can do the rest from that point. Can anybody help me to find the last row? Thanks ??? Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 bump Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 <?php function last_line ($filename) //Returns the last line of a file { $lines = file($filename); return $lines[count($lines)-1]; } ?> Let me know if you need further help with retrieving that specific value you were talking about. Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 Thanks! Yeah, In the example I gave, I need $canid from the else statement which is value [20] from that list (starting from 0). Thats the value in particular I need. Thanks again Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 Try this maybe: <?php $lines = file("./admin/data/candidates.DAT"); $last = $lines[count($lines)-1]; unset($lines); //Free some memory $vals = explode("|", $last); echo $vals[20]; //$canid unset($vals); //Free memory ?> If this doesn't work, please show an example of a line. Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 The following is printing out multiple values of the 20th value on the first line. $file = file("./admin/data/candidates.DAT"); $file = array_reverse($file); $last = $file[count($file)-1]; foreach($file as $i => $val){ $data[$i] = explode("|", $file[$i]); if($data[$i][5] == "uploaded"){ list($name, $email, $tel, $cv, $canid, $uploaded) = explode("|", $file[$i]); } else { list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $file[$i]); $vals = explode("|", $last); echo $vals[20]; //$canid unset($vals); //Free memory } } Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 You didn't open the foreach... <?php $file = file("./admin/data/candidates.DAT"); $file = array_reverse($file); foreach($file as $i => $val){ { $data[$i] = explode("|", $val); if($data[$i][5] == "uploaded"){ list($name, $email, $tel, $cv, $canid, $uploaded) = explode("|", $val); } else { list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $val); } echo $canid."<br>"; } ?> By the way, why are you printing the data upside down? Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 I have a mixture of row lengths in my .DAT file. Some rows are 5 keys in length while the rest are more. Reversing the file seemed the best way to me for using the list() function. Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 This is how I'd do it.. Tell me if it works. <?php $file = file("./admin/data/candidates.DAT"); foreach($file as $i => $val){ { $data[$i] = explode('|', $val); if($data[$i][5] == "uploaded") $canid = $data[$i][4]; else $canid = $data[$i][20]; echo $canid."<br>"; } ?> Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 Yeah. Sure does! Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 Im still having problems integrating what you showed me into my code though.. $file = file("./admin/data/candidates.DAT"); $file = array_reverse($file); foreach($file as $i => $val){ { $data[$i] = explode("|", $val); if($data[$i][5] == "uploaded"){ list($name, $email, $tel, $cv, $canid, $uploaded) = explode("|", $val); } else { list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $val); } $fileb = file("./admin/data/candidates.DAT"); $last = $fileb[count($fileb)-1]; $vals = list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $last); echo $fileb[20]; } } Whats wrong with that? Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 Wait a second.. Could you explain once clearly what exactly are you trying to do with the whole piece of code? Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 I need value 20 ($canid) of the last row of my candidates.DAT file to be the value for my hidden field. <input type="hidden" name="v" value="<? $file = file("./admin/data/candidates.DAT"); $file = array_reverse($file); foreach($file as $i => $val){ { $data[$i] = explode("|", $val); if($data[$i][5] == "uploaded"){ list($name, $email, $tel, $cv, $canid, $uploaded) = explode("|", $val); } else { list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $val); } $fileb = file("./admin/data/candidates.DAT"); $last = $fileb[count($fileb)-1]; $vals = list($name, $gender, $address, $town, $county, $postcode, $dobd, $dobm, $doby, $tel, $worktel, $mob, $email, $qualifications, $skills, $employment, $work, $availability, $job, $salary, $canid, $notes, $placed, $consultant) = explode("|", $last); echo $fileb[20]; } } ?>"> Thanks Quote Link to comment Share on other sites More sharing options...
Orio Posted June 10, 2008 Share Posted June 10, 2008 So why use all the loops and everything to fetch all of the data? If you are looking only for that value, you don't need to store all of the data in an array. Simply use the piece of code I supplied before: <?php $lines = file("./admin/data/candidates.DAT"); $last = $lines[count($lines)-1]; unset($lines); //Free some memory $vals = explode("|", $last); echo $vals[20]; //$canid ?> And if this row might be a 5 columned row, this is what you need: (Works both for short and long rows) <?php $lines = file("./admin/data/candidates.DAT"); $last = $lines[count($lines)-1]; unset($lines); //Free some memory $vals = explode("|", $last); if($vals[5] == "uploaded") $canid = $vals[4]; else $canid = $vals[20]; echo $canid; ?> If that's not what you're looking for, I totally lost you. Orio. Quote Link to comment Share on other sites More sharing options...
Schlo_50 Posted June 11, 2008 Author Share Posted June 11, 2008 Just what I was after, Thanks. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.