Jump to content

Archived

This topic is now archived and is closed to further replies.

birdie

*SOLVED* php/mysql display help

Recommended Posts

hi, i'm sure this is easy but i have completely forgot how to do this kind of thing so i'm after help :-).

i have a column that is named 'pid'.

[code]
while($row = mysql_fetch_array($sql)
{
echo $row['pid']; //this gives all of the pid's..
}
[/code]


how could i echo the pid's like this..

1,2,3,4,5 insead of 12345?

thanks alot!

Share this post


Link to post
Share on other sites
Simple do this:
[code]while($row = mysql_fetch_array($sql))
{
    echo $row['pid'] . ',';
}[/code]and you'll get a result of 1,2,3,4,5,

If you want to not show the ending , after 5 then do this:

[code]$pid = ''; //setup the variable pid
while($row = mysql_fetch_array($sql))
{
    $pid .= $row['pid'] . ',';
}
//this removes the last character from value in $pid
$pid = substr($pid, 0, strlen($pid)-1);[/code]
You should now get a result like 1,2,3,4,5

Share this post


Link to post
Share on other sites
[!--quoteo(post=353951:date=Mar 11 2006, 04:44 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 11 2006, 04:44 PM) [snapback]353951[/snapback][/div][div class=\'quotemain\'][!--quotec--]
Simple do this:
[code]while($row = mysql_fetch_array($sql))
{
    echo $row['pid'] . ',';
}[/code]and you'll get a result of 1,2,3,4,5,

If you want to not show the ending , after 5 then do this:

[code]$pid = ''; //setup the variable pid
while($row = mysql_fetch_array($sql))
{
    $pid .= $row['pid'] . ',';
}
//this removes the last character from value in $pid
$pid = substr($pid, 0, strlen($pid)-1);[/code]
You should no get a result like 1,2,3,4,5
[/quote]
ok thanks!

Share this post


Link to post
Share on other sites
A different way is to use a temporary array and the implode() function:
[code]<?php
$tmp = array(); // make sure we start with an empty array
while($row = mysql_fetch_array($sql))
{
    $tmp[] = $row['pid'];
}
$pid = implode(',',$tmp);
?>[/code]

Ken

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.