Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 797251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:46:26+00:00 2026-05-14T22:46:26+00:00

I have a table that I cannot get to size correctly. The table populates

  • 0

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>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T22:46:26+00:00Added an answer on May 14, 2026 at 10:46 pm

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

    Also you can 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 your edited question it’s better for you 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 your CSS issue, open up your css file, and write “!important” beside the class which is being used by “<td>“. This will prefer your CSS class to apply than inline style. Also provide the way you are applying the word-wrap…

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.