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
Share on other sites

<?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
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.

Link to comment
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.