Display PHP array information in an HTML table

I am facing an issue with an array that contains a record set generated by the CodeIgniter model.

When attempting to display these records in an HTML table using my view, the layout is not rendering correctly. Here is a snippet of my view:

<html>
<head>
    <title>Fuel Order:</title>
</head>
<body onload="window.print()">
<div class="col-xs-12 table-responsive">
<table class="table table-bordered" style="font-size: 11px;">
<tbody>
<?php
    if (!empty($printData)) {
        foreach ($printData as $item) {
?>
            <tr>        
                <td><p style="position: absolute;top: 20cm;right: 14cm"><?=$item->item_name?></p></td>
                <td><p style="position: absolute;top: 20cm;right: 11.5cm"><?=$item->fuel_qty?> Litres</p></td>
                <td><p style="position: absolute;top: 20cm;right: 6.5cm"><?=$this->amount_word?></p></td>                           
            </tr>           
</tbody>
</table>
</div>
</body>
</html> 

Though the view appears to be correct, the issue arises when the records start overlapping each other.

Answer №1

To adjust the positioning of multiple records, consider modifying the top value based on the height of the values or preprinted grid

Ensure that the loop is properly closed BEFORE the </tbody>

<table class="table table-bordered"  style="font-size: 11px; ">
<tbody>
<?php
if (!empty($printData)) {
  $offset=2; // cm
  $cnt=0;
  foreach ($printData as $item) {
?>
<tr>        
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 14cm"><?=$item->item_name?></p></td>
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 11.5cm"><?=$item->fuel_qty?> Litres</p></td>
  <td><p style="position: absolute;top: <?= ($cnt*$offset)+20 ?>cm;right: 6.5cm"><?=$this->amount_word?></p></td>                           
</tr> 
<? $cnt++; 
  } // make sure to close your foreach loop HERE
  ?>
</tbody>

Answer №2

It is recommended to remove the style attributes from the <p> tags as having identical positions for all of them may cause issues.

Consider setting the position of the table instead.

Answer №3

I suggest making a small adjustment in your code setup. Consider ending your foreach loop within the table, instead of placing it outside. Additionally, try positioning your if statement before the table tag to avoid confusion. It is advisable to structure your statements in a clear manner using the following format (utilizing : for clarity). Also, eliminate position: absolute as it may alter the position of your elements relative to their parent container. For repositioning, opt for position: relative. To summarize:

<html>
<head>
    <title>Fuel Order:</title>
</head>
<body onload="window.print()">
<div class="col-xs-12 table-responsive">
    <?php if (!empty($printData)): ?>
        <table class="table table-bordered" style="font-size: 11px;">
            <tbody>
            <?php foreach ($printData as $item): ?>
                <tr>
                    <td><p><?= $item->item_name ?></p></td>
                    <td><p><?= $item->fuel_qty ?> Litres</p></td>
                    <td><p><?= $this->amount_word ?></p></td>
                </tr>
            <?php endforeach; ?>
            </tbody>
        </table>
    <?php endif; ?>
</div>
</body>
</html>

This revised structure should hopefully improve your coding experience :)

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 display and conceal div elements

<ol class="novice"> <li> <p class="glava">HTML</p> <div class="vsebina"> <p>Hyper Text Markup Language (slovensko jezik za označevanje nadbesedila...</p> </div> </li> <li> ...

I am looking to transfer an image stored in a database onto a card

One issue I am facing is that when trying to display an image from a database, uploaded by a user and showing on a card with additional information like name and price, an icon resembling a paper with green appears in the output. Here's my code snippe ...

I find it confusing how certain styles are applied, while others are not

Working on my portfolio website and almost done, but running into issues with Tailwind CSS. Applied styling works mostly, but some disappear at certain breakpoints without explanation. It's mainly affecting overflow effects, hover states, and list sty ...

Issue with Jquery: Checkbox fails to get checked in Internet Explorer 7

Having trouble with checking a checkbox, I've attempted the following steps: $('#someId').attr('checked','checked'); $('#someId').attr('checked', true); Both of these methods are effective for I ...

Bootstrap: Organize Columns Simultaneously with Push and Pull for Customized Column Ordering

I am experiencing an issue with the concurrent use of "Push" and "Pull" column ordering classes. I would like my columns to be ordered like this: However, the result on medium and larger screen sizes is <section style="direction:rtl;"> <div cla ...

"Trouble encountered when attempting to add a CSS class to a select option in Firefox version 5

Here is the HTML code I am working with: <select id="WageBenifits_PayType" name="WageBenifits.PayType"> <option class="selectOption" value="" selected="selected">Please Select Value</option> <option value="COM">COM - COMMIS ...

What is the process for adding tailwind CSS via npm?

Before, I was including Tailwind using a CDN. Now, I have installed it with npm and all three .css files are included, but the styles are not appearing in my HTML document. Here is the directory structure and a link to style.css The content of tailwind.c ...

Retrieve text excluding a specific class or id using jQuery

I need help with extracting text from an HTML div. <div id="example"> I am writing a <span class='outer'>query for help <span class='inner'>on a coding forum</span></span> </div> const te ...

What causes the delay in CSS animations?

Is there a way to trigger an "updating" image to spin while a long JavaScript function is running? I am currently using JQuery to add a class called "spinning", defined in my CSS for the animation. The problem is that when I add the class and then call a l ...

Employing CSS selectors to target the subsequent element that is accessible

Here is the structure of my HTML: <input type = "checkbox" style = "display:none" id = "select"> <label for = "select" id = 'click'> click </label> <div class = 'next'> </div> I am trying to style t ...

Once the input is disabled, what is the best way to delete the previously inserted text?

Here's the scenario: I have two options that display different questions each. If a user decides to switch their option at the last minute, any inputs within option 1 will hide and show option 2. However, all input texts need to be removed and disabl ...

Unable to retrieve the values from document.getElementById("") or document.getElementById("").innerHtml appears to be malfunctioning

I am currently working on an HTA application that includes multiple <input type="text"> fields for users to input data. My goal is to pass this data to a VBScript for processing. Below is the HTML structure: <div id="tableContent" name="tableCon ...

Class for Eliminating the Background Image Using Bootstrap

Is there a Bootstrap class that can be used to remove a background image from a div? Currently, I have this style defined in my CSS: background-image: linear-gradient(to bottom, rgba(0,0,0,0.1), rgba(0,0,0,0)); I would like to remove it using: bg-img-non ...

Issues with interpreting PHP within HTML documents

I'm having trouble using PHP on my HTML page. I've added AddType application/x-httpd-php .html to the .htaccess file as recommended, but when I try to access the page in a browser, a dialog box pops up asking if I want to save the file or open it ...

Elements within the div are overlapping each other

Despite adding margin, my h3 tag and p tag in a div keep overlapping. .title { margin: 0 0 26px; width: 335px; height: 28px; font-size: 24px; font-stretch: normal; font-style: normal; line-height: 36px; letter-spacing: 0; text-align: c ...

Automatically adjusting input box size and position in real-time

I am working on a form that includes a button and an input field. When the user clicks on the button "ADD MORE FIELDS," I want to dynamically generate a new input box. Although I have tried using code from this jsfiddle link and it works, my goal is to ach ...

What is the method of posting to PHP using JavaScript without utilizing Ajax?

My current code involves a drag and drop file uploader to PHP using ajax, but it seems that my web host does not support ajax. Is there an alternative method to achieve this without using ajax? Specifically, I am facing issues with the UploadFile functio ...

Disable text input in Flatpickr and remove the default iPhone calendar

We are facing an issue with the iPhone and iPad when using flatpickr for our calendars. When the calendar is opened, the cursor automatically focuses on the text box, triggering the default iPhone calendar to appear below. This problem does not occur on An ...

Utilize a variable within an HTML attribute

Currently utilizing Angular and I have the following HTML snippet <input onKeyDown="if(this.value.length==12 && event.keyCode!=8) return false;"/> Is there a way for me to incorporate the variable "myNum" instead of the ...

Tips for creating a form-flip, similar to a card-flip effect, using web technologies

Looking to create a card flip effect similar to the one shown here. Once the sign up process is finished, the card will smoothly flip over to reveal the other side with a transitioning effect. ...