Re: [PHP] Database abstraction?



Stut wrote:
On 17 Apr 2008, at 13:33, Jason Pruim wrote:
Maybe I'm showing my ignorance here in programming... Maybe it's because I delt with a kid who decided to scream for a good share of the night and I'm sleep deprived :) But in my current system I have this:

echo <<<HTML

<div>
<table border="1">
<tr>
<td><a href="?order=a">First Name</a></td>
<td><a href="?order=b">Last Name</a></td>
<td><a href="?order=c">Address 1</a></td>
<td><a href="?order=d">Address 2</a></td>
<td><a href="?order=e">City</a></td>
<td><a href="?order=f">State</a></td>
<td><a href="?order=g">Zip</a></td>
<td><a href="?order=h">Code</a></td>
<td><a href="?order=i">ID #</a></td>
HTML;

while($row = mysqli_fetch_assoc($result)) {
//display the info using heredoc syntac
echo <<<HTML
<tr>
<td>{$row['FName']}</td>
<td>{$row['LName']}</td>
<td>{$row['Add1']}</td>
<td>{$row['Add2']}</td>
<td>{$row['City']}</td>
<td>{$row['State']}</td>
<td>{$row['Zip']}</td>
<td>{$row['XCode']}</td>
<td>{$row['Record']}</td>
HTML;
}

What I want to do is take that first block of code, the one with the links for sorting, and instead of this: "<td><a href="?order=a">First Name</a></td>" I want to put something more like this: "<td><a href="?order=a">$FIELDNAMES[$field{$id}]</a></td>" So I can grab the field names from the database, instead of hardcoding them into my program.

I think I am sleep deprived because the more I tried to clarify it... the harder it got for me to understand :)

echo <<<HTML
<div>
<table border="1">\
HTML;

$first = true;
while ($row = mysqli_fetch_assoc($result)) {
if ($first)
{
echo '<tr>';
foreach (array_keys($row) as $field) {
echo '<td>'.htmlentities($field).'</td>';
}
echo '</tr>';
$first = false;
}

// Output the row here as above
}

-Stut


Very good example, but I would remove data retrieval from the display of data.

<?php

$dataSet = array();
while ($row = mysqli_fetch_assoc($result)) {
$dataSet[] = $row;
}

# do some other stuff

# now display
# Check to see if there is anything in the dataSet
if ( count($dataSet) > 0 ) {

echo '<table>';

# Get the names of the columns from the first result set
$headers = array_keys($dataSet[0]);

# Print the column names
echo '<tr>';
foreach ( $headers AS $column_name ) {
echo "<th>{$column_name}</th>";
}
echo '</tr>';

# Now print your data
foreach ( $dataSet AS $row ) {
echo <<<ROW
<tr>
<td>{$row['FName']}</td>
<td>{$row['LName']}</td>
<td>{$row['Add1']}</td>
<td>{$row['Add2']}</td>
<td>{$row['City']}</td>
<td>{$row['State']}</td>
<td>{$row['Zip']}</td>
<td>{$row['XCode']}</td>
<td>{$row['Record']}</td>
</tr>
ROW;
}
echo '</table>';
}

?>

--
Jim Lucas

"Some men are born to greatness, some achieve greatness,
and some have greatness thrust upon them."

Twelfth Night, Act II, Scene V
by William Shakespeare

.



Relevant Pages

  • Re: [PHP] Database abstraction?
    ... because I delt with a kid who decided to scream for a good share of the night and I'm sleep deprived:) But in my current system I have this: ... echo <<<HTML ...
    (php.general)
  • Re: [PHP] I know this is not easy and Im not stupid but...
    ... echo; ... [PHP] I know this is not easy and I'm not stupid but... ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)
  • Re: [PHP] IFs!
    ... Below is my query, it does a conditional search based on info put in a form ... mssql_errorwhen I should be getting the echo, ... Always round up using ceil() ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)
  • Re: [PHP] Variable not showing up.
    ... The variable, $linkspage, in the link, will not replace with information from the database for some reason. ... in the query. ... echo ''.$filename.''; ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)
  • Re: [PHP] Cant use copy() to copy files
    ... Ben Stones wrote: ... works first of without success): ... Obviously it doesn't echo 5, it just echo's the file name which is a sign it ... "Some men are born to greatness, some achieve greatness, ...
    (php.general)