By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,244 Members | 1,772 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,244 IT Pros & Developers. It's quick & easy.

Odd data column display problems

P: n/a
LRW
I'm doing the following to try to display queried data into verticle
columns, and I'm getting odd behaviors.
For example, if there are only 4 items to display, it skips item number 3:

Item1 Item4
Item 2

If there are 10 items, it duplicates some:

Item 1 Item 4 Item 7
Item 2 Item 5 Item 8
Item 3 Item 6 Item 9
Item 4 Item 7 Item 10

Any help with what I'm doing wrong, I would really appreciate!!
Thanks
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}

$sct_display_X = explode("|", $sct_tempdisp);
$items = $sct_display_X;
For($i = 0; $i< (count($items)/3); $i +=1){
$cat_listx .= "<tr><td width=\"33%\">".$items[$i]."</td><td
width=\"33%\">".$items[$i+3]."</td><td>".$items[$i+6]."</td></tr>";
}

$cat_list = "<table width=\"100%\">".$cat_listx."</table>";
Jul 17 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
LRW wrote:
<snip>
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}


Try this here:

echo htmlentities($sct_tempdisp);

<snip>
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #2

P: n/a
LRW

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
LRW wrote:
<snip>
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a
href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}


Try this here:

echo htmlentities($sct_tempdisp);

<snip>


Uhm, that simply prints the actual tags to the page instead of DOING the
HTML tags. Printing "<a etc" to the screen instead of making a link.
Jul 17 '05 #3

P: n/a
LRW wrote:

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
LRW wrote:
<snip>
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a
href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}


Try this here:

echo htmlentities($sct_tempdisp);

<snip>


Uhm, that simply prints the actual tags to the page instead of DOING the
HTML tags. Printing "<a etc" to the screen instead of making a link.


Yes, ... and then you proceed to explode() that on "|".

Q. Where are the "|"s you are about to explode() on?
--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #4

P: n/a
LRW

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
LRW wrote:

"Pedro Graca" <he****@dodgeit.com> wrote in message
news:sl*******************@ID-203069.user.uni-berlin.de...
LRW wrote:
<snip>
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a
href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}

Try this here:

echo htmlentities($sct_tempdisp);

<snip>


Uhm, that simply prints the actual tags to the page instead of DOING the
HTML tags. Printing "<a etc" to the screen instead of making a link.


Yes, ... and then you proceed to explode() that on "|".

Q. Where are the "|"s you are about to explode() on?


Here...

$sct_tempdisp .= "<a href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";

See it at the end of the line there?
I don't think it's the exploding the $sct_tempdisp into an array that's the
problem (is it?) Because I get individual...chunks, for lack of the proper
term, just either skipped ones or doubled ones. I THINK the problem is in
the dividing up the array into the columns.

Jul 17 '05 #5

P: n/a
LRW wrote:
I'm doing the following to try to display queried data into verticle
columns, and I'm getting odd behaviors.
For example, if there are only 4 items to display, it skips item number 3:

Item1 Item4
Item 2

If there are 10 items, it duplicates some:

Item 1 Item 4 Item 7
Item 2 Item 5 Item 8
Item 3 Item 6 Item 9
Item 4 Item 7 Item 10

Any help with what I'm doing wrong, I would really appreciate!!
Thanks
$sql_cats = "SELECT * FROM tbl_subcategory WHERE sct_category =
'$proto_cat'";
$result_cats = @mysql_query($sql_cats, $dbh);
$numrows_cats = mysql_num_rows($result_cats);
while ($row_cats = mysql_fetch_array($result_cats)) {
$sct_id = $row_cats[sct_id];
$sct_name = $row_cats[sct_name];
$sct_tempdisp .= "<a href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}

$sct_display_X = explode("|", $sct_tempdisp);
$items = $sct_display_X;
For($i = 0; $i< (count($items)/3); $i +=1){
$cat_listx .= "<tr><td width=\"33%\">".$items[$i]."</td><td
width=\"33%\">".$items[$i+3]."</td><td>".$items[$i+6]."</td></tr>";
}

$cat_list = "<table width=\"100%\">".$cat_listx."</table>";


You're right about the visible problem not being the "|".

You have to review your for() loop. The way you have it coded produces
this output:

$items[0] $items[3] $items[6]
$items[1] $items[4] $items[7]
$items[2] $items[5] $items[8]
$items[3] $items[6] $items[9]
$items[4] $items[7] $items[10]
$items[5] $items[8] $items[11]
$items[6] $items[9] $items[12]
$items[7] $items[10] $items[13]
$items[8] $items[11] $items[14]
... ... ...

The value you add to the index must be variable, not the constant 3 (or
6) as you have.
So just 'find' the initial value for the second and third column

$col1st = ???; // for 10 elements, should be 4
$col2nd = ???; // for 10 elements, should be 6

and redo your loop, don't forgetting to check whether ($col1st+$i) and
($col2nd+$i) are greater than count($items), in which case you shouldn't
add them to $cat_lists.

for ($i = 0; $i < count($items)/3); ++$i) {
$cat_lists .= "<tr><td>" . $items[$i] . "</td><td>"
. $items[$col1st+$i] . "</td><td>"
. $items[$col2nd+$i] . "</td></tr>";
}
Happy Coding :-)

PS. But you still have a (little) problem with the "|"

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #6

P: n/a
LRW
Pedro Graca <he****@dodgeit.com> wrote in message news:<sl*******************@ID-203069.user.uni-berlin.de>...

You're right about the visible problem not being the "|".

You have to review your for() loop. The way you have it coded produces
this output:

$items[0] $items[3] $items[6]
$items[1] $items[4] $items[7]
$items[2] $items[5] $items[8]
$items[3] $items[6] $items[9]
$items[4] $items[7] $items[10]
$items[5] $items[8] $items[11]
$items[6] $items[9] $items[12]
$items[7] $items[10] $items[13]
$items[8] $items[11] $items[14]
... ... ...

The value you add to the index must be variable, not the constant 3 (or
6) as you have.
So just 'find' the initial value for the second and third column

$col1st = ???; // for 10 elements, should be 4
$col2nd = ???; // for 10 elements, should be 6

and redo your loop, don't forgetting to check whether ($col1st+$i) and
($col2nd+$i) are greater than count($items), in which case you shouldn't
add them to $cat_lists.

for ($i = 0; $i < count($items)/3); ++$i) {
$cat_lists .= "<tr><td>" . $items[$i] . "</td><td>"
. $items[$col1st+$i] . "</td><td>"
. $items[$col2nd+$i] . "</td></tr>";
}
Happy Coding :-)

PS. But you still have a (little) problem with the "|"


$sct_tempdisp .= "<a
href=\"category.php?disp=2&cat_id=".$sct_id."\"
class=\"tah12OrangeBold\">".$sct_name."</a><br /><br />|";
}
$sct_display_X = explode("|", $sct_tempdisp);
$items = $sct_display_X;//array() //assume a bunch of items in this
array
$col1 = (count($items)/2); //
$col2 = (count($items)/3); //
For($i = 0; $i< (count($items)/3); ++$i){
$cat_listx .= "<tr><td width=\"33%\">".$items[$i]."</td><td
width=\"33%\">".$items[$col1+$i]."</td><td>".$items[$col2+$i]."</td></tr>";
}
$cat_list = "<table width=\"100%\"
align=\"center\">".$cat_listx."</table>";

GAHR! I suck! I either can't do math or have no concept of logic...or
both!
I've tried so many different ways of trying to define $col1 and $col2,
and whether I should or not have played with
(count($items)/3)
and I just can't get it to evenly divide the items into three columns
with no repeats! =P
I suck.
What do I need to do to define $col1 and $col2? IS it a division of
the number of items in $items? If so, I can keep playing with that. If
it's some other formula, or if I need to focus on something else,
lemme know? I can keep playing with the proper lines until I figure it
out, but not knowing if I'm even working on the right lines makes it
really frustrating.

The exploding on the pipe? I can't see the problem...unless, it's the
threat of what would happen if there's a legitimate pipe within the
string that it shouldn't explode on? Is that it?

Thanks for your help!
Liam
Jul 17 '05 #7

P: n/a
LRW wrote:
Pedro Graca <he****@dodgeit.com> wrote in message news:<sl*******************@ID-203069.user.uni-berlin.de>...
But you still have a (little) problem with the "|" The exploding on the pipe? I can't see the problem...


When you explode("|", ...) a string with 4 "|" you get *five* elements.
Your original string generates one too many element for the $items
array.

explode() a shorter version of it, and you should be ok:

$items = explode('|', substr($xxx, 0, -1));
$col1 = (count($items)/2); //
$col2 = (count($items)/3); //
For($i = 0; $i< (count($items)/3); ++$i){
$cat_listx .= "<tr><td width=\"33%\">".$items[$i]."</td><td
width=\"33%\">".$items[$col1+$i]."</td><td>".$items[$col2+$i]."</td></tr>";
} <snip> I've tried so many different ways of trying to define $col1 and $col2,
and whether I should or not have played with
(count($items)/3)
Yes, count($items)/3 is important.
and I just can't get it to evenly divide the items into three columns
with no repeats! =P
Write down what you want to happen for 6, 7, 8, 9, 10, 11, ... elements
and try to figure the formulas from there.

6 elements $items[0] $items[2] $items[4] 6 => 2 => 4
$items[1] $items[3] $items[5]

7 elements $items[0] $items[3] $items[5] 7 => 3 => 5
$items[1] $items[4] $items[6]
$items[2] -- --

8 elements $items[0] $items[3] $items[6] 8 => 3 => 6
$items[1] $items[4] $items[7]
$items[2] $items[5] --

9 elements $items[0] $items[3] $items[6] 9 => 3 => 6
$items[1] $items[4] $items[7]
$items[2] $items[5] $items[8]

...... ... ... ... ...

What do I need to do to define $col1 and $col2? IS it a division of
the number of items in $items? If so, I can keep playing with that. If
it's some other formula, or if I need to focus on something else,
lemme know? I can keep playing with the proper lines until I figure it
out, but not knowing if I'm even working on the right lines makes it
really frustrating.


I used division [count()/3] and modulus [count()%3] ...

I put my (not so) little version below. Try to make up your own rather
than using mine :-)
M |
Y |
V
( |
N |
O |
T |
V
S |
O |
) |
V
L |
I |
T |
T |
L |
E |
V
V |
E |
R |
S |
I |
O |
N |
V
// example data
$items = array('0', '1', '2', '3', '4', '5', '6', '7', '8', '9');

// 6 items ==> $col1 = 2;
// 7 ==> 3
// 8 ==> 3
// 9 ==> 3
$col1 = (int)((count($items) + 2) / 3);
// 6 items ==> $col1 = 2; $col2 = 4;
// 7 ==> 3 5
// 8 ==> 3 6
// 9 ==> 3 6
$col2 = $col1 * 2;
if (count($items) % 3 == 1) --$col2;

// initialize $cat_listx
$cat_listx = '';

// build HTML
for ($i = 0; $i < $col1; ++$i) {
$cat_listx .= '<tr>';

// first column is always completely filled
$cat_listx .= '<td>' . $items[$i] . '</td>';

// only fill second column if it won't repeat in third column
if ($col1 + $i < $col2) {
$cat_listx .= '<td>' . $items[$col1 + $i] . '</td>';
} else {
$cat_listx .= '<td>(no data)</td>';
}

// only fill second column if there is enough data
if ($col2 + $i < count($items)) {
$cat_listx .= '<td>' . $items[$col2 + $i] . '</td>';
} else {
$cat_listx .= '<td>(no data)</td>';
}

$cat_listx .= '</tr>';
}

echo $cat_listx;

--
Mail to my "From:" address is readable by all at http://www.dodgeit.com/
== ** ## !! ------------------------------------------------ !! ## ** ==
TEXT-ONLY mail to the whole "Reply-To:" address ("My Name" <my@address>)
may bypass my spam filter. If it does, I may reply from another address!
Jul 17 '05 #8

P: n/a
LRW
Pedro Graca <he****@dodgeit.com> wrote in message news:<sl*******************@ID-203069.user.uni-berlin.de>...
--snip--

Write down what you want to happen for 6, 7, 8, 9, 10, 11, ... elements
and try to figure the formulas from there.
- snip -
I put my (not so) little version below. Try to make up your own rather
than using mine :-)


Man, I feel like those times sitting in college math with a blank
expression on my face because I'm completely not understanding
something I feel like if extremely basic and simple. A logic synapse
in mah brain is refusing to fire.

While I "get" what the later part of the code is doing, counting up to
the next item for the column, etc...I still just can't grok the method
of figuring out how to divide the amount into three columns. I have a
feeling it's basic, simple stuff...but I'm just not getting it.

You've already spent more time than necessary trying to help me, and I
appreciate it! I think I have enough info in this thread for me to try
to figure it out...expecially with your example. I understand things
better if I can just sit and reverse engineer something that's working
and figure out why it's working.

Thanks for your time and help!!
Liam
Jul 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.