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>
Related
best regards.
I know perhaps some's considering my question is quite stupido. But I've been trying looking for to this simple scripting. As my post title above, how to make the query results into a url. I have this scripts:
$result = mysqli_query($con,'SELECT......');
echo "<table border='1'>
<tr>
<th>Title</th>
<th>Author</th>
<th>Publisher</th>
<th>Category</th>
<th>Link</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['title'] . "</td>";
echo "<td>" . $row['author_name'] . "</td>";
echo "<td>" . $row['publisher_name'] . "</td>";
echo "<td>" . $row['cat_name'] . "</td>";
echo "<td>" . $row['url_flipbook'] . "</td>";
echo "</tr>";
}
echo "</table>";
I want to make the result from $row ->'url_flipbook' into a url. The $row -> 'url_flipbook' will produce a html pages that users can click on it, it's located to a folder in my localhost.
The database field:
| url_flipbook |
-----------------
/myspace/click-it/info_1.html
I want that query results became a link for the output.
I've tried:
<a href=''echo $row['url_flipbook'];'?>''>FLIPBOOK</a>"</td>";
echo "<td>" . <a href='$row['url_flipbook']>FLIPBOOK</a>"</td>";
Nothing works...if you could help me to solve this problems. Thank you so much...
best regards,
Kris
Just concat the string to the href:
echo "<td>FLIPBOOK</td>";
or alternatively:
echo '<td>FLIPBOOK</td>';
did u try
echo "<td>" . "<a href='" . $row['url_flipbook'] . "' >FLIPBOOK</a></td>";
try this
echo "<td><a href='".$row['url_flipbook']."'>FLIPBOOK</a></td> ";
Currently I'm creating just a simple website that I'm fooling around with. Users can add movies to watch, and then can view them later on. What my current problem is, is this.
Sorry for the large image. As you can see its displaying the first result correctly, but the second result gets all skrewy and displays at the top of the screen. My code for displaying the data is:
$result = mysql_query("SELECT * FROM `movies`");
echo "
<table id=\"allTable\" align=\"center\" border=\"0\" cellpadding=\"4\" cellspacing=\"0\" width=\"100%\">
<tr>
<th>ID</th>
<th>Movie</th>
<th>Genre</th></tr>";
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
echo "</table>";
}
Any help would be greatly appreciated!
EDIT Fixed the problem right after I created this. Removed from while loop and put it under. Fixed.
echo "</table>"; should be moved outside of your while($row = mysql_fetch_array($result))
while($row = mysql_fetch_array($result)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['genre'] . "</td";
echo "</tr><br />";
}
echo "</table>";
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.
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>";