JohnHall Posted October 21, 2009 Share Posted October 21, 2009 Hi - noob here, I have a sql select query that returns rows from a table (5 columns - name, email addr, phone#, etc) and I have those results formatted and displayed using a while loop and an echo that puts the fields into a table. while($row = mysqli_fetch_array($result)) { $date = $row['timestamp']; $office = $row['office']; .... echo '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>...... That works fine, but now I would also like to e-mail the formatted table. I'm very new to php/mysql so don't know where to begin. Any help is greatly appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/ Share on other sites More sharing options...
taquitosensei Posted October 21, 2009 Share Posted October 21, 2009 instead of echo '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>...... do $body.= '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>...... the dot will concatenate each line onto the previous then you just use $body in the body of your e-mail. Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/#findComment-941048 Share on other sites More sharing options...
cags Posted October 21, 2009 Share Posted October 21, 2009 Instead of echo'ing to the screen, concatinate to a variable. You can then just use that variable as the body text for an e-mail. $message = ""; while($row = mysqli_fetch_array($result)) { $date = $row['timestamp']; $office = $row['office']; ... $message .= '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......'; } mail("me@example.com", "Subject", $message); Edit: D'oh Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/#findComment-941049 Share on other sites More sharing options...
JohnHall Posted October 21, 2009 Author Share Posted October 21, 2009 Thanks - works great! I had actually tried that before - but I got the undefined variable error, as I did this time as well. So I set the $message = NULL; at the beginning of the script, and it works now. Is doing that proper though - in terms of good coding practice? Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/#findComment-941060 Share on other sites More sharing options...
JohnHall Posted October 21, 2009 Author Share Posted October 21, 2009 $message = ""; $message .= '<TR><TD style="width: 40pt">'.$date.'</TD><TD style="width: 40pt">'.$office.'</TD>......'; } It looks like you answered my other question already! Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/#findComment-941068 Share on other sites More sharing options...
cags Posted October 21, 2009 Share Posted October 21, 2009 Undefined variable just means your using a variable that doesn't currently exist. In this case it doesn't really matter as it just creates it for you. It was to prevent the notice that I put message at the top equal to an empty string. Quote Link to comment https://forums.phpfreaks.com/topic/178447-solved-emailing-formatted-mysql-table-data/#findComment-941070 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.