Jump to content

Method echo'ing HTML out of order


flambo

Recommended Posts

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

Link to comment
Share on other sites

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>

Link to comment
Share on other sites

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?

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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>';
		}

		}
	}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.