Jump to content

[SOLVED] Echoing Table


cheechm

Recommended Posts

Hi,

For some reason with this script I get the titles (Event/Year/Team) after every row.

echo "<table class=\"events\" width=\"90%\">";
echo "<tr>";
echo "<th>Event</th>";
echo "<th>Year</th>";
echo "<th>Team</th>";
echo "</tr>";
echo "<tr><td>";
echo "$row[EventName]";
echo "</td>";
echo "<td>";
echo "$row[Year]";
echo "</td>";
echo "<td>";
printf($link2, $row['EventName'], $row['Year']);
echo "</td></tr>";
echo "</table>";

This is a search, so there won't always be data in the table.

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/
Share on other sites

Hi,

For some reason with this script I get the titles (Event/Year/Team) after every row.

echo "<table class=\"events\" width=\"90%\">";
echo "<tr>";
echo "<th>Event</th>";
echo "<th>Year</th>";
echo "<th>Team</th>";
echo "</tr>";
echo "<tr><td>";
echo "$row[EventName]";
echo "</td>";
echo "<td>";
echo "$row[Year]";
echo "</td>";
echo "<td>";
printf($link2, $row['EventName'], $row['Year']);
echo "</td></tr>";
echo "</table>";

This is a search, so there won't always be data in the table.

 

 

What the heck

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/#findComment-336641
Share on other sites

echo "<table class=\"events\" width=\"90%\">\n";
echo "<tr>\n";
echo "<th>Event</th>\n";
echo "<th>Year</th>\n";
echo "<th>Team</th>\n";
echo "</tr>\n\n";
echo "<tr>\n";
echo "<td>$row[EventName]</td>\n";
echo "<td>$row[Year]</td>\n";
echo "<td>" . printf($link2, $row['EventName'], $row['Year']) . "</td>\n";
echo "</tr>\n";
echo "</table>\n";

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/#findComment-336647
Share on other sites

Well, presumably all of the code you posted is in a loop similar to:

 

while($row=mysql_fetch_assoc($result)){
//your code here
}

 

You should only place the echoing of the rows of the table inside the while loop. This is, afterall, the piece of code you want repeated.

 

Overall, it would look something like:

 

<?php
$sql = "SELECT * FROM `yourtable`";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo '<table><tr><td>Column1</td><td>Column2</td></tr>';
if($num > 0){
while($row = mysql_fetch_assoc($result)){
echo '<tr><td>'.$row['field1'].'</td><td>'.$row['field2'].'</td></tr>';
}
}else{
echo '<tr><td colspan="2">No results found!</td></tr>';
}
echo '</table>';
?>

 

This is just a rough example. You will, of course, need to modify this to work with your existing script.

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/#findComment-336651
Share on other sites

Well, presumably all of the code you posted is in a loop similar to:

 

while($row=mysql_fetch_assoc($result)){
//your code here
}

 

You should only place the echoing of the rows of the table inside the while loop. This is, afterall, the piece of code you want repeated.

 

Overall, it would look something like:

 

<?php
$sql = "SELECT * FROM `yourtable`";
$result = mysql_query($sql);
$num = mysql_num_rows($result);
echo '<table><tr><td>Column1</td><td>Column2</td></tr>';
if($num > 0){
while($row = mysql_fetch_assoc($result)){
echo '<tr><td>'.$row['field1'].'</td><td>'.$row['field2'].'</td></tr>';
}
}else{
echo '<tr><td colspan="2">No results found!</td></tr>';
}
echo '</table>';
?>

 

This is just a rough example. You will, of course, need to modify this to work with your existing script.

 

True I wasnt thinking about that part...

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/#findComment-336655
Share on other sites

Sorry. Should have put full code:

 

<?php

$con = mysql_connect("sql3.byethost7.com","b7_463487","Junior007");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("b7_463487_LPS", $con);

$link2 = '<a href="team.php?eventname=%s&year=%s" title="Team" rel="gb_page[422, 422]">Team</a>'; 


$result = mysql_query("SELECT * FROM results
WHERE EventName='$_POST[eventname]' AND Result = 'None'");

while($row = mysql_fetch_array($result))
  {
echo "<table class=\"events\" width=\"90%\">";
echo "<tr>";
echo "<th>Event</th>";
echo "<th>Year</th>";
echo "<th>Team</th>";
echo "</tr>";
echo "<tr><td>";
echo "$row[EventName]";
echo "</td>";
echo "<td>";
echo "$row[Year]";
echo "</td>";
echo "<td>";
printf($link2, $row['EventName'], $row['Year']);
echo "</td></tr>";
echo "</table>";
  }

mysql_close($con)
?>

Link to comment
https://forums.phpfreaks.com/topic/67126-solved-echoing-table/#findComment-336661
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.