Solved

Class some php....

Posted on 2013-11-30
6
235 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

CSS is a visual language used to classify objects and define rules about how they should be displayed. CSS skills aren’t restricted to developers anymore, there is a big benefit to having a basic understanding of the language, regardless of your occ…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this tutorial viewers will learn how to style a decorative dropcap for the first letter in a paragraph using CSS. In CSS, create a new paragraph class by typing "p.fancy": Then, to style only the first letter of the first sentence, include the ps…
In this tutorial viewers will learn how to embed custom externally-hosted Google Fonts using the Google Font API in CSS Go to the Google Fonts website at google.com/fonts: Browse or search based on font properties or name to find a suitable font for…

730 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