0

I am trying to create a table that I can customize completely with php. I will admit, i have forgotten some of the materials due to lack of practice and "accidentally" deleting my original files that "should" answer my question.

Well anyways, how do you create a table that (by only using php) has an alphabetical header, each individual column gets manipulated by a loop, and all gets displayed in a csv.

So far, i have created this, pretty simple table:

<?php 
$rows = 100 ; // define number of rows
$cols = 27;// define number of columns

echo "<table border='1'>"; 

for($tr=1;$tr<=$rows;$tr++){ 

    echo "<tr>"; 
        for($td=1;$td<=$cols;$td++){ 
               echo "<td>".$tr." &nbsp; ".$td."</td>"; 
        } 
    echo "</tr>"; 
} 

echo "</table>"; 

?>

It is a pretty simple table so far...

For the header, i am trying to achieve this:

#   a   b   c   d   ------>   x   y   z
1   
2
3
4
5
6
7
8
9
10

Each column has a unique bit of content, such as: a random letter, random number, etc.

Sorry guys, i am not asking you to create this for me, i am just looking for some advice/technique/resource/snippets to help me solve the problem.

4
Contributors
16
Replies
64
Views
3 Years
Discussion Span
Last Post by <M/>
Featured Replies
  • I am a bit confused by what you're trying to do, even though you explained it here. I guess I don't get the purpose behind the table you are trying to generate. Here is an idea for the headers though. <?php $rows = 100; // define number of rows $cols … Read More

  • 2
    diafol 3,669   3 Years Ago

    SImilar to pS: $table = '<table><thead><tr><th>#</th>'; $headings = range('a','z'); $rows = range(1,10); foreach($headings as $th) { $table .= "<th>$th</th>"; } $table .= "</tr></thead><tbody>"; foreach($rows as $td) { $table .= "<tr><td>$td</td>"; //if you want a blank table... // $table .= str_repeat("<td>&nbsp;</td>",count($headings)); //otherwise for($x=0; $x < count($headings); $x++) { $table .= "<td>" … Read More

  • 3
    diafol 3,669   3 Years Ago

    Out of curiosity what the hell *are* you trying to do? Read More

  • Are you just talking about zebra stripping the table rows? You can add a alternating class to each row and then use CSS to apply the color of background you want. $table = '<table><thead><tr><th>#</th>'; $headings = range('a','z'); $rows = range(1,10); foreach($headings as $th) { $table .= "<th>$th</th>"; } $table .= … Read More

  • 2
    diafol 3,669   3 Years Ago

    $table = '<table><thead><tr><th>#</th>'; $headings = range('a','z'); $rows = range(1,10); foreach($headings as $th) { $table .= "<th>$th</th>"; } $table .= "</tr></thead><tbody>"; foreach($rows as $td) { $table .= "<tr><td>$td</td>"; //Get all content containing 5 and style odd/even for($x=0; $x < count($headings); $x++) { $datum = mt_rand(1,50); $strDatum = (string) $datum; if(strstr($datum,'5'))$strDatum .= … Read More

2

I am a bit confused by what you're trying to do, even though you explained it here. I guess I don't get the purpose behind the table you are trying to generate.

Here is an idea for the headers though.

<?php 
$rows = 100; // define number of rows
$cols = 25; // define number of columns
echo "<table border='1'>";

$letters = range('a', 'z');
echo "<tr>"; 
    for($th=0;$th<=$cols;$th++){
           echo "<th>{$letters[$th]}</th>";
    } 
echo "</tr>";

for($tr=1;$tr<=$rows;$tr++){
    echo "<tr>"; 
        for($td=0;$td<=$cols;$td++){ 
               echo "<td>".$tr." &nbsp; ".$td."</td>";
        } 
    echo "</tr>";
} 
echo "</table>"; 
?>
Votes + Comments
That makes good sense :D
2

SImilar to pS:

$table = '<table><thead><tr><th>#</th>';
$headings = range('a','z');
$rows = range(1,10);

foreach($headings as $th)
{
    $table .= "<th>$th</th>";
}
$table .= "</tr></thead><tbody>";
foreach($rows as $td)
{
    $table .= "<tr><td>$td</td>";
    //if you want a blank table...
   // $table .= str_repeat("<td>&nbsp;</td>",count($headings));
    //otherwise
    for($x=0; $x < count($headings); $x++)
    {
        $table .= "<td>" . mt_rand() . "</td>";   
    }

    $table .= "</tr>";
}
$table .= "</tbody></table>";
echo $table;

If you want to prettify the html, use \n and \t.

Edited by diafol

Votes + Comments
Oh, now i see!
0

oh okay, now i see what i was forgetting to do. thanks you guys!

A quick question of curiousity, how does one make each column unique from the other? In other words, how does one make column A have random numbers, column B has sorted numbers, C has random letters, C has sorted letters, etc.

0

Another question of curiosity, how do i set up my if statement so that if a number (for example) has the number 5 in it, put a asterisk next to it?

0

Out of curiosity what the hell are you trying to do?

hehe, i knew i would get this question at some point.

Well anyways, i am trying to enhance my skills with php and mysql so that i get good at working with databases and eventually create something great such as a property search (what i am aiming towards atm), ecommerce stores, and so on. Problem is that i forgot some of the tricks i learned about a year ago due to lack of practice and so on...

Eventually i will be considered as being "good" in the next 2 years.

Does that make sense, or at least what i am aiming at?

0

@diafol, i was trying to add something like this into the table you provided:

                    $table .= "<td>" . mt_rand(9,99) . "</td>"; 
                        if ($x % 2 !=0)
                            {
                            $x = $even;
                            }
                            else 
                            {
                            $x = $odd; 
                            }

Can you explain to me the proper way in highlighting even and odd numbers (such as make evens blue and odds red?)

Sorry if i am asking a lot of questions.... i will admit, i am a very curious person :D

Edited by <M/>

2

Are you just talking about zebra stripping the table rows? You can add a alternating class to each row and then use CSS to apply the color of background you want.

$table = '<table><thead><tr><th>#</th>';
$headings = range('a','z');
$rows = range(1,10);
foreach($headings as $th)
{
    $table .= "<th>$th</th>";
}
$table .= "</tr></thead><tbody>";
foreach($rows as $td)
{
    $rowCount = ($rowCount + 1) % 2;
    $table .= "<tr class='row$rowCount'><td>$td</td>";
    //if you want a blank table...
   // $table .= str_repeat("<td>&nbsp;</td>",count($headings));
    //otherwise
    for($x=0; $x < count($headings); $x++)
    {
        $table .= "<td>" . mt_rand() . "</td>";   
    }
    $table .= "</tr>";
}
$table .= "</tbody></table>";
echo $table;

Then you just set a CSS class like

.row0{background:red;}
.row1{background:blue;}
Votes + Comments
Not quite but i see what your up to :D
1

Another question of curiosity, how do i set up my if statement so that if a number (for example) has the number 5 in it, put a asterisk next to it?

I was typing this code up, but in the mean time it looks like this thread received more responses and the topic changed slightly... in any case, I'll post the example I came up with since you previously asked for the example..

    for($x=0; $x < 20; $x++)
        {
            $r=mt_rand(1,10);
            if ($r=="5")
            {
            $r="5*";
            } 
                $table .= "<td>$r</td>";   
        }

Sorry if its too late and your interests have moved on..

Edited by JorgeM

Votes + Comments
:D
0

Sorry if its too late and your interests have moved on..

Nope... same question, just multiple parts to the question ;D

@pixelsoul, ummm... not exactly zebra stripes. I was refering to the fact that if a number in the table is even, make it red and if a number is odd make it blue.

@JorgeM, i will play around with your snippet of code. I will update you on the progress in approximately 30 minutes (I have to take my dogs out for a walk...)

2
$table = '<table><thead><tr><th>#</th>';
$headings = range('a','z');
$rows = range(1,10);

foreach($headings as $th)
{
    $table .= "<th>$th</th>";
}
$table .= "</tr></thead><tbody>";
foreach($rows as $td)
{
    $table .= "<tr><td>$td</td>";

    //Get all content containing 5 and style odd/even
    for($x=0; $x < count($headings); $x++)
    {
        $datum = mt_rand(1,50);
        $strDatum = (string) $datum;
        if(strstr($datum,'5'))$strDatum .= '*';
        $class = ($datum % 2) ? 'odd' : 'even';
        $table .= "<td class='$class'>$strDatum</td>";    
    }

    $table .= "</tr>";
}
$table .= "</tbody></table>";

?>

<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Untitled Document</title>
<style>
    .odd{
        color:#F00; 
    }
    .even{
        color:#00F; 
    }
</style>
</head>

<body>

<?php echo $table;?>

</body>
</html>

Edited by diafol

Votes + Comments
PERFECT!
0

Thanks diafol, the snippet you provided works great!

Last "quick" question.

If one were to style each individual column, how would they do that if the columns are created with a loop (in my case)?

That should be my last question :D

Thank you guys :D

0

@diafol, i was playing around with the last snippet you provided and i was wondering if it was possible to change line 19 in a way where it will replace the last number (only if it is a 5) with a symbol like an asterisk?

Now that should conclude all my questions on this thread.

Thank you guys ;D

1

You could provide a class for each td or provide an nth child. The latter would be my preference:

tr td:nth-child(2) {  
    color: #ccc;
}

That gives column 2 grey text - but only the body cells (td) - not the header (th), which is what I assum you want.

If you have a few different tables and only want to style one of them add some specificity...

table.myspecialtable tr td:nth-child(2) {  
    color: #ccc;
}

replace the last number (only if it is a 5) with a symbol like an asterisk?

For that you could use a preg_replace:

$strDatum = preg_replace('/5$/', '*', $datum);

Note the '$' as the end of the pattern, denoting that the replacement should only happen if it is at the end of the string. So '50' will not be changed.

If you have further styling questions, perhaps it would be better to post to that forum. You'll get better advice there. I can't speak for others in this PHP forum, but I know my CSS is pretty rudimentary.

Edited by diafol

Votes + Comments
oh, that makes sense :D
0

If you have further styling questions, perhaps it would be better to post to that forum. You'll get better advice there.

I don't think i will be getting too deeply with styling just yet. Styling is the easy part :D That is why i am working on the functions first :D

I can't speak for others in this PHP forum, but I know my CSS is pretty rudimentary.

Mehhh.... your better than me at least (and i consider myself pretty good) :D

0

Thanks guys for helping me solve my problem!

I will be asking more questions on this topic in the future, so don't be surprised if you see a question similar to this :D

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.