Lifelesspeople.com

 Forum FAQsForum FAQs  Knowledge BaseKnowledge Base  RulesRules   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   HostingHosting   RegisterRegister 
 DonateDonate   WikiWiki   ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

comma after printed database records

 
Lifelesspeople.com Forum Index -> Web Architects' Abode
Post new topic   Reply to topic View previous topic :: View next topic  
Author Message
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2963
Location: Earth

PostPosted: Thu Nov 15, 2007 8:03 am    Post subject: comma after printed database records Reply with quote

I'm trying to list some names from the database if certain conditions are met and display those on the page

Code:
echo "Masters: ";
$sqlmem = "SELECT memberlist.name FROM masteries, memberlist WHERE masteries.deck = '" . $deck . "' AND masteries.Member_ID = memberlist.ID";       
$resultmem = mysql_db_query($db_name, $sqlmem, $con);
if (!$resultmem) { echo( mysql_error()); }
while ($row = mysql_fetch_array($resultmem)) {
$mem = $row["name"];
echo "$mem, "; }
echo "</p>" ;


The above code is what i'm using which works fine.

I want to make it so that if there is only 1 record, it doesn't display a comma, and it doesn't display a comma for the last record

At the moment it displays a comma after every record.

How do i go about making it display the way i want, and not the way it is?
_________________
Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Back to top
 
LP-SolidRaven
Dictator of the Dump


Joined: 06 Jun 2004
Posts: 7034
Location: The cheese is made out of moon

PostPosted: Thu Nov 15, 2007 12:00 pm    Post subject: Reply with quote

Try replacing:
Code:

if (!$resultmem) { echo( mysql_error()); }
while ($row = mysql_fetch_array($resultmem)) {
$mem = $row["name"];
echo "$mem, "; }


With:
Code:

$first = true;
if (!$resultmem) { echo( mysql_error()); }
while ($row = mysql_fetch_array($resultmem)) {
$mem = $row["name"];
echo (($first==false)?' , ':'').$mem; $first = false;}


Simple things like that are often so stupid that they'll work.
_________________
Quote:

<bart416> I just realized something
<bart416> we celebrate the fact that this piece of rock made one rotation around a glowing ball of plasma that is kept together due to its own gravity well
<njsg> HAPPY NEW YEAR
<Easter> ^^
Back to top
 
exsanguination
Forum Regular


Joined: 27 Apr 2005
Posts: 415
Location: Australia

PostPosted: Thu Nov 15, 2007 6:01 pm    Post subject: Reply with quote

SolidRaven wrote:
Try replacing:
Code:

if (!$resultmem) { echo( mysql_error()); }
while ($row = mysql_fetch_array($resultmem)) {
$mem = $row["name"];
echo "$mem, "; }


With:
Code:

$first = true;
if (!$resultmem) { echo( mysql_error()); }
while ($row = mysql_fetch_array($resultmem)) {
$mem = $row["name"];
echo (($first==false)?' , ':'').$mem; $first = false;}


Simple things like that are often so stupid that they'll work.


Strike 1.

Your code will never output the comma between the first and second items, and will always output a comma between all others.

Code:

#this is dubious error handling at best
if (!$resultmem) { echo( mysql_error()); }

$numresults = mysql_num_rows($resultmem);
$counter = 1;

while ($row = mysql_fetch_array($resultmem)) {
    $mem = $row["name"];
    echo (($counter < $numresults)?' , ':'') . $mem;
    $counter++;
}


EDIT: Strike one for me, SR's code above works. Mine doesn't since you'd need to append the comma, rather then prepend!


Last edited by exsanguination on Thu Nov 15, 2007 6:32 pm; edited 1 time in total
Back to top
 
krt
...


Joined: 11 Jan 2005
Posts: 4620
Location: Down Under

PostPosted: Thu Nov 15, 2007 6:16 pm    Post subject: Reply with quote

exsanguination, not using PHP anymore? Silly

Those # comments are obsolete and variables always need a $ sign prepended (edit: credit though, you corrected the latter before I pointed it out).

SolidRaven's code works as it prepends the comma so it only doesn't add one before the 0th and 1st items (which is correct)... hope that made sense. Your one would work if you appended the ternary expression instead.

I'd consider an array and a join but its probably overkill here.

Here is a simplified version:

Code:
echo 'Masters: ';
$sqlmem = "SELECT mem.name FROM masteries mas, memberlist mem " .
          "WHERE mas.deck = '$deck' AND mas.Member_ID = mem.ID";       
$resultmem = mysql_query($sqlmem) or die(mysql_error());
$first = true;
while ($row = mysql_fetch_array($resultmem)) {
    echo ($first ? '' : ', ') . $row['name'];
    $first = false;
}
echo '</p>' ;
Back to top
 
exsanguination
Forum Regular


Joined: 27 Apr 2005
Posts: 415
Location: Australia

PostPosted: Thu Nov 15, 2007 6:31 pm    Post subject: Reply with quote

krt wrote:
exsanguination, not using PHP anymore? Silly

Those # comments are obsolete and variables always need a $ sign prepended (edit: credit though, you corrected the latter before I pointed it out).

SolidRaven's code works as it prepends the comma so it only doesn't add one before the 0th and 1st items (which is correct)... hope that made sense. Your one would work if you appended the ternary expression instead.


Stupid $signs...and yes, I was wrong. ignore my stupidity Very Happy
Back to top
 
Desbrina
Jadeite


Joined: 11 Jun 2005
Posts: 2963
Location: Earth

PostPosted: Fri Nov 16, 2007 4:49 am    Post subject: Reply with quote

thanks to all of you. I've managed to get it to do what i wanted, now i can edit the other pages that need the same thing done
_________________
Midnight Tempest - A Sailor Moon TCG
Balanced Force - A Star Wars TCG
Back to top
 
LP-SolidRaven
Dictator of the Dump


Joined: 06 Jun 2004
Posts: 7034
Location: The cheese is made out of moon

PostPosted: Sat Nov 17, 2007 3:26 am    Post subject: Reply with quote

exsanguination wrote:
krt wrote:
exsanguination, not using PHP anymore? Silly

Those # comments are obsolete and variables always need a $ sign prepended (edit: credit though, you corrected the latter before I pointed it out).

SolidRaven's code works as it prepends the comma so it only doesn't add one before the 0th and 1st items (which is correct)... hope that made sense. Your one would work if you appended the ternary expression instead.


Stupid $signs...and yes, I was wrong. ignore my stupidity Very Happy

Hey I told you the solution was so stupid that it might look crappy.
_________________
Quote:

<bart416> I just realized something
<bart416> we celebrate the fact that this piece of rock made one rotation around a glowing ball of plasma that is kept together due to its own gravity well
<njsg> HAPPY NEW YEAR
<Easter> ^^
Back to top
 
Display posts from previous:   
Post new topic   Reply to topic    Lifelesspeople.com Forum Index -> Web Architects' Abode All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Home | Hosting | News | Forum | Links | System Status | About | Archive | Donate ]
Powered by phpBB © 2001, 2002 phpBB Group
All trademarks and copyrights on this page are owned by their respective owners. Posts and comments are owned by the poster. Everything else © 2001 - 2007 Lifelesspeople.com