Page 1 of 2

[SOLVED] split() in PHP

Posted: Wed Dec 03, 2003 5:06 am
by UnimatrixZer0
Hey all,

I'm having a slight problem with one component of a "suite" of programs I'm creating.

The part I'm having the problem with is splitting a set of information I have in one field of a mySQL database.

The field would contain information like this:

|3315|Pencils|2|10.24|ON|3320|Textas|2|15.32|ON|||||||||||||||

The repeating "|" at the end means that the remaining 3 lines of info are blank.

These are the lines of code I made up to repeat breaking the information up, until there is nothing left to do:

while(list($articleNum, $productPackage, $quantity, $price, $gst) = split("|", $returnArticles))
{
// Process information details here.
}

However, I get this error "Warning: split(): REG_EMPTY" when I run the script. Can anyone help?

Posted: Wed Dec 03, 2003 5:16 am
by JayBird
The | character is a special character in regular expressions, which split() expects. So, you can use

Code: Select all

split("\|",$line)
which escapes the | character.


Although, since you're not really using a regular expression, you'd be
better off (more efficient) to just use

Code: Select all

explode('|',$line)
and have the same effect.

Posted: Wed Dec 03, 2003 5:19 am
by Luis Almeida
Hi,

Try using expolde, like the following;

Code: Select all

<?php
$split = explode("|", $data);
$a = $split[0]; 
$b = $split[1]; 
$c = $split[2]; 
.....

?>

Posted: Wed Dec 03, 2003 5:21 am
by UnimatrixZer0
I just changed it to your explode example, and it doesn't throw out an error anymore... But it seems to sit in a loop now?

Posted: Wed Dec 03, 2003 5:30 am
by JayBird
You haven't just changed your code to

Code: Select all

while(list($articleNum, $productPackage, $quantity, $price, $gst) = explode("|", $returnArticles)) 
{ 
// Process information details here. 
}
Have you?

You need to read your file into an array, then itterate through the array using foreach then use explode to extract each line.

Something like this

Code: Select all

// Open and Read file

forach($file as $file_line) {
    list($articleNum, $productPackage, $quantity, $price, $gst) = explode("|", $file_line);
}
Mark

Posted: Wed Dec 03, 2003 5:34 am
by UnimatrixZer0
Sorry to ask all these questions, but could you give an example of what you are talking about...

I've been writing PHP and using mySQL for a while, but I can't seem to overcome this problem. Thank you for all your help so far :)

Posted: Wed Dec 03, 2003 5:45 am
by JayBird
Just realised you are reading from a MySQL database, i really should read the questions more carefully :)

Can you show us more of your code including the queries and stuff, then i can help ya.

Mark

Posted: Wed Dec 03, 2003 5:50 am
by UnimatrixZer0
$esraQuery = mysql_query("SELECT * FROM eSRA WHERE id='$esra'");
$esraContent = mysql_fetch_array($esraQuery);

mysql_close();

This is the block of data in the field "returnArticles":

|1921|Pump|2|24||3315|Cherry Coke|4|13.52|ON|242|Coke|6|11.21|ON|||||||||||||||||||||||||||||||||||

Should I have each item on a new line, like this?:

1921|Pump|2|24|
3315|Cherry Coke|4|13.52|ON
242|Coke|6|11.21|ON
||||
||||

Posted: Wed Dec 03, 2003 5:58 am
by JayBird
can i ask why you are storing the data like this in the database?

Mark

Posted: Wed Dec 03, 2003 6:04 am
by UnimatrixZer0
There are a whole heap of other fields pertaining to other information...

This particular set of details (returnArticles field) must be directly related to the record, and because there would be a lot of information going through the database, I didn't want to make another database that stores each individual line of information submitted with the form and relate it back to the other information record.

It's sort of hard to explain... But I thought the way I've done it would be easier.

Posted: Wed Dec 03, 2003 6:20 am
by JayBird
you could have had another table to store the info in returnArticles, then link the two table together using unique id's to tie the information together.

After, thats what databases are for, storing information.

But, if you don't wanna do that and continue in the way you have started, i'm not sure i can help cos i don't understand the information you are trying to split.


Mark

Posted: Wed Dec 03, 2003 6:25 am
by UnimatrixZer0
I suppose I thought that storing all the individual return article lines in a seperate database and linking them together would require heaps of information being exchanged all over the place... Where if I put it all in one record, it would save a lot of time and effort for the purpose the script is being created for.

Posted: Wed Dec 03, 2003 6:37 am
by UnimatrixZer0
Here is the entire code, if that helps you understand my problem a bit better? Thanks again.

mysql_connect("localhost", "user", "password") or die("Unable to connect to MySQL");
mysql_select_db("database") or die("Unable to select database");
$esraQuery = mysql_query("SELECT * FROM eSRA WHERE id='$esra'");
$esraContent = mysql_fetch_array($esraQuery);

mysql_close();

echo <<<END
<table border="0" cellspacing="1" cellpadding="5" bgcolor="#C0C0C0">
<tr>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Article#</b></font></td>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Product/Package</b></font></td>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Qty.</b></font></td>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Price</b></font></td>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Value</b></font></td>
<td bgcolor="#333333"><font face="Verdana" size="1" color="#FFFFFF"><b>Tax</b></font></td>
</tr>
END;

while(list($articleNum, $productPackage, $quantity, $price, $gst) = explode("|", $returnArticles))

{

$value = $price * $quantity;
$value = round($value, 2);

if($gst == "ON")
{
$gstCost = $value * 1.1;
$gstCost = round($gstCost, 2);
}
else
{
$gstCost = "0.00";
}

$totalValue = $totalValue+$value;
$totalTax = $totalTax+$gstCost;

echo <<<END
<tr>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$articleNum</font></td>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$productPackage</font></td>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$quantity</font></td>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$price</font></td>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$value</font></td>
<td bgcolor="#FFFFFF"><font face="Verdana" size="1">$gstCost</font></td>
</tr>
END;
}

$completeTotal = $totalValue+$totalTax;
$completeTotal = round($completeTotal, 2);

echo <<<END
<tr>
<td bgcolor="#999999" colspan="4" align="right"><font face="Verdana" size="1"><b>Sub-totals</b></font></td>
<td bgcolor="#999999"><font face="Verdana" size="1">$totalValue</font></td>
<td bgcolor="#999999"><font face="Verdana" size="1">$totalTax</font></td>
</tr>
<tr>
<td bgcolor="#333333" colspan="4" align="right"><font face="Verdana" size="1" color="#FFFFFF"><b>Total
$</b></font></td>
<td bgcolor="#333333" colspan="2"><font size="1" face="Verdana" color="#FFFFFF">$completeTotal</font></td>
</tr>
</table>
END;

Posted: Wed Dec 03, 2003 6:38 am
by JayBird
if you can tell me how you want

|3315|Pencils|2|10.24|ON|3320|Textas|2|15.32|ON|||||||||||||||

splitting, i might be able to help ya

What are you doing with it once it is split?

I take it that line contain more than one product!?

Mark

Posted: Wed Dec 03, 2003 6:42 am
by UnimatrixZer0
That line you have quoted has two products in it... and a few blank things at the end.

I could make it so each product is on a new line in the record, ie:

3315|Pencils|2|10.24|ON
3320|Textas|2|15.32|ON
||||
||||

If you think it would be easier to extract the data?