| Author |
Message |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2963 Location: Earth
|
Posted: Thu Nov 15, 2007 8:03 am Post subject: comma after printed database records |
|
|
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
|
Posted: Thu Nov 15, 2007 12:00 pm Post subject: |
|
|
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
|
Posted: Thu Nov 15, 2007 6:01 pm Post subject: |
|
|
| 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
|
Posted: Thu Nov 15, 2007 6:16 pm Post subject: |
|
|
exsanguination, not using PHP anymore?
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
|
Posted: Thu Nov 15, 2007 6:31 pm Post subject: |
|
|
| krt wrote: | exsanguination, not using PHP anymore?
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  |
|
| Back to top |
|
| |
Desbrina Jadeite

Joined: 11 Jun 2005 Posts: 2963 Location: Earth
|
|
| Back to top |
|
| |
LP-SolidRaven Dictator of the Dump

Joined: 06 Jun 2004 Posts: 7034 Location: The cheese is made out of moon
|
Posted: Sat Nov 17, 2007 3:26 am Post subject: |
|
|
| exsanguination wrote: | | krt wrote: | exsanguination, not using PHP anymore?
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  |
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 |
|
| |
|
|
|