Class some php....

Posted on 2013-11-30
Last Modified: 2013-12-02
Hello. . . How can i put a class to into a fetch array?

This is what i got:
while($record = $myData->fetch_array()){
echo "<form action=admin.php method=post>";
echo "<tr>";
echo "<td>" . "<input type=hidden name=ID value=" . $record['ID'] . "> </td>";
echo "<td>" . "<input type=text name=_dato size='10' value='" . $record['dato'] . "'> </td>";
echo "<td>" . "<input type=text name=_c2 size='15' value='" . $record['c1'] . "'> </td>";
echo "<td>" . "<input type=text name=_c3 size='15' value=" . $record['c2'] . "> </td>";
echo "<td>" . "<input type=text name=_c4 size='15' value=" . $record['c3'] . "> </td>";
echo "<td>" . "<input type=text name=_c5 size='15' value=" . $record['c5'] . "> </td>";
echo "<td>" . "<input type=text name=_c6 size='15' value=" . $record['c6'] . "> </td>";
echo "<td>" . "<input type=submit name=opdater_dato value=Opdater" . "> </td>";
echo "</tr>";
echo "</form>";

Open in new window

I managed to change the size (line 5), but what if i wanna center the text inside of the textbox? I tried different variations, nothing worked. . . I guees im just missing the easy way to create a CSS class that will make it possible to change the td or just the textbox.
Question by:Mike Kristensen
LVL 15

Expert Comment

ID: 39686932
If you could please elaborate little more it would be great.

do you mean to say you want to add it in <td> like echo "<td class="hi">"
and all class file would be same is it so . please confirm
LVL 38

Expert Comment

by:Tom Beck
ID: 39686939
If you wanted to center text in the textbox for example you could add a class to the input.

echo "<td>" . "<input class='centered' type=text name=_dato size='10' value='" . $record['dato'] . "'> </td>";

.centered { text-align:center }
LVL 109

Accepted Solution

Ray Paseur earned 250 total points
ID: 39686958
If you use fetch_array() you have chosen the least efficient way of accessing the data (AntiPHPractice #26).  If you use the fetch_object() methods instead, the punctuation suddenly becomes much easier since you avoid a lot of fiddly quotes and brackets.  If you couple that with HEREDOC notation, you'll find that the resulting HTML is easier to build and modify (for things like adding classes to the HTML elements).  You can read the HTML directly without having to interpret the quoted literal strings that are artifacts of PHP.  This is untested code, but it shows the general design that a professional would use to simplify the notation.

while($record = $myData->fetch_object())
    $form = <<<ENDFORM
<form action=admin.php method=post>
<td><input type="hidden" name="ID"    value="$record->ID" /> </td>
<td><input type="text"   name="_dato" value="$record->dato" class="d" /> </td>
<td><input type="text"   name="_c2"   value="$record->c1"   class="c" /> </td>
<td><input type="text"   name="_c3"   value="$record->c2"   class="c" /> </td>
<td><input type="text"   name="_c4"   value="$record->c3"   class="c" /> </td>
<td><input type="text"   name="_c5"   value="$record->c5"   class="c" /> </td>
<td><input type="text"   name="_c6"   value="$record->c6"   class="c" /> </td>
<td><input type="submit" name="opdater_dato" value="Opdater" /> </td>

    echo $form;

Open in new window

Note that the attributes of the input controls are correctly quoted and the size= attribute has been removed.  You want to do things like that with CSS.  You want to use valid HTML at all times.  It takes a little bit more forethought to do that, but the results will be easier to use, maintain and deploy across the emerging multitude of varying platforms.
Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

LVL 53

Assisted Solution

COBOLdinosaur earned 250 total points
ID: 39686986
While I would generally not recommend using a table to layout a form instead of doing it correctly with fieldsets; the basic code using HEREDOC is clean and simple, and you can further simplify without  having to put a class on every input.

By giving the form a class and then targeting the children:

<form class="theform" action="admin.php" method="post">

Then in the stylesheet:

.theform input {?*put your default input styles here*/}
If you need to target a specific input the you can use the name atribute:
.theform input[name=_dato] {/*specific style for the named input}

Now you don't need to do anything with the PHP generation except put the id on the form, and all changes and experimentation is simplified by just quick changes to the stylesheet.  That give you complete separation oof content and presentation, which is the reason for using CSS in the first place.  Plus the same stylesheet can beused throuout the site to give all forms a common presentation without having a lot of coding to do.


Author Closing Comment

by:Mike Kristensen
ID: 39691127
Thx alot. . . I still use table, but the HEREDOC is very perfect.

Nice and clean, and CSS for styling. I love it.

COBOL i will still use class, but only because it fits the project vs. targeting children.
LVL 53

Expert Comment

ID: 39691140
That is the beauty of CSS there is almost always more than one way to do things that are equally stable and standards compliant.  


Featured Post

ScreenConnect 6.0 Free Trial

Discover new time-saving features in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
The viewer will learn how to count occurrences of each item in an array.
The viewer will receive an overview of the basics of CSS showing inline styles. In the head tags set up your style tags: (CODE) Reference the nav tag and set your properties.: (CODE) Set the reference for the UL element and styles for it to ensu…

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question