Solved

Class some php....

Posted on 2013-11-30
6
226 Views
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.
0
Comment
Question by:Mike Kristensen
6 Comments
 
LVL 15

Expert Comment

by:Insoftservice
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
0
 
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 }
0
 
LVL 108

Accepted Solution

by:
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>
<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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 53

Assisted Solution

by:COBOLdinosaur
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.



Cd&
0
 

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.
0
 
LVL 53

Expert Comment

by:COBOLdinosaur
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.  

Cd&
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Styling your websites can become very complex. Here I'll show how SASS can help you better organize, maintain and reuse your CSS code.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
In this tutorial viewers will learn how to customize the background color and font color of highlighted text using the ::selection element in CSS Begin by defining the selected text as an element in CSS by typing "::selection": Style the ::selection…
The viewer will learn how to dynamically set the form action using jQuery.

706 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now