Jump to content

[SOLVED] if...else generates no output for else


machar

Recommended Posts

Hi,

This is my first ever post and I'm a complete novice at any developing. php freaks tutorials have been fantastic in helping me learn.

This code works fine when there are results to the mysql query, ie when $num is greater than 0.

But I want to give the users a message when there are no records, ie when $num=0. Nothing happens and the result is a blank page. I have not posted the mysql query because it is working fine when there are results.

 

<table border="1" cellspacing="2" cellpadding="2">

<tr>

<th>soil test date</th>

<th>ph</th>

<th>Phosphorus mg/l</th>

<th>Potassium mg/l</th>

</tr>

<h1><center>Information for <? echo $field_name; ?></center></h1>

<?php

if ($num>0)

{

$i=0;

while ($i < $num) {

 

$soil_testdate=mysql_result($result,$i,"Soil_Tests.date_test");

$pH=mysql_result($result,$i,"Soil_Tests.pH");

$P_mgpl=mysql_result($result,$i,"Soil_Tests.P_mgpl");

$K_mgpl=mysql_result($result,$i,"Soil_Tests.K_mgpl");

 

?>

 

<tr>

<td><? echo Date::convert($soil_testdate, 'Y-m-d H:i:s', 'd-M-Y ') . "\n"; ?></td>

<td><? echo $pH; ?></td>

<td><? echo $P_mgpl; ?></td>

<td><? echo $K_mgpl; ?></td>

</tr>

<?php

$i++;

}}

else echo "No soil test records for this field";

?>

</table>

 

Thanks Ken,

This is the full page:

 

<?php
include '../password_check.php';
include '../dbinfo.inc.php';
include '../dateclass.php';

$field_name=$_POST['field_name'];
$query="SELECT Soil_Tests.date_test,Soil_Tests.pH,Soil_Tests.P_mgpl,Soil_Tests.K_mgpl FROM Soil_Tests WHERE Soil_Tests.field_name='$field_name' ORDER BY Soil_Tests.date_test DESC";
$result=mysql_query($query);

$num=mysql_numrows($result)or die(mysql_error());

?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../web.css" />
</head>
<body>
<div id="navigation">
<?php
include '../navigation.html';
?>
</div>
<div id="content">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>soil test date</th>
<th>ph</th>
<th>Phosphorus mg/l</th>
<th>Potassium mg/l</th>
</tr>
<h1><center>Information for <? echo $field_name; ?></center></h1>
<?php
if ($num>0)
{
$i=0;
while ($i < $num) {

$soil_testdate=mysql_result($result,$i,"Soil_Tests.date_test");
$pH=mysql_result($result,$i,"Soil_Tests.pH");
$P_mgpl=mysql_result($result,$i,"Soil_Tests.P_mgpl");
$K_mgpl=mysql_result($result,$i,"Soil_Tests.K_mgpl");

?>

<tr>
<td><? echo Date::convert($soil_testdate, 'Y-m-d H:i:s', 'd-M-Y ') . "\n"; ?></td>
<td><? echo $pH; ?></td>
<td><? echo $P_mgpl; ?></td>
<td><? echo $K_mgpl; ?></td>
</tr>
<?php
$i++;
}}
else echo "No soil test records for this field";
?>
</table>
<?php
$query="SELECT Lime_Application.date_application,Lime_Application.rate FROM Lime_Application WHERE Lime_Application.field_name='$field_name' ORDER BY Lime_Application.date_application DESC";
$result=mysql_query($query);

$num=mysql_numrows($result)or die(mysql_error());

mysql_close();
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>lime application date</th>
<th>lime rate t/ha</th>
</tr>
<?php
$i=0;
while ($i < $num) {

$lime_date=mysql_result($result,$i,"Lime_Application.date_application");
$lime_rate=mysql_result($result,$i,"Lime_Application.rate");

?>

<tr>
<td><? echo Date::convert($lime_date, 'Y-m-d H:i:s', 'd-M-Y ') . "\n"; ?></td>
<td><? echo $lime_rate; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
</div>
<div id="advertising">
<?php
include '../advertising.php';
?>
</div>
</body>
</html>

 

I don't know the correct way to use indentation; I will search for a tutorial.

Simon

 

(edited by kenrbnsn to put in the


tags)

I copied and pasted your first code into my editor and previewed.  It returned your else statement for me stating 'No soil test records for this field'.

 

So there is nothing wrong with that code.

 

<?php
    if ($num>0) {
        $i=0;
        while ($i < $num) {
            $soil_testdate=mysql_result($result,$i,"Soil_Tests.date_test");
            $pH=mysql_result($result,$i,"Soil_Tests.pH");
            $P_mgpl=mysql_result($result,$i,"Soil_Tests.P_mgpl");
            $K_mgpl=mysql_result($result,$i,"Soil_Tests.K_mgpl");
?>

    <tr>
    <td><?php echo Date::convert($soil_testdate, 'Y-m-d H:i:s', 'd-M-Y ') . "\n"; ?></td>
    <td><?php echo $pH; ?></td>
    <td><?php echo $P_mgpl; ?></td>
    <td><?php echo $K_mgpl; ?></td>
    </tr>
<?php
            $i++;
            }  // end while
    } else {
        echo "No soil test records for this field";
    } // end if $num>0
?>

 

 

Thanks for that.

I have just tried using internet explorer instead of firefox and still get exactly the same result; I get the output I want when there are records, but can't get the no records message to display.

Simon

 

Ken, sorry I misunderstood what you meant by 'between


tags'; I see from your edit what to do. Thanks.

Thanks for that.

I have just tried using internet explorer instead of firefox and still get exactly the same result; I get the output I want when there are records, but can't get the no records message to display.

Simon

 

Ken, sorry I misunderstood what you meant by 'between


tags'; I see from your edit what to do. Thanks.

 

 

Well, what's it displaying? Anything or still showing results for an entry?

There is nothing, just a blank page.

 

I have a user entry form to input the "field name" which gives $field_name.

In the mysql database there are tables 'Soil_Tests' and 'Lime_Application', both including a field 'field_name'.

Not all fields (in the farm sense) have soil test records, and not all have lime application records.

The script is working well when the field name entered is one that has soil test records or lime application records, but when the field (in the farm sense) has neither I just get a blank page.

 

 

I made a few changes to your code.

 

When you select fields from one table, you do not have to prefix the field name with the table name. Using the function mysql_fetch_assoc() can greatly simplify your code.

 

<?php
include '../password_check.php';
include '../dbinfo.inc.php';
include '../dateclass.php';

$field_name=mysql_real_escape_string($_POST['field_name']);
$query="SELECT date_test,pH,P_mgpl,K_mgpl FROM Soil_Tests WHERE field_name='$field_name' ORDER BY date_test DESC";
$result=mysql_query($query) or die("Problem with the query: $query<br />" . mysql_error());
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="../web.css" />
</head>
<body>
<div id="navigation">
<?php
include '../navigation.html';
?>
</div>
<div id="content">
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>soil test date</th>
<th>ph</th>
<th>Phosphorus mg/l</th>
<th>Potassium mg/l</th>
</tr>
<h1><center>Information for <? echo $field_name; ?></center></h1>
<?php
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
	$tmp = array();
	$tmp[] = date('d-M-Y',strtotime($row['date_test']));
	$tmp[] = $row['pH'];
	$tmp[] = $row['P_mgpl'];
	$tmp[] = $row['K_mgpl'];
	echo '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
}
} else
echo "No soil test records for this field";
?>
</table>
<?php
$query="SELECT date_application,rate FROM Lime_Application WHERE field_name='$field_name' ORDER BY date_application DESC";
$result=mysql_query($query) or die("Problem with the query: $query<br />" . mysql_error());;
?>
<table border="1" cellspacing="2" cellpadding="2">
<tr>
<th>lime application date</th>
<th>lime rate t/ha</th>
</tr>
<?php
while ($row = mysql_fetch_assoc($result)) {
	$tmp = array();
	$tmp[] = date('d-M-Y',strtotime($row['date_application']));
	$tmp[] = $row['rate'];
	echo '<tr><td>' . implode('</td><td>',$tmp) . '</td></tr>';
}
?>
</table>
</div>
<div id="advertising">
<?php
include '../advertising.php';
?>
</div>
</body>
</html>

 

I'm not sure what

Date::convert($soil_testdate, 'Y-m-d H:i:s', 'd-M-Y ')

does, but I believe using the strtotime() function in combination with the date() function does the same thing.

 

It's quite possible that the else is outputing the correct line, but since it's in a <table> and not in a <tr><td> block, the browsers might be eating it. Do a "show source" of the web page after it's generated to see if the string is there.

 

 

See if this helps.

 

Ken

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.