[SOLVED] split() in PHP
Moderator: General Moderators
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
[SOLVED] split() in PHP
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?
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?
The | character is a special character in regular expressions, which split() expects. So, you can use
which escapes the | character.
Although, since you're not really using a regular expression, you'd be
better off (more efficient) to just use
and have the same effect.
Code: Select all
split("\|",$line)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)- Luis Almeida
- Forum Commoner
- Posts: 33
- Joined: Tue Apr 01, 2003 4:22 am
Hi,
Try using expolde, like the following;
Try using expolde, like the following;
Code: Select all
<?php
$split = explode("|", $data);
$a = $split[0];
$b = $split[1];
$c = $split[2];
.....
?>-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
You haven't just changed your code to
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
Mark
Code: Select all
while(list($articleNum, $productPackage, $quantity, $price, $gst) = explode("|", $returnArticles))
{
// Process information details here.
}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);
}-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
$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
||||
||||
$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
||||
||||
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
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.
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.
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
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
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
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.
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am
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;
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;
-
UnimatrixZer0
- Forum Newbie
- Posts: 17
- Joined: Wed Dec 03, 2003 5:06 am