Forum Moderators: coopster
For example:
The default will display bedrooms, baths, price, address, and city from the search.
Then if they hit a button, the page will display all the same info, but more info such as commission and contact info.
Would a switch statement be the best way to go about doing this? I tried to do it, but didn't get any of my sql results in the default page. So I removed the switch and put the code back to normal.
Here is my code:
<?
if ($numberOfRows==0) {
?>
Sorry. No records found !
<?
}
else if ($numberOfRows>0){
$i=0;
?>
<body>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="100%">
<TR>
<TD><B>BR</B></TD>
<TD><B>BA</B></TD>
<TD><B>Listing Price</B></TD>
<TD><B>Address</B></TD>
<TD><B>City</B></TD>
</TR>
<?
while ($i<$numberOfRows)
{
if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$bed = MYSQL_RESULT($result,$i,"bed");
$bath = MYSQL_RESULT($result,$i,"bath");
$price = MYSQL_RESULT($result,$i,"price");
$address = MYSQL_RESULT($result,$i,"address");
$city = MYSQL_RESULT($result,$i,"city");
$baseComm = MYSQL_RESULT($result,$i,"price") * .03;
$agentFname = MYSQL_RESULT($result,$i,"listingAgentFname");
$agentLname = MYSQL_RESULT($result,$i,"listingAgentLname");
$agentPhone = MYSQL_RESULT($result,$i,"listingAgentPhone");
$agentName = $agentFname ." " . $agentLname;
?>
<TR BGCOLOR="<? echo $bgColor; ?>">
<TD nowrap><? echo $bed; ?></TD>
<TD nowrap><? echo $bath; ?></TD>
<TD nowrap><? echo $price; ?></TD>
<TD nowrap><? echo $address; ?></TD>
<TD nowrap><? echo $city; ?></TD>
</TR>
<?
$i++;
} // end while
?>
</TABLE>
<?
} // end of if numberOfRows > 0
?>
</body>
Any ideas or help would be greatly appreciated, Thanks.
Something like that:
www.yoursite.com/page.php?extended=on
and in the code:
<TD nowrap><? echo $address; ?></TD>
<TD nowrap><? echo $city; ?></TD>
<?
if ($_GET["extended"]=="on"):
?>
<TD nowrap><? echo $extendedinfo1; ?></TD>
<TD nowrap><? echo $extendedinfo2; ?></TD>
<?endif;?>
</TR>
Bye!
Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource
Any ideas on how to fix that?
Maybe I messed up something, but I made a button to turn "extended" on like this:
<a href="page.php?extended=on">Extended View</a>
So I'm not sure if that's the correct way to do it, please let me know.
Thanks
$pt = $_POST['propType'];
$c = $_POST['city'];
$b = $_POST['bed'];
$pt2 = "'". $pt ."'";
$c2 = "'". $c ."'";
$orderByQuery = " ORDER BY price DESC";
$sql = "SELECT * FROM db_table WHERE propType= (" . $pt2 . ") AND city= (" . $c2 . ") AND bed= " . $b . " AND price BETWEEN " . $_POST['priceMin'] . " AND " . $_POST['priceMax'] . $orderByQuery;
$result = MYSQL_QUERY($sql);
$numberOfRows = MYSQL_NUM_ROWS($result);
Any ideas? It works fine until I hit the button for the extended view.
Thanks again for all your help
Also verify that nothing messes with you $result variable, and remember that the correct syntax for all php functions is all lowercase.
<?
$orderByQuery = " ORDER BY price DESC";
$pt = $_POST['propType'];
$c = $_POST['city'];
$b = $_POST['bed'];
$pt2 = "'". $pt ."'";
$c2 = "'". $c ."'";
$sql1 = "SELECT * FROM priorityListingsSub WHERE propType= (" . $pt2 . ") AND city= (" . $c2 . ") AND bed= " . $b . " AND price BETWEEN " . $_POST['priceMin'] . " AND " . $_POST['priceMax'] . " AND bonusType='SU' " . $orderByQuery;
$result1 = mysql_query($sql1);
$numberOfRows1 = mysql_num_rows($result1);
$sql2 = "SELECT * FROM priorityListingsSub WHERE propType= (" . $pt2 . ") AND city= (" . $c2 . ") AND bed= " . $b . " AND price BETWEEN " . $_POST['priceMin'] . " AND " . $_POST['priceMax'] . " AND bonusType='BO' " . $orderByQuery;
$result2 = mysql_query($sql2);
$numberOfRows2 = mysql_num_rows($result2);
if ($numberOfRows1 ==0) {
?>
Sorry. No records found !
<?
}
else if ($numberOfRows1 >0){
$i=0;
?>
<html>
<body>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="100%">
<TR>
<TD><B>Listing #</B></TD>
<TD><B>BR</B></TD>
<TD><B>BA</B></TD>
<TD><B>Listing Price</B></TD>
<TD><B>Complex</B></TD>
<TD><B>Address</B></TD>
<TD><B>City</B></TD>
<?php if ("aView1"== "on"): ?>
<TD><B>Comm</B></TD>
<TD><B>Bonus</B></TD>
<TD><B>Total Comm</B></TD>
<?php endif;?>
<TD><B>Details</B></TD>
<TD><B>Add</B></TD>
</TR>
<?php
while ($i< $numberOfRows1)
{
if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$listingNum = mysql_result($result1,$i,"bonusType") . mysql_result($result1,$i,"propID");
$bed = mysql_result($result1,$i,"bed");
$bath = mysql_result($result1,$i,"bath");
$price = mysql_result($result1,$i,"price");
$complex = mysql_result($result1,$i,"propType");
$address = mysql_result($result1,$i,"address");
$city = mysql_result($result1,$i,"city");
$baseComm = mysql_result($result1,$i,"price") * .03;
$bonus = mysql_result($result1,$i,"price") * .02;
$total = $baseComm + $bonus;
$bonusType = mysql_result($result1,$i,"bonusType");
$add = '<input type ="checkbox" name="cb[]" value="">';
$agentFname = mysql_result($result1,$i,"listingAgentFname");
$agentLname = mysql_result($result1,$i,"listingAgentLname");
$agentPhone = mysql_result($result1,$i,"listingAgentPhone");
$agentName = $agentFname ." " . $agentLname;
$details = '<a href="details.pdf">Details</a>';
?>
<TR BGCOLOR="<?php echo $bgColor; ?>">
<TD nowrap><?php echo $listingNum; ?></TD>
<TD nowrap><?php echo $bed; ?></TD>
<TD nowrap><? echo $bath; ?></TD>
<TD nowrap>$<?php echo number_format($price, 0, '', ','); ?></TD>
<TD nowrap><?php echo $complex; ?></TD>
<TD nowrap><?php echo $address; ?></TD>
<TD nowrap><?php echo str_replace("_"," ",$city); ?></TD>
<?php if ("aView1"== "on"): ?>
<TD nowrap>$<?php echo number_format($baseComm, 2, '.', ','); ?></TD>
<TD nowrap>$<?php echo number_format($bonus, 2, '.', ','); ?></TD>
<TD nowrap>$<?php echo number_format($total, 2, '.', ','); ?></TD>
<?php endif; ?>
<TD nowrap><?php echo $details; ?></TD>
<TD nowrap><?php echo $add; ?></TD>
</TR>
<?php
$i++;
} // end while
?>
</TABLE>
<?php
} // end of if numberOfRows > 0
?>
<?php
// Start of the "Bonus" Listings
if ($numberOfRows2 ==0) {
?>
Sorry. No records found !
<?
}
else if ($numberOfRows2 >0){
$j=0;
?>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="100%">
<TR>
<TD><B>Listing #</B></TD>
<TD><B>BR</B></TD>
<TD><B>BA</B></TD>
<TD><B>Listing Price</B></TD>
<TD><B>Complex</B></TD>
<TD><B>Address</B></TD>
<TD><B>City</B></TD>
<?php //if ("aView1"== "on"): ?>
<TD><B>Comm</B></TD>
<TD><B>Bonus</B></TD>
<TD><B>Total Comm</B></TD>
<?php //endif; ?>
<TD><B>Details</B></TD>
<TD><B>Add</B></TD>
</TR>
<?php
while ($j< $numberOfRows2)
{
if (($j%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$listingNum = mysql_result($result2,$j,"bonusType") . mysql_result($result2,$j,"propID");
$bed = mysql_result($result2,$j,"bed");
$bath = mysql_result($result2,$j,"bath");
$price = mysql_result($result2,$j,"price");
$complex = mysql_result($result2,$j,"propType");
$address = mysql_result($result2,$j,"address");
$city = mysql_result($result2,$j,"city");
$baseComm = mysql_result($result2,$j,"price") * .03;
$bonus = mysql_result($result2,$j,"price") * .02;
$total = $baseComm + $bonus;
$bonusType = mysql_result($result2,$j,"bonusType");
$add = '<input type ="checkbox" name="cb['.$i.']" value='.$i.'">';
$agentFname = mysql_result($result2,$j,"listingAgentFname");
$agentLname = mysql_result($result2,$j,"listingAgentLname");
$agentPhone = mysql_result($result2,$j,"listingAgentPhone");
$agentName = $agentFname ." " . $agentLname;
$details = '<a href="details.pdf">Details</a>';
?>
<TR BGCOLOR="<?php echo $bgColor; ?>">
<TD nowrap><?php echo $listingNum; ?></TD>
<TD nowrap><?php echo $bed; ?></TD>
<TD nowrap><? echo $bath; ?></TD>
<TD nowrap>$<?php echo number_format($price, 0, '', ','); ?></TD>
<TD nowrap><?php echo $complex; ?></TD>
<TD nowrap><?php echo $address; ?></TD>
<TD nowrap><?php echo str_replace("_"," ",$city); ?></TD>
<?php //if ("aView1"== "on"): ?>
<TD nowrap>$<?php echo number_format($baseComm, 2, '.', ','); ?></TD>
<TD nowrap>$<?php echo number_format($bonus, 2, '.', ','); ?></TD>
<TD nowrap>$<?php echo number_format($total, 2, '.', ','); ?></TD>
<?php //endif; ?>
<TD nowrap><?php echo $details; ?></TD>
<TD nowrap><?php echo $add; ?></TD>
</TR>
<?php
$j++;
} // end while
?>
</TABLE>
<?php
} // end of if numberOfRows > 0
?>
<div id="view"><a href="works4.php?aView1=on">Agent View</a></div>
</body>
</html>
and thats it.
That said, you should NOT use so extensively mysql_result: that is a very slow function which should be used when you need to retrieve only one "cell" (one field of one record) of a big result set without using the rest of the results.
The classical way of reading results is this:
$result=mysql_query("here the sql query");
while ($record=mysql_fetch_assoc($result)):
echo $record["fieldname1"]
endwhile;
As you see, you don't need to know in advance how many records are there, and since you code shows anyway the full result set, why reading the number of rows? Also, the function mysql_fetch_assoc returns an array keyed on field names, and that means that you don' have to create temporary intermediate variables such as you do in your code, you could just do an echo $record["fieldname"]!
That said, try this code:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>
</title>
</head>
<body>
<?
$orderByQuery = " ORDER BY price DESC";
$pt = $_POST['propType'];
$c = $_POST['city'];
$b = $_POST['bed'];
$pt2 = "'". $pt ."'";
$c2 = "'". $c ."'";
$sql1 = "SELECT * FROM priorityListingsSub WHERE propType= (" . $pt2 . ") AND city= (" . $c2 . ") AND bed= " . $b . " AND price BETWEEN " . $_POST['priceMin'] . " AND " . $_POST['priceMax'] . " AND bonusType='SU' " . $orderByQuery;
$result1 = mysql_query($sql1);
$numberOfRows1 = mysql_num_rows($result1);
$sql2 = "SELECT * FROM priorityListingsSub WHERE propType= (" . $pt2 . ") AND city= (" . $c2 . ") AND bed= " . $b . " AND price BETWEEN " . $_POST['priceMin'] . " AND " . $_POST['priceMax'] . " AND bonusType='BO' " . $orderByQuery;
$result2 = mysql_query($sql2);
$numberOfRows2 = mysql_num_rows($result2);
if ($numberOfRows1 ==0) {
?>Sorry. No records found !
<?
}
else if ($numberOfRows1 >0){
$i=0;
?>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="100%">
<TR><TD><B>Listing #</B></TD><TD><B>BR</B></TD><TD><B>BA</B></TD><TD><B>Listing Price</B></TD><TD><B>Complex</B></TD><TD><B>Address</B></TD><TD><B>City</B></TD>
<?php if ($_GET["aView1"]== "on"): ?><TD><B>Comm</B></TD><TD><B>Bonus</B></TD><TD><B>Total Comm</B></TD>
<?php endif;?><TD><B>Details</B></TD><TD><B>Add</B></TD>
</TR>
<?php
while ($i< $numberOfRows1)
{
if (($i%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$listingNum = mysql_result($result1,$i,"bonusType") . mysql_result($result1,$i,"propID");
$bed = mysql_result($result1,$i,"bed");
$bath = mysql_result($result1,$i,"bath");
$price = mysql_result($result1,$i,"price");
$complex = mysql_result($result1,$i,"propType");
$address = mysql_result($result1,$i,"address");
$city = mysql_result($result1,$i,"city");
$baseComm = mysql_result($result1,$i,"price") * .03;
$bonus = mysql_result($result1,$i,"price") * .02;
$total = $baseComm + $bonus;
$bonusType = mysql_result($result1,$i,"bonusType");
$add = '<input type ="checkbox" name="cb[]" value="">';
$agentFname = mysql_result($result1,$i,"listingAgentFname");
$agentLname = mysql_result($result1,$i,"listingAgentLname");
$agentPhone = mysql_result($result1,$i,"listingAgentPhone");
$agentName = $agentFname ." " . $agentLname;
$details = '<a href="details.pdf">Details</a>';
?>
<TR BGCOLOR="<?php echo $bgColor; ?>">
<TD nowrap>
<?php echo $listingNum; ?></TD>
<TD nowrap>
<?php echo $bed; ?></TD>
<TD nowrap>
<? echo $bath; ?></TD>
<TD nowrap>$
<?php echo number_format($price, 0, '', ','); ?></TD>
<TD nowrap>
<?php echo $complex; ?></TD>
<TD nowrap>
<?php echo $address; ?></TD>
<TD nowrap>
<?php echo str_replace("_"," ",$city); ?></TD>
<?php if ($_GET["aView1"]== "on"): ?>
<TD nowrap>$
<?php echo number_format($baseComm, 2, '.', ','); ?></TD>
<TD nowrap>$
<?php echo number_format($bonus, 2, '.', ','); ?></TD>
<TD nowrap>$
<?php echo number_format($total, 2, '.', ','); ?></TD>
<?php endif; ?>
<TD nowrap>
<?php echo $details; ?></TD>
<TD nowrap>
<?php echo $add; ?></TD>
</TR>
<?php
$i++;
} // end while
?>
</TABLE>
<?php
} // end of if numberOfRows > 0
?>
<?php
// Start of the "Bonus" Listings
if ($numberOfRows2 ==0) {
?>Sorry. No records found !
<?
}
else if ($numberOfRows2 >0){
$j=0;
?>
<TABLE CELLSPACING="0" CELLPADDING="3" BORDER="1" WIDTH="100%">
<TR><TD><B>Listing #</B></TD><TD><B>BR</B></TD><TD><B>BA</B></TD><TD><B>Listing Price</B></TD><TD><B>Complex</B></TD><TD><B>Address</B></TD><TD><B>City</B></TD>
<?php //if ($_GET["aView1"]== "on"): ?><TD><B>Comm</B></TD><TD><B>Bonus</B></TD><TD><B>Total Comm</B></TD>
<?php //endif; ?><TD><B>Details</B></TD><TD><B>Add</B></TD>
</TR>
<?php
while ($j< $numberOfRows2)
{
if (($j%2)==0) { $bgColor = "#FFFFFF"; } else { $bgColor = "#C0C0C0"; }
$listingNum = mysql_result($result2,$j,"bonusType") . mysql_result($result2,$j,"propID");
$bed = mysql_result($result2,$j,"bed");
$bath = mysql_result($result2,$j,"bath");
$price = mysql_result($result2,$j,"price");
$complex = mysql_result($result2,$j,"propType");
$address = mysql_result($result2,$j,"address");
$city = mysql_result($result2,$j,"city");
$baseComm = mysql_result($result2,$j,"price") * .03;
$bonus = mysql_result($result2,$j,"price") * .02;
$total = $baseComm + $bonus;
$bonusType = mysql_result($result2,$j,"bonusType");
$add = '<input type ="checkbox" name="cb['.$i.']" value='.$i.'">';
$agentFname = mysql_result($result2,$j,"listingAgentFname");
$agentLname = mysql_result($result2,$j,"listingAgentLname");
$agentPhone = mysql_result($result2,$j,"listingAgentPhone");
$agentName = $agentFname ." " . $agentLname;
$details = '<a href="details.pdf">Details</a>';
?>
<TR BGCOLOR="<?php echo $bgColor; ?>">
<TD nowrap>
<?php echo $listingNum; ?></TD>
<TD nowrap>
<?php echo $bed; ?></TD>
<TD nowrap>
<? echo $bath; ?></TD>
<TD nowrap>$
<?php echo number_format($price, 0, '', ','); ?></TD>
<TD nowrap>
<?php echo $complex; ?></TD>
<TD nowrap>
<?php echo $address; ?></TD>
<TD nowrap>
<?php echo str_replace("_"," ",$city); ?></TD>
<?php //if ($_GET["aView1"]== "on"): ?>
<TD nowrap>$
<?php echo number_format($baseComm, 2, '.', ','); ?></TD>
<TD nowrap>$
<?php echo number_format($bonus, 2, '.', ','); ?></TD>
<TD nowrap>$
<?php echo number_format($total, 2, '.', ','); ?></TD>
<?php //endif; ?>
<TD nowrap>
<?php echo $details; ?></TD>
<TD nowrap>
<?php echo $add; ?></TD>
</TR>
<?php
$j++;
} // end while
?>
</TABLE>
<?php
} // end of if numberOfRows > 0
?>
<div id="view">
<a href="works4.php?aView1=on">Agent View</a>
</div>
</body>
</html>