Jump to content

[SOLVED] Get last value


Schlo_50

Recommended Posts

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

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.

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

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.

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.

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?

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

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.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.