Jump to content

Splitting & Processing Data from a Database


SteSpeak

Recommended Posts

Hi, I'm working on a project at the moment dealing with E-Commerce.

The products though which are allowed to be added are Tyres and Rims.

 

Anybody whose dealt with these in any way before should know you don't just walk into a shop

See a rim, pick it up and buy it incase it doesn't fit.

 

So my point is, with this type of product there is so much information that is required to be processed for 1 single rim.

I already knew about splitting values retrieved from a database, but it became a little bit more complicated.

 

In my database I plan on splitting my data into imaginary catagories (Rim Size).

So I use a forward slash (/) to determine when a catagory ends.

 

Each field in a row of the rims database will have the same amount of forward slashes.

And sometimes there may be several options for each rim, so I use a minus sign (-) to seperate data inside a catagory.

 

I had difficulty trying to split all of this data up so I am lookin for suggestions or advice.

 

I will quote the data I am trying to split up.

 

Available Finishes: 0/0/ms_rp/ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/0/0/0/0/0

 

Available Tyre Sizes: 0/0/5.5x13/6x14-6x14-6x14/7x15-7x15-7x15-7x15-7x15-7x15-7x15-7x15/7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16/8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17/8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8.5x18-8.5x18-8.5x18-8.5x18-8.5x18-8.5x18/0/0/0/0/0

 

Available Tyres

- Budget Tyres

- Premium Tyres

 

ET Numbers: 0/0/38/37-18-37/38-18-38-38-38-38-38-38/35-18-35-35-42-35-42-35-42-42-18-35/35-18-35-35-35-35-40-35-18-35/33-33-42-33-42-33-18-33-42-37-33-42-33-20-33-42/0/0/0/0/0

 

Stud Pattern: 0/0/4x100/4x100-4x108-5x100/4x100-4x108-4x108-5x100-5x108-5x110-5x112-5x114,3/4x100-4x108-5x100-5x108-5x108-5x110-5x110-5x112-5x114,3-5x120-5x120/4x100-4x108-5x100-5x108-5x110-5x112-5x112-5x114,3-5x120-5x120/5x100-5x108-5x108-5x112-5x112-5x114,3-5x120-5x120-5x120-5x130-5x112-5x112-5x114,3-5x120-5x120-5x120/0/0/0/0/0

 

Initial Cost: 0/0/73.50/78.00-78.00-78.00/81.00-81.00-81.00-81.00-81.00-81.00-81.00-81.00/96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00/121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50/156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00/0/0/0/0/0

 

If this is simply not possible to do, then I may need to try another option, such as making a ID Group for rims, and having each set of info as a individual row. But it's still a complicated system.

 

I apologise for the long post.

Thanks.

Take a look at the explode function. It might be too crude for your application (as I am not familiar with what it is you need exactly), but give it a try.

 

You could loop through and break up the string on each forward slash (/). If you needed to, you could then again split up the dashes (-) and use that data too.

 

If you start to form some code, we can definitely help you go from there. 

Definitely redesign your database and create new fields and/or table relations.

 

Here's a parsing example:

 

<pre>
<?php
$rows = array(
	'0/0/ms_rp/ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp-ms_rp/0/0/0/0/0',
	'0/0/5.5x13/6x14-6x14-6x14/7x15-7x15-7x15-7x15-7x15-7x15-7x15-7x15/7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16-7.5x16/8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17-8x17/8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8x18-8.5x18-8.5x18-8.5x18-8.5x18-8.5x18-8.5x18/0/0/0/0/0',
	'0/0/38/37-18-37/38-18-38-38-38-38-38-38/35-18-35-35-42-35-42-35-42-42-18-35/35-18-35-35-35-35-40-35-18-35/33-33-42-33-42-33-18-33-42-37-33-42-33-20-33-42/0/0/0/0/0',
	'0/0/4x100/4x100-4x108-5x100/4x100-4x108-4x108-5x100-5x108-5x110-5x112-5x114,3/4x100-4x108-5x100-5x108-5x108-5x110-5x110-5x112-5x114,3-5x120-5x120/4x100-4x108-5x100-5x108-5x110-5x112-5x112-5x114,3-5x120-5x120/5x100-5x108-5x108-5x112-5x112-5x114,3-5x120-5x120-5x120-5x130-5x112-5x112-5x114,3-5x120-5x120-5x120/0/0/0/0/0',
	'0/0/73.50/78.00-78.00-78.00/81.00-81.00-81.00-81.00-81.00-81.00-81.00-81.00/96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00-96.00/121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50-121.50/156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00-156.00/0/0/0/0/0',
);
foreach ($rows as $row) {
	echo '<b>', $row, '</b><br>';
	$sections = explode('/', $row);
	foreach ($sections as &$section) {
		$section = explode('-', $section);
	}
	print_r($sections);
}
?>
</pre>

Thanks for the quick reply, sorry for not explaining well.

 

Basically an admin can use a form to add new products.

Although the possibilities for each product is almost limitless.

It's really customizable

 

So my problem is, I'm never going to have a set amount of data within those sub-catagories (-)

So I need my webpage to be able to count, process and use the information.

 

I found that if I try and use an array, i'm unable to predict how many arrays there actually are and make something

useful out of it.

 

I hope this helps you to understand furthermore.

What about something like this, where you create a table for each feature, then link the tire to as many features as you need?

Tire

====

id | name

-----------------------

1  | Michelin Something

2  | Firestone Radial

 

Finish

========

id | name

----------------

1  | Rubber?

2  | White wall?

 

Tire_Finish

===========

tire_id | finish_id

-------------------

1      | 1

1      | 2

2      | 1

Hey sorry for the late reply, had a few troubles lately.

Thanks for the help I've thought about a way to split up the dynamic data.

 

There are 14 different Rim Sizes

 

10" - 12" - 13" - 14" - 15" - 16" - 17" - 18" - 19" - 20" - 21" - 22" - 24" - 26"

 

So the data in this field is always going to be static, and I use 0 or 1 as a "no / yes" with 1 being yes.

My data for this particular rim (size) is:

 

0/0/1/1/1/1/1/1/0/0/0/0/0/0

 

My script is able of detecting which rims are available... So if I use ET Numbers as an example of how I could process the data.

I plan on splitting every field into 14 arrays, each array resembling a rim size i.e.

 

$et_array[0]; to $et_array[13];

 

Each array holding the data for that rim size, so if we was to look at $et_array[3]; (Rim Size : 14")

It would show:

 

37-18-37

 

I know how to split this array at each minus.

So basically there are 3 different rims at size 14 available.

 

The possibilities though for my client are endless, he could add a rim with 17 rims at size 14, rather than 3.

So I need to be able to count how many values are in the array, so I can process the data.

 

Is it possible to count a dynamic array in terms of values before or after splitting it?

 

I have in mind, counting how many values are in the array (3) and then setting that to a temporary variable

And then use that variable with maybe a while / for statement to process the data.

 

I will start to work at some code examples whilst I wait to see if anyone has any suggestions.

I apologise for the long post but I hope this best explains what I plan on doing.

 

Best Regards

- Stephen

Ah, No worries I finally managed to do what I set out to do.

I'll post some snippets for anybody who might have similar problems

 

<?php

function test_script()
{
global $dbhost,$dbname,$dbuser,$dbpass;
$dbh = mysql_connect($dbhost,$dbuser,$dbpass);
	mysql_select_db($dbname);

header_1();

// For Testing Purposes, use a static ID
$rimid = "1";

$res = mysql_query("SELECT * FROM rims WHERE id = '$rimid'");
while ($row = mysql_fetch_array($res)){

	// Main Variables containing our rim data
	$id = $row[0];
	$rimcat = $row[1];
	$name = $row[2];
	$descen = $row[3];
	$descfr = $row[4];
	$wheels = $row[5];
	$sizes = $row[6];
	$et = $row[7];
	$sp = $row[8];
	$finish = $row[9];
	$price = $row[10];
	$image = $row[11];

	// Lets split up the wheel data by each "/"
	// $rims[0] - [13] contains either 0/1 this can be used as a switch
	$rims=split("[/]",$wheels);

	// Lets split the other data up into 14 arrays too
	$sizes_array=split("[/]",$sizes);
	$et_array=split("[/]",$et);
	$sp_array=split("[/]",$sp);
	$finish_array=split("[/]",$finish);
	$price_array=split("[/]",$price);


	if($rims[3] == "1"){

		echo "<table width=547 border=0 valign=top cellpadding=0 cellspacing=0>
			 <tr>
			 <td><p class=normal><b>No.</b></p></td>
			 <td><p class=normal><b>Tyre Size</b></p></td>
			 <td><p class=normal><b>ET Number</b></p></td>
			 <td><p class=normal><b>Stud Pattern</b></p></td>
			 <td><p class=normal><b>Finish</b></p></td>
			 <td><p class=normal><b>Price</b></p></td>
			 </tr>";

		// Split up our data
		$sizes_value=split("[-]",$sizes_array[3]);
		$et_value=split("[-]",$et_array[3]);
		$sp_value=split("[-]",$sp_array[3]);
		$finish_value=split("[-]",$finish_array[3]);
		$price_value=split("[-]",$price_array[3]);

		// Count all our values
		$c1 = count($sizes_value);
		$c2 = count($et_value);
		$c3 = count($sp_value);
		$c4 = count($finish_value);
		$c5 = count($price_value);

		$row_error = "0";
		$i = "0";

		if($c1 != $c2){ echo "<b>Error</b>: Values for Sizes & ET are not equal!<br>"; $row_error = "1"; }
		if($c1 != $c3){ echo "<b>Error</b>: Values for Sizes & SP are not equal!<br>"; $row_error = "1"; }
		if($c1 != $c4){ echo "<b>Error</b>: Values for Sizes & Finish are not equal!<br>"; $row_error = "1"; }
		if($c1 != $c5){ echo "<b>Error</b>: Values for Sizes & Price are not equal!<br>"; $row_error = "1"; }

		if($row_error == "0"){
			while ($i < $c1){

			$number = $i + 1;

				echo "<tr>
					 <td><p class=normal>".$number.".</p></td>
					 <td><p class=normal>".$sizes_value[$i]."</p></td>
					 <td><p class=normal>".$et_value[$i]."</p></td>
					 <td><p class=normal>".$sp_value[$i]."</p></td>
					 <td><p class=normal>".$finish_value[$i]."</p></td>
					 <td><p class=normal>".$price_value[$i]."</p></td>
					 </tr>";


				$i++;
			}
		}			

		echo "</table><br><br>";

	}			

}

header_2();

}

?>

 

Btw the snippet only shows the code for rim size 14, I would copy a chunk and paste then edit it for all the other rim sizes but it should give you an idea of what I did.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.