Page 1 of 2

unserializing - removing comma on last entry

Posted: Thu Aug 17, 2006 7:39 pm
by matt1019
Hi Guys,

I have done most of the dirty work:
adding checkboxes and using "serialize()" to add it to the database, then then calling "unserialize()" to parse the data... with desired seperation**...

**desired separation = Using commas

However, how can I remove a comma after the last word from the database?

Here's how I am "parsing" out of the database:

Code: Select all

$getuhf = unserialize($data['user_favanimals']);

foreach ($getuhf as $listuhf)
{
	echo $listuhf.', ';
}
I know it has to do with this line:

Code: Select all

echo $listuhf.', ';
as that is the line that spits out records from the DB.... but what?

When viewed, I get:
Lion, Tiger, Donkey, Fox,
Notice the comma after Fox... I would like to limit the comma insertion to the second to last word... i.e., it should not insert comma after donkey.

Thank You in Advance ;)

-Matt

Posted: Thu Aug 17, 2006 7:43 pm
by feyd
Don't echo, store it to a string. Then you can use substr(). Alternately, you may be interested in implode().

Posted: Thu Aug 17, 2006 7:43 pm
by s.dot

Code: Select all

$getuhf = unserialize($data['user_favanimals']);
$getuhf = implode(', ',$getuhf);
echo $getuhf;

Posted: Thu Aug 17, 2006 7:48 pm
by matt1019
@feyd:
Thanks, I used Scottayy's suggestion and worked perfectly... also, I am reading up on the substr() right now... may need it in the near future it looks like. Thanks feyd.

@scottayy:
Thanks... works just as expected/wanted. Thanks scottayy!

Another thing to note: when I used scottay's suggested code, I didn't need to use the "foreach" function. Good thing... no loops!

EDIT: This makes me think... if I had not used foreach in my original "faulty" code, would I have still been able to fetch the records... hmmm.

Thank you guys,

-Matt

Posted: Thu Aug 17, 2006 8:01 pm
by s.dot
Or you could be creative.

Code: Select all

$arr = unserialize($data);
$count = count($arr);
$str = '';

$i = 0;
foreach($arr AS $el)
{
   if($i == 0)
   {
      $str .= $el;
   } else
   {
      $str .= ', '.$el;
   }
   $i++;
}

echo $str;
:lol:

Posted: Thu Aug 17, 2006 8:03 pm
by feyd
scottayy wrote:Or you could be creative.

Code: Select all

$arr = unserialize($data);
$count = count($arr);
$str = '';

$i = 0;
foreach($arr AS $el)
{
   if(($i != 0) && ($i < $count))
   {
      $str .= ', '.$el;
   }
   $i++;
}

echo $str;
:lol:
psst, code skips the first element of the array.

Posted: Thu Aug 17, 2006 8:05 pm
by s.dot
boooooooo there goes me appearing all cool

Posted: Thu Aug 17, 2006 8:09 pm
by matt1019
hmm gets the job done too.

Thanks!! Scottayy!!! :) :)

Another quick question... related to the same topic.
This is is a bit tricker though :P

Ok, in the "Bio Secion" where users set thier favorite animals, how can I automatically check the boxes if they have saved lion as their favorite animal?

did I confuse anybody?
ok, here's what I mean:

Let's say I have:
Lion, Tiger, Fox
As my favorite animals....

When I go back to change it, how can the "Lion, Tiger, Fox" be auto checked? (as I have already saved them in the DB as my fav. animals?

here's my checkboxes:

Code: Select all

<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Lion\" /> Lion
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Tiger\" /> Tiger
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Donkey\" /> Donkey
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Fox\" /> Fox
So, based on my example, I should have:

Code: Select all

<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Lion\" checked/> Lion
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Tiger\" checked/> Tiger
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Donkey\" /> Donkey
<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Fox\" checked/> Fox
Thanks Guys,

-Matt

Posted: Thu Aug 17, 2006 8:13 pm
by s.dot
You already have your favorite animals in an array. So use that.

Example:

Code: Select all

$animals = unserialize($data);  // or however else you get them into an array

echo "<input type=\"checkbox\" id=\"favanimals[]\" name=\"favanimals[]\" value=\"Lion\"";

if(in_array('Lion',$animals))
{
   echo ' checked';
}

echo " /> Lion";

Posted: Thu Aug 17, 2006 8:19 pm
by matt1019
Genius, scottayy, genius!

Simply Genius!!!

I have never looked at "in_array" before as I had very minimal to do with arrays in PHP in the past... but now, that is next on my read-it-up list.... after I get done with substr()

thanks once again ;)

-Matt

Posted: Thu Aug 17, 2006 8:29 pm
by s.dot
Arrays are invaluable.

http://us2.php.net/array

Posted: Thu Aug 17, 2006 8:34 pm
by matt1019
More on the reading table??? 8O 8O

ME = EXCITED!!! :P :P

It's funny... I can't motivate myself to read until I find that I need to.... guess I should have started with php as a "in-school-class" instead of a "hobby/passion"

-Matt

Posted: Thu Aug 17, 2006 8:36 pm
by s.dot
Mine's totally been learning as a hobby/passion. Slow process though.

PS: I like our chat thread. :lol:

Posted: Thu Aug 17, 2006 8:39 pm
by feyd
If you're excited by the array functions, you may also joygasm over the string functions.

Posted: Thu Aug 17, 2006 8:39 pm
by matt1019
:D :D before one of the mods come in and say "Off Topic" :D
Slow process though.
at least your process is 12 notches faster than mine :D:D

-Matt