BoostedM3 Posted January 14, 2009 Share Posted January 14, 2009 I've got a MySQL field with rows containing data such as MKX - September 11, 2008<br>Sable - September 11, 2008<br>Milan - September 11, 2008<br>Mariner - September 11, 2008<br> I'm trying to explode this data at "<br>" and then again at "-" so that I'm left with only the "models" (ex: MKX, Sable, etc.) from there I'm trying to count the total number of occurences of the "models" in the entire database, while comparing this data with another field which has a value of either "sold" or "unsold". The ultimate goal is to be able to list the "models" with how many were "sold" or "unsold" The problem is, I'm not sure I can do that with the way the data is currently setup in the database... Hopefully I could get a few pointers here to steer me in the right direction. I'm pretty good with PHP and MySQL but this one has just got me stumped! Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/ Share on other sites More sharing options...
Zhadus Posted January 14, 2009 Share Posted January 14, 2009 <?php $string = 'MKX - September 11, 2008<br>Sable - September 11, 2008<br>Milan - September 11, 2008<br>Mariner - September 11, 2008<br>'; $newString = explode('<br>', $string); for ($x = 0; $x < sizeof($newString); $x++) { $values = explode('-', $newString[$x]); if ($values[0] != '') $models[] = $values[0] } ?> The array $models will contain your models as long as the database is formatted like that test string you provided. Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737074 Share on other sites More sharing options...
BoostedM3 Posted January 14, 2009 Author Share Posted January 14, 2009 Thank you kindly Zhadus, That part I had no problem doing, my main issue here is that I can't figure out how to count the number of times each "model" is marked either "sold" or "unsold" Ex: Say the output returns 20 results with the value "MKX" well I'm trying to figure out how many times the MKX was marked as "sold" MySQL Table Example: -------------------- Field: model ------ Data: ----- MKX - September 11, 2008<br>Sable - September 11, 2008<br>Milan - September 11, 2008<br>Mariner - September 11, 2008<br> Field: status ------ Data: ----- Sold Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737189 Share on other sites More sharing options...
Zhadus Posted January 14, 2009 Share Posted January 14, 2009 If you do as described and you get the list of models, and then pull the data (sold or unsold) into an array, the model and the information of whether it's sold or not, will share the same index number. i.e. Model: Status: MKX Sold Sable Unsold Milan Unsold Mariner Sold $models[0] = MKX & $status[0] = Sold $models[2] = Milan & $status[2] = Unsold Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737191 Share on other sites More sharing options...
BoostedM3 Posted January 14, 2009 Author Share Posted January 14, 2009 Thanks again Zhadus, Sorry for not pointing this out before, but in the event there were two of the same model in a single field (ex: MKX - September 11, 2008<br>MKX - September 11, 2008<br>) Would the index numbers not get skewed? Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737273 Share on other sites More sharing options...
premiso Posted January 14, 2009 Share Posted January 14, 2009 Thanks again Zhadus, Sorry for not pointing this out before, but in the event there were two of the same model in a single field (ex: MKX - September 11, 2008<br>MKX - September 11, 2008<br>) Would the index numbers not get skewed? They would not, since you are not referencing the array by "MKX" you are referencing it by index (0,1,2). Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737277 Share on other sites More sharing options...
flyhoney Posted January 14, 2009 Share Posted January 14, 2009 I think, combining everything that was talked about, you are looking for this: <?php // input $string = 'MKX - September 11, 2008<br>Sable - September 11, 2008<br>Milan - September 11, 2008<br>Mariner - September 11, 2008<br>'; // output $models = array(); $string = explode('<br>', $string); foreach ($string as $model_date) { if ($model_date) { list($model, $date) = explode('-', $model_date); $model = trim($model); if (trim($date)) { if (isset($models[$model])) { $models[$model]++; } else { $models[$model] = 1; } } } } print_r($models); ?> results: Array ( [MKX] => 1 [sable] => 1 [Milan] => 1 [Mariner] => 1 ) Quote Link to comment https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737286 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.