Problem with SQL output in <table>

Hello,

I’m pretty new to PHP and I was reading already a lot.
I really like it, but actually im a bit stuck.
I think I just need a little (or big) push in the right direction.

I would like, that regarding to my SQL Database, I get output in a table on a PHP Page.
The table is supposed to get filled by two conditions.
If both are true there should be an “X” in the table cell, if not then there should be anyway a cell, but empty.

I was able to manage it with it with giving each cell a unique variable, that would be filled if the conditions for a cell would be true.
That was a lot of code and more feeling, like I would try to make a bad copy of Excel.

Actually I’m trying to figure out some better way.
I was already able to create a table with two separate “for” loops, but then im just able to check for one condition. If I try to put the loops together I get trouble with structure of my table.

So im stuck right here. The code down here is not working, because $startdate is changed already in the first loop.

Is this even the right way? I feel like I’m just waisting my time.
Thanks in advance for any help & critic.

I am having a really hard time pasting a picture of your code into my editor. How about posting the actual code.

Hello,
thank you for your response.

Here is the code:

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

$o_startdate = '26.11.2019';
$startdate = date('Y-m-d', strtotime($o_startdate));

echo '<tr>';
echo '<td> TG Member / Data';
for ($tg = 1; $tg <=10; $tg++)
{
echo '<td>'.$startdate;
$startdate = date('Y-m-d', strtotime($startdate . " + 1 day")); 
}

for ($tg = 1; $tg <=10; $tg++)
{
echo '<tr>';
echo '<td>TG';
echo sprintf('%03d', $tg) . "";


if ($row['tg_s']==$startdate && $row['tg_id']==$tg)
{
echo '<td>X';
}
else`Preformatted text`
{
echo '<td>';
}
}
echo '</table>';

I don’t see what result you expect. Also your example is not reproduceable, as any data in $row is missing.

Hello,
thank you for your response.
That’s not the full code.
$row is filled by data from a SQL Database.
That’s not the issue. It’s working fine.

My point was, that I try to create a table, that has on the head row dates and on the left side number of members. If a member nr. is connected to a date (by existing entry in my database) it should mark “X” in the cell, if not then just an empty cell.

In the end I would like to have a table with size 10x10 where I can see which member nr is related to which date.

I was feeling like I’m on the wrong way, because I was stuck for few days.

You can reset your $startdate anytime by making a new assignment.

If you first define what the (valid) html markup will be for the table heading row and the table body row(s), it will be much easier to write code that produces that markup. Note: you will have one table body row for each tg_id value.

Table heading row -
<tr><th>TG Member / Data</th> <th>2019-11-26</th> <---- loop for 9 more dates here... </tr>
Table body row(s) -
<tr><th>TGxxx</th> <td>X</td> <---- loop for 9 more dates here... </tr>

Once you get your code to the point where it is producing the correct html table markup, you can worry about producing the correct content in the table body cells.

1 Like

Hard to say, but I think the OP wants a Pivot Table.

Sounds like a cross reference chart. I think if you follow phdr’s advice on getting the structure down, filling in the data will be easier.

Thank you all for your replies! Actually im working much, but I will report my results as soon I get a chance to try your solutions. Thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service