Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Rhinorhino on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Logic for e-mail program 3

Status
Not open for further replies.

EnsignPulver

IS-IT--Management
Joined
Apr 18, 2004
Messages
17
Location
US
Hello Friends,

I have the following little snippet:

$email=mysql_result($result,$i,"email");

echo "<b>$email,<br><hr><br>";

++$i;
}

?>

What I get is x@z.com, p@t.com,

What I need is x@z.com, p@t.com

I need to build a little logic to say "add commas between each email address, then don't add a comma after the last one!"

The object of my script is to provide the contents of my mail field so that people can copy and paste all of the group's email addies into their mail program.

Thanks for any guidance!! I'm stuck.

ENS Pulver
 
Give us a little more info here please.
1) Where are you getting the list of these email addresses. Are you reading them from a database?

2) What is $i in your example?

3) Whatever $i is, ++$i; doesn't look right although $i++; might be.
 
Sorry smash:

1) Yes, I am pulling the entire contents of my database's "email" field. It's a simple roster database with one table.

2) It's from:

$i=0;
while ($i < $num) {

....CODE....

++$i;
}

Just a little loop to take each row of my result and output the data.

Thank you for your response. Hope I have been clearer.

ENS

 
What's $num now?
And like I said before change ++$i; to $i++; to increrment its value. Whatever the value of $num is stop the counting not when it has been reached and matched to $i but rather one before that value and add the comma after each entry. Then you can add the last one without the comma by using something like $email.=

Post a litttle more of your code so I can show you more of what I mean here.
 
Thank you. Here's the whole thing. That "incrementer" seems to be working, though. :)

mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM phonelist";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $num) {


$email=mysql_result($result,$i,"email");

echo "<b>$email,<br><hr><br>";

++$i;
}

?>
 
personally i would do this:
Code:
if ($i < $num) { echo "<b>$email,<br><hr><br>"; }
if ($i == $num) ( echo "<b>$email<br><hr><br>"; }

and the difference between ++$i and $i++ is that ++$i adds one to $i and then returns it and $i++ adds 1 to $i and then returns..... Look in the php manual for that one although in this case I don't think it matters.
 
I'm rushing out of the office so there may be a typo or 2 in my post but I hope you'll get my idea here...

Since your'e using mysql_result which requires the result of a valid query, a row (starting at 0) and the field name which you're doing with the line:
$email=mysql_result($result,$i,"email");

So first loop until you have 1 less than the total entries. Find out this info by:

$num=mysql_numrows($result);
$oneless=$num - 1;

then change

while ($i < $num) { to: while ($i < $oneless) {

Now echo your emails with the comma.

Then outside the loop add 1 to the final value of $i

Echo this without the comma

Your code should look like this now:
mysql_connect(localhost,$username,$password);
@mysql_select_db($database) or die( "Unable to select database");
$query="SELECT * FROM phonelist";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$oneless=$num - 1;

echo "<b><center>Database Output</center></b><br><br>";

$i=0;
while ($i < $oneless) {


$email=mysql_result($result,$i,"email");

echo "<b>$email,<br><hr><br>";

++$i;
}

//increment $i once more
$i++;
$email=mysql_result($result,$i,"email");
echo "<b>$email <br><hr><br>";

?>


Again I don't have time to test it now and there may be a typo. Also note: coping from the forum and pasting it into your texdt editor may not always work. If you can try it by retyping my changes by hand
 
There's always a better way I think Moonspell's version would work and is by far shorter....
 
or in psuedo code

$first="yes";
@do the SQL
while $row... {
if $first=="yes" {
$line = $row;
$first="no";
}
else
$line = $line . "," . $row;
}



 
Smash,

I thought you might be interested in the result I got with your code -- which I typed in as per your suggestion. May I impose on you for your thoughts? Many thanks also to ingres and moon.

To understand this message, know that I have TEN records presently in the table. The little script displays all ten correctly (although the last one still has a comma, which we didn't want) then:

Warning: mysql_result(): Unable to jump to row 11 on MySQL result index 3 in test.php on line 36

I'm not certain about the meaning of "index 3" but line 36 is:

35 $i++;
36 $email=mysql_result($result,$i,"email");
37 echo "<b>$email <br><hr><br>";

I'm very grateful your expert thoughts.

ENS

 
this is not pretty cause i am at work:
Code:
<?php
mysql_connect(localhost,user,pass);
mysql_select_db(database) or die( "Unable to select database");
$query="SELECT * FROM phonelist";
$result=mysql_query($query);
$num = mysql_num_rows($result);
for ($i=0; $i<$num-1; $i++)
{
$line = mysql_result($result,$i,"email");
echo "$line,";
}
$line = mysql_result($result,$i,"email");
echo "$line";
?>
also try and use the correct formats for the commands ie not mysql_numrows but mysql_num_rows! also don't close the database connection before you are finished with it!
 
Thanks moon. I'll admit I've been known not to cut corners when it comes to canonical code, but maybe slice a little off the side!

I'll pop it in and see what I see. Thanks again.

ENS
 
Moon,

It's a very interesting thing that's transpiring with your code. I am trying to noodle out the logic that is dictating the abberrance. It's subtle.

I still get xyz@f.com, email@blah.rr com, (with the comma).

So I added 'Test' to the final $line to see if our second clause was doing anything:

$line = mysql_result($result,$i,"email");
echo "$line TEST";

Which results in the last email where we wanted 'email@blah.rr com' being rendered as :

email@blah.rr.com, TEST

Thank you for your thoughtful assistance. We are closing in on this devilish problem!

ENS
 
hmm, i think you need to check your coding!! I just copied and pasted the code i posted and ran it with a dummy database I setup on my machine here and this is the output i got:

dummy@here,test@here,123@here.com

note, no trailing comma! do you perhaps have a comma on the last entry in your database? the exact code is as follows (as this is a temp server I will leave the code exactly as it is)
Code:
<?php
mysql_connect(localhost,"root","");
mysql_select_db("test") or die( "Unable to select database");
$query="SELECT * FROM phonelist";
$result=mysql_query($query);
$num = mysql_num_rows($result);
for ($i=0; $i<$num-1; $i++)
{
$line = mysql_result($result,$i,"email");
echo "$line,";
}
$line = mysql_result($result,$i,"email");
echo "$line";
?>
 
Hmmmm...wow thanks for checking it against the dummy database.

I'll keep working it. I don't see any commas in the database, but it's something like that. I'll dicker around with a fresh database and what have you.

Many thanks.

ENS

 
Wow -- you're a genius, Moon. There's something about that last entry that's weird! Code works like a charm.

Check it out:

email@blah.rr.com,,TEST@TEST.COM

I'll kick the database into shape...gosh, I wish I had thought of that about, oh, 30 code revisions ago!! :)

Thanks again.

ENS
 
make sure you don't have any empty fields in the table as they will cause this due to there being no error checking!
 
That was it. Empty record. Just had the same thought. :(
 
footnote:
if you only require email addresses, then
SELECT email FROM your_database....etc instead of SELECT * would be more appropriate.

Also to avoid empties,
SELECT email FROM yourdatabase WHERE email is not null; should return a better result still.

Moons code has an interesting feature, it will display the first record of the set last.

All in all though, good solid advice with an array of coding styles showing up :-)

______________________________________________________________________
There's no present like the time, they say. - Henry's Cat.
 
hmm, agree, select email would be better but i was tired and had a crying baby to contend with! i was working from the given code but i would probably have gone for the fetch_row command but hey ho, it seems to work
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top