Enjoy an ad free experience by logging in. Not a member yet?
Register .
01-13-2012, 05:32 PM
PM User |
#1
New to the CF scene
Join Date: Dec 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Split string and get first 4 characters
I have got a script running where I need to split a string and update parts of it. Would now like to update parts of it depending the value of part of the string.
PHP Code:
//$string = "2011-12-13;;1;;0;;3,2011-12-14;;1;;0;;1,2011-12-15;;1;;0;;5,2011-12-16;;1;;0;;1;"; $updatedSets = array(); $updatedString = '' ; $sets = explode ( ',' , $string ); foreach( $sets as $set ) { $setArray = explode ( ';;' , $set ); //$setArray[2] corresponds to the season //$setArray[1] corresponds to the price //echo "<br />".$set; if( $setArray [ 3 ] == 1 ) { $setArray [ 2 ] = $high ; } array_push ( $updatedSets , implode ( ';;' , $setArray )); } $updatedString = implode ( ',' , $updatedSets );
I had managed to get the first 4 characters of
.
It was somthing like this:
PHP Code:
$thisyear= $setArray ; echo substr ( $thisyear , 0 , 4 );
But I am not 100% sure how to tie it in with my script so that I can achieve somthing like the following:
PHP Code:
if(( $setArray [ 3 ] == 1 )AND( $thisyear == 2012 )) { $setArray [ 2 ] = 500 ; }
So that only sections of the string which have 2012 in them are updated.
Any pointers would be greatly appreciated.
Cheers
Rich
01-13-2012, 07:26 PM
PM User |
#2
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
How many strings will there (or could there) be?
And how often do you have to do this checking and updating?
I'm thinking about putting it all into a temporary MySQL table,
doing all of the crunching, and regenerating the table back
into a string. MySQL queries are fast and efficient, and much
easier to do than PHP line-by-line commands.
.
01-13-2012, 07:59 PM
PM User |
#3
New to the CF scene
Join Date: Dec 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
Effectively there will be one string updated manually occasionally.
Its for a calendar system so the string has data for everyday of about 2 years.
Cheers
Rich
01-13-2012, 08:10 PM
PM User |
#4
New Coder
Join Date: Jan 2012
Posts: 10
Thanks: 0
Thanked 1 Time in 1 Post
Quote:
Originally Posted by
meadsy25
Effectively there will be one string updated manually occasionally.
Its for a calendar system so the string has data for everyday of about 2 years.
Cheers
Rich
That sounds like a very ineffective way of storing that kind of data.
You should really consider using a database to store the data.
That will not only make it easier, but it will be alot safer aswell.
01-13-2012, 08:31 PM
PM User |
#5
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
I was going to ask more about how and why the data is stored like it is.
Perhaps it's some existing system they have in place? Maybe Meadsy can
elaborate more on the importance of the data structure and why it has to
be like that.
.
01-13-2012, 08:46 PM
PM User |
#6
New to the CF scene
Join Date: Dec 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
It is in a database, but unfortunately it's stored this way from a previous system.
01-13-2012, 10:33 PM
PM User |
#7
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
What kind of database is it in?
Stored in some sort of text file?
If I were to attempt to read your database,
how would I do that? Is it on your server, online somehow?
01-13-2012, 11:12 PM
PM User |
#8
New to the CF scene
Join Date: Dec 2011
Posts: 8
Thanks: 1
Thanked 0 Times in 0 Posts
It is just a MySQL table that has a single string in it.
Here's a snippet.
Code:
2012-01-13;;1;;400;;2;;3,2012-01-14;;1;;400;;2;;3,2012-01-15;;1;;400;;2;;3,2012-01-16;;1;;400;;2;;0,2012-01-17;;1;;400;;2;;0,2012-01-18;;1;;400;;2;;0,2012-01-19;;1;;400;;2;;0,2012-01-20;;1;;400;;2;;0,2012-01-21;;1;;400;;2;;0,
I know its not the best way to store data but its all I have to work with for now.
01-14-2012, 01:41 AM
PM User |
#9
Master Coder
Join Date: Jun 2003
Location: Cottage Grove, Minnesota
Posts: 9,056
Thanks: 8
Thanked 1,032 Times in 1,023 Posts
Describe what you might "find" and "change" ...
Example, you want to find some string and change it ...
What is the name of the table(s) where the string(s) are stored?
What is the current name of your column(s) where that large string is stored.
How many rows is it?
If we know the structure of your MySQL table, maybe there's a better way to do this.
Jump To Top of Thread
Thread Tools
Rate This Thread
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
All times are GMT +1. The time now is 08:30 AM .
Advertisement
Log in to turn off these ads.