Forum Moderators: coopster

Message Too Old, No Replies

Comma Parsing

Comma Parsing

         

blunick

7:38 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



Below is my script, i'm adding a comma after each output from the array, how do i parse out the last comma? You can see what i tried, but obviously it is not working.

$numofrows = mysql_num_rows($query);

for($i = 0; $i < $numofrows; $i++)
{
$row = mysql_fetch_array($query);

$str = "".$row['SGID']."";
$str .= ",";
$value = substr_replace($str,"",-1);
echo $value;

}

cameraman

8:16 pm on Sep 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



To answer your question,
$value = substr($str,-1);

But how about this instead:
while($row = mysql_fetch_array($query)
$rows[] = $row['SGID'];

$value = implode(',',$rows);

blunick

9:03 pm on Sep 8, 2008 (gmt 0)

10+ Year Member



I don't know what the heck im missing, but nothing is working...

cameraman

9:18 pm on Sep 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You would do the substr I posted after the for loop.
Or, the three lines under 'how about this' replace the for loop entirely, and doesn't need the substr() at all.

blunick

12:47 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



I tried both of these suggestions and im not getting anything. Is it a PHP 5.0 thing?

Here is my exact code with the new while loop...

//Check if the user info validates the db
$query = mysql_query("SELECT * FROM `CustomerUserSG` WHERE `Username` = '$Username'");
$numofrows = mysql_num_rows($query);

while($row = mysql_fetch_array($query)
$rows[] = $row['SGID'];
$value = implode(',',$rows);

echo $value;

cameraman

6:12 pm on Sep 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Grr, I gave you an error - I missed a parenthesis:
while($row = mysql_fetch_array($query))

That should have been generating an error though - it didn't?
After you add the parenthesis, if you're still not getting anything:

Were you getting anything before you posted the first time?
Variable names are case sensitive in PHP - is $Username correct, with the capital letter? Is the field name in the table SGID, all caps?

Make these changes to make sure you can see what's going on:

//Check if the user info validates the db
$query = mysql_query("SELECT * FROM `CustomerUserSG` WHERE `Username` = '$Username'");
if(!$query) {
echo mysql_error();
exit;
} // EndIf error in query
$numofrows = mysql_num_rows($query);
if(!$numofrows) {
echo "Username is '$Username'<br>\n";
exit;
} // EndIf empty result
while($row = mysql_fetch_array($query)) {
if(!isset($row['SGID'])) {
echo "The row is looking like this:<br>\n";
print_r($row);
exit;
} // EndIf field isn't set
$rows[] = $row['SGID'];
} // EndWhile getting rows
$value = implode(',',$rows);

echo $value;

blunick

6:38 pm on Sep 9, 2008 (gmt 0)

10+ Year Member



Ok that worked...the FOR loop did not work it worked with the WHILE loop and of course that damn parenthesis at the end. It had to be just the right combination.

Great advice that you so much!