dcahrakos Posted September 3, 2008 Share Posted September 3, 2008 Hey, I am currently working on a project that stores user data into a field in a mysql db like so: username|1|1|Dcahrakos<end> random|10|10|Just some Text<end> and I need to loop through all the data in the field, and first separate it by the <end> tag, then after that is done, I need to separate it by the | tags. Now, for the life of me I cant get a working script that will loop through all the data, split it, and store all the data that gets split into variables...I need to eventually parse this data for a script that will generate an image based on this data. So my question is, does anyone know a good way to loop through all the data(as there will be varying amounts), then get the array's into separate variables for each? I just cant seem to loop through and store all the data into an array for some reason, everything ive tried has failed. any help would be greatly appreciated. Thanks. Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted September 3, 2008 Share Posted September 3, 2008 What's your query string look like most of the data manipulation should be done by the database. Quote Link to comment Share on other sites More sharing options...
dcahrakos Posted September 3, 2008 Author Share Posted September 3, 2008 Well right now I just have it fetching the data out of the database like so $query = mysql_query("SELECT * from sig WHERE uid='1'") or die(mysql_error()); $result = mysql_fetch_array($query); $textdata = $result['textdata']; so after I get it from the database, then is when I have to split, and organize everything into arrays, then parse the data. Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 <?php $textdata = 'username|1|1|Dcahrakos<end> random|10|10|Just some Text<end>'; $group = explode( '<end>', $textdata ); $arraydata = array(); foreach ( $group as $part ) { $part = trim($part); if ( !empty($part) ) $arraydata[] = explode( '|', $part ); } print_r( $arraydata ); ?> Quote Link to comment Share on other sites More sharing options...
dcahrakos Posted September 3, 2008 Author Share Posted September 3, 2008 Thats perfect, now all I will have to do is loop through the array, then parse each element. Thanks alot! Quote Link to comment Share on other sites More sharing options...
discomatt Posted September 3, 2008 Share Posted September 3, 2008 It's also possible to make an associative array with the script, or use the username as the key of the array's first dimension. Simply store the exploded $part in a placeholder array, and define $arraydata[ $placeholder[0] ] = array( 'num1' => $placeholder[1], 'num2' => $placeholder[2], 'text' => $placeholder[3] ); Quote Link to comment Share on other sites More sharing options...
dcahrakos Posted September 3, 2008 Author Share Posted September 3, 2008 The way it is now, actually works perfectly for me, Just get the size of the first dimension, then do a loop through them all and parse the second dimension since the second dimensions size never changes, there will always be 4 elements in it. Thanks for the help Quote Link to comment Share on other sites More sharing options...
DyslexicDog Posted September 3, 2008 Share Posted September 3, 2008 Why are you storing all your data in one column in your database? The point of a database is to handle these kind of things. Quote Link to comment Share on other sites More sharing options...
DarkWater Posted September 3, 2008 Share Posted September 3, 2008 That's a horrible way to store database information. Read up on "database normalization". Quote Link to comment 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.