Jump to content

[SOLVED] Incrementing value in PHP


LemonInflux

Recommended Posts

OK, here's what I have:

 

I have a file, for the purpose of demonstration, we shall call it test.php.

Within test.php, we have the following:

 

1||tags more stuff to search for||article of some description here||comments

 

And then, we might have another line such as:

 

2||other tags to search for||article of some description here||comments

 

Right, here's my question unto you PHP freaks;

 

I am capable of setting up comments, articles, tags, everything but the first number. So, to cut the whole thing short, I'm looking for a way of getting PHP to read the file, look for the highest value number in that first array, add 1 to it, and write a line that will be:

 

Highest+1||tags||article||comments

 

So, is there anyway to do this? I'm sure it'll be a function called MAX or something, where you just look for the highest value in that using a foreach loop, but I haven't found one. any ideas?

Link to comment
https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/
Share on other sites

If you can't use a database and are stuck with a flat file.....

 

you'll need to loop over each line, getting the first value of each line (I'd use explode on the double-pipes), find the max, then add 1. I'd keep track of the biggest number and replace it if/when the first value is larger than the biggest number:

 

// Inside loop

$bignum = ($newnum > $bignum)?$newnum:$bignum;

What I mean is:

 

foreach($data as $line)
{
$info = explode('|', $line);
$info[0] *code here*;
}

 

I'm not sure how to do it at all. I guess I could use the max function, but I have no idea how I'd work with it for this. It finds the max number in a set of numbers.

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.