Jump to content

Recommended Posts

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!

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/
Share on other sites

<?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.

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737074
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737189
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737191
Share on other sites

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).

 

 

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737277
Share on other sites

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
)

Link to comment
https://forums.phpfreaks.com/topic/140824-explode-and-compare/#findComment-737286
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.