Table formatting in PHP - php

I am trying to make a table in PHP.
All things are fine but all the rows are printed in same line.
How to print new line after each row in table? But in html there is no need to write extra code for new line why it is showing not a new line with PHP?
Here is that part of code:
<div class="contest-table" id="contest-table">
<table class="contest-details" id="contest-details">
<tr>
<th>CODE</th>
<th>NAME</th>
<th>START</th>
<th>END</th>
</tr>
<?php
//Show contest detials -> Contest Code|Contest Name |Start Date | End Date
$con=mysqli_connect("localhost","root","chandan","judge");
$result=mysqli_query($con,"SELECT * FROM judge_contest ");
echo "<tr>";
while($row = mysqli_fetch_array($result))
{
$contest_code=$row['contest_code'];
$contest_name=$row['contest_name'];
$contest_start_date=$row['start_date'];
$contest_end_date=$row['end_date'];
echo "<td>";
echo " $contest_code ";
echo "</td>";
echo "<td>";
echo " $contest_name ";
echo "</td>";
echo "<td>";
echo $contest_start_date;
echo "</td>";
echo "<td>";
echo $contest_end_date;
echo "</td>";
}
echo "</tr>";
?>
</table>
</div>

The problem is obvious, because you have put the statement echo "<tr>" and echo "</tr>" outside the while loop. You should put these two statement into the while loop.

echo '<tr>'
and
echo '</tr>'
should be inside the wile loop

Related

Html Table not echoing

i am trying this from 5hrs don't know i am going wrong.My table echos results but not echo total for the same.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
}
}
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?></table>
Remove the } } after the closing tag of the tr.
if you can't close the whole PHP block then you probably did something wrong in your syntax.
I don't know if you have more code or not, if you do - post it so we will get more accurate view of your code.
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr>
<?php
(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
(I get these entreis correct )
(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
}
?>
</table>
You can remove the wrong bracket, OR check that the two bracket are opened when fetching data
<table width="102%" border="0">
<tr>
<th><b>Amount</b></th>
<th ><strong>County</strong></th>
</tr><?php
//(this fetches data ...$row = mysqli_fetch_array($res) or die(mysqli_error($db)))
echo "<tr>";
echo "<td>" . $price . "</td>";
echo "<td>" . $rui['county'] . "</td>";
echo "</tr>";
//(I get these entreis correct )
//(Below table is not echoed)
echo "<tr>";
echo "<td> .<b>Total </b>. </td>";
echo "<td>" .$x. "</td>";
echo "<td></td>";
echo "</tr>";
?>
</table>

LEFT JOIN on PHP

I have tried the same code below on my c# app and it works fine, but when i tried the same code on php with the tweak of instead of column number on c# i replaced it with column name but the result is different.
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
Result should be:
<table border=1>
<tr>
<th>Symbol</th>
<th>Net Volume A</th>
<th>Net Volume B</th>
<th>Last Update</th>
</th>
</tr>
<tr>
<td>BITCOIN</td>
<td>2.5</td>
<td>3.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>LITECOIN</td>
<td>1.5</td>
<td>5.5</td>
<td>2018.02.05 10:44</td>
</tr>
<tr>
<td>HASHCOIN</td>
<td>0.5</td>
<td>0.5</td>
<td>2018.02.05 10:44</td>
</tr>
</table>
but thats not the case.
The result shows both net volume of fxexposure.netvolume.
I hope you can help me with this.
Thanks...
just a suggestion you have two time netvolumn so you should use alias
sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume`,
`fxexposure`.`netvolume` as netvolume2,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume2"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
If you assign an alias to the returned colums you can address them correctly in the php
$sql ="SELECT
`adexposure`.`symbol`,
`adexposure`.`netvolume` as `netvolume_A`, /* ALIAS */
`fxexposure`.`netvolume` as `netvolume_B`,
`adexposure`.`lastupdate`
FROM `adexposure`
LEFT JOIN `fxexposure` ON `adexposure`.`symbol` = `fxexposure`.`symbol`";
$result = $conn->query($sql);
if($result->num_rows>0)
{
echo "<table>";
echo "<tr>";
echo "<th>Symbol</th>";
echo "<th>Net Volume A</th>";
echo "<th>Net Volume B</th>";
echo "<th>Last Update</th>";
echo "</tr>";
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>" .$row["symbol"]."</td>";
echo "<td>" .$row["netvolume_A"]."</td>";<!-- /* ALIAS */ -->
echo "<td>" .$row["netvolume_B"]."</td>";
echo "<td>" .$row["lastupdate"]."</td>";
echo "</tr>";
}
echo "</table>";
}
else
{
echo "0 results";
}
Read these two lines carefully:
echo "<td>" .$row["netvolume"]."</td>";
echo "<td>" .$row["netvolume"]."</td>";
You are using the same name to mean two different things, so there is no way for PHP to give a different result on those lines.
The solution is to give the values unique column aliases in your SQL:
`adexposure`.`netvolume` as netvolume_as,
`fxexposure`.`netvolume` as netvolume_fx,
And then those are the names in your PHP:
echo "<td>" .$row["netvolume_as"]."</td>";
echo "<td>" .$row["netvolume_fx"]."</td>";

How can I display each record individually into a while loop?

I use this while loop to display events from facebook. I want to add an href link in each record that will open a new tab displaying only this event. I have created links for each record but I can't understand how I can make the link to lead to that event.
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<table class='table table-hover table-responsive table-bordered'>";
echo "<tr>";
echo "<td rowspan='6' style='width:20em;'>";
echo "<img src=". $row["COVER_PHOTO"]." width='200px' />";
echo "</td>";
echo "</tr>";
echo "<tr>";
echo "<td style='width:15em;'>What:</td>";
echo "<td><b>". $row["NAME"]."</b></td>";
echo "</tr>";
//owner
echo "<tr>";
echo "<td>Who:</td>";
echo "<td>". $row["OWNER"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>What kind:</td>";
echo "<td>". $row["PAGE_CAT"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>When:</td>";
echo "<td>". $row["START_DATE"]." at ". $row["START_TIME"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>Where:</td>";
echo "<td>". $row["PLACE"]."</td>";
echo "</tr>";
echo "<tr>";
echo "<td>";
echo "<a href='view.php?more=". $row["EVENT_ID"]."' target='_blank' <?php echo >Λεπτομέρειες</a>";
echo "<tr>";
echo "<td>Description:</td>";
echo "<td>". $row["DESCRIPTION"]."</td>";
echo "</tr>";
Your code for this page is just fine. Now to display each individual event in the view.php file, you will need to access the data (in your case the event ID, stored in more variable) sent via GET method in the view.php page, as thus:
view.php
<?php
if(isset($_GET['more']))
{
$more = $_GET['more'];
}
?>
The value of the more variable sent via the URL will now be saved in the $more PHP variable. For example, if the page called is view.php?view=22, the $more variable will have the value of 22.
You can now use this $more variable - which stores the event ID - to fetch whatever other detail you require.
echo "<a href='view.php?more=". $row["EVENT_ID"]."' target='_blank'> Λεπτομέρειες</a>";

echo a table horizontally php

I have been trying to work this out for hours and I’m not sure how to achieve the correct result, hopefully someone will be able to help me.
I have the below code that will echo the result is a table vertically, but I want the cells next to each other horizontally, how can I achieve this?
<?php
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo '<div style="width:100%;">';
while($row = mysql_fetch_array($boxlink))
echo "<tr>";
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>
Brill that worked thanks a lot, why is it the small things can cause such a problem!
Move your tr tags outside of the loop. Each time a tr tag is seen it makes another row.
try this:
<?php
echo '<div style="width:100%;">';
echo "<table border='1' cellpadding='1' width='100%' bordercolor='000099'border='solid'>
";
echo "<tr>";
while($row = mysql_fetch_array($boxlink))
{
echo "<td>" . $row['page_page_title'] . "</td>";
}
echo "</tr>";
echo "</table>";
echo '</div>';
?>

PHP loop - creating a table using a loop and giving it headers

I am a few stages further in learning PHP but I have come to another annoying pit stop. I have a really simple bit of code that retrieves book items from my database. I am displaying them in an html table however because it is a loop, if I use the th tags for table header I get a header above every single data item!
Here is my code extract: (as you can see I have put my th tags as comments as that doesn't work)
<table border="0">
<br />
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
//echo "<tr>";
//echo "<th>";
//echo "Book Title";
//echo "</th>";
//echo "<th>";
//echo "Book Author";
//echo "</th>";
//echo "<th>";
//echo "Book Publisher";
//echo "</th>";
//echo "<th>";
//echo "Book ISBN";
//echo "</th>";
//echo "</tr>";
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
Move those echos out of your loop. Also, you shouldn't have a <br /> directly inside of a <table> tag.
Move your table header code outside of the loop.
IDIOT! Sorry guys....
Needed to put the th tags outside of the loop.... simple I know but easy to miss when your learning!
[=
Simply take the header outside the loop, so echo before you begin your loop but after the opening<table>
You have to move the headers above the loop:
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>"
echo "<td>";
echo "<a href='addtolist.php? bookname=".$bookname."&bookauthor=".$bookauthor."&bookpub=".$bookpub."&bookisbn=".$bookisbn."'>[+]</a>";
echo "</td>";
echo "<td>";
echo $bookname;
echo "</td>";
echo "<td>";
echo $bookauthor;
echo "</td>";
echo "<td>";
echo $bookpub;
echo "</td>";
echo "<td>";
echo $bookisbn;
echo "</td>";
echo "<td>";
echo "<a href='deletecd.php?bookname=".$bookname."'>Delete</a>";
echo "</td>";
echo "</tr>";
$count = $count + 1;
}
?>
<table border="0">
<tr>
<th>Book Title</th>
<th>Book Author</th>
<th>Book Publisher</th>
<th>Book ISBN</th>
</tr>
<?php
$count = 0;
while ($count < $numrow)
{
$row = mysql_fetch_array($results);
extract($row);
echo "<tr>";
echo "<td>";
...
What is static, stays static.
Wjat is dynamic, becomes PHP

Categories