×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!
  • Students Click Here

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Jobs

Show Single Row in Foreach

Show Single Row in Foreach

Show Single Row in Foreach

(OP)
I wrote a function to put the page number at the bottom of each page and I understand in general why it's not working but cannot work out the logistics to make it work. This is being run inside a foreach loop after the rest of the content is written to the screen. $PageNo (and $RevDate) are coming from a column in the data itself. Appearance-wise it is presenting the page numbers properly but in the wrong places so the first page number appears after the first row of data instead of after the 41st row, then the second one appears the row after the 41st row, etc. Any ideas?

CODE --> PHP

function PageNo($PageNo, $RevDate) {
	$RevDate = ($RevDate) ? strtoupper(gmdate('M j, Y', $RevDate)) : "";
	global $last_pageno;
	if ($PageNo != $last_pageno) :
		$last_pageno = $PageNo;
		$DisplayPageNo = "<div id=\"css-pageno\">\n";
		$DisplayPageNo .= "<div class=\"row\">\n";
		$DisplayPageNo .= "<div class=\"col\">$PageNo<div class=\"revdate\">REVISED $RevDate</div></div>\n";
		$DisplayPageNo .= "</div>\n";
		$DisplayPageNo .= "</div>\n";
		$DisplayPageNo .= "<div class=\"page-break\"></div>\n";
		return $DisplayPageNo;
	endif;
} 

RE: Show Single Row in Foreach

How exactly is it being called in the foreach loop? Since this basically just returns HTML we really need to see the loop.

How is $PageNo being set? Why are you using a Global?

If you need to output a page number after every 41 rows, you can keep an extra counter for it in your loop and do something like:

CODE

if($counter >= 41)
{
echo PageNo($PageNo,$revDate);
$counter=0;
$PageNo++;
}
Your loop should increase your counter every time it runs, so you would need a $counter++ somewhere in your loop outside of the IF.




----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Show Single Row in Foreach

(OP)
I don't know in advance how many rows there will be on any given page and used 41 only as an example of one of the data sets. $PageNo is a value from a field in the database and the foreach loop is below with the function being called toward the end of it. I did not show the database connections as there are no issues with them.

The purpose of global $last_pageno; is to initiate the variable and set it without any value. Then, in the loop, a condition looks to see if it is different than $PageNo and this is the part that is broken in the function. Since $PageNo will always be different than $last_pageno the first time around because $last_pageno as yet has no value until it is set on the next line by $last_pageno = $PageNo;. This code was written to present one-time headers at the top of blocks of data where this makes sense (a working example is at the top of the foreach and another farther down) but not at the bottom of the page for data that is above it as I am trying to make it do now.

CODE

global $last_groupnumber;
	$rowNumber = 0; // For counter
	foreach ($rowView as $row) : 
		$rowNumber++; // increment counter
		$ID = ($row['ID']) ? $row['ID'] : "";
		$BaseGroup = ($row['BaseGroup']) ? $row['BaseGroup'] : "";
		$GroupNo = ($row['GroupNumber']) ? $row['GroupNumber'] : "";
		$BaseName = ($row['BaseName']) ? $row['BaseName'] : "";
		$GroupName = ($row['GroupName']) ? LinkAbbvr($row['GroupName']) : "";
		$Name = ($row['Name']) ? LinkAbbvr($row['Name']) : "";
		$PartNo = ($row['PartNo']) ? $row['PartNo'] : "";
		$Models = ($row['Models']) ? $row['Models'] : "";
		$Description = ($row['Description']) ? $row['Description'] : "";
		$PageNo = ($row['PageNo']) ? $row['PageNo'] : "";
		$SubPage = ($row['SubPage']) ? $row['SubPage'] : "";
		$RevDate = ($row['RevDate']) ? $row['RevDate'] : "";
		$Edition = ($row['Edition']) ? $row['Edition'] : "";
		$Quantity = ($row['Quantity']) ? $row['Quantity'] : "";

		PartsRange($SelectWhere, '', $GroupName,  $rowNumber);

		if ($GroupNo != $last_groupnumber) :
			$last_groupnumber = $GroupNo;?>
			<div id="css-group">
				<div class="row">
					<div class="col"><?=$GroupNo;?></div>
					<div class="col"><?=$Name;?></div>
				</div>
			</div>
			<?=GroupNotes($GroupNo);?>
		<?php endif; ?>

		<div id="css-table">
			<div class="row">
				<div class="col"><?=$PartNo;?></div>
				<div class="col"><?=$Models;?></div>
			<div class="col"><?=$Description;?></div>
		<div class="col"><?=$Quantity;?></div>
	</div>
	</div>

	<?=PartsNotes($GroupNo, $PartNo);?>
	<?php $FullPageNo = $PageNo.$SubPage;?>
	<?=PageNo($FullPageNo, $RevDate);?>
<?php endforeach; 

RE: Show Single Row in Foreach

(OP)
It's not the solution I was looking for but I did find a work-around that does the job. By feeding the ID field into the function, then checking which row has the last page number before it changes, it then shows the information from the function in the proper places. In the meantime, I am open to better ideas!

CODE

function PageNo($PageNo, $RevDate, $RowID) {
	$rowPage = "SELECT MAX(ID) 
		    FROM table_name 
		    WHERE PageNo=$PageNo";
	$PageID = DBLookup($rowPage, "db_name");
	$RevDate = ($RevDate) ? strtoupper(gmdate('M j, Y', $RevDate)) : "";
	if ($RowID == $PageID) :
		$DisplayPageNo = "<div id=\"css-pageno\">\n";
		$DisplayPageNo .= "<div class=\"row\">\n";
		$DisplayPageNo .= "<div class=\"col\">$PageNo<div class=\"revdate\">REVISED $RevDate</div></div>\n";
		$DisplayPageNo .= "</div>\n";
		$DisplayPageNo .= "</div>\n";
		$DisplayPageNo .= "<div class=\"page-break\"></div>\n";
		return $DisplayPageNo;
	endif;
} 

RE: Show Single Row in Foreach

So the PageNo comes from the database row?

And you just want to output the page html when it changes in the row data correct?

Why not simply keep the page number in a variable and compare with the row value during the iteration?

CODE

$PageNoToCompare = "valueoffirstpage";

foreach()
{
if($PageNo <> $row['PageNo']) //When page number changes, output the page info
  {
    $html = PageNo(...); //call your function to display the page html.
    $PageNo = $row['PageNo'];  //Set page number to new page number, and wait for it to change again. 
  }
...
 
} 

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Show Single Row in Foreach

(OP)
Yes, PageNo comes from the database row which I had tried to say in my original question but perhaps wasn't too clear and yes, I want to show the HTML when the page number changes.

Quote (vacunita)

Why not simply keep the page number in a variable and compare with the row value during the iteration?

That is exactly what I was trying to do but I wasn't sure what to use for comparison.

CODE --> PHP

$PageNoToCompare = "valueoffirstpage"; 

On this, what is valueoffirstpage? I don't know in advance what the first page will be.

RE: Show Single Row in Foreach

Quote:


On this, what is valueoffirstpage? I don't know in advance what the first page will be.

Do your page numbers not start at 0 or 1? Or are the page numbers starting at any point, like page 27? How can you not know what the first page will be? If you are outputting pages there has to be a starting point. what is that starting point?


There needs to be some order these rows come in that governs the page numbers being output.

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Show Single Row in Foreach

(OP)
This document is nearly 800 pages long so the data is grouped by some other value in such a way that the starting page of a given group can be anything, and each group is relatively short making it very manageable. The very first page of the first group is 1 but after that it changes based on the PageNo range of the group.

It really isn't important what the first page is as long as it can detect the change from first to second and so on. Each row has the PageNo value so once it gets to the point where page 27 changes to page 28 (using your example), the HTML is shown to represent the page above it.

Ordering is already done so no problem there.

RE: Show Single Row in Foreach

In that case then just the variable to any arbitrary value you want, and check for that on the first iteration of the foreach loop. THe first time the loop runs it will have a page number set to anything absolutely anything you want, and you immediately change it to the pageno of the first row. Then you continue normally. Its just so you have a starting point to compare to.

CODE

$PageNo = "doesntmatterwhatthis";

foreach()
{
  if($PageNo =="doesntmatterwhatthisis")
  {
$PageNo= $_row['PageNo']; //set pageno to first row's pageno. This will only happen once.
} //Do all your foreach stuff here if($PageNo <> $row['PageNo']) //When page number changes, output the page info, the first time, this is guaranteed to be false. { $html = PageNo(...); //call your function to display the page html. $PageNo = $row['PageNo']; //Set page number to new page number, and wait for it to change again. } ... }

----------------------------------
Phil AKA Vacunita
----------------------------------
OS-ception: Running Linux on a Virtual Machine in Windows which itself is running in a Virtual Machine on Mac OSx.

Web & Tech

RE: Show Single Row in Foreach

(OP)
Sounds good and makes perfect sense, thank you! I'll give it a try when I'm back home but if possible I would like to try to make it be self-contained within the function as it's being used in several different scripts.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close