How could I combine all of the comments that appear in $row["comment"] below into one giant string variable called $commentstring?
$sqlStr = "SELECT comment.comment, comment.datecommented, comment.commentid, comment.points, login.username
FROM comment
LEFT JOIN login ON comment.loginid=login.loginid
WHERE submissionid=$submissionid
ORDER BY comment.points DESC
LIMIT 100";
$tzFrom1 = new DateTimeZone('America/New_York');
$tzTo1 = new DateTimeZone('America/Phoenix');
$result = mysql_query($sqlStr);
$arr = array();
echo "<table class=\"commentecho\">";
$count = 1;
while ($row = mysql_fetch_array($result)) {
$dt1 = new DateTime($row["datecommented"], $tzFrom1);
$dt1->setTimezone($tzTo1);
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-top:3px solid #DE2A00;" class="commentname2user">'.$row["username"].'</td>';
echo '<td style="border-bottom:3px solid #DE2A00; border-top:3px solid #DE2A00; border-right:3px solid #DE2A00;" rowspan="4" class="commentname1" id="comment-' . $row["commentid"] . '">'.stripslashes($row["comment"]).'</td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00;" class="commentname2">'.$dt1->format('F j, Y').'</td>';
echo '</tr>';
echo '<tr style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:0px solid #DE2A00;">';
echo '<td style="border-left:3px solid #DE2A00;" class="commentname2"></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-left:3px solid #DE2A00; background-color: #DE2A00; border-bottom:3px solid #DE2A00;" class="commentname2user"><span class="">'.number_format($row["points"]).'<span></td>';
echo '</tr>';
echo '<tr>';
echo '<td style="border-bottom:0px solid #DE2A00; border-right:0px solid #DE2A00;" class="commentname2a"></td>';
echo '</tr>';
}
echo "</table>";
Declare $commentstring = ""; before the while-loop
in the while loop:
$commentstring .= $row["comment"];
If you put this before your while loop:
$commentstring = '';
and then inside the while loop you add onto it:
$commentstring .= $row['comment'];
Or together with a new line:
$commentstring .= $row['comment'] . '<br />';
Related
I am totally new to PHP and mySQL. I have pulled this data from mySQL database for a wordpress website but I would like to simplify my code so that it's more flexible so that I could customize my tables more easily. Any suggestion on where to start and also is there a standard way to do this ?
<?php
global $wpdb;
$result1 = $wpdb->get_results("SELECT ID, post_title, post_date, guid, post_type FROM wp_posts");
$result2 = $wpdb->get_results("SELECT name, term_id FROM wp_terms");
$result3 = $wpdb->get_results("SELECT object_id, term_taxonomy_id FROM wp_term_relationships");
echo "<style>";
echo "body {font-family: Arial;}";
echo ".table_container { padding: 10px 12px 0px 12px; border: 1px solid #ccc; }";
echo ".table_container th { background-color:white; color:black; font-weight:bold; border-left: 1px solid white;}";
echo "</style></head>";
echo "<body>";
echo "<div class=\"table_container\"><table>";
echo "<tr><th style=\"padding-left:10px;\">Yvirskrift</th><th>Dato</th><th>Talari</th><th>Lurta</th></tr>";
foreach ($result1 as $row1) {
if ($row1->post_type == "podcast") {
echo "<tr>";
echo "<td> $row1->post_title </td>";
$date = date("d-m-Y", strtotime($row1->post_date));
echo "<td> $date </td>";
foreach ($result3 as $row3) {
if ($row1->ID == $row3->object_id) {
foreach ($result2 as $row2) {
if($row3->term_taxonomy_id == $row2->term_id){
echo "<td> $row2->name </td>";
}
}
}
}
$listen = "<a href=$row1->guid>Lurta her</a>";
echo "<td> $listen </td>";
echo "</tr>";
}
}
echo "</table></div>";
?>
I am trying to get 2 simple for loops. The first for loop cycles through results from the database. The 2nd for loop prints out a table for each store based on the amount of weeks selected.
My code looks as follows
$retval = f_select_query($query_select_stores, $datarows);
$rowcount = count($datarows);
for ($counter = 0; $counter< $rowcount; $counter++) {
$store_name = $datarows[$counter]->store_name;
echo '<table style="width: 100%; border:1px solid black;">';
echo '<tr>';
echo '<th style="border: 1px solid black;">' . $store_name . '</th>';
for ($i=$week_number_start; $i<=$week_number_end; $i++){
echo '<th style="border: 1px solid black;>Week ' . $i . '</th>';
}
echo '</tr>';
echo '</table>';
}
And where the result is printed out, I get Week 2, Week 4, Week 6 and so on. Pretty much every 2nd week. Why is it doing that? I'm probably missing something very simple..
you have double quote missing in the seconde for loop, as Barmar mentionned, it's combining two elements.
so try this after add the missing double quote:
$retval = f_select_query($query_select_stores, $datarows);
$rowcount = count($datarows);
for ($counter = 0; $counter< $rowcount; $counter++) {
$store_name = $datarows[$counter]->store_name;
echo '<table style="width: 100%; border:1px solid black;">';
echo '<tr>';
echo '<th style="border: 1px solid black;">' . $store_name . '</th>';
for ($i=$week_number_start; $i<=$week_number_end; $i++){
echo '<th style="border: 1px solid black;">Week ' . $i . '</th>';
}
echo '</tr>';
echo '</table>';
}
I try to insert data from my php array into an html table. The code for this looks like
echo "<table style='border: 1px solid black'><tbody>";
foreach(['country','counter'] as $attribute){
echo "<tr><td>".$attribute."</td>";
foreach($analysis_data as $row){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
When the code is executed it looks like
I want the table to be vertical and not horizontal like this. What do I need to change in my code?
Interchange your two loops.
echo "<table style='border: 1px solid black'><tbody>";
echo "<tr><th>country</th><th>counter</th></tr>";
for ($analysis_data as $row) {
echo '<tr>';
foreach (['country','counter'] as $attribute){
echo "<td style='border: 1px solid black'>".$row[$attribute]."<td>";
}
echo '</tr>';
}
I use this but only one will display...
please help me I need to display same name in a table..
<?php
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>
Use session_start() at the top
<?php
session_start()
$id=$_SESSION['SESS_MEMBER_ID'];
$result = mysql_query("SELECT * FROM user_reservation WHERE id = '$id' ");
while($row = mysql_fetch_array($result)){
echo '<tr class="record" id="'.$row['status'].'">';
echo '<td style="border-left: 1px solid #C1DAD7;">'.$row['confirmation'].'</td>';
echo '<td>'.$row['firstname'].' '.$row['lastname'].'</td>';
echo '<td><div align="right">'.$row['email'].'</div></td>';
echo '<td><div align="left">';
echo '</div></td>';
echo '<td><div align="right">'.$row['status'].'</div></td>';
echo '<td><div align="center">delete</div></td>';
echo '</tr>';
}
?>
hello can somebody take a look with my code? i think i did not forget everything to close tag
i really dont know why it only display the first value everytime i click show all buton. i think the 3rd and 4rth to the last echo has something wrong. because the problem is on that part. i have morethan 1 data suppose to display but it only show the first value i inserted. here is my code so far.
$a=$_POST['dayfrom'];
$b=$_POST['dayto'];
$result1 = mysql_query ("SELECT s.*, r.name, r.pcode
FROM salessumarry s
JOIN rsales r ON s.reciept = r.reciept
WHERE s.register_mode = 'sales'
AND s.date BETWEEN '$a' AND '$b' group by id");
while($row = mysql_fetch_array($result1))
{
echo '<tr>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['date'].'</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['reciept'].'</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center"></div></td>';
echo '</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;">'.$row['total_purchased'].'<div align="center">';
echo '</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['transactioncode'].'</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center"></div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">';
$eee=$row['total'];
echo formatMoney($eee, true);
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['profit'].'</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['mode'].'</div></td>';
echo '<td style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['total'].'</div></td>';
echo '</div></td>';
echo '<tr>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Product Code</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Name</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Description</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Category</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Quantity Purchased</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Sub total</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Total</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Tax</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Profit</th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;font-size:10px;background-image:url(images/buts3.png);color:white"">Discount</th>';
echo'</tr>';
echo '<tr>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['pcode'].'</div></th>';
echo '<th style="border-color:#000000; border-style:solid; border-width:1px;"><div align="center">'.$row['name'].'</div></th>';
echo '</tr>';
echo '</tr>';
};
mysql_close($con);
?>
Use mysql_fetch_row instead of mysql_fetch_array
The latter fetch all the rows as an array.
You want a single row at a time instead.
Also you get a numerical array (not key-value pairs)
See http://php.net/manual/en/function.mysql-fetch-row.php
From the above page you'll see that mysql extension is deprecate and you should move to mysqli
Also as many have commented your code is not suitable for a production environment since vulnerable to sql injection.
You should use prepared statements:
http://php.net/manual/en/mysqli.quickstart.prepared-statements.php