Problem Printing Table Info

Codes here !

Moderators: egami, macek, gesf

Post Reply
huntsman69
New php-forum User
New php-forum User
Posts: 5
Joined: Sat Jul 01, 2017 10:15 am

Sun Feb 18, 2018 8:41 am

Hi,

I want to display a table of interest groups held in a mysql database in a html table. My loop prints out a new row, which is fine. The problem I have is that I want my table to print 4 columns i.e. the <td> tag four times. I am not sure of the code to repeat the <td> tag four times. I think I need to use a for loop but I am not sure of the syntax.

Any ideas would be gratefully accepted. Here is my code for the moment.

<table class="data-table">

<?php do { ?>
<tr>
<td><a href="#"><?php echo $rs_display_groups['GroupName']; ?></a></td>
</tr>
<?php } while ($rs_display_groups = mysqli_fetch_array($display_groups_result)); ?>

</table>

Thanks.

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Sun Feb 18, 2018 9:30 am

Code: Select all

<table class="data-table">
<?php
	while( $rs_display_groups = mysqli_fetch_array( $display_groups_results ) )
	{
		echo "<tr>";
		echo "<td>".$rs_display_groups['first-colum-name']."</td>";
		echo "<td>".$rs_display_groups['second-column-name']."</td>";
		echo "<td>".$rs_display_groups['third-column-name']."</td>";
		echo "<td>".$rs_display_groups['fourth-column-name']."</td>";
		echo "<tr>";
	}
?>
</table> 

Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

huntsman69
New php-forum User
New php-forum User
Posts: 5
Joined: Sat Jul 01, 2017 10:15 am

Sun Feb 18, 2018 9:38 am

Hi Zaim,

Thanks for your reply.

I can see what you're trying to explain, but each column is not a distinct name. Just want to repeat the name of the Interest Group across four columns and then create a new row. I am sure this would be a case of using the for Loop. I'm not sure how to write the syntax.

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 656
Joined: Mon Feb 22, 2016 5:52 pm

Sun Feb 18, 2018 11:01 am

There's no need to use a loop unless you really want to. What zaim has written for you is correct, all you have to do is change the 'first-colum-name', 'second-colum-name' etc. to 'Group_Name': it doesn't make much sense to repeat the same thing four times, but, that is what you have and I presume that there is good reason for it, if you need to process each cell below this, then a loop will make life more difficult.

Using a do - while loop appears to be wrong in this instance:

a) the loop will execute without checking that there is any data to process
b) you haven't (in the code given) read / prepared any data

huntsman69
New php-forum User
New php-forum User
Posts: 5
Joined: Sat Jul 01, 2017 10:15 am

Tue Feb 20, 2018 4:46 am

Hi,

That does seem to print four columns across but it is just repeating the same Group Name four times across each row. There is no unique name for each column in my database. I have a list of about 30 unique Group Names and these need to be repeated across four columns.

Thanks

User avatar
zaim
New php-forum User
New php-forum User
Posts: 29
Joined: Sat Jan 06, 2018 9:05 am
Location: Kedah, Malaysia.

Tue Feb 20, 2018 6:30 am

Hello,

sorry that I caught your point unclear. Seems like the issue persisted, I will leave hyper to aid you. I am sorry that I couldn't assist you any longer. Maybe because I am having a hard time learning English, which is apparently not my main language.

My apology.

Just to mention, did you inserted all the columns with 'Group Name'? Some more information regarding your table would be helpful or you could visualize your intended result by making a more relevant reference such as an image.
Last edited by zaim on Tue Feb 20, 2018 3:24 pm, edited 1 time in total.
Sorry if my answers aren't top notch but I will try my best to help anyone in need. :oops:

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 656
Joined: Mon Feb 22, 2016 5:52 pm

Tue Feb 20, 2018 12:46 pm

Explain clearly what you hope to achieve:
Just want to repeat the name of the Interest Group across four columns and then create a new row.
That does seem to print four columns across but it is just repeating the same Group Name four times across each row.
An example would help, along with some database field-names

Code: Select all

Group 1 | Group 1 | Group 1 | Group 1
data 1      data 2       data 3      data 4

or 

Group 1 | Group 2 | Group 3 | Group 4
data 1      data 2       data 3      data 4

huntsman69
New php-forum User
New php-forum User
Posts: 5
Joined: Sat Jul 01, 2017 10:15 am

Wed Feb 21, 2018 12:39 am

Hyper,

Thanks for your reply and looking into this. Yes it is like 2nd example.

GroupName 1 | GroupName 2 | GroupName 3 | GroupName 4
data 1 data 2 data 3 data 4

User avatar
hyper
php-forum Fan User
php-forum Fan User
Posts: 656
Joined: Mon Feb 22, 2016 5:52 pm

Fri Feb 23, 2018 10:29 am

Yes it is like 2nd example.
Then what zaim offered is the correct solution to your problem, now you need to look at how to put it into your code.

Post Reply