Jump to content

quick question


joebudden

Recommended Posts

hi guys,

 

Iv got a table that displays each record from a query.

 

The problem is theres a lot of records and I just wondered if there was a way of creating a 3 tables side by side to make the information displayed more compact but not using separate queries ??

 

here is the code so far

 

<?php
// get players A - J
$getPlayersSQL = "SELECT playerId,playerName,playerSurname FROM player ORDER BY playerSurname ASC";
$getPlayersRES = mysql_query($getPlayersSQL);
?>
<form action="createMyTeamConfirm.php" method="post">
<input type="submit" name="submit" value="Continue"><br /><br />
<input type="hidden" name="teamId" value="<?php echo $teamId ?>" />
<input type="hidden" name="leagueId" value="<?php echo $teamId ?>" />
<table width="200px" border="1" align="center" cellpadding="0" cellspacing="0">
<tr><td></td><td>Player Name</td></tr>
<?php
while($players = mysql_fetch_assoc($getPlayersRES))
{
// hold the players details in variables
$playerId = $players["playerId"];
$playerName = $players["playerName"];
$playerSurname = $players["playerSurname"];
// create the table rows
echo "<tr>";
echo "<td align=\"center\"><input type=\"checkbox\" name=\"add_players[]\" value=\"$playerId\" /></td>";
echo "<td align=\"center\">";
?>
<a href="javascript:openWindow('playerInformation.php?playerId=<?php echo $playerId;?>', 500, 400, 200, 200)">
<?php
echo $playerName."<b> ";
echo $playerSurname."</b>";
?>
</a>
<?php
echo "</td>";
echo "</tr>";
}
?>
</table>
</form>

Link to comment
https://forums.phpfreaks.com/topic/50270-quick-question/
Share on other sites

what you can is to use a if statement... then count how many times while has started over... every 3rd time you add </tr><tr>

 

something like this

 

if ($count == 3) {

echo "</tr><tr>";

$i = 1;

 

<?php

// Do all query stuff

$i=1;
while ($players = mysql_fetch_assoc($getPlayersRES))
{

if ($i == 3) {
echo "</tr><tr>";
$i=1;
}

// Do all the things you want to do here...
$i++

}
?>

 

PS: I haven't tested it, but it should work

Link to comment
https://forums.phpfreaks.com/topic/50270-quick-question/#findComment-246747
Share on other sites

Store the data in an array then use this method

 

<?php
$ar = range(1,12);

$k = count ($ar);

$cols = 4;
$rows = ceil($k/$cols);

echo '<table border="1">';
for ($r=0; $r < $rows; $r++) {
    echo '<tr>';
    for ($c=0; $c < $cols; $c++) {
        echo "<td>{$ar[$r+$rows*$c]}</td>";
    }
    echo '</tr>';
}
echo '</table>';
?>

gives->[pre]

1  4  7  10

2  5  8  11

3  6  9  12

[/pre]

 

If the data doesn't have to be in individual table cells, then something like this

<?php
include '../test/db.php';

$sql = "SELECT player_name, player_surname FROM player ORDER BY player_surname";
$res = mysql_query($sql) or die (mysql_error()."<p>$sql</p>");

$cols = 3;
$rows = ceil(mysql_num_rows($res)/$cols);
$k = 0;
echo '<table border="1"><tr>';
while (list($n, $s) = mysql_fetch_row($res)) {
    if ($k % $rows == 0) echo '<td>';
    echo "<b>$s</b><br>$n<br><br>";
    $k++;
    if ($k % $rows == 0) echo '</td>';
}
echo '</tr></table>'; 
?>

Link to comment
https://forums.phpfreaks.com/topic/50270-quick-question/#findComment-247096
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.