how to associate column record with table header?
Question by ceyrslan
I have a mysql table, one of its columns called parking space
has a number in it. However, for some records the ‘parking space’-column is empty. Now, I want to call just this column in a page table where the column heads are numbered from 1 – 200 (first column: 1; second column: 2;….) So, if there is the value ’12’ in the ‘parking space’-column, then this shall show up in column ’12’ and so on, and if there is no entry for a number then the column in the page table shall be left empty. How can I associate the numbers in ‘parking space’ with that page table?
...
<?php
$pagetable=array('1','2','3','4','5','6');//...until 200
foreach($pagetable as $value){
?>
<table border="1px" cellspacing="1" cellpadding="1">
<tr>
<th>
<?php echo $value ?>
</th>
</tr>
<?php
}
include("dbinfo.inc.php");
include_once("config.php");
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC");
$results = mysql_num_rows($result);
if ($results > 0){
$num=mysql_numrows($result);
$i=0;
while ($i < $num) {
$id=mysql_result($result,$i,"id");
$park=mysql_result($result,$i,"park");
?>
<tr>
<td style="background-color:;">
<?php echo $park; ?>
<br>
<?php if($park!=''){ ?>
<a href="single.php?&id=<?php echo $id; ?>"><?php echo $id; ?></a>
<?php
}
?>
</td>
</tr>
<?php
$i++;
}
}
?>
…
Answer by Starx
There are two ways, you can do this.
-
Create all 200 rows on the database table and query it. This will omit almost all of the headache to get what you want.
-
Do a for loop from 1 to 200 and compare the active item if it exists on the mysql results. For this you require an continuous numeric entry in the database to identify each columns individually.
What I mean is? Column 1 in the should have corresponding record indicating
1
somewhere in its record.Here is an example of this:
$result = mysql_query("SELECT * FROM contacts ORDER BY park ASC"); $results = mysql_num_rows($result); if ($results > 0) { $num=mysql_numrows($result); $rows = array(); while($row = mysql_fetch_row($result)) { $rows[$row['id']] = $row; }); //Now process it }