Add a hyperlink alongside every row in the table

My table comprises three columns in each row, and I am looking to include a link next to each row.

<table>
    <th>
        Name:
    </th>
    <th>
        Price:
    </th>
    <th>
        Description
    </th>

    <?php while ($row = $foods->fetch()) { ?>
        <tr>
            <td>
                <?php echo $row['Name']; ?>
            </td>
            <td>
                <?php echo $row['foodPrice']; ?>
            </td>
            <td>
                <?php echo $row['foodDescription']; ?>
            </td>
        <a class="editLink">roma</a>
        </tr>
    <?php } ?>
</table>

I attempted to place the link a following the last td, but it appears at the top of the table instead. Thank you in advance for your help.

Answer №1

Make sure to include a table cell for the link and ensure that the th tags are nested within a tr tag.

<table>
    <tr>
        <th>
            Name:
        </th>
        <th>
            Price:
        </th>
        <th>
            Description
        </th>
        <th>
        </th>
    </tr>

    <?php while ($row = $foods->fetch()) { ?>
        <tr>
            <td>
                <?php echo $row['Name']; ?>
            </td>
            <td>
                <?php echo $row['foodPrice']; ?>
            </td>
            <td>
                <?php echo $row['foodDescription']; ?>
            </td>
            <td class="link"><!-- Don't forget this -->
                <a class="editLink">roma</a>
            </td><!-- also this -->
        </tr>
    <?php } ?>
</table>

To customize the style of the link cell, apply a CSS class of link to the td containing the a tag. Then define the styling in CSS as shown below:

CSS

table{
    text-align: center;
}

td{
    width: 200px;
}

td.link{
    width: 50px;
}

View the Working Example: http://jsfiddle.net/y9C3G/

Answer №2

Having an <a> tag directly inside a <tr> is not considered valid HTML. To correct this, add another column to the table and place the link in its own cell. You may also want to consider adding another <th> in the first row or using the attribute colspan="2" for the Description column.

Furthermore, it is recommended that a table includes a <tbody> element, and I recommend organizing your header cells within a <thead>:

<table>
  <thead>
    ...
  </thead>
  <tbody>
    ...
  </tbody>
</table>

Answer №3

<tr>
    <td>Custom Text</td>
    <td>Custom Text</td>
    <td>Custom Text</td>
    <td class="myNewLinkClass"><a href=""></a></td>
</tr>

<style>
    .myNewLinkClass {
        width: 150px; /* Adjusting the width for td with new class */ 
    }
</style>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Unable to attach the event listener for the 'onchange' event to a div nested within a ul element

Here is the HTML code I am working with: <ul> <li> <a href="#"> <div></div> Actions <div></div> </a> <ul> <li> <a> &l ...

Issue with jQuery DataTables column.width setting failing to meet expectations

Currently, I am utilizing datatables for my project. According to the datatables documentation found here, it suggests using the columns.width option to manage column width. However, when I implement columns.width and render the table, it seems to disreg ...

I'm curious as to why the web browser is showing a timestamp below the image I have on my HTML page

Issue at Hand: I am currently working on coding a website that involves displaying an image. However, when the image is loaded, a timestamp appears underneath it in the web browser. Query: Could you please shed some light on why a timestamp is showing up ...

Is there a way for me to align my div with my header on the same line?

HTML: <h2> RAN shares false news story about Hershey's Oil Commitment</h2> <div class="date"> <div class="number">27</div> <div class="month">Oct</div> </div> <p>The Rainforest Action Netwo ...

Is it possible to apply capital spacing in CSS for OpenType fonts without using font-feature-settings?

Is it possible to adjust capital spacing (cpsp) for an element without impacting any other applied OpenType features? Unfortunately, utilizing the font-feature-settings is not a viable solution. For example, if we use font-feature-settings: 'cpsp&apo ...

Updating the logo image on mobile devices using responsive CSS styling

My goal is to show a different image to users on mobile devices using only CSS, as I am unable to access the HTML code. On mobile, I used display:none for the header-logo-image img { and then added a background-url to the div to successfully display my alt ...

Using jQuery AJAX to preload GIF images for CSS background-image loading

For some time, I have been utilizing jQuery AJAX to preload GIF images with the following code: $("#images").html("<img id='loader' src='../img/ajax-loader.gif' />"); Now, I am attempting to achieve a similar effect (such as a s ...

Embed HTML code to a website using a PHP script on a separate webpage

As a newcomer to PHP, I may have a silly question. Let's say I have a form: <form id="createNewGallery" name="newgallery" method="GET" action="code.php"><p><strong>please choose the number of pictures you want to upload: </str ...

The event listener is failing to trigger the JavaScript function upon click

I am experiencing an issue with an onClick listener triggering an AJAX call to a PHP script. It seems that the click is not being recognized. Any suggestions on what could be causing this? Index.html <html> <head> <script src="//ajax.googl ...

CSS Priority - determining the ultimate winner

I have an upcoming exam and I'm reviewing the previous exam paper that was given to me. The question is: When multiple style sheet rules are applied to the same element, which type of rule takes precedence? a. Any declaration from the browser b. ...

Edge and Internet Explorer fail to recognize the :focus CSS selector

I am utilizing CSS to make our placeholders jump up when clicked inside, but Edge is not recognizing the CSS. It works fine on every other browser except for Edge. I've tried various solutions but none seem to work on Edge. Any thoughts on what might ...

React Navigation Item Toolbar Misplacement

I've been trying to align the navigation item with the logo on the same line within the toolbar, but I'm facing an issue where they keep appearing in different rows. To see the error for yourself, check out the code sandbox here. This is how I s ...

Is it possible to display a variety of color schemes in just one console.log()?

My task involves working with an array of hexadecimal values, "colors": ["#d5dd90","#e6bb45","#ef9770"] To log these out in different colors, I used the following method: colors.forEach((value)=>{ console.log(& ...

Missing sidebar display

Hi there! I am having an issue with my sidebar not appearing correctly when I click on the toggle button. It seems to be moving to the side, but the sidebar itself is blank or transparent. I suspect that the problem lies within my JavaScript file. As a beg ...

The userscript will keep the window open as long as it remains inactive

Hello everyone, I'm excited to join the StackOverflow community and dive into userscripts for the first time! Putting aside any unnecessary details, I'm encountering a small issue with a script I recently created. (function () { $("#enbut"). ...

Utilizing SEO and incorporating special characters like !# in a website's URL

I came across an interesting concept about designing a website that utilizes AJAX to load each page section without compromising SEO. It involved incorporating !# in the URL, similar to the approach adopted by Twitter. However, I've been unable to loc ...

Is It Always Necessary to Specify a Width and Height for an Image?

Is it necessary to specify both the Width and Height of an image on a webpage for consistent display across all browsers, or is specifying just one sufficient? While it may appear fine on certain browsers, I want to double-check for accuracy. Appreciate ...

I am looking for guidance on removing the bottom line from the ionic 4 segment indicator. Any advice or tips on

.segment-button-indicator { -ms-flex-item-align: end; align-self: flex-end; width: 100%; height: 2px; background-color: var(--indicator-color); opacity: 1; } I am a beginner in hybrid app development and ...

Having issues when dynamically adding options to a multiselect dropdown

I'm working on dynamically adding options to a multiselect using AJAX: <select size='2' name="CraftCode" id=@ccrf class="form-control js-select manualentrydd" ></select> $.ajax({ type: "GET", url: "GetCraftCodes", data: ...

Using Jquery to loop through a function for every select box that is added dynamically

Encountering some challenges with jQuery while attempting to utilize the same function and iterate over select boxes that are dynamically generated with jQuery's clone() method. Here is a snippet of the content being cloned: <select name="expense ...