Jump to content


Photo

*SOLVED* php/mysql display help


  • Please log in to reply
3 replies to this topic

#1 birdie

birdie
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationBirmingham UK

Posted 11 March 2006 - 04:29 PM

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'.

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


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

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

thanks alot!

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 11 March 2006 - 04:44 PM

Simple do this:
while($row = mysql_fetch_array($sql))
{
    echo $row['pid'] . ',';
}
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:

$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);
You should now get a result like 1,2,3,4,5

#3 birdie

birdie
  • Members
  • PipPipPip
  • Advanced Member
  • 65 posts
  • LocationBirmingham UK

Posted 11 March 2006 - 04:45 PM

[!--quoteo(post=353951:date=Mar 11 2006, 04:44 PM:name=wildteen88)--][div class=\'quotetop\']QUOTE(wildteen88 @ Mar 11 2006, 04:44 PM) View Post[/div][div class=\'quotemain\'][!--quotec--]
Simple do this:
while($row = mysql_fetch_array($sql))
{
    echo $row['pid'] . ',';
}
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:

$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);
You should no get a result like 1,2,3,4,5
[/quote]
ok thanks!

#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 11 March 2006 - 07:41 PM

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

Ken




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users