Column in table not being filled [closed] - php

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 4 years ago.
Improve this question
This is driving me crazy. The table renders correctly in that the column headers are all correct and the first column is correct but then the second column is skipped and the data that should be there is in the 3rd column and the 3rd in the 4th column etc. When I look at the page source everything looks correct. But when I look at "elements" in Chrome's developers tools there is the extra . Here is the code:
echo '<table id="bordered">';
echo '<thead>';
echo '<tr>';
echo '<th></th>';
foreach($products as $product){
/*#var $product Products */
echo '<th>',$product->getProductName(),'</th>';
}
echo '</tr>';
echo '</thead>';
echo '<tbody>';
foreach($customers as $cust){
echo '<tr>';
echo '<td>',$cust->getCustomerName(),'<td>';
$prod = ForecastDisplay_db::getProductsQuantityForCustomer($cust, $products);
$i=0;
foreach($prod as $quantity){
$i++;
if($i == 1){
echo '<td>First Run</td>';
}else{
echo '<td>', $quantity['id'],' ',$quantity['quantity'],'</td>';
}
}
echo '</tr>';
}
echo '</tbody>';
echo '</table>';
I added that 'First Run' to make sure there wasn't a null in the first positions of the array's but there isn't. Anyone have an idea why that column is being skipped?

You did not closed the td tag correctly.
echo '<td>',$cust->getCustomerName(),'<td>';
should be
echo '<td>',$cust->getCustomerName(),'</td>';

Related

How do I show only the column name of the SQL table [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 3 years ago.
Improve this question
Currently my code consists of
$sql = "SHOW columns FROM tblexercise";
$result = mysqli_query($conn,$sql);
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>";
foreach ($row as $field => $value) {
echo "<td>" . $value . "</td>";
}
echo "</tr>";
}
This allows me to show the column names but it also includes all the attributes and types etc.
is there any way to show just the column name?
You don't need a loop inside a loop. You only need one foreach loop and then you can access the key Field which holds the name of the column. The query SHOW COLUMNS is explained in the MySQL doc. You can check in that link what are the results of this query and their sample values. Then you can decide which values you want to access.
$result = $conn->query("SHOW columns FROM tblexercise");
foreach ($result as $row) {
echo "<tr>";
echo "<td>" . $row['Field'] . "</td>";
echo "</tr>";
}

How can i display my data better. I'm having problems [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 7 years ago.
Improve this question
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr></br>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr></br>";
echo "</table>";
}
}
else {
echo "0 results";
}
I am having a problem with the layout of the data once its displayed from the database. Currently, the first record will be displayed in a clear table format, then any other record after that is just 'bunched up'. Any idea on how to solve this?
Just like you started your <table> before the loop, you need to close it after the loop, not inside it. Also, no HTML (like <br>) is allowed between the cells, so remove the <br>. Try this:
$sql = "SELECT moduleCode, moduleTitle FROM TIMETABLE";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo "<table>";
echo "<tr><td>Module Code</td><td>Module Title</td></tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>". $row["moduleCode"]. "</td><td>". $row["moduleTitle"]. "</td></tr>";
}
echo "</table>";
}
else {
echo "0 results";
}

I can't apply css for "while" looping [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
i want to apply css for a while looping but it returns only the first data if i use css , and without css it returns all the data normally. here is my code it's simple.
$users=mysql_query("select * from users");
while($user=mysql_fetch_array($users))
{
echo "<table>";
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</tr>";
echo "</td>";
echo "</table>";
}
Your problem is position:fixed. This tells the browser to lay all tables, one over the other, at window position right:500; left:500; top:100;. So, everything is there, but you tell the browser to put each new data set into a new table, and stack it in front of or behind the previous dataset (=table).
My best guess as to what you wanted to achieve is
$users=mysql_query("select * from users");
echo "<table>";
while($user=mysql_fetch_array($users))
{
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
$users=mysql_query("select * from users");
echo "<table>";
while($user=mysql_fetch_array($users))
{
echo "<tr>";
echo "<td class='user'>";
echo $user['pseudo'];
echo "</td>";
echo "</tr>";
}
echo "</table>";
Your HTML was malformed before. The tr was closed, before the td was (<tr><td></tr></td>) which is invalid. Also it makes more sense to wrap the loop in the table, instead of declaring it in every iteration.

assign value to checkbox using loop php [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
The $file-name should be for example: a.pdf,b.pdf,c.pdf,d.pdf respectively.
But why I cannot assign the value into the checkbox?
please help/__\
while($row = Mysqli_fetch_array($result))
{
$file-name = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]'value=<?php echo $filename ?>' > </td>";
}
Per the comments above, try the following:
while($row = Mysqli_fetch_assoc($result))
{
$filename = $row['Name'] . ".pdf";
echo "<tr>";
echo "<td><input type='checkbox' name='q1[]' value='".$filename."' /> </td>";
}
As the comments to your question mentioned you were assigning $file-name (an invalid variable name in PHP) and trying to use a (presumably) unassigned variable: $filename you also were missing an opening single-quote for the value attribute which could prevent it from properly rendering, and you can use string concatenation instead of trying to echo-inside-an-echo.

mysql_fetch_array, not valid - combining tables in one php page [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 9 years ago.
Improve this question
i've got the following code
$data = mysql_query(
"SELECT md.*, qr.*
FROM moduleDetails md
LEFT JOIN qResponses qr
ON qr.userno = md.userno");
print "<table border cellpadding=3>";
while($info = mysql_fetch_array( $data ))
{
print "<tr>";
print "<th>Faculty</th> <td>".$info['faculty'] . "</td>";
print "<th>Module Code</th> <td>".$info['moduleCode'] . "</td>";
print "<th>Date Started</th> <td>".$info['dateStarted'] . "</td>";
print "<th>Module Title</th> <td>".$info['moduleTitle'] . "</td>";
print "<th>School</th> <td>".$info['school'] . "</td></tr>";
}
print "</table>";
it's giving me a error on line 31, which is the $info = mysql_fetch.... etc
What have i done wrong?.. i can't see a fix for this, it wasnt working fine before before i involved two tables.. any help would be great - cant see whats wrong - new to joining tables.
Probably you get no results from the query. Execute the query to verify it gives result and check you have results before trying to fetch them
if (mysql_num_rows($data) == 0)
die("No Records");
elseif (mysql_num_rows($data) > 0)
{
while($info = mysql_fetch_array( $data ))
{
...............
}
}
else { die("Something else went wrong"); }
Or even better wrap it in a try/catch

Categories