Howdy!
I keep learning and learning. However, I need a little help.
First of all, the server does not support MySQL, therefore, I had to use a flat file system. I managed to make the flat file names random enough to be difficult to guess. So far so good.
This is a simple shopping cart system. I added a submit button next to each item (actually with the Item Number instead of the word “submit.” This allows PHP to add an entry into a flat file. That with a little sessions coding and refreshing the page after each submit, I have it solved and actually creating a flat file for the visitor. So far so good.
I can read the flat file into a “tables” setup – I set-up the data so it uses the delimiter “|” and that allows me to deal with the incoming data as fields with a different line per entry. So far so good.
I have two problems that may or may not be solved by PHP.
- Rather then try to sift through the flat file looking for duplicate entries (a visitor can merely press a button several times to accomplish this) - I think that preventing a duplicate entry may be the way to go. Is there a way to set an increment counter (using sessions, of course, so that when the visitor returns to the page from another page, the count is still available) to track an item number (i.e. D1, D2, D3, etc.) and it is equal to one or even if it exists, by-pass the write routine? 😕
Here is basically what I have:
Form Submit Section
<form action="catalogTwo.php4" method="POST">
<img src="http://www.whatever.com/catalogs/D01.jpg" alt="D01"><br />
<input type="submit" value="D1" name="D1">
</form>
Flat File Coding
$D1 = $SESSION['D1'];
$D1 = $POST['D1'];
if ($D1 == "D1")
{
$Full_unique = session_id();
$unique = substr($Full_unique, 0, 6); // $unique = session_id();
$myFile = $unique.".txt";
$fh = fopen($myFile, 'a') or die("can't open file");
$stringData = "D1 | Pink Belt with Silver Buckle | $100\n";
fwrite($fh, $stringData);
fclose($fh);
}
I could work from a short sample code if someone will be so kind.
- When the visitor views the flat file results in a tables set-up, is there a way that I can offer him or her the option to delete an item?
Here is what I have so:
<?php
$fp = fopen($myFile, 'r');
if (!$fp) {echo 'ERROR: Unable to open file.</table></body></html>'; exit;}
while (!feof($fp)) {
$line = fgets($fp, 1024); //use 2048 if very long lines
list ($field1, $field2, $field3) = split ('|', $line);
echo '
<tr>
<td align="center" valign="middle">'.$field1.'</td>
<td align="left" valign="middle">'.$field2.'</td>
<td align="center" valign="middle">'.$field3.'</td>
</tr>';
$fp++;
}
fclose($fp);
?>
By the way, my client has agreed to change servers after Christmas. Then I will set-up a MySQL database and redo this right.
Thanks in advance,
Bill 😉