Page 1 of 1

Variables outside the function in function

Posted: Tue Aug 22, 2006 8:17 pm
by KillaH425
Hi,

I am making a simple search result page and for some reason, I am having a day long brainfart and I need some help as to why my code isn't working at all. I want first to retrieve the results using a query, and then count them. Then, by setting the variable $display_limit to 20, I want to make it so that only 20 results are shown, and then a link to the next page with the next 20. Next I want to order the results in order of their package number, set in a different table than the one that contains the search results. The two tables are linked by two different id's, labeled PROP_ID ($prop_id) and REGISTRATION_ID ($id, same as $user_id). For some reason I cannot figure out how to do this easily because once I break it down, I realize that the one thing that will make it work will change everything. If you need any more explination as to how it is supposed to work, just ask. Here is the code that contains the function, and any other stuff:

Code: Select all

<?php
if ($_SESSION['PAGE_NAME'] == "searchpage.php") {
    $query2 = "SELECT * FROM prop_info WHERE CONTINENTS='North America' AND COUNTRY='United States' AND STATE='$statefull' AND DISPLAY='yes'";
} else {
    $query2 = "SELECT * FROM prop_info WHERE CONTINENTS='$con_name' AND COUNTRY='$country_name' $aa AND DISPLAY='yes'";
}
$result2 = mysql_query($query2);
$display_limit = '20';
$items_count = mysql_num_rows($result2);
if(isset($_GET['page_count'])) {
    $page_count = $_GET['page_count'];
} else {
    if($items_count > $display) {
        $page_count = ceil ($items_count/$display_limit);
    } else {
        $page_count = 1;
    }
}
if(isset($_GET['start'])){
    $start = $_GET['start'];
} else {
    $start = 0;
}
$query = $query2." LIMIT $start,$display_limit";
echo $query;
$result = mysql_query($query);
//------------------- To show Search Result ------------------------------------
function view_property() {
    global $query;
    $query = $GLOBALS["query"];
    $result = mysql_query($query);
    $int = 0;
    $prop_array = array();
    $user_array = array();
    while ($rw = mysql_fetch_array($result)) {
        $user_array[$int] = $id = $rw[0];
        $prop_array[$int] = $prop_id = $rw[17];
        $int++;
    if ($int != 0) {
        $count = count($prop_array);
//===========================  Platinum  =================================//
        for ($int_1 = 0; $int_1 < $count; $int_1++) {
            $query_pay = "SELECT * FROM payment WHERE PROP_ID=$prop_id AND USER_ID=$id AND PAY_STATUS='true' ORDER BY PACKAGE ASC";
            $result_pay = mysql_query($query_pay);
            while ($rw2 = mysql_fetch_array($result_pay)) {
                //$prop_id = $rw[18];
                //$id = $rw[0];
                $package = $rw2[9];
            }
            $num_pay = 0;
            $query_2 = "SELECT * FROM prop_info WHERE REGISTRATION_ID=$id AND PROP_ID=$prop_id";
            $result_2 = mysql_query($query_2);
            $int = 0;
            while ($row = mysql_fetch_array($result_2)) {
                $int++;
                $query1 = "SELECT * FROM images_info WHERE REGISTRATION_ID=$id AND PROP_ID=$prop_id";
                $result1 = mysql_query($query1);
                $a = 0;
                while ($row1 = mysql_fetch_array($result1)) {
                    $a++;
                    $image_name = $row1[1];
                }
                if ($a == 0) {
                    $image_name = "imgnotavailable.jpg";
                }
                if ($int >= $start && $int <= $display_limit) {
                    if ($package == "1") {
                ?>
                <tr class="boldrow">
                <td width="101" class="first">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php  echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>">Premium Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php  echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61" class="last">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
                <tr>
                <td width="100%" height="10" colspan="8">*</td>
                </tr>
                <?php
//================================== Gold ==================================//
                    } elseif ($package == "2") {
                ?>
                <tr class="gold">
                <td width="101" valign="middle" align="center">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>">Gold Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
                <tr>
                <td width="100%" height="10" colspan="8">*</td>
                </tr>
                <?php
//============================== Silver ===================================//
                    } elseif ($package == "3") {
                ?>
                <tr>
                <td width="101">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>&cp=<? echo $c_page;?>">Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
                <tr>
                <td width="100%" height="10" colspan="8">*</td>
                </tr>
                <?php
                    }
                }
            }
        }
    }
}
}
//databaseClose();
?>

Posted: Wed Aug 23, 2006 5:55 pm
by Ollie Saunders
I got bored reading that first paragraph you posted. Can you show me something that specifically isn't working and then I can help you find a solution.

Posted: Wed Aug 23, 2006 6:55 pm
by KillaH425
Actually, I can spare you. I just figured it out, after a long day of constantly trying new things, it finally clicked in my head. Check it out here. This is the final PHP code:

Code: Select all

<?php
error_reporting(E_ALL);
if ($_SESSION['PAGE_NAME'] == "searchpage.php") {
	$query2 = "SELECT REGISTRATION_ID, G_LOCATION, PROP_NAME, COM_ASSO, RATE_FROM, RATE_TO, CURRENCY, RATE_PER, PROP_TYPE, BED_ROOM, BATH_ROOM, SLEEP_ROOM, PETS, POOL, JACUZZI, WEB_SITE, DISPLAY, prop_info.PROP_ID, CONTINENTS, COUNTRY, STATE, REGION, CITY, PACKAGE FROM prop_info, payment WHERE prop_info.PROP_ID=payment.PROP_ID AND prop_info.REGISTRATION_ID=payment.USER_ID AND payment.PAY_STATUS='true' AND DISPLAY='yes' $where ORDER BY payment.PACKAGE";
} else {
	$query2 = "SELECT REGISTRATION_ID, G_LOCATION, PROP_NAME, COM_ASSO, RATE_FROM, RATE_TO, CURRENCY, RATE_PER, PROP_TYPE, BED_ROOM, BATH_ROOM, SLEEP_ROOM, PETS, POOL, JACUZZI, WEB_SITE, DISPLAY, prop_info.PROP_ID, CONTINENTS, COUNTRY, STATE, REGION, CITY, PACKAGE FROM prop_info, payment WHERE prop_info.PROP_ID=payment.PROP_ID AND prop_info.REGISTRATION_ID=payment.USER_ID AND payment.PAY_STATUS='true' AND CONTINENTS='$con_name' AND COUNTRY='$country_name' $aa AND DISPLAY='yes' ORDER BY payment.PACKAGE";
}
$display_limit = '20';
$items_count = mysql_num_rows(mysql_query($query2));
if (isset($_GET['page_count'])) {
	$page_count = $_GET['page_count'];
} else {
	if ($items_count > $display_limit) {
		$page_count = ceil ($items_count/$display_limit);
	} elseif ($items_count == 0) {
		$page_count = 0;
	} else {
		$page_count = 1;
	}
}
if ($page_count > 1) {
	$search_result = $page_count." pages,";
} elseif($page_count == 1) {
	$search_result = $page_count." page,";
} else {
	$search_result = " No results found.";
}
if ($items_count > 1) {
	$entries = $items_count." entries";
} elseif($items_count == 1) {
	$entries = $items_count." entry";
} else {
	$entries = "Try changing your search criteria.";
}
if (isset($_GET['start'])){
	$start = $_GET['start'];
} else {
	$start = 0;
}
$query = $query2." LIMIT $start,$display_limit";
//echo $query;
//view_property($query);
//------------------- To show Search Result ------------------------------------
function view_property($query) {
	$result = mysql_query($query);
	$int = 0;
	$prop_array = array();
	$user_array = array();
	$pack_array = array();
	while ($row = mysql_fetch_array($result)) {
		$user_array[$int] = $id = $row[0];
		$prop_array[$int] = $prop_id = $row[17];
		$pack_array[$int] = $package = $row[23];
		$int++;
//===========================  Platinum  =================================//
		$query1 = "SELECT * FROM images_info WHERE REGISTRATION_ID=$id AND PROP_ID=$prop_id";
		$result1 = mysql_query($query1);
		$a = 0;
		while ($row1 = mysql_fetch_array($result1)) {
			$a++;
			$image_name = $row1[1];
		}
		if ($a == 0) {
			$image_name = "imgnotavailable.jpg";
		}
		if ($package == 1) {//&cp=<? echo $c_page; && $int <= 20
                ?>
                <tr class="boldrow">
                <td width="101" class="first">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php  echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>">Premium Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php  echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61" class="last">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
				<tr>
				<td width="100%" height="10" colspan="8">&nbsp;</td>
				</tr>
				<?php
//================================== Gold ==================================//
		} elseif ($package == 2) {
                ?>
                <tr class="gold">
                <td width="101" valign="middle" align="center">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>">Gold Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
				<tr>
				<td width="100%" height="10" colspan="8">&nbsp;</td>
				</tr>
                <?php
//============================== Silver ===================================//
		} elseif ($package == 3) {
                ?>
                <tr>
                <td width="101">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>"><img border="0" src="photo/<?php echo $image_name; ?>" width="100" height="63"></a></td>
                <td width="111">
                <p align="center"><font size="2"><?php echo $row[2]; ?></font></p>
                <p align="center"><font size="2">
                <a href="view_detail_property.php?prop_id=<?php echo $prop_id; ?>">Property Details</a></font></td>
                <td width="117">
                <p align="center"><font size="2"><?php echo $row[1]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[9]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[10]; ?></font></td>
                <td>
                <p align="center"><font size="2"><?php echo $row[11]; ?></font></td>
                <td width="55">
                <p align="center"><font size="2"><?php echo $row[5]; ?></font></td>
                <td width="61">
                <p align="center"><font size="2"><?php echo $row[7]; ?></font></td>
                </tr>
				<tr>
				<td width="100%" height="10" colspan="8">&nbsp;</td>
				</tr>
                <?php
		}
	}
}
//databaseClose();
?>
What I did was I figured out how to query the whole first table (prop_info) and simply add on one column of the second table (payment) and sort by that column. It was right in front of me the whole time, as I read this section about joining in two different books and that was the first example in both, and the one I probably read the most. I reduced the 4 queries to really only 2.