Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p><a href="http://www.php.net/manual/en/language.operators.arithmetic.php" rel="nofollow"><code>+</code> and <code>+=</code></a> add numbers, <a href="http://www.php.net/manual/en/language.operators.string.php" rel="nofollow"><code>.</code> and <code>.=</code></a> append strings.</p> <p>Field names in the result array don't include the table name. You should reference (e.g.) <code>$row['TechName']</code>, not <code>$row['T.TechName']</code>.</p> <h3>Off Topic</h3> <p>Mixing DB access and output creates too high a coupling. Ideally, each would be handled in separate layers. You can start this process by using PDO or (in PHP 5.4 and greater) mysqli, and iterate with <code>foreach</code> over results instead of explicitly calling any methods that are a part of the DB results class interface. This magic is possible because <a href="http://php.net/PDOStatement" rel="nofollow"><code>PDOStatement</code></a> and (as of PHP 5.4) <a href="http://php.net/mysqli_result" rel="nofollow"><code>mysqli_result</code></a> support the <a href="http://php.net/Traversable" rel="nofollow"><code>Traversable</code></a> interface.</p> <p><a href="http://brainstormsandraves.com/articles/semantics/structure/#br" rel="nofollow"><code>&lt;br/&gt;</code></a> is rarely <a href="http://webstyleguide.com/wsg3/5-site-structure/2-semantic-markup.html" rel="nofollow">semantic</a>; use something more appropriate, such as a heading element, or apply styling to existing elements.</p> <p><code>&lt;th&gt;</code> isn't a valid parent of <code>&lt;td&gt;</code>; <code>&lt;tr&gt;</code> should be the parent and <code>&lt;th&gt;</code> should be used <em>in place of</em> <code>&lt;td&gt;</code>.</p> <p>While brackets are optional when a branch consists of a single statement, it's usually considered bad style to leave them out, as it occasionally leads to bugs.</p> <p>While <code>else if</code> is functionally equivalent to <code>elseif</code>, it's handled by the runtime as an <code>else</code> with an <code>if</code> as the body:</p> <pre><code>if (...) { } else { if (...) { } } </code></pre> <p><code>elseif</code> is the preferred form.</p> <p>If there's any chance that <code>$sort</code> could hold a non-string, <code>==</code> could evaluate to <code>TRUE</code> even (<code>0 == 'Tech'</code>). Use <a href="http://php.net/===" rel="nofollow">strict comparison (<code>===</code>)</a> to ensure the values are compared as strings.</p> <p>Apply the various suggested alterations and you'll wind up with something like:</p> <pre><code># Future improvement: abstract each section into methods that are part of separate layers # Database access $statement = 'SELECT T.TechName, C.ClientName, S.SiteName, L.Time, L.Type FROM Log AS L JOIN Tech AS T ON T.TechID=L.TechID JOIN Client AS C ON C.ClientID=L.ClientID JOIN Site AS S ON S.SiteID=L.SiteID'; if ($sort === 'Tech') { $query .= ' ORDER BY T.TechName ASC, L.Time DESC'; } elseif ($sort === 'Client') { $query .= ' ORDER BY C.ClientName ASC, L.Time DESC'; } try { $data = $db-&gt;query($statement); } catch (PDOException $exc) { $data = array(); # handle error appropriately: inform user that there was an internal error. Log the actual error and possibly notify the dev team. ... } # Output/User interface $fields = array('TechName' =&gt; 'Tech', 'ClientName' =&gt; 'Client', 'SiteName' =&gt; 'Site', 'Time' =&gt; 'Date and Time', 'Type' =&gt; 'Type'); if ($data) { ?&gt; &lt;h3&gt;Real-Time check in/check out&lt;/h3&gt; &lt;table&gt; &lt;thead&gt; &lt;tr&gt;&lt;th&gt;&lt;?php echo implode('&lt;/th&gt;&lt;th&gt;', $fields) ?&gt;&lt;/th&gt;&lt;/tr&gt; &lt;/thead&gt; &lt;tbody&gt; &lt;?php foreach ($data as $row) { ?&gt; &lt;tr&gt; &lt;?php foreach ($fields as $field =&gt; $label) { ?&gt; &lt;td&gt;&lt;?php echo $row[$field] ?&gt;&lt;/td&gt; &lt;?php } ?&gt; &lt;/tr&gt; &lt;?php } ?&gt; &lt;/tbody&gt; &lt;/table&gt; &lt;?php } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

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