Sending items fetched by while loop from mysql database in email - php

Need some help.
I am trying to create a script that sends an html email to a shopper with a list of all the orderd items. I want the items fetched from the database to be stored in a single variable so that I can embed it in the message variable of mail() function. Or Just the Items fetched to be embeded in the email.
Have tried the code below but it only displays 1 item and not all that are in the loop. How can I do it?
I'll be grateful for your help.
$message3 = "
</td>
</tr>
<tr bgcolor='#A8CE58'><td><b>ITEMS ORDERED</b></td></tr>
<tr>
<td>
<table border='' style='margin-bottom: 10px; margin-top:10px; padding: 1px;border-right: : 1px solid #000;'>
<tr align='left'>
<th>---</th>
<th>Name</th>
<th>Qty</th>
<th>Total</th>
</tr>";
$ress= mysqli_query($server, "SELECT * FROM ordered_products WHERE session='$session'");
mysqli_data_seek($ress, 0);
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM))
{
$message4 =
"
<tr align='left'>
<td><img src='e_images/$colms[8]' height='auto' width='50px'></td>
<td>$colms[2]</td>
<td >$colms[9]</td>
<td>ksh.$colms[5]</td>
</tr>";
}
$message = "$message3". $message4;

You need to concatenate the string
$message4 = '';
while($colms=mysqli_fetch_array($ress, MYSQLI_NUM)) {
$message4 .= //your html
}
What you doing now is overwrite the value of $message4 each time in the loop
With
.=
You solve this. Don't forget to declare the variable before the loop, else you will get a warning from PHP.

Related

get a hidden table to appear with with a click with php

Im creating a simple School grade system using only php and I'm stumped of trying to figure out how to get a hidden table to appear below the main table when click on a "durchschnitt number"
Below is the code for the main table
<?php
if (!isset ($_SESSION['Saved_contacts']))
$Kontakte = array (
array ("Hr.", "Fruehauf", "Dennis", "13.02.2002", "Brucknerweg 34", 5212, "Hausen", '<u>3.6</u>'),
array ("Fr.", "Kaufmann", "Katharina", "04.03.2002", "Neubertbogen 24", 1680, "Romont", "Durchschnitt"),
array ("Hr.", "Fiedler", "Marcel", "08.16.2002", "Via Stazione 98", 8143, "Stallikon", "Durchschinitt"),
array ("Hr.", "Oster", "Tim", "08.26.2002", "Via delle Vigne 98", 1773, "Vuaty", "Durchschinitt"),
array ("Fr.", "Eichelberger", "Tanja", "07.22.2002", "Semperweg 6", 4223, "Blauen", "Durchschinitt"));
else
$Kontakte = $_SESSION['Saved_contacts'];
?>
<div style="width: 80%; min-width: 550px">
<h2>Kontakt des Schülers ...</h2>
<table>
<tr> <th>Nr.</th> <th>Anrede</th> <th>Name</th> <th>Vorname</th> <th>Geburtsdatum</th> <th>Adresse</th> <th>PLZ</th> <th>Ort</th> <th>Durchschnitt</th> </tr>
<?php
for ($i=0; $i < count($Kontakte); $i++) {
echo "<tr> <td><em>".($i+1)."</em></td>" . "<td style='text-align: center'>".$Kontakte[$i][0]."</td>" .
"<td>".$Kontakte[$i][1]."</td>" . "<td>".$Kontakte[$i][2]."</td>" . "<td>".$Kontakte[$i][3]."</td>" .
"<td>".$Kontakte[$i][4]."</td>" . "<td>".$Kontakte[$i][5]."</td>" . "<td>".$Kontakte[$i][6]."</td>" . " <td>".$Kontakte[$i][7]."</td
" . " <td><</tr>";
}
?>
</table>
As you can see in the first aray on the last line i made a link so that i'm able to click it.
Below is the hidden table I want to hide and reappear
<div class="Note">
<div style="width: 80%; min-width: 550px">
<table class="grade_Fruehauf" style="">
<tr>
<th>Fruehauf</th>
</tr>
<tr>
<th>Deutsch</th>
<th>3.5</th>
</tr>
<tr>
<th>Math</th>
<th>3.5</th>
</tr>
<tr>
<th>Biologie</th>
<th>3.5</th>
</tr>
<tr>
<th>Französisch</th>
<th>4</th>
</tr>
<tr>
<th>Durchschnitt</th>
<th style="border-top:solid;">3.6<th>
</tr>
</table>
<div>
</div>
Appreciate your help :)
If you want to use PHP only then you can use session, on clicking link set a session variable in a different file and redirect back, check that session variable and show/hide w.r.t it. (though JavaScript is preferable)
It's better to use javascript.
You just need to add the onlick attribute on the "durchschnitt number" td like
<td onclick="show('tableid');"></td>
and specify an id to the div or the table (es.: <table class="grade_Fruehauf" style="" id="example">.
Then you need a javascript code like this:
<script>
function show(tableid){
var x=document.getElementById(tableid);
if (window.getComputedStyle(x).visibility === "hidden") {
x.style.visibility = "visible";
}else{
x.style.visibility = "hidden";
}
}
</script>

Form Processing: empty TD tags generated with foreach loop

I'm trying to process a pretty simple rental quote form, basically the form gets filled out and then the data gets sent to an email.
My issue is how the item section is being formatted. Right now I'm trying a table and populating it with a foreach loop. The main problem is it's generating empty td tags for some reason.
Here's the output, I've added some borders so you can see the empty cells being generated:
And here's the section of my php code where I'm processing this (if you'd like to see the entire code, just let me know):
<?php
if(isset($_POST['submit']))
{
$name = strip_tags($_POST['renter_name']);
$email = strip_tags($_POST['renter_email']);
$message = strip_tags($_POST['renter_message']);
$subject = "Quote Request";
$rentalItems = ($_POST['item']);
// $items = implode(',', $_POST['item']);
$msg = "<html><body style='font-family:Arial,sans-serif;'>";
$msg .= "<p><strong>Sent by:</strong> ".$name."</p>";
$msg .= "<p><strong>Items:</strong></p>";
$msg .="<table name='rental_items' style='border-collapse:collapse; border: 1px solid black;';>
<thead style='text-align:left;'>
<tr>
<th sytle='padding-right: 5px;'>Item Name and ID</th>
<th>Quantity</th>
</tr>
</thead>
<tbody>";
foreach($rentalItems as $item) {
$msg .="<tr>
<td style='border: 1px solid black;'>". $item['name'] ."</td>
<td style='border: 1px solid black;'>".$item['quantity']."</td>
</tr>";
}
$msg .= "</tbody>
</table>";
$msg .= "<p><strong>Message:</strong> ".$message."</p>";
$msg .= "</body></html>";
Currently I'm running a row and two cells in the loop. But for some reason it's treating each td as its own row.
Ultimately I would like that Quantity number along side the Name and ID info.
I'll be honest with you, I haven't worked with tables in years so it could possibly be something super simple I'm missing or have wrong. Or it could be how I'm structuring this whole thing in general.
Any advice or help would be greatly appreciated. Again, if you need any more info, just let me know.
Here's the item section of the form:
<input type="checkbox" name="item[][name]" value="Name: <?php the_title();?> <?php echo "<br>"?> ID: <?php echo get_post_meta($post->ID, 'id_number', true); ?>" id="<?php the_title(); ?>"/>
<label for="<?php the_title(); ?>"><img src="<?php the_post_thumbnail_url(); ?>" alt="<?php the_title(); ?>" class="form-image" /></label>
<input type="text" name="item[][quantity]"/>
As you are using item[][name] and item[][quantity] for the data, you will end up with alternate array elements containing the different values...
Array(0 => ("name" =>"somename"),
1 => ("quantity" =>1))
When you foreach over this array, it will display one value or the other, but not the 2 together.
So it would be easier (I think) to use a for loop and take it in steps of 2...
for($i = 0; $i < count($rentalItems); $i+=2) {
$msg .="<tr>
<td style='border: 1px solid black;'>". $rentalItems[$i]['name'] ."</td>
<td style='border: 1px solid black;'>".$rentalItems[$i+1]['quantity']."</td>
</tr>";
}
Alternatively - rename the arrays to be item[name][] and item[quantity][] so that the data is separate, but you can then use $item['name'][0] and $item['quantity'][0].

For loop is emailing one character per row in table

I have been stuck on this problem for a while. My requirements are to email data from mySQL to a client. I took code from another page in my company's codebase that works when it is emailed.
The problem that I am encountering is that it is emailing only one character per row.
Here is my code:
$message = "
<br /><hr /><br /><table style='font-size: 12px; width:100%'>
<tr>
<td td width='25%'><strong>Oligo Name</strong></td>
<td width='10%'><strong>Base</strong></td>
<td width='10%'><strong>Scale</strong></td>
<td width='10%'><strong>Mod</strong></td>
<td width='10%'><strong>Mod Position</strong></td>
<td style='word-break: break-all; width: 500px;'><strong>Oligo Sequence</strong></td>
<td width='15%' style='text-align: right'><strong>Price</strong></td>
</tr>";
$SampleID = 0;
$maxrow = $OligoQty;
for ($i; $i < $maxrow; $i++) {
if(trim($SampleName) == '' && trim($Sequence) == '')
continue;
// $SampleID++;
$bases = preg_replace('/\s+/', '', $Sequence);
$seq_length = strlen($bases);
$message .= "
<tr>
<td width='200'>{$SampleIDS} . {$SampleName[$i]}</td>
<td width='40'>{$sequence_length[$i]}</td>
<td width='40'>{$SampleScale[$i]}</td>
<td width='40'>{$modification[$i]}</td>
<td width='40'>{$mod_position[$i]}</td>
<td style='word-break: break-all; width: 500px;'>{$Sequence[$i]}</td>
</tr>";
</tr>";
}
When I remove the for loop, it prints out the row perfectly:
I believe there is something wrong with my for loop that is causing it to do that and I am not sure why. I hard coded $maxrow to 2 and that is why I am getting 2 rows for the email. Any help would be appreciated!

how to style php echo table

I have the following code and I would like to style it. Are there any ways to do this?
Specifically I want to center the Columns headings and the text within each cell.
echo "Variable Profile";
echo "<table>";
echo "<th>"."STATE"."</th>";
echo "<th>"."$column_name"."</th>";
foreach($lotsofasians as $lotsofasian){
echo "<tr>";
echo "<td>".$lotsofasian->state."</td>";
echo "<td>".$lotsofasian->$column_selected."</td>";
echo "</tr>";
}
echo "</table>";
I have tried putting something like align= 'center' in the tag but I cant get it to work.
With regard to styling HTML, there's nothing special about the fact that PHP is outputting it. You can still give your elements classes, IDs, inline styling or whatever - it's just that if PHP is involved you'll have to reference these in the echo output statements.
Just change the echo statement to include classes as required, e.g.
echo "<table class='some_class'>";
Try using css rather than using inline styles
css
table th,table td{
text-align:center;
}
See demo here
Your php is going to output something like this:
<table>
<th>state value</th>
<th>column value</th>
<tr>
<td>value</td>
<td>value</td>
</tr>
</table>
I would sneak a <tr> in there for your <th> elements as well:
<table>
<tr>
<th></th>
</tr>
Then, create some css rules:
table th, table td { padding: 5px; text-align: center; }
Use css-classes or inline styles.
Why you quote Variables? Your HTML is not valid.
simplest way:
echo 'Variable Profile';
printf('<table>
<thead>
<tr>
<th>%s</th>
<th>%s</th>
</tr>
</thead>
<tbody>', 'State', $column_name);
foreach($lotsofasians AS $index => $lotsofasian) {
$style = array();
/* Example 1: center text */
$style[] = 'text-align: center;';
/* Example 2: Set Background each second output */
if($index % 2 == 0) {
$style[] = 'background: #DDDDDD;';
}
printf('<tr style="%">
<td>%s</td>
<td>%s</td>
</tr>', implode('', $style), $lotsofasian->state, $lotsofasian->{$column_selected});
}
echo '</tbody>
</table>';

how to populate dropdown list values from mysql and show it inside the td of html table

I am trying to populate dropdown list values from mysql and show it inside the td of html table,i tried with below code but it not populating values from mysql can any help me how to do that.
<table id="CPH_GridView1" style="width:1452px">
<thead>
<tr>
<th style=" width:102px">Clien ID </th>
<th style=" width:100px">Country</th>
<th style=" width:248px">Network Name </th>
<th style="text-align:center; width:102px" >cppn </th>
</tr>
</thead>
<tbody>
<?php
$sql = mysql_query("SELECT * FROM clientpricenotifications");
while($rows=mysql_fetch_array($sql))
{
if($alt == 1)
{
echo '<tr class="alt">';
$alt = 0;
}
else
{
echo '<tr>';
$alt = 1;
}
echo '<td id="CPH_GridView1_clientid" style="width:140px" class="edit clientid '.$rows["id"].'">'.$rows["clientid"].'</td>
<td id="CPH_GridView1_country" style="width:160px" class="edit country '.$rows['id'].'">'.$rows["country"].'</td>
<td id="CPH_GridView1_networkname" style="width:156px" class="edit networkname '.$rows["id"].'">'.$rows["networkname"].'</td>';
?>
<td>
<select name=' . customer_name . '>
<?php
$query = 'SELECT cppn FROM clientpricenotifications';
$result = mysql_query($query, $db) or die(mysql_error($db));
while ($row = mysql_fetch_assoc($result))
{
echo '<option value="' . $row['id'] . '"> ' . $row['cppn'] . '</option>';
}
?>
</select>
</td>
</tr>'
}
?>
There seems to be a problem with this line:
<td> <select name='customer_name'>
Shouldn't it actually say either this:
<td> <select name="customer_name">
Or:
<td> <select name=' . customer_name . '>
And, that line is part of an echo statement that contains a string in single-quotes, but I can't see where the echo statement's closing single-quote is.
As a result, I think a large bulk of your output is being ignored by the browser because the tag is not being closed properly as some of the output is getting mangled. Check your output with View Source!
If your above code is complete, then I would guess that you're missing the connection to the MySQL server. See: http://www.php.net/manual/en/function.mysql-connect.php
For a related question with code sample, check the answer at: Create table with PHP and populate from MySQL
Not asked, but your table has non matching column widths defined in the styles: Clien(t) ID header 102px, while data cells are 140px.
Another place to look for is following line:
<td style="width:65px" class=" '.$rows["id"].'">
I would expect it should be the following:
<td style="width:65px" class="<?php echo $rows["id"] ?>">
As Vexen Crabtree mentioned, if you also check/post the html code of the HTML output, it would make it easier to diagnose the problem.

Categories