I have a strange issue, where I have a table being populated by info from a database.
When I select from the database, the information comes back exactly as I would expect.
For some reason in my HTML table I have an odd field I can't see the source off.
My table header row:
echo "<tr>\n";
echo "<th><h3>ID</h3></th>\n";
echo "<th><h3>First Name</h3></th>\n";
echo "<th><h3>Last Name</h3></th>\n";
echo "<th><h3>Odd additional field</h3></th>\n";
echo "<th><h3>Country</h3></th>\n";
echo "<th><h3>Room type</h3></th>\n";
echo "<th><h3>Number</h3></th>\n";
echo "<th><h3>Checkin Date</h3></th>\n";
echo "<th><h3>Nights</h3></th>\n";
echo "</tr>\n";
The field I have called 'Odd additional field appears even if I take it out, it just isn't named.
My code to populate the table:
while($row = $sth->fetch()){
echo "<tr url=\"edit.php?id=" . $row['id'] . "\">\n";
echo "<td>" . $row['id'] . "</td>\n";
echo "<td>" . $row['firstName'] . "</td>\n";
echo "<td>" . $row['lastName'] . "</h3><td>\n";
echo "<td>" . $row['country'] . "</td>\n";
echo "<td>" . $row['roomtype'] . "</td>\n";
echo "<td>" . $row['roomnumber'] . "</td>\n";
echo "<td>" . $row['checkin'] . "</td>\n";
echo "<td>" . $row['nights'] . "</td>\n";
echo "</tr>\n";
}
The resultant HTML that goes to the browser:
<table width = "80%" align="center" id="example">
<tr>
<th><h3>ID</h3></th>
<th><h3>First Name</h3></th>
<th><h3>Last Name</h3></th>
<th><h3>Country</h3></th>
<th><h3>Room type</h3></th>
<th><h3>Number</h3></th>
<th><h3>Checkin Date</h3></th>
<th><h3>Nights</h3></th>
</tr>
<tr url="edit.php?id=1">
<td>1</td>
<td>Joe</td>
<td>Schmoe</h3><td>
<td>canada</td>
<td>mdorm</td>
<td></td>
<td>2012-11-16</td>
<td>5</td>
</tr>
</table>
Which all seems fine, and yet when viewing in a browser, this is what I see.
How can I trace this mysterious field?
echo "<td>" . $row['lastName'] . "</h3><td>\n";
should be
echo "<td>" . $row['lastName'] . "</td>";
OP Asked for an example on the preferred way to write html for his example. Please don't treat this as an actual answer to his question:
<?php while($row = $sth->fetch()): ?>
<tr data-url="edit.php?id=<?php echo $row['id']; ?>">
<td><?php echo $row['id'] ; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['lastName']; ?></h3><td>
<td><?php echo $row['country']; ?></td>
<td><?php echo $row['roomtype']; ?></td>
<td><?php echo $row['roomnumber']; ?></td>
<td><?php echo $row['checkin']; ?></td>
<td><?php echo $row['nights']; ?></td>
</tr>
<?php endwhile; ?>
Here's why this is preferred, though the opinion may vary from person to person:
Maintainability:
if code is easier to read, it is easier to maintain. when you echo html in php you are subject to escaping html, making it more cluttered to read...
<?php echo "<div class=\"someclass\">" . $content . "</div>"; ?>
effectively the same as:
<?php echo '<div class="someclass">' . $content . '</div>'; ?>
except that the second example (single quotes is string literal) does not require escaping the double quotes. However, in either case, anyone who has to maintain this would much prefer this:
<div class="someclass"><?php echo $content; ?></div>
Because it is cleaner. Why? It just is. Also, many of the editors people use will highlight syntax differently for each language, so that in part makes it easier to read.
There may be cases where echoing html in php makes some sense, but treat it very sparingly. Anyone that reads your code after you will appreciate it.
Side note - you might want to change the html attribute "url" in your example to "data-url" as this is another convention you should use to keep people happy. Custom attributes are supported, but it is not a good idea to just make them up as you go. But if you have to, data- is the standard prefix for custom attributes.
Related
I am new to mysqli I think I have the basic's under me but I am battling to a button in a table that i can execute.
This is my code:
while($row = mysqli_fetch_array($result))
{
echo "<tr align='left'>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['sysdate'] . "</td>";
echo "<td>" . $row['systime'] . "</td>";
echo "<td>" . $row['controller'] . "</td>";
echo "<td>" . $row['sla_client'] . "</td>";
echo "<td>" . $row['notes'] . "</td>";
echo "<td>" **BUTTON TO BE INSERTED HERE** "</td>";
echo "</tr>";
}
The below code obviously is not working in the above table
<button onclick="window.location.href = '../master.php';">Click Here</button>
You may find below solution:
while($row = mysqli_fetch_array($result))
{ ?>
<tr align='left'>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['sysdate']; ?></td>
<td><?php echo $row['systime']; ?></td>
<td><?php echo $row['controller']; ?></td>
<td><?php echo $row['sla_client']; ?></td>
<td><?php echo $row['notes']; ?></td>
<td><?php echo $row['id']; ?></td>
<td>BUTTON text TO BE INSERTED HERE</td>
</tr>
<?php }
Summary:
Just don't put Too much HTML in PHP, it's bad practice.
I would suggest: You could use something like this with HTML "href" tag.
Below is the Example of how to do it:
while($row = mysqli_fetch_array($result))
echo "<tr align='left'>";
echo "<td>".$row['id'] . "</td>";
echo "<td>".$row['sysdate'] . "</td>";
echo "<td>".$row['systime'] . "</td>";
echo "<td>".$row['controller'] . "</td>";
echo "<td>".$row['sla_client'] . "</td>";
echo "<td>".$row['notes'] . "</td>";
/* you could also use something like this with HTML href Tag */
echo "<td><a href=master.php><button>Click Here</button></a></td>";
echo "</tr>";
}
I have a simple form which is designed to allow a user to enter numeric values and then sent them to a page which calculates the result.
The problem I'm having is that post variables that includes a space in their name don't seem to be recognised.
The form is created like so:
<?php
$minerals = mysqli_query($con, "SELECT * FROM items");
?>
<table border='1'>
<tr>
<td>Product: </td>
<td>Quanitity: </td>
<td>Buyback Price: </td>
</tr>
Minerals
</br>
</br>
<?php
while($row = mysqli_fetch_array($minerals)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . "Amount: <input type='text' value='0' name='" . $row['name'] . "'>" . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
if($row['name'] == "Morphite") {
break;
}
}
echo "</table>";
?>
And I'm trying to retrieve the post data like so:
echo '<table border="1">';
echo '<tr><td>Product</td><td>Value</td></tr>';
while($item = mysqli_fetch_array($items)) {
echo '<tr>';
echo '<td>';
echo $item['name'];
echo '</td>';
echo '<td>';
echo $_POST[$item['name']];
echo '</td>';
echo '</tr>';
}
echo '<tr><td>Total</td><td></td></tr>';
echo '</table>';
But any 'name' in the database that contains a space causes nothing to be displayed on the other end.
Since you can never be sure what is written in the database (invalid characters for html etc) you should encode this value before using it as inputs name-attribute. So try maybe htmlspecialchars or strip off the spaces and non-printable and invalid characters. Also if you have a primary key (integer) in your database you could use this to identify the record instead of the name.
Try replacing
echo $_POST[$item['name']];
with
echo $_POST["{$item['name']}"];
I'm trying to make a page that once someone logs in, they can view the contents of the database that is associated with them in a clear, concise and organized way. Currently, I just have PHP echoing HTML and variables to make the page. i.e. I did what I knew would work.
I am new to everything programming and have done Javascript DOM manipulation so I can't see this echo thing can't being the right.
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['Employee_No'] . "</td>";
echo "<td>" . $row['Job_No'] . "</td>";
echo "<td>" . $row['Job_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
I know that technically, this will work, but I am just looking for basic information on how to do this properly, preferably in an easy to understand format.
http://php.net/manual/en/book.dom.php
I found this, it looks like it is potentially what I need, but I am not experienced enough to know and it's not the most beginner friendly documentation.
In PHP when you create HTML output, you're not bound to the DOM. Because different to javascript, there is no DOM to share with the browser. Instead you can just create text output like with echo in your question. That's perfectly alright.
if you want, you can also divide your php code from html code in this way:
phpcode.php
while($row = mysql_fetch_array($result))
{
global $firstName, $lastName;
$firstName=$row['First_Name'];
$lastName=$row['Last_Name'];
include('htmlcode.php');
}
htmlcode.php
<?php global $firstName, $lastName; ?>
<tr>
<td><?php echo $firstName; ?></td>
<td><?php echo $lastName; ?></td>
<tr>
This is not needed most of times but can be helpful in some cases if you want a better readable code.
Just add echo "<table>"; before the while loop and the table should be rendered properly. You may need to use CSS style to further beautify the table
Have a look at PHP PDO. It will be easier when interacting with the database. Your code will look something like this:
echo "<table>";
foreach ($conn->query($query) as $row) {
echo "<tr>";
echo "<td>" . $row['First_Name'] . "</td>";
echo "<td>" . $row['Last_Name'] . "</td>";
echo "<td>" . $row['Employee_No'] . "</td>";
echo "<td>" . $row['Job_No'] . "</td>";
echo "<td>" . $row['Job_Name'] . "</td>";
echo "</tr>";
}
echo "</table>";
I always prefer the following, using pinouchon's code.
<table>
<?php foreach ($conn->query($query) as $row): ?>
<tr>
<td> <?= $row['First_Name'];?></td>
<td> <?= $row['Last_Name'];?></td>
<td> <?= $row['Employee_No'];?></td>
<td> <?= $row['Job_No'];?></td>
<td> <?= $row['Job_Name'];?></td>
</tr>
<?php endforeach;?>
</table>
Here is my code. I am fairly new to PHP and mySQL. I'm having a hard time figuring out why my fetch is returning the string "Array". Also, is there a better way to write all of the echo statements? What's proper php coding rules?
while($row = mysql_fetch_array($question))
{
$answerID = $row['intQAID'];
$getAnswer = mysql_query("SELECT cBody FROM tblQA WHERE intResponseID = $answerID AND intPosterID = '17'");
$answerBody = mysql_fetch_array($getAnswer);
echo "<tr class='forum'>";
echo "<td class='forum'>" . $row['intQAID'] . "</td>";
echo "<td class='forum'>" . substr($row['cBody'], 0, 150) . "</td>";
echo "<td class='forum'>" . $row['cCategory'] . "</td>";
echo "<td class='forum'>" . $row['username'] . "</td>";
echo "<td class='forum'>" . $row['post_time'] . "</td>";
echo "</tr>";
echo "<tr class='forum'>";
echo "<td class='forum'></td>";
echo "<td class='forum'>$answerBody</td>";
echo "<td class='forum'></td>";
echo "<td class='forum'></td>";
echo "<td class='forum'></td>";
}
echo "</table></div></div>";
It is returning Array because mysql_fetch_array is supposed to return an array. This is specified in the documentation and the name of the function!
Because it returns an array, it will still return one even if that array only has one item in it.
Your syntax is wrong. The mysql_fetch_array returns an array by default. If you try echo an array PHP outputs "Array". There are a lot of good examples in the PHP manual on the command.
The heredoc format is great for echoing large blocks. Alternatively you could end your php block and just start it again when needed (which is probably what I would do in this situation).
<?php
// lots of PHP statements
?>
<table>
....
<td><?php echo $variable; ?></td>
....
<?php
// resume php
?>
Some people use short tags in their code for echo. I personally don't although it's a matter of personal preference. The new versions of PHP have them enabled by default but older server installations may not support them.
Try this
<?php
$query = mysql_query("SELECT cBody FROM tblQA WHERE intResponseID = $answerID AND intPosterID = '17'");
while($row = mysql_fetch_assoc($query)):
?>
<tr>
<td><?php echo $row['field_name']; ?></td>
<td><?php echo $row['field_name']; ?></td>
<td><?php echo $row['field_name']; ?></td>
<td><?php echo $row['field_name']; ?></td>
<!-- and so on -->
</tr>
<?php endwhile; ?>
<?php>
while($row = mysql_fetch_array($result2))
{
echo "<tr>";
echo "<td>" . $row['IDNO'] . "</td>";
echo "<td>" . $row['ADDRESS'] . "</td>";
echo "<td>" . $row['LASTNAME'] . "</td>";
echo "<td>" . $row['FIRSTNAME'] . "</td>";
echo "<td>" . <a href='update.php'>view</a> . "</td>";
echo "</tr>";
}
echo "</table>";
}
?>
That's my code, I really don't know the correct format of putting links inside the php tags:
echo "<td>" . <a href='update.php'>view</a> . "</td>";
Please help
When using PHP to make webpages, the strings you echo out are pretty much always in the context of a HTML document. That is, if you want to output a HTML link, just echo it:
echo "<td><a href='update.php'>view</a></td>";
PHP is a templating language, there's no need to be throwing HTML around in strings.
<?php while ($row= mysql_fetch_array($result2)) { ?>
<tr>
<td><?php echo htmlspecialchars($row['IDNO']); ?></td>
<td><?php echo htmlspecialchars($row['ADDRESS']); ?></td>
<td><?php echo htmlspecialchars($row['LASTNAME']); ?></td>
<td><?php echo htmlspecialchars($row['FIRSTNAME']); ?></td>
<td>
view
</td>
</tr>
<?php } ?>
Note the use of HTML-escaping. Without this, < and & characters in your strings will be copied into the raw HTML, causing potential cross-site scripting security problems. Whether using PHP templating or sticking strings together, always HTML-escape plain text output.
If you are not pulling any dynamic values into the HTML for the cell with the link, you do not have to do any string concatenation here. Simply print out the HTML as a string:
echo "<td><a href='update.php'>view</a></td>";
However, it does not seem very useful to have the same link in every row of the table. Maybe you need to add a querystring parameter to the linked URL? To do this you will need to do string concatenation. The example below should get you started (notice the position of the quotes):
echo "<td><a href='update.php?id=" . $row['ID'] . "'>view</a></td>";
your "link" is the same HTML tag as <TD>. So, treat it as well
echo "<td><a href='update.php'>view</a></td>";
or if you want to pass parameter try
echo "<td><a href='update.php/value=".$val."'>view</a></td>";