Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-06-2013, 09:44 PM   PM User | #1
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 45
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
Displaying Array Values

I am having a hard time displaying an array values once. when I loop through the first value repeats itself. how do I display the array values in a single row.

I should see the following:

English, Spanish

Code:
while($t = mysql_fetch_array($genres)) {
     $gens[] = $t['gen_name'];				   
     foreach ($gens as $msg) {					
           $v .= $msg . ", ";						
     }
}
chellert is offline   Reply With Quote
Old 02-06-2013, 10:44 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,650
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
This is due to you adding to an array. Since $gens[] is appending on each iteration of $genres, and then within each iteration of $genres it iterates $gens, it will just grow as each record goes through. Lets say you have three records of $genres, 'pop', 'rock', 'jazz' in that order. The iterations of the while would leave the $v in the following states:
  1. pop,
  2. pop, pop, rock
  3. pop, pop, rock, pop, rock, jazz,

So if I had to guess what you are looking for, it is this:
PHP Code:
while ($t mysql_fetch_assoc($genres))
{
    
$gens[] = $t['gen_name'];
}
$v implode(', '$gens);

// or

$i 0;
$v '';
while (
$t mysql_fetch_assoc($genres))
{
    if (
$i++ > 0)
    {
        
$v .= ', ';
    }
    
$v .= $t['gen_name'];

Both of which should result in $v being 'pop, rock, jazz' if I didn't biff it.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
chellert (02-07-2013)
Old 02-07-2013, 12:04 AM   PM User | #3
chellert
New Coder

 
Join Date: Mar 2012
Location: Ontario, Canada
Posts: 45
Thanks: 8
Thanked 0 Times in 0 Posts
chellert is an unknown quantity at this point
thank you again for all of your help
chellert is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:58 PM.


Advertisement
Log in to turn off these ads.