• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 252
  • Last Modified:

Class some php....

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.
0
Mike Kristensen
Asked:
Mike Kristensen
2 Solutions
 
InsoftserviceCommented:
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
0
 
Tom BeckCommented:
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 }
0
 
Ray PaseurCommented:
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>
<tr>
<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>
</tr>
</form>
ENDFORM;

    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.
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
COBOLdinosaurCommented:
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.



Cd&
0
 
Mike KristensenIT administratorAuthor Commented:
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.
0
 
COBOLdinosaurCommented:
That is the beauty of CSS there is almost always more than one way to do things that are equally stable and standards compliant.  

Cd&
0

Featured Post

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now