how to put links in php - php

<?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>";

Related

Adding php variable into a href link into a table

I am working on uploading a file along with a description and table and then displaying it in a table format. My problem is that I'm not sure how to link my the path for the uploaded file into the table so the user can click on the link in the table and it will download.
Code:
This is what I'm attempting to use>
<?php
include 'connect.php';
$result = mysqli_query($con,"SELECT DocDate, Description, DocFile FROM Documents");
echo "<table border='0' width='100%'>
<col width='50'>
<col width='100'>
<tr>
<th>Date</th>
<th>Description</th>
<th>File</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td>" $row['DocFile'] "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
If you feel it to be usefull I'm happy to add the code where I upload the file to my server.
EdiT Sorry I put in the wrong variable thing into my table, I don't think it changes it too much
Is this what you're looking for?
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['DocDate'] . "</td>";
echo "<td>" . $row['Description'] . "</td>";
echo "<td> " . $row['DocFile'] . " </td>";
echo "</tr>";
}
This is how you can do
echo '<td>'.$name.'</td>';
I prefer to use ' ' and within it have the HTML since HTML will contain a lot of "" so no need to use escape them and then separate PHP and HTML with concatenation.
you have syntax error in this line in while loop:
echo "<td>" $name "</td>";
should be :
echo "<td> $name </td>";
In this line you have an error:
echo "<td>" $row['DocFile'] "</td>";
You need to scape the " character:
echo "<td> " . $row['DocFile'] . " </td>";
With your syntax you are creating an error because you are ending the string after the td tag.
If you are using a web url which uses a php variable in the url and want to open a new tab when you click on the hyper link, use this
echo "<td><a target='_blank' href=\"http://view.php?Id=".$row['Id']."\">". $row['Id'] ."</a></td>";
This is helpful when you have to use a php variable in the hyperlink and is being used in a table. Clicking on the Id will open the page related to that Id in a new tab in this case.

<tr> and <td> tags overwriting <pre>

Is there a reason why <tr> and <td> tags would work whilst inside a <pre> tag?
I am using a <pre> tag to show some PHP code on a website and within the code are various instances of the html tags <tr> and <td>, these are jumping out of the <pre> tags and executing at the bottom. Why would they do this??
They are included after echo and inside "" and '' and both are executing the same. I have no pre in my CSS and can't find any similar problems on the net.
Example:
echo '<table border="1">';
echo "<tr><td>A</td><td>B</td><td>C</td><td>D</td><td>E</td></tr>";
while($info = mysql_fetch_array($data)){
echo "<tr>";
echo "<td>" . $info['a'] . "</td>";
echo "<td>" . $info['b'] . "</td>";
echo "<td>" . $info['c'] . "</td>";
echo "<td>" . $info['d'] . "</td>";
echo "<td>" . $info['e'] . "</td>";
echo "</tr>";
};
echo "</table>";
I have tried using print, no change
You have to encode the < and > as HTML entities.
https://mothereff.in/html-entities
https://html.spec.whatwg.org/multipage/named-characters.html#named-character-references
so < would be < and > would be >
I think the specific use of <pre> tag is to display the element as what is it, even non- multiple spaces will be display, nor a breakline by a keyboard (non-<br /> tag), and not to fail a HTML tag. You can use this format <tag> to fail a tag. But I didn't know if <pre> can fails a PHP code.

Hidden field in 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.

Making a Dynamic Page Using PHP

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>

Query fetch returning 'Array'

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; ?>

Categories