/    Sign up×
Community /Pin to ProfileBookmark

Dynamic Address Table

Hey,
I wanted to do a dynamic vertical table with data from MySQL. The table works fine when it’s done horizontally, displaying all the addresses in the address book.
I.E.

[code=html]
<table border=”0″>
<?php do { ?>
<tr>
<td><?php echo $row_dir_us[‘add_1’]; ?></td>
<td><?php echo $row_dir_us[‘add_2’]; ?></td>
<td><?php echo $row_dir_us[‘city’]; ?></td>
<td><?php echo $row_dir_us[‘zip’]; ?></td>
<td><?php echo $row_dir_us[‘state’]; ?></td>
<td><?php echo $row_dir_us[‘country’]; ?></td>
</tr>
<?php } while ($row_dir_us = mysql_fetch_assoc($dir_us)); ?>
</table>[/code]

But when I modified it so it would display vertically, with only 3 addresses per row, it only displays the first address ‘x’ times. ‘x’ being the number of addresses in the address book.
I.E.

[code=html]<?php do{
if($cols == 0){
echo “<tr>”;
}?>
<?php if($regs <= $totalRows_dir_us){?>
<td align=”center”>
<table border=”0″>
<tr>
<td align=”center”><?php echo $row_dir_us[‘add_1’]; ?></td>
</tr>
<tr>
<td align=”center”><?php echo $row_dir_us[‘add_2’]; ?></td>
</tr>
<tr>
<td align=”center”><?php echo $row_dir_us[‘city’] . “, “; echo $row_dir_us[‘zip’]; ?></td>
</tr>
<tr>
<td align=”center”><?php echo $row_dir_us[‘state’] . “, “; echo $row_dir_us[‘country’]; ?></td>
</tr>
</table>
</td><?php
$regs++;
$cols++;
} ?>
<?php if($cols == 3 && $regs < $totalRows_dir_us){
$cols = 0;
echo “</tr><tr>&nbsp;</tr>”;
}
if($regs == $totalRows_dir_us){
if($cols < 3){
echo “</tr>”;
}
echo “</table>”;
}?>
<?php } while ($row_dir_us <= mysql_fetch_assoc($dir_us)); ?>[/code]

Hope someone can help.
Thanks

to post a comment
PHP

6 Comments(s)

Copy linkTweet thisAlerts:
@NogDogOct 07.2010 — I typically use a pattern along these lines:
[code=php]
<table>
<tr>
<?php
$data = array();
for($ix = 1; $ix <= 7; $ix++) {
$data[] = range('a', 'c');
}
$counter = 0;
$cols = 3;
while($row = mysql_fetch_assoc($result)) {
if($counter++ % $cols == 0) {
echo "</tr>n<tr>n";
}
printf(
"<td>%s<br />n%s<br />n%s</td>n",
htmlspecialchars($row['col1']),
htmlspecialchars($row['col2']),
htmlspecialchars($row['col3'])
);
}
while($counter++ % $cols != 0) {
echo "<td>&nbsp;</td>n";
}
?>
</tr>
</table>
[/code]
Copy linkTweet thisAlerts:
@boloydeauthorOct 07.2010 — Thanks for the reply, I'll try it out.

I just don't get the first part due to my being a noob. What is the $data array used for?

Do I get the data from MySQL and put it in that array?
Copy linkTweet thisAlerts:
@boloydeauthorOct 07.2010 — I tried it out, but somehow this skips the first address in the address book. Moved some stuff around to see if it worked with no success, not sure what else to try.

Again thanks for the reply.
Copy linkTweet thisAlerts:
@BelrickOct 08.2010 — FYI

[code=php]
echo $row_dir_us['state'] . ", "; echo $row_dir_us['country'];
[/code]


Is easier to write as

[code=php]
echo $row_dir_us['state'] . " , ". $row_dir_us['country'];
[/code]
Copy linkTweet thisAlerts:
@NogDogOct 08.2010 — Thanks for the reply, I'll try it out.

I just don't get the first part due to my being a noob. What is the $data array used for?

Do I get the data from MySQL and put it in that array?[/QUOTE]


Oops...that was left over from some test code I put in to make sure I didn't type something stupid. Ignore the $data stuff (between the <?php tag and the $counter = 0 line).
Copy linkTweet thisAlerts:
@NogDogOct 08.2010 — I tried it out, but somehow this skips the first address in the address book. Moved some stuff around to see if it worked with no success, not sure what else to try.

Again thanks for the reply.[/QUOTE]


Most likely you have a call to mysql_fetch_*() prior to starting the while() loop. Either get rid of that call (probably the best way), or do a [url=http://www.php.net/manual/en/function.mysql-data-seek.php]mysql_data_seek()[/url] to row 0 (zero) before the while loop starts.
×

Success!

Help @boloyde spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.25,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...