I am setting up a report on individuals by extracting data from a MySQL database. (last name, first name, unique index, etc) This starts with the user entering a person’s last name on a form, then the name is submitted to a PHP page using POST. A search of the database is done and matching records are returned including the unique index with each name.
If the name requested is unique in the database I want to go directly to the PHP page which builds the report in HTML and displays it. The unique index must be preserved so that the database may be queried again using this information.
If the name is not unique then a page will be displayed with a list of names from which one may be chosen via a hyperlink which links to the same page as in the para above. The link must carry the unique index as above so that the object page “sees” the same type of input.
I have tried branching depending on whether the number of records returned is “1” or more than one, but PHP treats the link generated from the multi-record page as an index and the other as a variable(?). I may not be explaining this well but I need a way of passing the unique index to the target page in both multiple record instances and single record instances.
Show us your code. It sounds a relatively simple task, but it’s hard to imagine what is causing your code to not work as you expect it to.
I’m never sure whether the kind of by-pass you describe is a good thing from a user perspective. One the one hand you miss out an unnecessary step by not prompting them to choose from a list with only one entry on it, on the other you risk them being confused by not being able to select their choice.
Thanks for your reply.Here is the code: first the form page with the output “indivrpt”
<form method="post" action="indivreport/onename.php">
<h3 style="margin-top: 0px; margin-bottom: 10px">Individual Record Search</h3>
<p style="margin-top: 0px; margin-bottom: 10px">Looking for a specific name?</p>
<p style="margin-top: 0px; margin-bottom: 10px">Enter last name below and
click<br>on Submit.</p><p>Not sure of the spelling? Enter at least<br> the first three characters.</p>
<input type="text" name="indivrpt" size="20" maxlength="20" tabindex="1" />
<p style="margin-top: 0px; margin-bottom: 10px">
<input type="submit" value="Submit" name="B1" /> <input type="reset" value="Reset" name="B2" /></p>
</form>
Then the target page “onename.php” (excerpt)
// Store what the user's form choice was
$indivrpt=mysqli_real_escape_string($link, $_REQUEST['indivrpt']);
//set global variables
global $badgename;
global $badgetitle;
global $multivar;
?>
<?php
//Query only the information required to determine number of results
$result = mysqli_query ($link, "SELECT Regt_No from hkvca_cforcedata.geninfo where geninfo.Last_Name LIKE '$indivrpt'");
//var_dump($result);
//store the query in a result set
if (!$result)
{
$error = 'Error fetching information: ' . mysqli_error($link);
include 'error.html.php';
exit();
}
?>
<?php
//if there are multiple results display them with a link for the user to
//choose. If there is only one record display the report immediately
if (mysqli_num_rows($result) > 1)
{
$multivar = "2";
$title = "Individual Report";
//include Header block from web root
include $_SERVER['DOCUMENT_ROOT'] . '/cforcedata/includes/header.inc.php';
?>
<div id="outer">
<?php
//include the left navigation html
include $_SERVER['DOCUMENT_ROOT'] . '/cforcedata/includes/leftnav.inc.php';
print "<div id='centrecontent'>";
$table = mysqli_query ($link, "SELECT Regt_No, Rank, Last_Name, First_Name, Regiment, Platoon, Company from geninfo where geninfo.Last_Name LIKE '$indivrpt'");
$num = mysqli_num_rows($table);
echo "<h1>Search Results 'C' Force Members: $indivrpt ...</h1>";
echo "<p>Your search returned: $num results.</p>";
echo $multivar;
echo "<p>To display a report on an individual, select their Regimental Number.</p>";
//print mysqli_num_rows($table);
?>
<br />
<table class="tabledata90">
<tr>
<th>Regimental Number</th>
<th>Rank</th>
<th>Last Name</th>
<th>First Name</th>
<th>Regiment</th>
<th>Platoon</th>
<th>Company</th>
</tr>
<?php
while ($row = mysqli_fetch_object($table))
{
print "<tr><td><a href=indivdetailed.php?regtno=$row->Regt_No>$row->Regt_No</a></td>";
print "<td>$row->Rank</td>";
print "<td>$row->Last_Name</td>";
print "<td>$row->First_Name</td>";
print "<td>$row->Regiment</td>";
print "<td>$row->Platoon</td>";
print "<td>$row->Company</td></tr>";
}
print "</table></div></div>";
include $_SERVER['DOCUMENT_ROOT'] . '/cforcedata/includes/footer_np.inc.php';
echo "</body></html>";
}
elseif (mysqli_num_rows($result) == 1)
{
$multivar = "1";
$result = mysqli_query ($link, "SELECT Regt_No from hkvca_cforcedata.geninfo where geninfo.Last_Name LIKE '$indivrpt'");
// print "only one record";
// var_dump($result);
while ($row = mysqli_fetch_object($result))
$regtno = $row->Regt_No;
As you can see I’m trying to set $regtno (which is the unique index) so it will be available no matter if there are multiple names or one name. Now the code for the actual report page (excerpt):
if ($multivar ="1")
{
$cleanRgtno = $regtno;
}
else {
$cleanRgtno = mysqli_real_escape_string($link, $_GET['regtno']);
};
//code that produces the rest of the report follows
It appears that the value for $multivar will not carry across when the intermediate page (multiple names) is used.
I’ve only included the code that I think is needed to examine the problem.
// Store what the user's form choice was
$indivrpt=mysqli_real_escape_string($link, $_REQUEST['indivrpt']);
//set global variables
global $badgename;
global $badgetitle;
global $multivar;
?>
<?php
if(!is_numeric($indivrpt)) {
//Everything the same as before... until
echo "</body></html>";
die(); //If you've got nothing else to output.
} // end of "If num_rows > 1" here.
elseif (mysqli_num_rows($result) == 1) {
$row = mysqli_fetch_object($result);
$indivrpt = $row->Reg_No;
}
} //End of "If Is Not Numeric" here.
if(is_numeric($indivrpt)) { // If you die()'d earlier, this is unnecessary.
//Do your individual report.
Hi StarLion, thanks for these suggestions. Unfortunately the last name is an actual last name and only gets linked to the Regimental Number once it is used as input for a database query. At that point there could be several names the same and so multiple records could be produced, or the name might be unique which would result in one record. The Regimental Number is unique, but it is treated as text as there is a letter at the beginning of the string.
So, I’m not sure what else to do, but you’ve given me good suggestions regarding cleaning up my code.
You can stick with the same code as above, but change it to check for preg_match("%^[a-z]\d+$%",$indivrpt), or you can stop using 1 variable for multiple things (last name/regno) and have your URL’s in the multiple-match table give a different variable name.
The light has come on. I simply created a second individual report page that is used when the name is unique. Not pretty, but it gets the job done. Thanks to StarLion and droopsnoot for taking the time to help out.
Well, you’re not showing how you intend to carry $multivar across from one page to the next - if you need it for some reason, send it as a parameter in the URL.
I think at the end of your code, once you’ve decided there’s a single result, I’d then output headers to redirect to the link so you’re still using the same code.
and when you get there, just use $_GET[‘regtno’] as the reference. So my pseudo-code would be
get the name to search for
do the query
if we have more than one result:
present the list of results with links to detailpage and regtno in the URL
if we have exactly one result:
do a header redirect to detailpage with the single regtno in the URL
if we have no results:
do something else
That should mean the detail page can be the same regardless.
That is an excellent suggestion! Worked like a charm and much more elegant than my solution. Thanks very much! I had come across this function before but did not appreciate that it could be used this way.