Reputation: 45
I am creating a table to store information based on virtual companies and their stock values. I am aiming to make the table look correct while also being able to redirect the user to a separate page based on what stock they would like to buy.
I am using the following code at the moment:
PHP:
<table width = "50%" align = "center" <tr><th>Company</th><th>Price</th>
<?php
while ($row = mysqli_fetch_assoc($result))
{
$companyName = $row['companyName'];
$price = number_format($row['price'], 2);
echo "<tr><td align = 'left'>" . $companyName . "</td><td align = 'right'>" . $price . "
<form method = 'get' action = 'testing.php'><button type = 'submit' name = 'varname' value = '" . $companyName . "'>Hey</button></form>
</td></tr>";
(I currently have buttons called 'Hey' but these should essentially say 'Buy').
This will all redirect to a testing page at the moment where, based on the button that the user has pressed, a certain company name will appear. The code below works in doing that but I have one slight problem at that is with the user interface. This is what the main table looks like on the page:
Compared to what I would like it to look like when I remove the tags within the table:
As you can see, there are massive white spaces in the first picture compared to the second picture and I am aware that this is based on the tags. However, without the form tags, my program does not do what it is supposed to do.
I've researched various other posts which say that I should put the tags outside of the table. However, when trying this, I've noticed that this cannot work as the table is being displayed in a while loop.
My question is this: is there a way to fix this (i.e. by placing the tags elsewhere) or do I need to approach this problem of displaying the company the user chooses in an entirely different matter.
Upvotes: 0
Views: 86
Reputation: 3149
<form>
html tag does create a lot of blank space too. And you are creating this tag multiple times by leaving it inside your loop. So:
1) exclude <form method = 'get' action = 'testing.php'>
(and the respective <\form>
) from your loop. This will correct your code logic and probably resolve your design questions.
If you want less space within your table you could aggregate cellspacing=0 cellpadding=0
as additional properties inside your <table>
tag --> observe your openning table tag isn't closing before th and td!
Upvotes: 1
Reputation: 15847
First major issue is the first line of your PHP:
<table width = "50%" align = "center" <tr><th>Company</th><th>Price</th>
it should be and make your percentages fit your specific needs.
<table width = "50%" align = "center">
<tr>
<th width="60%">Company</th>
<th width="40%">Price</th>
</tr>
Buttons by default are inline-block so they should align next to text unless the cell width isn't wide enough.
Upvotes: 2