Solved

Class some php....

Posted on 2013-11-30
6
241 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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 110

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
Don't Cry: How Liquid Web is Ensuring Security

WannaCry is just the start. Read how Liquid Web is protecting itself and its customers against new threats.

 
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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

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

These days socially coordinated efforts have turned into a critical requirement for enterprises.
This article discusses four methods for overlaying images in a container on a web page
HTML5 has deprecated a few of the older ways of showing media as well as offering up a new way to create games and animations. Audio, video, and canvas are just a few of the adjustments made between XHTML and HTML5. As we learned in our last micr…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

622 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