Welcome to WebmasterWorld Guest from 54.144.231.243

Forum Moderators: coopster & jatar k

Building dynamic row

Only the last row is displayed

   
11:30 am on Jan 17, 2007 (gmt 0)

5+ Year Member



I'm stumped. After 3 days of picking apart my "for loop" I cannot understand why my page is not being constructed as expected. I'm using class objects to build pages on the fly, based on selected criteria. The pages have a table style layout with three columns. I'm building each 'row', and then after all the rows are completed I'm displaying the finished product.

It starts with this in my index:
$display = $page->_init($select);

In the _init routine I'm parsing the request and begin gathering what I need from the database. Once I know how many rows to build, I go into a foreach loop and put together all the pieces. The foreach loop contains calls to specific functions to accomplish this. Each of the functions will contain something like this :

$this->$page .= "blah";

The result that I'm getting is that only the last row is returned. I can verify that all the information is getting processed, but not all of it getting returned. In fact, it appears that each consecutive row is overwriting the previous row.

Page is defined in my object like this, preceeding any functions:
var $page;

My usage of .= is based on the belief that $this->$page will accumulate everything as I work throught the loop. I'm obvioulsy mistaken, can someone enlighten me?

3:45 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



I'm trying to wrap my head around what you are doing so I can offer direction. But the first issue I notice right off is your use of the 'page' variable ...

$this->$page .= "blah";

... the syntax looks incorrect. You should not have that dollar sign there if I understand how you intend to use the variable.

5:16 pm on Jan 17, 2007 (gmt 0)

5+ Year Member



Here's a slimmed view of the function being referenced from the index page. $param will determine what items, or group of items, will be displayed. An array, $list, is created and contains detail data for each of the items. So far, so good. My product details are in an array referenced by $v, and there are 10 elements (0 thru 9) in that array. I use the 10th element as a trigger to create the page segment.
Everything works, except that I only get the last two segments, or the last row.


function _init($param) {
// process param options
// and return an array of all
// items on this page
foreach ($list as $k => $v) {
foreach ($v as $k2 => $v2) {
$item_array[] = $v2;
// alternate left to right
if ($k2 == 9) {
if(($k % 2) == 1) {
$this->_buildLeftContent($item_array);
} else {
$this->_buildRightContent($item_array);
}
$item_array = array();
}
}
}
return $this->$page;
}

I'm ready to toss this mess in the bit bucket and start over with a clean screen.

5:25 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member



The dollar sign in the page variable still seems out of place, meaning I don't see any local function or variable reference to $page anywhere so I am assuming you mean to be returning
return $this->page;
without the dollar sign there.
6:17 pm on Jan 17, 2007 (gmt 0)

5+ Year Member



Doesn't work without the dollar sign. $page is declared as a variable in the class. This is a simple foreach loop, and only the last pair of items are getting displayed.

Back to the drawing board.

8:05 pm on Jan 17, 2007 (gmt 0)

WebmasterWorld Senior Member henry0 is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Shouldn't it be instead of
$this->$page .= "blah";

$this->page=$page;

12:44 pm on Jan 18, 2007 (gmt 0)

5+ Year Member



<henry, the reply I originally typed has been rescinded until I can look it over again.. meanwhile>

In case it was missed earlier, the variable $page is declared in the class as a public variable. All of that works, meaning, I do get the page output that I expect, sort of. The loop is the problem, where consecutive loops will overwrite the previous output, and I'm only returning the results from the last iteration of the loop.

This is such a basic problem, I can't believe I've spent 4 days so far. Is it Friday yet? (Now I'm going to go have a look at Henry's suggestion)

 

Featured Threads

Hot Threads This Week

Hot Threads This Month