Jump to content

SORT PROBLEM!!


schnuffelbuffel

Recommended Posts

hi everybody!

I hope somebody can help me out! I have a code as folloed:

 

<?php
$nhood = mysql_query($nh);
if($nhoods = mysql_fetch_array($nhood)){
     do{
$lists2 = $l['listingsdb_id'];

// DEFINE OUTPUT VALUES 



$bedrooms = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Bedrooms'"));
$bathrooms = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Bathrooms'"));
$neighborhood = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Neighborhoods'"));
$unitcondition = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Unit_Condition'"));
$dateavailable  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Date_Available'"));
$owner  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Owner'"));
$buildingstyle  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Building_Style'"));
$kitchen  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Kitchen'"));
$floor  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Floors'"));
$laundry  = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Laundry'"));
$address = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Detail_Address'"));
$rent = mysql_fetch_array(mysql_query("SELECT listingsdbelements_field_value FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists2' AND listingsdbelements_field_name = 'Rent'"));

?>



<tr bgcolor="<?php echo altcolor()?>">

<td class="listings"><?php echo($rent['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($bedrooms['listingsdbelements_field_value'])?></td>
<td class="listings"><?php echo($bathrooms['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($dateavailable['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($unitcondition['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($buildingstyle['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($kitchen['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($laundry['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($floor['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($neighborhood['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($address['listingsdbelements_field_value'])?></td>
    <td class="listings"><?php echo($owner['listingsdbelements_field_value'])?></td>

</tr>



<?php 
     }while ($nhoods = mysql_fetch_array($nhood));
     
}
?>

 

 

Is there any way I can order this by rent, bedrooms, bathrooms, kitchen, date available and so on...?

 

THX a lot!!

SB

Link to comment
Share on other sites

hi,

thanks for the replies. I'm sorry fenway but I do not exactly know what you mean by using a single statement?

 

Well I guess I have to explain the whole case! I have a table that looks like this:

 

TABLE name = default_en_listingsdbelements

listindsdb_id           listingsdbelements_field_name          listingsdbelements_field value
       1                              Rent                                            1500
       1                              Bedrooms                                          2
       1                              Bathrooms                                         1
       1                              Neighborhoods                              Lakeview
       1                              Address                                   3660 N Lake Shore Drive
       2                              Rent                                            2000
       2                              Bedrooms                                          3
       2                              Bathrooms                                         2
       2                              Neighborhoods                              Lincoln Park
       2                              Address                                   2300 N Sheffield
       3                              Rent                                              980
.... and so on....!

 

Unfortunately I cannot change the tables structure since it was organized this way. What I try to do is to search this table for certain values. Let's say we search for a 2 Bedrooms / 1 Bathrooms in Lakeview. So we can see that the apartment with the listings_id = 1 fits the search criteria.  I now  want all the output values for listing 1 like Rent price, Neighborhood, Address and so on...

 

So far I did this:

 

1. Step: I get all the listings_id numbers with all the values selected from the search form

$listid ="SELECT listingsdb_id, COUNT(*) AS Total FROM default_en_listingsdbelements WHERE (listingsdbelements_field_value = '$bedrooms' OR 
listingsdbelements_field_value = '$bathrooms' OR (listingsdbelements_field_name = 'Rent' AND (listingsdbelements_field_value >= $rentmin AND 
listingsdbelements_field_value <= $rentmax))) GROUP BY listingsdb_id HAVING Total = 3";


 

2.Step: I look now for the neighborhoods selected from a multiple selection form. The selections are stored in the array "neighborhoods[]"

 

if(strlen($_POST['neighborhoods']) > 0){ 
$listsid = mysql_query($listid);
if($l = sort(mysql_fetch_array($listsid))){
     do{


$lists = $l['listingsdb_id'];

$nh = "SELECT listingsdb_id FROM default_en_listingsdbelements WHERE listingsdb_id = '$lists'";

if(strlen($neighborhoods[0]) > 0){
$nh .= "AND listingsdbelements_field_value ='$neighborhoods[0]'";
}

if(strlen($neighborhoods[1]) > 0){
$nh .= "OR (listingsdb_id = '$lists' AND listingsdbelements_field_value ='$neighborhoods[1]')";
}

...and so on for each neighborhood selected.

 

3. Step: Define the values for the output:

 

$nhood = mysql_query($nh);
if($nhoods = mysql_fetch_array($nhood)){
     do{
$lists2 = $l['listingsdb_id'];

plus the code I posted above!

 

Well it shows me all the listings I was searching for but I need a way to sort them by the output values like Rent, Neighborhoods, Address...

I have to say that I started to learn php and sql 2-3 weeks ago and do not have lots of experiences. I was proud of myself I came that far but I'm sure there is an easier and more effective way to come to the same result.

 

However, I would truly appreciate if somebody could give me a hint how I can sort the output values!

 

Thanks

Link to comment
Share on other sites

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.