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

Troubleshooting CodeIgniter Route Problems with the Homepage URL

I added a new route in my routes.php file, but it seems to be causing some problems. $route['default_controller'] = "index"; $route['404_override'] = ''; $route['(:any)'] = "oyna/oyun/$1"; I am trying to redirect / ...

PubNub's integration of WebRTC technology allows for seamless video streaming capabilities

I've been exploring the WebRTC sdk by PubNub and so far, everything has been smooth sailing. However, I'm facing a challenge when it comes to displaying video from a client on my screen. Following their documentation and tutorials, I have writte ...

The CSS 'top' attribute is without impact

As I begin my journey with CSS, I am eager to grasp some of its behaviors. In the CSS file, the following code is defined: #spa { position : absolute; top : 8px; left : 8px; bottom : 8px; right : 8px; min-height : 500px; min-width : 500px ...

When trying to access a view through a controller, the CSS is failing to apply properly

I'm having issues with CSS not working in my Codeigniter project, even when using the base_url() function. < link href="< ? php echo base_url(); ?> application/views/vehicle/css/bootstrap.min.css" rel="stylesheet" media="screen" /> ...

Prevent the line from crossing over the circle in CSS

How can I prevent the line from intersecting the circle? I want the line to be outside the circle. The background (in this case, of the li tag) will be transparent. #project-wrapper { width: 100%; /* Adjusting the position of the element on the pa ...

How come certain invisible screen elements suddenly become visible when the document is printed?

When creating a play-off tournament bracket, I face the challenge of adjusting for varying numbers of participants in each round. Typically, there are 2^n participants in each tour: 16, 8, 4, 2 which can be easily accommodated in a flex column layout. Howe ...

Creating a dynamic design with a responsive background image and three columns of engaging content placed on top using Bootstrap

I'm struggling to translate a design mockup into Bootstrap 3.3.6 Here is an image showcasing the concept I am aiming for: https://i.sstatic.net/1jZmh.png The blue background represents an image, with grey text boxes overlaid on top (one of which in ...

When attempting to open a popup form by clicking a button, the code fails to function on IE6

Everything seems to be running smoothly on Firefox, however I am encountering issues with Internet Explorer 6. Here is a snippet of the problematic code: document.getElementById('layout').style.opacity = .7 document.getElementById('layout&a ...

Step-by-step guide to creating a dynamic button that not only changes its value but also

I am trying to implement a translation button on my website that changes its value along with the text. Currently, I have some code in place where the button toggles between divs, but I am struggling to make the button value switch as well. Given my limit ...

Denied rendering of design due to incompatible MIME type

Just delved into the world of node / express and decided to test my skills by creating a simple tip calculator app. However, I seem to have hit a roadblock with loading my CSS. The console keeps throwing this error: "Refused to apply style from &apos ...

Unable to adjust the padding of a div while the Bootstrap 4 navbar is in a collapsed state on mobile devices

I am facing an issue with a fixed navbar at the top of my page. Below the navbar, I have the page content which includes a Bootstrap 4 image carousel. The problem arises on mobile devices where I need to add top-padding to the page-container (below the nav ...

A dynamic Angular search box designed for filtering columns in a table

I have a functioning table code that displays data related to domains: Now, I would like to enhance this table by adding a dynamic search box specifically for filtering the column named domain. As the user types in new characters in the search box, the ta ...

Getting rid of a particular jQuery animation

I have been searching all over the site, but my limited knowledge prevented me from finding the right solution. I've been working on my website and had previously used a different theme. I have made several changes and am quite happy with it overall. ...

Getting the value of a JavaScript variable and storing it in a Python variable within a Python-CGI script

Is there a way to capture the value of a JavaScript variable and store it in a Python variable? I have a Python-CGI script that generates a selection box where the user can choose an option from a list. I want to then take this selected value and save it ...

What could be causing my HTML button to malfunction when attempting to navigate to a different section of the webpage?

Just starting out and developing my website. My hosting provider is IPage, but I'm running into an issue. When I click on a button to switch to another section of the site, it's not working as expected. Here's the code snippet: <button on ...

CSS transition effect to show content: display: block

After experimenting with different styles for another element, I added padding, height, and opacity to the div. However, there seems to be no transition effect with the current CSS code. Can anyone explain why? When a button is clicked, the class .show is ...

What is the best way to showcase the dropdown menu items of a bootstrap navbar in a horizontal layout instead of vertical?

While utilizing Bootstrap 4, I encountered an issue with the dropdown section where the items are displayed vertically instead of horizontally. I want these dropdown items to be arranged horizontally next to each other. <nav class="navbar navbar-expand ...

Guide on validating an Australian phone number with the HTML pattern

When it comes to PHP, I have found it quite simple to validate Australian phone numbers from input using PHP Regex. Here is the regex pattern I am currently using: /^\({0,1}((0|\+61)(2|4|3|7|8)){0,1}\){0,1}(\ |-){0,1}[0-9]{2}(\ | ...

Streamlined approach to triggering ng-change on a single textbox

Consider this array within an angular controller: somelist = [ { name: 'John', dirty: false }, { name: 'Max', dirty: false }, { name: 'Betty', dirty: false } ]; To iterat ...

Validating HTML Forms Using PHP

I am experimenting with a basic HTML login form, but I can't seem to get it to function properly. This is all new to me as I am just starting out with PHP programming. Any assistance would be greatly appreciated! <?php $correctPassword = 'ho ...