homepage Welcome to WebmasterWorld Guest from 54.211.95.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Building dynamic row
Only the last row is displayed
smells so good




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

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?

 

coopster




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

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.

smells so good




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

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.

coopster




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

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.
smells so good




msg:3222563
 6:17 pm on Jan 17, 2007 (gmt 0)

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.

henry0




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

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

$this->page=$page;

smells so good




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

<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)

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved