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>';
}
Related
I'm trying to put the results of an SQL query inside of a table and everytime theres a new result to make a new table data box. My code looks like
<html>
<style>
table {
font-family: arial, sans-serif;
border-collapse: collapse;
width: 100%;
}
td, th {
border: 1px solid #dddddd;
text-align: left;
padding: 8px;
}
</style>
<?php
<table style="width:100%">
<tr>
<th>
message
</th>
</tr>
while ($row = $resultSet->fetch_assoc()) {
echo "<td>" . $row['message'] . "</td>";
}
?>
But it makes a new box horizontally. How can I make it vertical and how can I also make it so it works for more than just one th element?
You need to add <tr>'s (which indicates new rows in your table)
while ($row = $resultSet->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row['message'] . '</td>';
echo '</tr>';
}
If you want to use more <th>'s, then you need to add more <td>'s.
<table style="width:100%">
<tr>
<th>
message
</th>
<th>
foo
</th>
</tr>
while ($row = $resultSet->fetch_assoc()) {
echo '<tr>';
echo '<td>' . $row['message'] . '</td>';
echo '<td>' . $row['foo'] . '</td>';
echo '</tr>';
}
?>
</table>
<th> and <td> are for setting columns and <tr> are for setting rows. So just make sure you have the same number of <th>'s and <td>'s.
I would recommend that you read up on how HTML tables work.
wrapp a <tr> short for table row around your <td> table data.
while ($row = $resultSet->fetch_assoc()) {
echo "<tr><td>" . $row['message'] . "</td></tr>";
}
Don't forget to end the table after the loop if thats what you want to do.
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>';
}
This question already has answers here:
When to use single quotes, double quotes, and backticks in MySQL
(13 answers)
Closed 6 years ago.
I am a newbie in PHP. I am working on searching function, but it does not work well and I could not find why. The problem is; the $query has been sent and accepted well however it could not find the $query in the database even though the $query existed. I think, the $sql command might be wrong somewhere, but could not find it anyway. Thank you.
Here is my code: asset_search.php
<?php
//Search data in database
$query = $_GET['query'];
$min_length = 3;
if(strlen($query) >= $min_length)
{
//$query = htmlspecialchars($query);
//$query = mysql_real_escape_string($query);
$query = strtoupper($query);
$sql = "SELECT * FROM asset WHERE ('asset_name' LIKE '%".$query."%')";
$result = mysqli_query($conn, $sql);
$row_cnt = mysqli_num_rows($result);
$count = 0;
if($row_cnt > 0)
{
echo "<table style='padding: 5px; font-size: 15px;'>";
echo "<tr><th style='width: 30px; border: 1px solid black; align:'center''>No</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Status</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Sub-identifier</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Name</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Asset Type</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Brand</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Service Tag/ Product Tag/ Product S/N</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>CSM Tag</th>";
echo "<th style='width: 200px; border: 1px solid black; align:'center''>Action</th></tr>";
while($row = mysqli_fetch_assoc($result))
{
echo "<tr><td align='center'>" . ++$count . "</td>";
echo "<td align='center'>" . $row["asset_status"] . "</td>";
echo "<td align='center'><a href='asset_viewfull.php?asset_id=" . $row["asset_id"] . "'><ins>" . $row["asset_subidentifier"] . "</a></ins></td>";
echo "<td align='center'>" . $row["asset_name"] . "</td>";
echo "<td align='center'>" . $row["asset_type"] . "</td>";
echo "<td align='center'>" . $row["asset_brand"] . "</td>";
echo "<td align='center'>" . $row["asset_sertag"] . "</td>";
echo "<td align='center'>" . $row["asset_csmtag"] . "</td>";
if($row["asset_status"] == "DISPOSE")
{
echo "<td align='center'><a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}else
{
echo "<td align='center'><a href='asset_editform.php?asset_id=" . $row["asset_id"] . "'>Edit</a>";
echo " ";
echo "<a href='asset_delete.php?asset_id=" . $row["asset_id"] . "'>Delete</a>";
echo " ";
echo "<a href='asset_disposeform.php?asset_id=" . $row["asset_id"] . "'>Dispose</a>";
echo " ";
echo "<a href='asset_print.php?asset_id=" . $row["asset_id"] . "'>Print</a></td></tr>";
}
}
}else
{
echo "<tr> There is no asset in the database </tr>";
}
echo "</table>";
}
else
{
echo "<script languange = 'Javascript'>
alert('Minimum length is' .$min_length);</script>";
}
//Close connection
mysqli_close($conn);
$count = 0;
?>
Change your query to the following:
SELECT * FROM asset WHERE (`asset_name` LIKE '%".$query."%')
Note the `` around asset_name instead of ''
you should try this without the brackets sometimes it trows out the search,
$sql = "SELECT * FROM asset WHERE `asset_name` LIKE '%{$query}%'";
this is how i preform this task and has never failed me yet!
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
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 />';