tags:

views:

228

answers:

4

I have a table that I cannot get to size correctly. The table populates with information from a database via a loop. Sometimes if the data is too long the table extends past where it should. When the data is that long I want the data to wrap in the cells so the table stays where it should. I have tried the normal table data but it isn't working. Any ideas?

<?php
    echo "<table>
        <tr>
            <th>id</th>
            <th>700-number</th>
            <th>First name</th>
            <th>Last name</th>
            <th>Email</th>
            <th>Response</th>
            <th>Created On</th>
        </tr>";

    $num = mysql_num_rows($result);

    for ($i = 0; $i < $num; $i++)
    {
        $row = mysql_fetch_array($result);

        $id = $row['id'];
        $school_id = $row['school_id'];
        $fname = $row['first_name'];
        $lname = $row['last_name'];
        $email = $row['email'];
        $attending = ($row['attending'] == 0) ? 'No' : 'Yes';
        $date = $row['created_on'];

        $class = (($i % 2) == 0) ? "td2" : "td1";

        echo "<tr>";
            echo "<td class=" . $class . ">$id</td>";
            echo "<td class=" . $class . ">$school_id</td>";
            echo "<td class=" . $class . ">$fname</td>";
            echo "<td class=" . $class . ">$lname</td>";
            echo "<td class=" . $class . ">$email</td>";
            echo "<td class=" . $class . ">$attending</td>";
            echo "<td class=" . $class . ">$date</td>";
        echo "</tr>";
    }
?>
</table>

EDIT

This table is displayed in a container that is fixed to 800px wide, so setting a percentage for the table will not work. I want to set it to a specific pixel size, like 600px. I would also rather not edit the CSS, I want to fix the size by modifying the code I have posted.

EDIT

There has to be someone who knows the answer to this. I have tried all the suggestions so far, in HTML and CSS but to no avail. I know it has to be some small problem with my code that I am just not seeing (i.e. an open tag, a missing semi-colon, a single quote where it should be a double quote, ect.). I have been using firebug trying to track down the cause of this problem and I have found that when I remove all data from the CSS classes .td1 and .td2 that the width I have specified sticks just fine. Is there some reason the width would get messed up by a CSS class?

EDIT

Finally got this working right with wordwrap and sizing the tags manually. Here is the correct code:

<table>
    <tr>
        <th width="16px">ID</th>
        <th width="81px">700<br />Number</th>
        <th width="90px">First<br />Name</th>
        <th width="90px">Last<br />Name</th>
        <th width="181px">E-Mail</th>
        <th width="74px">Attending</th>
        <th width="82px">Created<br />On</th>
    </tr>
<?php
$num = mysql_num_rows($result);

for ($i = 0; $i < $num; $i++)
{
    $row = mysql_fetch_array($result);

    $id = $row['id'];
    $school_id = $row['school_id'];
    $fname = $row['first_name'];
    $lname = $row['last_name'];
    $email = $row['email'];
    $attending = ($row['attending'] == 0) ? 'No' : 'Yes';
    $date = $row['created_on'];

    $wrap_id = wordwrap($id, 4, "\n", TRUE);
    $wrap_school_id = wordwrap($school_id, 9, "\n", TRUE);
    $wrap_fname = wordwrap($fname, 10,"\n", TRUE);
    $wrap_lname = wordwrap($lname, 10, "\n", TRUE);
    $wrap_email = wordwrap($email, 20, "\n", TRUE);
    $wrap_attending = wordwrap($attending, 3, "\n", TRUE);
    $wrap_date = wordwrap($date, 10, "\n", TRUE);

    $class = (($i % 2) == 0) ? "td2" : "td1";

    echo "<tr>";
        echo "<td class=" . $class . ">$wrap_id</td>";
        echo "<td class=" . $class . ">$wrap_school_id</td>";
        echo "<td class=" . $class . ">$wrap_fname</td>";
        echo "<td class=" . $class . ">$wrap_lname</td>";
        echo "<td class=" . $class . ">$wrap_email</td>";
        echo "<td class=" . $class . ">$wrap_attending</td>";
        echo "<td class=" . $class . ">$wrap_date</td>";
    echo "</tr>";
}

?>

</table>
A: 
<table width="90%">

Have you tried this?

Gabriel
A: 

Try setting the style to

word-wrap: break-word;

(I assume you have data with no spaces which is causing the table to expand.) You may also have to explicitly set the width of the table.

takteek
Tired it, doesn't work.
typoknig
+1  A: 

Fix the Height and width of the table so as to get the wrapped up data into the table...

Also u cn use wordwrap function Wraps a string to a given number of characters using a string break character.

syntax: string wordwrap ( string $str [, int $width = 75 [, string $break = "\n" [, bool $cut = false ]]] )

str

    The input string.
width

    The column width.
break

    The line is broken using the optional break parameter.
cut

    If the cut is set to TRUE, the string is always wrapped at or before the specified width. So if you have a word that is larger than the given width, it is broken apart. (See second example).


Returns the given string wrapped at the specified column. 

UPDATED REPLY

As per ur edited question its better for u to use word wrap as I have explained above..

For Example:

<?php
$text = "Typoking wants to know about "Php html table is too wide".";
$newtext = wordwrap($text, 20, "<br />\n");

echo $newtext;
?>

The above example will output:

Typoking wants to kn<br />
ow about "Php html t<br />
able is too wide".

for more information go to : PHP.net

EDITED

As per ur css issue,

open up ur css file, and write "!important" beside the class which is being used by "<td>" This will prefer ur css class to apply than inline style.. try it.. also provide the way u r applying the wordwrap...

OM The Eternity
I get what you are saying, and I have tried wordwrap, but it isn't working with the code I have for some reason.
typoknig
Why are you **intentionally** misspelling words? The whole answer looks awful unprofessional that way.
BalusC
@BalusC Excuse me, Why would I misspell the words? And where I have done so? And if u r talking about short forms, like I just did it in this statement, its not misspelling, None of the user is being paid for being Professional here, and we are not here to show our professionalism, we are here to spread the word of knowledge. If the seeker understands the answer and gains from it, thats the main aim.... I am here to learn and make other learn.... Having big reputation score doesn't mean u can point to anyone u want for rubsh reasons
OM The Eternity
I figured it out, I used wordwrap as you suggested. It wasn't working at first because I was using it wrong, but I finally got it... see edited question.
typoknig
"Rubsh" is spelled "Rubbish" :) You help me with my code I'll help you with your spelling. If I would have read your answer better the first time I could have had this fixed yesterday. Thanks again.
typoknig
N e time, :) pls do, people find it proud to point on unnecessary points which could be understood easily if they have a bit common sense like u just applied on "Rubsh" :) ,People have gone command prompt, until and unless they get proper command they cannot understand.. :)
OM The Eternity
A: 

Use like this, you will get correct answer. Onething you can adjust no of character in wordwrap according the width.

<?php
    echo "<table width='800'>
        <tr>
            <th width='50'>id</th>
            <th width='100'>700-number</th>
            <th width='150'>First name</th>
            <th width='150'>Last name</th>
            <th width='100'>Email</th>
            <th width='150'>Response</th>
            <th width='100'>Created On</th>
        </tr>";

    $num = mysql_num_rows($result);

    for ($i = 0; $i < $num; $i++)
    {
        $row = mysql_fetch_array($result);

        $id = $row['id'];
        $school_id = $row['school_id'];
        $fname = $row['first_name'];
        $lname = $row['last_name'];
        $email = $row['email'];
        $attending = ($row['attending'] == 0) ? 'No' : 'Yes';
        $date = $row['created_on'];

        $class = (($i % 2) == 0) ? "td2" : "td1";

        echo "<tr>";
            echo "<td class=" . $class . ">$id</td>";
            echo "<td class=" . $class . ">wordwrap($school_id, 8, '\n', 1); </td>";
            echo "<td class=" . $class . ">wordwrap($fname, 15, '\n', 1);</td>";
            echo "<td class=" . $class . ">wordwrap($lname, 20, '\n', 1);</td>";
            echo "<td class=" . $class . ">wordwrap($email, 20, '\n', 1);</td>";
            echo "<td class=" . $class . ">wordwrap($attending, 30, '\n', 1);</td>";
            echo "<td class=" . $class . ">wordwrap($date, 10, '\n', 1);</td>";
        echo "</tr>";
    }
?>
</table>
Karthik
I said in my original question that I already tried the normal way of doing it and it didn't work for some reason.
typoknig
what is the problem are you getting now? according the width you can fix the character in wordwrap, that will solve the problem.
Karthik
If I use wordwrap as you have it in your answer then "wordwrap" will actually appear on the screen. I am unsure how to correctly use wordwrap in the code I have.
typoknig
Your answer was almost right, but your application of wordwrap was incorrect. Still, it got me thinking. Thanks for your help.
typoknig
welcome. If you got solution it is good for us.
Karthik