Note that there are some explanatory texts on larger screens.

plurals
  1. POCreate HTML table with arbitrary number of rows in PHP
    text
    copied!<p>I know little about PHP so decided that the creation of a web-based tool for generating Red Hat kickstart files will be a good project to learn with. Among other things, the tool will parse a CSV file and generate a table containing the data taken from it. The input file is in the following format:</p> <pre> host1,10.153.196.248,255.255.255.0,10.153.196.1,00:50:56:ac:69:cb ,10.153.157.113,255.255.255.128,10.153.157.1, ,10.153.157.241,255.255.255.128,10.153.157.129, ,/home,10,,, ,swap,10,,, ,/opt,60,,, ,/data,30,,, ,,,,, host2,10.153.155.124,255.255.255.128,10.153.155.1,00:50:56:ac:69:ce ,10.153.157.114,255.255.255.128,10.153.157.1, ,10.153.157.242,255.255.255.128,10.153.157.129, ,/home,10,,, ,swap,10,,, ,/opt,60,,, ,/data,30,,, ,,,,, </pre> <p>Each section of text represents the information for one server. The fields are as follows:</p> <pre> hostname,eth0 IP, eth0 netmask, eth0 gateway, eth4 MAC null,eth1 IP, eth1 netmask, eth1 gateway, null blank,eth2 IP, eth2 netmask, eth2 gateway, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,partition name, partition size in GB, null, null null,null,null,null,null </pre> <p>At the moment, I can parse it and generate a table with each row in the input file being a row in the table. The function that handles this:</p> <pre><code>function processFile($workFile) { if (file_exists($workFile)) { print '&lt;table&gt;'; $fh = fopen("$workFile", 'rb'); if ($fh) { for ($line = fgets($fh); !feof($fh); $line = fgets($fh)) { $line = trim($line); $info = explode(',', $line); print '&lt;tr&gt;&lt;td&gt;' . $info[0] . '&lt;/td&gt;&lt;td&gt;' . $info[1] . '&lt;/td&gt;&lt;td&gt;' . $info[2] . '&lt;/td&gt;&lt;td&gt;' . $info[3] . '&lt;/td&gt;&lt;/tr&gt;'; } } else { print "Failed to open $workFile"; } print '&lt;/table&gt;'; } else { print "File $workFile does not exist"; } } </code></pre> <p>Which generates:</p> <pre> host1 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size host2 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size </pre> <p>This is a start. However, not every server is going to have four partitions. Some will have many more, some will have one or two fewer. And not knowing that information ahead of time puts a hindrance on what I want to do which is to add a row below the partition information for each server as well as probably break each server up into its own table. Something along the lines of this:</p> <pre> host1 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size partition 5 partition 5 size partition 6 partition 6 size How many disks? [Text Field} host2 eth0 IP eth0 netmask eth0 gateway eth1 IP eth1 netmask eth1 gateway eth2 IP eth2 netmask eth2 gateway partition 1 partition 1 size partition 2 partition 2 size partition 3 partition 3 size partition 4 partition 4 size How many disks? [Text Field} </pre> <p>My prevailing thought is that I'm going to have to have a field in the CSV file indicate that the row contains partition information. It seems the easiest approach. I'm wondering if there is another means I could use, though, that doesn't require altering the format of the input file.</p> <p>I'll also have to figure out how to use the line containing all null fields as the section delimiter.</p> <p>Any thoughts on how I can approach this will be appreciated.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload