flambo Posted February 23, 2012 Share Posted February 23, 2012 Hi, I have the following method but I can not work out why the html it echo's comes out in reverse? public function pageNmLoop ($pageCnt, $pageStartListings, $filter) { for ($n=1; $n<=$pageCnt; $n++){ if ($n > $this->recordLimit) { break; } if ($n*$this->recordLimit>=intval($pageStartListings)){ $stylePg = '" style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; ">' . $n . '</a>'; } else { $stylePg = '">' . $n . '</a>'; } echo '| <a href="?pageStartListings=' . $n*$this->recordLimit . '&filter=' . rawurlencode($filter) . $stylePg ; } } The HTML it produces is as follows: Page: <a href="?pageStartListings=0&filter=listingStatus=0">1</a> | <a style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; " href="?pageStartListings=100&filter=listingStatus%3D0">1</a> | <a style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; " href="?pageStartListings=200&filter=listingStatus%3D0">2</a> | <a style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; " href="?pageStartListings=300&filter=listingStatus%3D0">3</a> Probably some obvious newbie error but be grateful for a pointer. Many thanks Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/ Share on other sites More sharing options...
thomasw_lrd Posted February 23, 2012 Share Posted February 23, 2012 What are you expecting it to print? Seems like we have similar problems. I'm running into the same sort of thing with printing out to excel. Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/#findComment-1320504 Share on other sites More sharing options...
flambo Posted February 23, 2012 Author Share Posted February 23, 2012 Hi, Possibly, I did read your post and wonder the same thing. basically this: <a style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; " href="?pageStartListings=300&filter=listingStatus%3D0">3</a> should be echo'ing this: <a href="?pageStartListings=300&filter=listingStatus%3D0" style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px;" >3</a> Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/#findComment-1320506 Share on other sites More sharing options...
flambo Posted February 23, 2012 Author Share Posted February 23, 2012 After do some searching I wondered whether the problem might be because I am not escaping the speech marks in the HTML with a \, but on doing this I just get the error The requested URL /masteradmin/\" was not found on this server. Maybe I'm close, not sure why I should get that error. Looks like its trying reference my root directory instead of escape the character? Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/#findComment-1320568 Share on other sites More sharing options...
Psycho Posted February 23, 2012 Share Posted February 23, 2012 Regarding "best practices" you should not be echo'ing content within functions/methods. They should "return" content to where they were called and echo'd there. I would also suggest that you use a class instead of defining the style attributes in the links. It will make your code a lot simpler. But, I don't see the problem. You have a for() loop where $n runs from 1 to $pageCnt. The output shows three links that go from 1 to 3. You do have a link that precedes those, but it must have been generated outside this method since it isn't preceded with a "|" Also, the way the code is built is really illogical. Making the $stylePg variable contain the style, closing of the A tag and the label of the tag is confusing. Anyway, it looks like you are creating pagination links and that you are trying to pass the value to be used in the LIMIT clause. That's not how it's done. Simply pass a page number and calculate the LIMIT start value in the code that generates the page. Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/#findComment-1320574 Share on other sites More sharing options...
flambo Posted February 24, 2012 Author Share Posted February 24, 2012 Hi Psycho, Thanks for posting and for the advise. Have learned something new regarding best practice so have taken that on board. The code is the way it is as I am converting an old ASP site to PHP and just mirrored the code - agreed, its not right and I will have to change it. For what its worth the reason why my escape \ did not work was because I had the echo encased in apostrophe, instead of quotes. Also changed the code for the following and it worked: public function pageNmLoop ($pageCnt, $pageStartListings, $filter) { for ($n=1; $n<=$pageCnt; $n++){ if ($n > $this->recordLimit) { break; } $s = $n+1; if ($n*$this->recordLimit==intval($pageStartListings)){ echo '| <a href="?pageStartListings=' . $n*$this->recordLimit . '&filter=' . rawurlencode($filter). ' " style="border: 1px solid #aaaaaa; background-color: #CCCCCC; padding: 3px; ">' . $s . '</a>'; } else { echo '| <a href="?pageStartListings=' . $n*$this->recordLimit . '&filter=' . rawurlencode($filter). ' ">' . $s . '</a>'; } } } Quote Link to comment https://forums.phpfreaks.com/topic/257642-method-echoing-html-out-of-order/#findComment-1320753 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.