LemonInflux Posted September 23, 2007 Share Posted September 23, 2007 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? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/ Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Hah, oops, max worked. Topic solved. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353433 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Upon reading through, I'm still none-the-wiser. Anyone have any ideas about this? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353435 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 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; Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353464 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 A) Yeah, has to be flat for this project. B) Full code example, please? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353477 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 bump.. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353482 Share on other sites More sharing options...
BlueSkyIS Posted September 23, 2007 Share Posted September 23, 2007 Full code example, please? Seriously?? What have you tried? Some tips: there is a function that reads all lines of a file into an array. Then loop over the elements of the array, exploding each one to get the first number. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353487 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 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. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353506 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 If you always append a record with the new highest value, the highest will aways be the last one. <?php $data = file('test.txt'); $last = end($data); list($highval) = explode ('||', $last); Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353514 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 Oooh, I see. So then, to add a new record 1 highger, you'd just to ($last(highval) + 1);? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353516 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 My code extracts the highest value in the file so far. Your next will be $highval + 1 Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353520 Share on other sites More sharing options...
rarebit Posted September 23, 2007 Share Posted September 23, 2007 Yes, we like 'end()', a new one on me, but what if the file isn't fully chronological? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353523 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 My solution was conditional If you always append a record with the new highest value, ... but it does seem that that is the intent. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353525 Share on other sites More sharing options...
LemonInflux Posted September 23, 2007 Author Share Posted September 23, 2007 It's always going to be like that, yes. Although, they might end up not being entirely chronological, but if it's selecting the last, even if the numbers are 1, 200, 201, 250, it'll still pick 250, right? Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353531 Share on other sites More sharing options...
Barand Posted September 23, 2007 Share Posted September 23, 2007 Best way to find out is try it. Quote Link to comment https://forums.phpfreaks.com/topic/70359-solved-incrementing-value-in-php/#findComment-353537 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.