/    Sign up×
Community /Pin to ProfileBookmark

How to output an sql search by column

Hi… PHP again.. ?

PHP’s default output of data is one row only.. my problems is if i have 150 data on my database i want to output it by 3 column…
50 for 1st column 50 for 2nd column 50 for 3rd column

[COLOR=”Red”]PHP Default output
outputdata1
|
|
|
|
|
|
|
outputdata150[/COLOR]

i want it to be by column
[COLOR=”RoyalBlue”]outputdata1 outputdata51 outputdata101
| | |
| | |
| | |
| | |
| | |
| | |
| | |
outputdata50 outputdata100 outputdata150[/COLOR]

how can i do that … :>

to post a comment
PHP

11 Comments(s)

Copy linkTweet thisAlerts:
@NogDogSep 26.2011 — Use mysql_num_rows() or the equivalent for whichever DB extension you are using to determine how many rows were returned by the query. Divide by 3 (rounding up probably), to determine how many rows per column. Then use a counter variable as you loop through your results to determine when it's time to start the next column.
Copy linkTweet thisAlerts:
@chris0Sep 26.2011 — do it manually using php arrays,

just do a simple loop

[code=php]

$output = array();
$i =0;
$count = 0;
while ($row = mysql_fetch_array($result, MYSQL_NUM)) {
if (!isset($output[$i])) $output[$i] = array();
$output[$i][] = $row;
$count++;
if ($count>=50) $i=1;
if ($count>=150) $i=2;
}

[/code]


now use $output[0], $output[1], $output[2]
Copy linkTweet thisAlerts:
@SyCoSep 26.2011 — Here's a rough but functional example.

[code=php]
<?
$max_rows=5;//configurable variable

//create array
$arr=range('a', 'z');

$i=0;
$col=1;
foreach ($arr as $letter) {
$display_arr[$col][]=$letter;
if($i<$max_rows){
$i++;
}else{
$i=0;
$col++;
}
}

echo '<table border=1><tr>';
foreach($display_arr as $column => $letter_array){
echo '<td valign="top" align="center" width="20">';
foreach($letter_array as $display_letter){
echo $display_letter.'<br/>';
}
echo '</td>';
}
echo '</tr></table>';[/code]
Copy linkTweet thisAlerts:
@NogDogSep 28.2011 — If you load all the data into an array, then you can just use array_chunk() to split it into columns, e.g.:
[code=php]
<?php
$cols = 3;
$arr=range('a', 'z');
$chunks = array_chunk($arr, ceil(count($arr) / $cols));
?>
<table>
<tr style='vertical-align:top'>
<?php
foreach($chunks as $chunk) {
echo "<td>n";
foreach($chunk as $value) {
echo $value."<br />n";
}
echo "</td>n";
}
?>
</tr>
</table>
[/code]

Output:
<i>
</i>a j s
b k t
c l u
d m v
e n w
f o x
g p y
h q z
i r
Copy linkTweet thisAlerts:
@rizzaocampoauthorOct 07.2011 — i am using digits for my value...

[code=php]
<?php
$cols = 3;
$arr=range('a', 'z');
$chunks = array_chunk($arr, ceil(count($arr) / $cols));
?>
<table>
<tr style='vertical-align:top'>
<?php
foreach($chunks as $chunk) {
echo "<td>n";
foreach($chunk as $value) {
echo $value."<br />n";
}
echo "</td>n";
}
?>
</tr>
</table>

[/code]


would this be applicable by numbers?

this is my codes..
[code=php]
$x = 0;
$cols = 0;
while ()
for($x=0;$x<=0;$x++)
{

echo "<a href='".$row['email_name']."'>".$row['resto_name']. "</a>";
;
if($cols == 3)
{
echo "</tr><tr>";
}
$cols++;

}
echo "</td></tr></table>";

[/code]
Copy linkTweet thisAlerts:
@rizzaocampoauthorOct 09.2011 — i got it... the only problem is, how can i put the $value as a link for the output?

<a href='".$row['email_name']."'>".$row['resto_name']. "</a>

should i put???

$value = <a href='".$row['email_name']."'>".$row['resto_name']. "</a> ??

thanks

this is my code: i have replaced the letter to numbers because in my database i am using numbers to get my other value, is this correct?

[code=php]
<?php
$cols = 3;
$arr=range('1', '219');
$chunks = array_chunk($arr, ceil(count($arr) / $cols));
?>
<table width="50%">
<tr style='vertical-align:top'>
<?php
foreach($chunks as $chunk) {
echo "<td>n";
foreach($chunk as $value) {
echo $value."<a href='".$row['email_name']."'>".$row['resto_name']. "</a>";
}
echo "</td>n";
}
?>
</tr>
</table>
[/code]
Copy linkTweet thisAlerts:
@rizzaocampoauthorNov 04.2011 — how can i put this on a 3 column output from database from data #1 until data #227 and the output will be the name of the restaurants, chunck is working but it doesn't seems to be the output that i wanted,

[code=php]
<?php
$cols = 3;
$arr=range('1', '219');
$chunks = array_chunk($arr, ceil(count($arr) / $cols));
?>
<table width="50&#37;">
<tr style='vertical-align:top'>
<?php
foreach($chunks as $chunk) {
echo "<td>n";
foreach($chunk as $value) {
echo $value."<a href='".$row['email_name']."'>".$row['resto_name']. "</a>";
}
echo "</td>n";
}
?>
</tr>
</table>

[/code]


i've tried this code and the output is

101 hawker 101 hawker 101 hawker

1521 Resto 1521 Resto 1521 Resto

until the end of the restaurant listing.
Copy linkTweet thisAlerts:
@rizzaocampoauthorNov 04.2011 — http://www.flickr.com/photos/69399746@N07/6310910993/


see the attachment file, that would be my proposed output. it's not working on chunck.

THANK YOU VERY MUCH IN ADVANCE FOR THE HELP.
Copy linkTweet thisAlerts:
@SyCoNov 16.2011 — I don't know why I can't post a long reply anymore on this site but I attached it. Perhaps someone else can try to post it?

It times out for me in FF, Chrome and IE and is the main reason I don't post much here anymore. Too much hassle.

Anyway, reply attached.
Copy linkTweet thisAlerts:
@rrfuertesNov 16.2011 — Try to use this code:

[code=php]
<table class="table2">
<?php
$result = mysql_query("SELECT * FROM database",$db);
$rows = mysql_num_rows($result);
echo '<tr>';
while ($myrow = mysql_fetch_array($result)) {
$counter++;
$totalrow++;
echo '<td>'.$myrow['data'].'</td>';
if ($totalrow == $rows) {
echo '</tr>';
}elseif ($counter%3==0) {
echo '</tr><tr>';
}}
?>
</table>
[/code]



though the output would become like this:
[CODE]
data1 data2 data3
data4 data5 data6
data7 data8 data9
[/CODE]
Copy linkTweet thisAlerts:
@SyCoNov 17.2011 — Try to use this code:

[code=php]
<table class="table2">
<?php
$result = mysql_query("SELECT * FROM database",$db);
$rows = mysql_num_rows($result);
echo '<tr>';
while ($myrow = mysql_fetch_array($result)) {
$counter++;
$totalrow++;
echo '<td>'.$myrow['data'].'</td>';
if ($totalrow == $rows) {
echo '</tr>';
}elseif ($counter%3==0) {
echo '</tr><tr>';
}}
?>
</table>
[/code]



though the output would become like this:
[CODE]
data1 data2 data3
data4 data5 data6
data7 data8 data9
[/CODE]
[/QUOTE]


See the link in post #9 for what the OP is trying to do. Splitting data down columns is a little trickier than across.
×

Success!

Help @rizzaocampo 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.15,
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,
)...