Jump to content

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.

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.