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
Related
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>';
}
?>
Thanks to stackoverflow and its great solution, I have found a way to limit the characters in a table but it doesn't work for me. I tried a lot but with no success.
This is my table
<?php
$result = mysqli_query($conn,"SELECT * FROM library ORDER BY `CreatedTime` DESC");
echo "<table class='table-fill' border='0' cellpadding='0' cellspacing='0'>
<tr>
<th position='fixed' overflow='hidden' width='10%'>Book Name</th>
<th width='5%'></th>
</tr>";
while($row = mysqli_fetch_array($result) ) {
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo '<td style="max-height: 10px;">' . $str . '</td>';
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
if (strlen($row['bookname']) > 1) $str = substr($row['bookname'], 0, 1) . "...";
}
echo"</table>";
?>
This is how it looks like
Any help will be appreciated.
I'm doing this out of my head so forgive me any format issues and such...
Move the string length check to just under the while.
Overwrite $row['bookname'] instead of creating $str.
Remove the line with:
echo '<td style="max-height: 10px;">' . $str . '</td>';
Result:
while($row = mysqli_fetch_array($result) ) {
if (strlen($row['bookname']) > 9) $row['bookname'] = substr($row['bookname'], 0, 9) . "...";
echo "<tr>";
echo "<td colspan='2' style='padding-bottom: 0;'><a href='library.details.php?id=". $row['id']."' target='content' class='positiontitle-link'><font style='text-shadow: none; font-weight: 800;'>" . $row['bookname']. "</td>";
echo "</tr>";
echo "<tr style='border-top-width: 0; padding-top: 0;'>";
echo "<td style=' padding-top: 0; padding-left: 15px; width: 40%;'> <font color='gray'>Author :</font> " .($row['authorname'] ). "</td>";
echo "<td width='5%' style=' padding-top: 0;'> <font color='gray'>Year Published </font>" . $row['yearpublished'] . "</td>";
echo "</tr>";
}
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 />';