Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Show Single Row in Foreach

Status
Not open for further replies.

PCHomepage

Programmer
Feb 24, 2009
609
US
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?

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

 
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)
{
[indent]echo PageNo($PageNo,$revDate);[/indent]
[indent]$counter=0;[/indent]
[indent]$PageNo++;[/indent]
}
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
 
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'] : "";
		[b]$PageNo = ($row['PageNo']) ? $row['PageNo'] : "";[/b]
		$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;?>
	[bold]<?=PageNo($FullPageNo, $RevDate);?>[/bold]
<?php endforeach;
 
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, [b]$RowID[/b]) {
	[b]$rowPage = "SELECT MAX(ID) 
		    FROM table_name 
		    WHERE PageNo=$PageNo";
	$PageID = DBLookup($rowPage, "db_name");[/b]
	$RevDate = ($RevDate) ? strtoupper(gmdate('M j, Y', $RevDate)) : "";
	if ([b]$RowID == $PageID[/b]) :
		$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;
}
 
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
 
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.

vacunita said:
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.

PHP:
$PageNoToCompare = "valueoffirstpage";

On this, what is valueoffirstpage? I don't know in advance what the first page will be.
 
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
 
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.
 
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")
  {
[indent]   $PageNo= $_row['PageNo']; //set pageno to first row's pageno. This will only happen once.  [/indent]
  }

//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
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top