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

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.