using array to keep track?

My form’s “Textarea” receives this data (as a matter of fact, exactly like this, I didn’t make it up haha):

dog,white
cat,white
cat,black
cat,white
dog,black
bird,white

After clicking “submit,” the exact desired result is:

bird: 1 white
cat: 2 white, 1 black
dog: 1 white, 1 black

I am having a dickins of a time trying to get an array to capture the quantities of white and black.

(And, I’m sure I’ll be embarrassed to find out the answer is probably one simple line of code…) :slight_smile:

Thank you in advance!

Try posting your code if you want help

A hint I will give you, you will need explode

I am willing to bet the data is not being pulled correctly from the source to achieve the desired end result.

Hello, I know about using php’s “explode” function

<?php print ''; $text = trim($_POST['textareaname']); $textAr = explode("\n", $text); $textAr = array_filter($textAr, 'trim'); foreach ($textAr as $line) { echo "$line
"; } print ''; I just need to know what stuff to do to my array to count that stuff

You still haven’t provided the source of your data. Why are you making it difficult to help you?

Because I did something STUPID – I tried to “dumb it down” so I could get a quick simple answer, and then I was going to apply the quick simple answer to what I REALLY needed help with. I’m sure I’m not the first to try this tactic – I just have this fear that if I tell my REAL question, it will scare everybody away and I’ll get NO answers… :-X

Anyway, my tactic backfired (obviously :’( )… so now I’ll “tell the truth” and explain what I really need.

Using a handheld scanner, I scan the barcode numbers from the back cover of my Used Textbooks into the textarea of my web-based PHP script form. Like this:

1234567890 2345678901 2345678901 2345678901 1234561231 3333333333 3333333333

As you can see above, there can be duplicate books.

Books, by default, are USED.

However, if some books are NEW, I scan a special “666” barcode right after I’ve scanned the new book.

For example, if the first book, second book, and the last book are NEW, here is what the scan would look like:

1234567890 666 2345678901 666 2345678901 2345678901 1234561231 3333333333 3333333333 666

Here is the “PHP Help” I need your help with.

Using the 2nd example above (where I scan the “666” because some of the books are NEW) After I click the “Submit” button, I want the result to look exactly like this:

1234567890: 0 used, 1 new
2345678901: 2 used, 1 new
1234561231: 1 used, 0 new
3333333333: 1 used, 1 new

Because I have NO knowledge about arrays, I really need your help. I know your answer will probably look something like this:

<?php #I already know how to get the textarea data into an array: # $data = explode(',',preg_replace("/[, ]+/",',',strtoupper(preg_replace("/[[:space:]\n\r]/i",',',$data)))); # Because I'm dumb about arrays, I am clueless what to do next. Please don't tell me to unplug my computer and take it back :D

Now we are getting somewhere. Before we get into this, what are ALL the options that are available to do with your scan data. We already know you can put every scan into the same textbox.

How does the scan data get to your web based form? Could you please provide a link to your scanner manufacturer and the model number.

  • Always post what you really need help with. We are here to help.

The last actual system I worked on was an RF scanner system used for order picking, bin lookup, and inventory. Based on that, why are you adding everything to a single textarea?

You can do a single text input that on change would send an ajax request that would enter it into a database. Our RF guns were more like Android powered tablets, but it is what the industry is moving too.

I am not grilling you on what you did or why you did it, but keeping things simple makes more robust code that is easier to maintain in the long run.

Mary, I agree with both Kevin’s and Astonecipher’s comments. But, I also feel you need to learn about how
arrays work. I created a page to solve your problem and added in various displays and comments to walk
you thru how it works. It displays two arrays so you can see how the data is stored into them.

Since your list is an array of entries, you could use the PHP function array_unique() to pull a list of numbers
without dups. Then, you could use that to reparse the array to look for each and create a third array that
would hold a new list of the required outputs. But, I found that extra work. I came up with a one-pass type
of routine that creates a second array with your counts in place. It looks a bit complicated at first because it
had to check for items that were not previously set up. ( PHP does not like you to access array values that
have never be set up. ) And, some code to do the check for “666” records and skip over them when they
were the primary key. The page has a form on it that posts to itself to process the entries in the textarea.
Then, it displays the results and two arrays for you to review.

Copy this to a new page. Upload to your server. Go to the page. It will show garbage as it does not have
the data in place as yet. Press the submit button and it will process the data you gave us and should show
the results with other details to help you learn about this. Of course, you would remove all the extra code
for these.

[php]

Testing Array Counter 1234567890 666 2345678901 666 2345678901 2345678901 1234561231 3333333333 3333333333 666

INPUT: <?PHP $data = $_POST["data"]; $data = explode(',',preg_replace("/[, ]+/",',',strtoupper(preg_replace("/[[:space:]\n\r]/i",',',$data)))); // First print to see the data... print_r($data); echo "

"; $count = count($data); echo "Total items found: " . $count . "

";

// Next loop thru the origianl and add counts into the new unique array using a secondary dimention…
$results = array();
for($i=0; $i<$count; $i++) {
// Check if next item is “666”, if it is, then mark as new, if not, just mark it…
if ($data[$i]!=“666”) { // Do not do this check for 666 entries…
if ($i<$count) { // If not last item (which can not have a following 666 or actually be an 666…)
if ($data[$i+1]==“666”) {
if (isset($results[$data[$i]][1])) {
$results[$data[$i]][1] = $results[$data[$i]][1] + 1;
} else {
$results[$data[$i]][1] = 1;
}
} else {
if (isset($results[$data[$i]][2])) {
$results[$data[$i]][2] = $results[$data[$i]][2] + 1;
} else {
$results[$data[$i]][2] = 1;
}
}
} else { // Not a 666 and is last item, so add it as USED…
if (isset($results[$data[$i]][2])) {
$results[$data[$i]][2] = $results[$data[$i]][2] + 1;
} else {
$results[$data[$i]][2] = 1;
}
}
}
}
// Now the new array’s [1] is the count of new ones and the [2] is all the non-new ones…
echo “

”;
echo "RESULTS data: ";
print_r($results);
echo “

”;
// Now display the unique entries and the counts…
echo “RESULTS:
”;
// Loop thru the results array checking for missing entries and display them in the correct format
foreach($results as $book => $value) {
echo $book . ": ";
if (isset($value[2])) {
echo $value[2];
} else {
echo “0”;
}
echo " USED “;
if (isset($value[1])) {
echo $value[1];
} else {
echo “0”;
}
echo " NEW
”;
}
?>

[/php]

Hope you learn something from this… Oh, also, I did not use any shortcodes so you can follow how it all
works. ( Short codes are sometimes hard to follow for a newbie… ) Good luck…

[member=43746]ErnieAlex[/member] , I believe your answer is for a problem she “thinks” she has and not really what is actually needed. She is scanning bar code data. I have to believe she is saving that data to a database which means the real solution is not manipulating arrays. I also suspect the data is not optimally captured and likely not optimally stored in the DB.

We have yet to be provided all the pertinent info to give a real solution. Nice job on going the extra mile.

I agree 100%, Kevin! But, she did mention she knew little about arrays. That is why I created a short page
to show what is inside them. Was just trying to make Mary think a little… Yes, she needs to tell us more
about her project. Not sure if this is for a lesson or a real life project. But, didn’t take very long to whip it up.

I would say this is a real issue. But there are a few parts to an inventory control system, one of the most important it the database schema. I wouldn’t mind use cases. You can simplify the problem, but I can also see that making the UX more cumbersome.

Yes, Astonecipher, I agree! Inventory control means database. And, normally, you would just enter one
item at a time, certainly not thru a text-area control box. but, no mention of the database or the actual use
of the system was mentioned. The original post was theory about cats and dogs. The real question involved
book ID’s and special codes. (“666”) Perhaps Mary will respond and let us know if she solved it or needs
more help.

I’m no expert with barcodes, but from I understand of them is that they give what the item is, location, amount and price which of course is driven by a database. My limited understanding is that you would read the information in from the scanner, save it to a database table (if adding it to the database) or read the information in from the database if just scanning it. The information would be put into an array or object if reading it from a monitor of some sort. If I were doing this I would put it in an object and I’m dating myself a little bit but when I first started programming they were referred as records. ;D

To me the OP is making it harder doing it this way, but like I said I know nothing of barcodes and just making a bunch of generalizations. I used to work for a paint manufacturer and when the laborers where filling the containers (pails or drums) they would scan the label to enter it into inventory. It was pretty cool system and eliminated a lot of errors. The laborer had the option of scanning in just one label per skid (32 5-gallon pails per skid (aka palette) or 4 drums (55 gallons each) per skid) or individually. If they had a partial they could enter the amount of gallons that was in the container also.

Yes, I hear you… I have played with a couple of them for clients at their offices. A lot of the inexpensive
ones basically just scan the barcode and turn it into text. I think I still have one in a box in storage. It
would scan to a text field. Then, if you selected to scan a second one, the first one would be posted to the
DB and you could just keep going. I am guessing that Mary has one of those and is loading all of the scans
into one large text-area instead of process each separately. I am sure she will explain once she reads all
of our posts.

Oh, and yes, why else would you use a barcode scanner if you don’t have it attached to a DB at some point?

The couple I worked with was for a doctor’s office to scan labels on folders that brought up patient data.
They could hold the barcoded folder over a reader and the data popped up on the screen. Another was a
salsa maker company. A large amount of product. Always smelled like cooked tomatoes in that building!

Sponsor our Newsletter | Privacy Policy | Terms of Service