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 Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/ 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 ??? Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561917 Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 bump Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561929 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561931 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 Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561933 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561936 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 } } Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561942 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561946 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561951 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561955 Share on other sites More sharing options...
Schlo_50 Posted June 10, 2008 Author Share Posted June 10, 2008 Yeah. Sure does! Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561962 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? Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561963 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-561964 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 Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-562052 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-562082 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. Link to comment https://forums.phpfreaks.com/topic/109545-solved-get-last-value/#findComment-563011 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.