Jump to content

Recommended Posts

Ok, I've played with this for hours now, and I know it's something stupid! I'm trying to get all the rows in a table that match a query. It's always displaying one less row than it has, you'll notice I display the number of rows and then actually echo the rows, but for ex, it will say 3 results, and then display 2 results :(

 

Here's the source code

 

<script language="JavaScript" type="text/javascript">
function validate() {
if (document.searchcase.search_value.value == "") {
alert("Please enter text to search!")
}
}
</script>
<link href="/epso/style.css" rel="stylesheet" type="text/css" />
<form action="" method="post" name="searchcase">

<?php

$search_id = $_POST['search_id'];
$search_value = $_POST['search_value'];
$submit = $_POST['search'];

//On SUBMIT
if (isset($submit)) {

//Check if there's any text, if not, show the table
if ($search_value == NULL) {
show_search_table();
}

else{ //If there is text

$query = "SELECT * FROM case_information WHERE $search_id LIKE '$search_value%'";
$result = mysql_query($query) or die(mysql_error());
$nums = mysql_num_rows($result) or die(mysql_error());
$row = mysql_fetch_assoc($result) or die(mysql_error());

if ($nums == 1) { //If there is only 1 result
	 echo "
	<table border=0>
	<tr><td class=case_information colspan=2>ISSUED IN THE COUNTY OF <B>{$row['county_issued']}</b><br>
	IN THE STATE OF <b>{$row['state_issued']}</b><br>
	COURT CASE NUMBER <b>{$row['case_number']}</b>
	</tr>
	<tr><td class=style_box>{$row['plaintiff']}<br><small>Plaintiff</small>
	<br><b>vs.</b><br>
	{$row['defendant']}<br><small>Defendant</small></td></tr>
	</table>
	";
	}

elseif ($nums == 0) {
	show_search_table();
	echo "<span id=\"error_message\">There are no cases matching your search</span>";
	}

elseif ($nums > 1) {
	echo "<span id=\"numbers\">Your search returned <b>$nums</b> results</span><br />";
	echo "
	<table id=\"results_table\">
		<tr id=\"results_table_header\">
		<th>CASE NUMBER</th>
		<th>CLIENT FILE NUMBER</th>
		<th>PLAINTIFF</th>
		<th>DEFENDANT</th>
		<th>ADDRESS</th>
		<th>STATUS</th>
		<th>SERVER</th>
		<th> </th>
		</tr>
	";
	while ($row = mysql_fetch_assoc($result)) {
				echo "
				<tr>
				<td>{$row['case_number']}</td>
				<td>{$row['client_reference']}</td>
				<td>{$row['plaintiff']}</td>
				<td>{$row['defendant']}</td>
				<td>{$row['addressone']}</td>
				<td>{$row['status']}<nobr></td>
				<td>{$row['serverID']}</td>
				<td>Change</td>
				</tr>
				";	
				}	
	echo "</table>";
	$i = $i + 1;
	}
}

}

elseif(!isset($sumbit)) //No search yet, so show the table
{
show_search_table();
}

function show_search_table() 
{
echo '
<table border="0" style="background-color:#CCCCCC">
<tr><td>
Search by:</td>
<td>
<select name="search_id">
';
$search_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_options as $search_value=>$search_variables)
{
echo "<option value=$search_value>$search_variables</option>";
}

echo '
</select>
</td>
<td><input type="text" id="search_field" name="search_value" /></td>
<td><input type="submit" name="search" onClick="validate()" /></td>
</tr></table>
';
}
?>


Link to comment
https://forums.phpfreaks.com/topic/72413-solved-getting-all-the-rows-from-a-table/
Share on other sites

It might be because you already fetch the first row below the mysql_num_rows query.. Try to move that inside the if statement that prints if $num == 1:

 

 <script language="JavaScript" type="text/javascript">
function validate() {
if (document.searchcase.search_value.value == "") {
alert("Please enter text to search!")
}
}
</script>
<link href="/epso/style.css" rel="stylesheet" type="text/css" />
<form action="" method="post" name="searchcase">

<?php

$search_id = $_POST['search_id'];
$search_value = $_POST['search_value'];
$submit = $_POST['search'];

//On SUBMIT
if (isset($submit)) {

//Check if there's any text, if not, show the table
if ($search_value == NULL) {
show_search_table();
}

else{ //If there is text

$query = "SELECT * FROM case_information WHERE $search_id LIKE '$search_value%'";
$result = mysql_query($query) or die(mysql_error());
$nums = mysql_num_rows($result) or die(mysql_error());

if ($nums == 1) { //If there is only 1 result
	 $row = mysql_fetch_assoc($result) or die(mysql_error());
                 echo "
	<table border=0>
	<tr><td class=case_information colspan=2>ISSUED IN THE COUNTY OF <B>{$row['county_issued']}</b><br>
	IN THE STATE OF <b>{$row['state_issued']}</b><br>
	COURT CASE NUMBER <b>{$row['case_number']}</b>
	</tr>
	<tr><td class=style_box>{$row['plaintiff']}<br><small>Plaintiff</small>
	<br><b>vs.</b><br>
	{$row['defendant']}<br><small>Defendant</small></td></tr>
	</table>
	";
	}

elseif ($nums == 0) {
	show_search_table();
	echo "<span id=\"error_message\">There are no cases matching your search</span>";
	}

elseif ($nums > 1) {
	echo "<span id=\"numbers\">Your search returned <b>$nums</b> results</span><br />";
	echo "
	<table id=\"results_table\">
		<tr id=\"results_table_header\">
		<th>CASE NUMBER</th>
		<th>CLIENT FILE NUMBER</th>
		<th>PLAINTIFF</th>
		<th>DEFENDANT</th>
		<th>ADDRESS</th>
		<th>STATUS</th>
		<th>SERVER</th>
		<th> </th>
		</tr>
	";
	while ($row = mysql_fetch_assoc($result)) {
				echo "
				<tr>
				<td>{$row['case_number']}</td>
				<td>{$row['client_reference']}</td>
				<td>{$row['plaintiff']}</td>
				<td>{$row['defendant']}</td>
				<td>{$row['addressone']}</td>
				<td>{$row['status']}<nobr></td>
				<td>{$row['serverID']}</td>
				<td>Change</td>
				</tr>
				";	
				}	
	echo "</table>";
	$i = $i + 1;
	}
}

}

elseif(!isset($sumbit)) //No search yet, so show the table
{
show_search_table();
}

function show_search_table() 
{
echo '
<table border="0" style="background-color:#CCCCCC">
<tr><td>
Search by:</td>
<td>
<select name="search_id">
';
$search_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_options as $search_value=>$search_variables)
{
echo "<option value=$search_value>$search_variables</option>";
}

echo '
</select>
</td>
<td><input type="text" id="search_field" name="search_value" /></td>
<td><input type="submit" name="search" onClick="validate()" /></td>
</tr></table>
';
}
?>

Ok, I've re-written the page so in my opinion it's cleaner, but i'm still fairly new, anyway, now when I'm hit submit, nothing happens...

 

<?php
//Turn the form values into PHP variables
$search_value = $_POST['search_value'];
$search_id = $_POST['search_id'];
$submit = $_POST['submit'];

//Checks the number of results returned from $result
function check_number_of_results() {

//Query the search options
$query = "SELECT * FROM case_information WHERE $search_id LIKE '$search_value%'";
$result = mysql_query($query);

$nums = mysql_num_rows($result); //Get the number of rows queried

//If there are 0 results, echo the 0 result table
if ($nums == 0) {
	zero_results();
} //Ends IF

//If there is 1 result, echo the row
elseif ($nums == 1) {
	one_result();
} //Ends IF

//If there are many rows, echo only a "teaser" of the row
elseif ($nums > 1) {
	multiple_rows();
} //Ends IF

// Closes the check_number_of_results
} 

function zero_results() {
echo "<span id=\"error_message\"> There are no results for your search. </span>";
search_table();
}

function one_result() {
$one_row = mysql_fetch_row($result);
echo "Case Number:".$one_row['case_number']."<br />
	  Client Reference Number:".$one_row['client_reference']."<br />
	  ";
}

function multiple_rows() {
$multiple_row = mysql_fetch_assoc($result);
echo "<table id='results_table'><tr><th>Case Number</th><th>Client Reference Number</th></tr>";
while ($multiple_row = mysql_fetch_assoc($result)) {
	echo "<tr><td>".$multiple_row['case_number']."</td><td>".$multiple_row['client_reference']."</td></tr>";
	}
}

function search_table() {
echo "
<table border='0' style='background-color:#CCCCCC'>
<tr><td>
Search by:</td>
<td>
<select name='search_id'>
";

$search_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_options as $search_value=>$search_variables )
{
echo "<option value=$search_value>$search_variables</option>";
}

echo "
</select>
</td>
<td><input type='text' id='search_field' name='search_value' /></td>
<td><input type='submit' name='submit' /></td>
</tr></table>
";
}

if  (isset($submit)) {
check_number_of_results();
}
elseif (!isset($submit)) {
search_table();
}
?>


<?php

if  ($submit=="true") {
check_number_of_results();
}
elseif ($submit=="false") {
search_table();
} ?>

 

 

or even

 

 

<?php
if  ($submit) {
check_number_of_results();
}
elseif (!$submit) {
search_table();
} ?>

Ok, Now I'm back to everything working EXCEPT when there are multiple row, it is always 1 short.........aaaaaahhhhhhh!!!!!! It's driving me crazy

 

<?php
function zero_results($query, $result, $nums, $search_value) {
echo "<span id=error_message> There are $nums results for your search. </span>";
search_table();
}

function one_result($query, $result) {
$one_row = mysql_fetch_assoc($result);
echo "Case Number: {$one_row['case_number']}<br />";
echo "Client Reference Number:".$one_row['client_reference']."<br />
	  ";
return;
}

function multiple_rows($query, $result, $nums) {
$multiple_row = mysql_fetch_assoc($result);

echo "<table id='results_table'><tr><th>Case Number</th><th>Client Reference Number</th></tr>";

while ($multiple_row = mysql_fetch_assoc($result)) {
		echo "<tr><td>{$multiple_row['case_number']}</td><td>{$multiple_row['client_reference']}</td></tr>";
}
return;
}

function search_table() {
echo "
<form action='' method='post'>
<table border='0' style='background-color:#CCCCCC'>
<tr><td>
Search by:</td>
<td>
<select name='field_name'>
";

$search_field_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_field_options as $db_field_name=>$pretty_display_name )
{
echo "<option value=$db_field_name>$pretty_display_name</option>";
}

echo "
</select>
</td>
<td><input type='text' id='input_field' name='input_field' /></td>
<td><input type=submit name=submit value='Search' /></td>
</tr></table>
</form>
";
return;
}

//Turn the form values into PHP variables
$field_name = $_POST['field_name'];
$input_field = $_POST['input_field'];
$submit = $_POST['submit'];

//Checks the number of results returned from $result
function check_number_of_results($field_name, $input_field, $submit) {

//Query the search options
$query = "SELECT * FROM case_information WHERE '$field_name' LIKE '$input_field%'";
$result = mysql_query($query) or die(mysql_error());

echo $field_name;
echo $input_field;
echo $submit;

$nums = mysql_num_rows($result); //Get the number of rows queried

//If there are 0 results, echo the 0 result table
if ($nums == 0) {
	zero_results($query, $result);
}

//If there is 1 result, echo the row
elseif ($nums == 1) {
	one_result($query, $result);
}

//If there are many rows, echo only a "teaser" of the row
elseif ($nums > 1) {
	multiple_rows($query, $result, $nums);
}

return;
//Closes check_number_of_results	
}


if (isset($submit)) {
check_number_of_results();
}
elseif (!isset($submit)) {
search_table();
}
?>

Ok, so I that worked......but gosh dangit! now no matter what I do I always go to multiple_rows();

 

<?php

function zero_results($query, $result, $nums, $search_value) {
echo "<span id=error_message> There are $nums results for your search. </span>";
search_table();
}

function one_result($query, $result) {
$one_row = mysql_fetch_assoc($result);
echo "Case Number: {$one_row['case_number']}<br />";
echo "Client Reference Number:".$one_row['client_reference']."<br />
	  ";
return;
}

function multiple_rows($query, $result, $nums) {
$multiple_row = mysql_num_rows($result);

echo "<table id='results_table'><tr><th>Case Number</th><th>C. Ref. Number</th><th>Plaintiff</th><th>Defendant</th><th>Address</th><th>Person to be Served</th></tr>";

while ($multiple_row = mysql_fetch_assoc($result)) {
		echo "<tr><td>{$multiple_row['case_number']}</td><td>{$multiple_row['client_reference']}</td><td>{$multiple_row['plaintiff']}</td><td>{$multiple_row['defendant']}</td><td>{$multiple_row['addressone']}<br />{$multiple_row['city']}, {$multiple_row['state']} {$multiple_row['zip']}</td><td>{$multiple_row['person_to_be_served']}</tr>";
}
return;
}

function search_table() {
echo "
<form action='' method='post'>
<table border='0' style='background-color:#CCCCCC'>
<tr><td>
Search by:</td>
<td>
<select name='field_name'>
";

$search_field_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_field_options as $db_field_name=>$pretty_display_name )
{
echo "<option value=$db_field_name>$pretty_display_name</option>";
}

echo "
</select>
</td>
<td><input type='text' id='input_field' name='input_field' /></td>
<td><input type=submit name=submit value='Search' /></td>
</tr></table>
</form>
";
return;
}

//Turn the form values into PHP variables
$field_name = $_POST['field_name'];
$input_field = $_POST['input_field'];
$submit = $_POST['submit'];

//Checks the number of results returned from $result
function check_number_of_results($field_name, $input_field, $submit) {

//Query the search options
$query = "SELECT * FROM case_information WHERE '$field_name' LIKE '$input_field%'";
$result = mysql_query($query) or die(mysql_error());

$nums = mysql_num_rows($result); //Get the number of rows queried

//If there are 0 results, echo the 0 result table
if ($nums == 0) {
	zero_results($query, $result);
}

//If there is 1 result, echo the row
elseif ($nums == 1) {
	one_result($query, $result);
}

//If there are many rows, echo only a "teaser" of the row
elseif ($nums > 1) {
	multiple_rows($query, $result, $nums);
}

return;
//Closes check_number_of_results	
}


if (isset($submit)) {
check_number_of_results();
}
elseif (!isset($submit)) {
search_table();
}
?>


remove 1st row from function multiple_rows()

function multiple_rows($query, $result, $nums) {
//$multiple_row = mysql_fetch_assoc($result);

echo "<table id='results_table'><tr><th>Case Number</th><th>Client Reference Number</th></tr>";

while ($multiple_row = mysql_fetch_assoc($result)) {
		echo "<tr><td>{$multiple_row['case_number']}</td><td>{$multiple_row['client_reference']}</td></tr>";
}
return;
}

<?php

function zero_results($query, $result, $nums, $search_value) {
echo "<span id=error_message> There are $nums results for your search. </span>";
search_table();
return;
}

function one_result($query, $result) {
$one_row = mysql_fetch_assoc($result);
echo "Case Number: {$one_row['case_number']}<br />";
echo "Client Reference Number: {$one_row['client_reference']}<br />
	  ";
return;
}

function multiple_rows($query, $result, $nums) {

echo "<table id='results_table'><tr><th>Case Number</th><th>C. Ref. Number</th><th>Plaintiff</th><th>Defendant</th><th>Address</th><th>Person to be Served</th></tr>";

while ($multiple_row = mysql_fetch_assoc($result)) {
		echo "<tr><td>{$multiple_row['case_number']}</td><td>{$multiple_row['client_reference']}</td><td>{$multiple_row['plaintiff']}</td><td>{$multiple_row['defendant']}</td><td>{$multiple_row['addressone']}<br />{$multiple_row['city']}, {$multiple_row['state']} {$multiple_row['zip']}</td><td>{$multiple_row['person_to_be_served']}</tr>";
}
return;
}

function search_table() {
echo "
<form action='' method='post' name='search_form'>
<table border='0' style='background-color:#CCCCCC'>
<tr><td>
Search by:</td>
<td>
<select name='field_name'>
";

$search_field_options = array ("case_number" => "Case Number", "uniqueID" => "Job ID", "defendant" => "Defendant", "plaintiff" => "Plaintiff", "addressone" => "Address");

foreach( $search_field_options as $db_field_name=>$pretty_display_name )
{
echo "<option value=$db_field_name>$pretty_display_name</option>";
}

echo "
</select>
</td>
<td><input type='text' id='input_field' name='input_field' /></td>
<td><input type=submit name=submit value='Search' /></td>
</tr></table>
</form>
";
return;
}

//Turn the form values into PHP variables
$field_name = $_POST['field_name'];
$input_field = $_POST['input_field'];
$submit = $_POST['submit'];

//Checks the number of results returned from $result
function check_number_of_results($field_name, $input_field, $submit) {

//Query the search options
$query = "SELECT * FROM case_information WHERE '$field_name' LIKE '$input_field%'";
$result = mysql_query($query) or die(mysql_error());

$nums = mysql_num_rows($result); //Get the number of rows queried

//If there are 0 results, echo the 0 result table
if ($nums == 0) {
	zero_results($query, $result);
}

//If there is 1 result, echo the row
elseif ($nums == 1) {
	one_result($query, $result);
}

//If there are many rows, echo only a "teaser" of the row
elseif ($nums > 1) {
	multiple_rows($query, $result, $nums);
}

return;
//Closes check_number_of_results	
}


if (isset($submit)) {
check_number_of_results($query, $result, $nums);
}
elseif (!isset($submit)) {
search_table();
}
?>

[code]

[/code]

The reason your code always displays one less row than it has is because you call....

 

$row = mysql_fetch_assoc($result) or die(mysql_error());

 

outside of any of your function calls. This places the first row into the $row array and then moves the pointer to the next row waiting for the next call to mysql_fetch_assoc().

 

The logic of having all the different types of results be called from seperate functions really isn't all that logical and actually makes your code harder to read and harder to impliment but its up to you I guess.

Wow, well after many late nights working on this, I've finally got it working, displaying everything it should, when it should. Thank you all for the help, and I'm sure I'll be back with more questions, I'm writing my first PHP Application.

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.