unserializing - removing comma on last entry

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

unserializing - removing comma on last entry

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Don't echo, store it to a string. Then you can use substr(). Alternately, you may be interested in implode().
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Code: Select all

$getuhf = unserialize($data['user_favanimals']);
$getuhf = implode(', ',$getuhf);
echo $getuhf;
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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:
Last edited by s.dot on Thu Aug 17, 2006 8:09 pm, edited 2 times in total.
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

boooooooo there goes me appearing all cool
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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";
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Arrays are invaluable.

http://us2.php.net/array
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post 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
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post by s.dot »

Mine's totally been learning as a hobby/passion. Slow process though.

PS: I like our chat thread. :lol:
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

If you're excited by the array functions, you may also joygasm over the string functions.
matt1019
Forum Contributor
Posts: 172
Joined: Thu Jul 06, 2006 6:41 pm

Post 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
Post Reply