I'm having trouble getting this into one table. Right now I am using two tables and having them side by side but I don't think that is the best solution. I have the table all set up I just can't figure out how to get this into one.
while ($row = mysql_fetch_assoc($query)) {
$result .= "<tr> <td> {$row['a']} the {$row['b']} </td> </tr>";
}
echo $thegames;
This produces something like:
james the giant
rick the monster
chris the goblin
What I want is to be able to add more tds from a separate while loop...
while($secondrow = mysql_fetch_assoc($secondquery)) {
$first = "<td> {$secondrow['alias']} </td>";
$second = "<td> {$secondrow['number']} </td>";
}
So in the end I would like it would look like this:
james the giant $secondrow['alias'] $secondrow['number']
rick the monster $secondrow['alias'] $secondrow['number']
etc...
Hope this makes sense. What I'm doing now is creating two separate tables and trying to line them up but don't like that method. Any help would be great.
Judging by your code, you don't actually need to do two queries. If you are expecting 4 columns for each row, there's no need to do multiple while loops either. Rather, a change in the structure of your query to use a JOIN; that will enable you to get the full result set in one shot.
As it has been mentioned, please, don't use mysql_*
functions in new code. They are no longer maintained and are officially deprecated.
I'll show you how to do a JOIN with PDO, making some assumptions about your table names and primary keys based on the code you've shown so far:
$pdo = new PDO("mysql:host=localhost;dbname=database", '-username-', '-password-');
$sql = '
SELECT
`players`.`id`,
`players`.`a`,
`players`.`b`,
`player_info`.`alias`,
`player_info`.`number`
FROM
`players`
LEFT JOIN
`player_info` ON `players`.id = `player_info`.`player_id`
';
$players = $pdo->query($sql);
$row_data= array();
while ($row = $players->fetch()) {
$row_data[] = '
<td>'.$row->a.' the '.$row->b.'</td>
<td>'.$row->alias.'</td>
<td>'.$row->number.'</td>
';
}
echo '<table><tr>'.implode('</tr><tr>', $row_data).'</tr></table'>;
If the join proves to be impractical (not typical), then you should build your data array and generate the HTML as two seperate steps:
// gather the data
$data = array();
$first = $pdo->query($sql);
while($arow = $first->fetch()) {
$one_data['a'] = $arow->a;
$one_data['b'] = $arow->b;
$second = $pdo->query($second_sql);
while($brow = $second->fetch()) {
$one_data['alias'] = $brow->alias;
$one_data['number'] = $brow->number;
}
$data[] = $one_data;
}
// now generate HTML
print '<table>';
foreach ($data as $row) {
print '
<tr>
<td>'.$row['a'].' the '.$row['b'].'</td>
<td>'.$row['alias'].'</td>
<td>'.$row['number'].'</td>
</tr>
';
}
print '</table>';
And as a side note... columns named a
and b
are just a bad. idea. Those columns represent data, so give them labels that reflect what that data is. Dealing with mystery columns TODAY might be fine... you remember what they mean. 6 months from now? A year from now? Use meaningful names for all columns and variables -- why not?
Documentation
PDO
- http://www.php.net/manual/en/class.pdo.phpPDO::query
- http://www.php.net/manual/en/pdo.query.phpPDOStatement
- http://www.php.net/manual/en/class.pdostatement.phpPDOStatement::fetch
- http://www.php.net/manual/en/pdostatement.fetch.phpmysql_query
DEPRECATED! - http://php.net/manual/en/function.mysql-query.phpJOIN
- http://dev.mysql.com/doc/refman/5.0/en/join.htmlYou can combine your 2 sets into 1 array and loop over that while printing.
edit; someone found this a bad answer, but that means that you agree with the above result which puts fetching sql rows in your template.
You first prepare your data for the template and THEN you loop over it and print it.