Question

In: Computer Science

Write a script that will output a table where the rows and cells are labeled. The...

Write a script that will output a table where the rows and cells are labeled. The script should be versatile, so one could easily change the number of rows and cells the script should output. For this exercise have the script create a table with 15 rows and 5 cells. Refer to the tables lesson for information on how to create a table. You need to write all the PHP functionality above the doctype and display the resulting HTML string via a variable in the echo statement within the body element

Solutions

Expert Solution


step-1 : Define two variables 'row_count' and 'col_count' two hold the number of rows and cells in the table.The number of rows and cells in the table ,can easily be changed by changing the values of the variables 'row_count' and 'col_count' .

step-2 : Initialize the variable 'table' with a null string. The variable 'table' holds the output string ,ie the table to be displayed.

step-3 : Check whether the variable 'row_count' is greater than zero and append the html tag '<table>' .Also append '<tr>' tag by using a for loop to generate the required number of rows(row_count)

step-4 : Check whether the variable 'col_count' is greater than zero and append the html tag '<td>' and the label of the cell   by using a for loop to generate the required number of cells(col_count)

step-5: Output the table in the body section using an echo statement.The table contains the specified number of rows and columns and each cell is labelled as 'cell' followed by row number and column number.The row number and column number are seperated by a coma(,).

The code is given below,

<?php

$row_count = 15 ;
$col_count = 5 ;

$table = '' ;

if ( $row_count > 0) {
   $table .= '<table>' ;
    for($i = 1; $i <= $row_count; $i++){
      $table .= '<tr>' ;
      if ($col_count > 0) {
        for ($k=1; $k <= $col_count ; $k++) {
          $table .= '<td>'.' cell '.$i.','.$k.'</td>' ;
        }
      }
      $table .= '</tr>' ;
    }
    $table .= '</table>' ;
}

?>


<!DOCTYPE html>
<html>
<head>
<style>
    td {
      border: 1px solid black;
      width: 100px;
    }
</style>
<title></title>
</head>
<body>
    <?php echo $table; ?>
</body>
</html>

Please refer to the screenshot of the code to understand the indentation of the code.

0) { $table" src= "https://media.cheggcdn.com/coop/e03/e03adfc2-44e1-426c-82df-4a86d8221111/1601874500405_code-table.png" style="height:617px;width:705px;" />

The output of the code is shown below,


Related Solutions

write a script that will output a table where the rows and cells are labeled the...
write a script that will output a table where the rows and cells are labeled the script should be versatile so one can change the number of rows and cells the script outputs have the script create a table with 15 rows and 5 cells refer to the tables lesson for info on how to create a table you need to write all the PHP functionality above the doctype and display the remaining HTML string via a variable in the...
Write a script to display the following patterns on the screen. Number of rows and columns...
Write a script to display the following patterns on the screen. Number of rows and columns are taken from the command arguments; if they are missing, set default to 3 (rows) and 4 (columns). Hint: you will use a nested loop. **** **** **** a) Display the source code in an editor (#4-11) b) Execute your script in the terminal, and display the command and the result (#4-12)
modify code to write the output as an HTML table to a file in the output...
modify code to write the output as an HTML table to a file in the output directory. The file that is saying to work at : SOURCE CODE IN PERL: print "Enter principal amount: "; $P=; while($P<=0) { print "Principal must be positive. Try again: "; $P=; } print "Enter number of times interest is applied in a year: "; $n=; while($n!=12 && $n!=4 && $n!=2 && $n!=1) { print "It must be 12, 4, 2 or 1. Try again:...
Imagine the year is 2116, and the periodic table has been expanded to 25 rows. Write...
Imagine the year is 2116, and the periodic table has been expanded to 25 rows. Write a set of four quantum numbers for the outermost election of the halogen in the last row.
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result:             25   69   51   26 171...
Write a Python program which adds up columns and rows of given table as shown in...
Write a Python program which adds up columns and rows of given table as shown in the specified figure. Example test case (the four lines below “Input cell value” are input from user, and the five lines below “Results:” are the output of the program.): Input number of rows/columns (0 to exit) 4 Input cell value: 25 69 51 26 68 35 29 54 54 57 45 63 61 68 47 59 Result: 25 69 51 26 171 68 35...
I need a copy of organized data in a spreadsheet with rows and columns labeled, can...
I need a copy of organized data in a spreadsheet with rows and columns labeled, can anyone help me with this please? I am doing a made up experiment where I see if eating vegetarian diets makes someone healthier. I am ‘supposed’ to find a group of people at school willing to participate and change their diet, pull their names from a jar, and randomly assign who will try the vegetarian diet and who’s diet will remain the same that...
Summary Lewis wants you to write another script that shows a table of events at the...
Summary Lewis wants you to write another script that shows a table of events at the Lyman Hall Theater over the next two weeks from the current date. He has already created three arrays for use with the script: The eventDates array containing a list of dates and times at which theater events are scheduled. The eventDescriptions array containing the description of those events. The eventPrices array containing the admission prices of those events. Lewis has already written the page...
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n'...
Matlab script that uses loops to create a multiplication table (matrix) where the user enters 'n' (nxn chart).
Write a SQL script to add another table to your database. Include these fields in your...
Write a SQL script to add another table to your database. Include these fields in your Product table: Field Name Description Data Type Sample Value ProductID Product ID integer 5 ProductName Product Name varchar(50) candle Description Product Description varchar(255) Bee’s wax candle picUrl Filename of the product’s picture varchar(50) candle.gif Price Product Price decimal 10.99           ProductID should be the Primary Key. It is an auto-increment field.           The Price field stores prices to 7 significant digits and to 2...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT