Re: Database abstraction?
| From: | Jason Pruim | Date: | Thu, 17 Apr 2008 18:27:22 +0000 |
| Subject: | Re: Database abstraction? | ||
| References: | 1 2 3 4 5 6 7 8 9 10 11 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-273211@lists.php.net to get a copy of this message | ||
On Apr 17, 2008, at 2:22 PM, Jim Lucas wrote:
Jim Lucas wrote:I did notice that, it was an easy fix :) I'll definitely be reading up on join... It works perfectly for what I'm doing! I may have to rewrite a few other functions I have that are using the actual field names... Thanks Jim! -- Jason Pruim Raoset Inc. Technology Manager MQC Specialist 3251 132nd ave Holland, MI, 49424-9337 www.raoset.com japruim@raoset.comJim Lucas wrote:Shoot, same typo add a single quote at the end of the first echo lineJason Pruim wrote:I should shoot my own foot, you should encode the values before you do this. Something like this would do the job foreach ( $dataSet AS $row ) { echo '<tr><td>; echo join('</td><td>', array_map('htmlentities', $row)); echo '</td></tr>'; }On Apr 17, 2008, at 11:14 AM, Jim Lucas wrote:This would do it. foreach ( $dataSet AS $row ) { echo '<tr><td>.join('</td><td>', $row).'</td></tr>'; }Stut wrote:Both ideas worked great... Thank you! Now I do still have one question, I decided that what Jim put up worked a little bit better for me so I'm going off of his code, what I want to do now though, is take this code:On 17 Apr 2008, at 13:33, Jason Pruim wrote: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 ) {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 <<<HTMLecho <<<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 syntacecho <<<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 :)<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 } -Stutecho "<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>'; } ?>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;}And turn it into some kind of a loop that would pull the info dynamically so I can have something more like this: <?PHP while($dataSet <= $num_rows) { //print dynamic fields based off of field names echo <<<ROW <tr> <td>$dataSet['$field']</td> </tr> ROW; } ?> And what would print on the page would be something along the lines of: <?PHP echo <<<TEST <tr> <td>Jason</td> <td>Pruim</td> <td>My Address</td> <td>My Other Address</td> </tr> TEST; ?> Etc. Etc. Etc. I'm trying to avoid hardcoding things like field names so that I can use this software for multiple customers without having to completely customize it for everyone since so many parts will be the same. Maybe though I'm trying to abstract the wrong part of the program... I could do it to a few other parts and just customize this one... Need to think about that. Any ideas are greatly appreciated :) -- Jason Pruim Raoset Inc. Technology Manager MQC Specialist 3251 132nd ave Holland, MI, 49424-9337 www.raoset.com japruim@raoset.com