/    Sign up×
Community /Pin to ProfileBookmark

2 or 3 columns of data…

Hi,

I am sure this question has been asked many times before but I have been unable to find a satisfactory answer ?

Anyway, here goes:

How would I go about splitting data from a data source (e.g. MySQL) into 2 or 3 columns?

More precisely, I wish to know how to put data into three columns per row with two spacer columns (actually, that’s 5 columns isn’t it… on well?).

[code=php]
<table>
<tr>
<td>$data</td>
<td><img src=”./spacer.gif” /></td>
<td>$data</td>
<td><img src=”./spacer.gif” /></td>
<td>$data</td>
</tr>
<tr>
<td>$data</td>
<td><img src=”./spacer.gif” /></td>
<td>$data</td>
<td><img src=”./spacer.gif” /></td>
<td>$data</td>
</tr>
</table>[/code]

I think it involves the modal (%) operator(?) but I don’t understand how it works… :os

Any help you can give will be greatly appreciated… Thanks.

to post a comment
PHP

4 Comments(s)

Copy linkTweet thisAlerts:
@scrotayeAug 05.2005 — What type of data are you expecting to receive from the database? You can use explode() to split data at a specific character (ex: n or " "(space)) or use a regex to split the data into patterns
Copy linkTweet thisAlerts:
@artoAug 06.2005 — Perhaps something like this?
[code=php]
echo('<table> <tr>');
$i=1;
while ($row=mysql_fetch_assoc($result)) {
echo('<td>'.$row['column'].'</td>');

if ($i%3)
echo('<td><img src="spacer.gif"/></td>');
else
echo('</tr> <tr>');

$i++;
}
echo('</tr> </table>');[/code]

Arto
Copy linkTweet thisAlerts:
@atwauthorAug 06.2005 — I was thinking of system for taking contact details (name, address, email address, tel, fax, etc...) from a db and formatting it to print on an Avery labels sheet; so every row is a label... ?

Example: [URL=http://imageshack.us][IMG]http://img323.imageshack.us/img323/5809/atwlabelsexample6bj.gif[/IMG][/URL]

Thanks for the help so far...

[B]@arto:[/B] Given the new info, does your code do this ([I]sorry, I am still very new to PHP -- improving all the time though[/I])? ?
Copy linkTweet thisAlerts:
@artoAug 07.2005 — Given the new info, does your code do this[/QUOTE]Yes, you just need to output all needed columns from db:
[code=php]echo('<table> <tr>');
$i=1;
while ($row=mysql_fetch_assoc($result)) {
echo('<td>');
echo($row['name'].'<br>');
echo($row['address'].'<br>');
echo($row['city'].'<br>');
echo($row['country'].'<br>');
// others if needed
echo('</td>');

if ($i%3)
echo('<td><img src="spacer.gif"/></td>');
else
echo('</tr> <tr>');

$i++;
}
echo('</tr> </table>');[/code]

Arto
×

Success!

Help @atw 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.24,
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,
)...