I'm new to OOP (and programming for that matter, started a few months ago) so classes and objects are still a bit of a foreign concept to me. Anyhow, I'm attempting to store a mix of strings and variables in an array for later processing. See method drawtable()
. drawtable()
prints the tableheaders
just fine, but tableheaders
is just an array of strings. The issue comes when I try to print the tablelink
, and text
properties, which I've attempted to include variables in. What am I doing wrong here? Note: the session variables output fine.
I would also appreciate any general critiques of the code below, as I'm new and attempting to nip any bad habits in the bud. Many thanks.
<?php
class table{
function __construct($type){
if($type == "submittals"){
$this->tableheaders = array('Specification Section', 'Submittal #', 'Description', 'Vendor or Manufacturer', 'Date Received By GC', 'File', 'Date Submitted', 'File', 'Date Requested for Review', 'Date Returned to GC', 'File', 'Status', 'Date Returned to Subcontractor or Vendor');
$this->query = "***SELECT Query***";
$this->tablelink = array("/pd/upload/" . $_SESSION['current_project'] . "/s/" . $row['file'], "edit_submittal.php?submittal_id=", "edit_submittal.php?submittal_id=" . $row['submittal_id']);
$this->text = array($row['number'] . " - " . $row['name'], $this->row['submittal_num']);
}
}
function drawtable() {
$this->numheaders = count($this->tableheaders);
$this->db = mysqli_connect(***db connection info***);
$this->results = mysqli_query($this->db, $this->query);
$this->num_results = mysqli_num_rows($this->results);
echo " <table border=\"1\">
<tr>";
for ($i = 0; $i < $this->numheaders; $i++) {
echo "<th>" . $this->tableheaders[$i] . "</td>";
}
echo " </tr>";
for ($j=0; $j < $this->num_results; $j++) {
$row = mysqli_fetch_assoc($this->results);
echo " <tr>";
for($k = 0; $k < $this->numheaders; $k++) {
echo " <td><a href=\"" . $this->tablelink[$k] . "\">" . $this->text[$k] . "xxx</a></td>";
}
}
echo " </tr>
</table>";
}
}
?>
Update:Okay well this works, but I'm sure it's still has an air of noob sloppiness. Suggestions? Thanks
<?php
class Table{
function __construct($type){
if($type == "submittals"){
$this->table_headers = array('Specification Section', 'Submittal #', 'Description', 'Vendor or Manufacturer', 'Date Received By GC', 'File', 'Date Submitted', 'File', 'Date Requested for Review', 'Date Returned to GC', 'File', 'Status', 'Date Returned to Subcontractor or Vendor');
$this->query = "***SELECT query***";
}
}
function draw_table($type) {
$this->num_headers = count($this->table_headers);
$this->db = mysqli_connect(***db connection***);
$this->results = mysqli_query($this->db, $this->query);
$this->num_results = mysqli_num_rows($this->results);
echo " <table border=\"1\">
<tr>";
for ($i = 0; $i < $this->num_headers; $i++) {
echo "<th>" . $this->table_headers[$i] . "</td>";
}
echo " </tr>";
for ($j=0; $j < $this->num_results; $j++) {
$row = mysqli_fetch_assoc($this->results);
if($type == "submittals") {
$this->table_link = array("/pd/upload/" . $_SESSION['current_project'] . "/s/" . $row['file'], "edit_submittal.php?submittal_id=", "edit_submittal.php?submittal_id=" . $row['submittal_id']);
$this->text = array($row['number'] . " - " . $row['name'], $row['submittal_num'], $row['description']);
}
echo " <tr>";
for($k = 0; $k < $this->num_headers; $k++) {
echo " <td><a href=\"" . $this->table_link[$k] . "\">" . $this->text[$k] . "</a></td>";
}
}
echo " </tr>
</table>";
}
function get_table_headers() {
echo $this->table_headers;
}
function get_query() {
echo $this->query;
}
function get_table_link() {
echo $this->table_link;
}
function get_text() {
echo $this->text;
}
}
?>