简体   繁体   中英

php mysql adding values to array based on IF statements

Using Joomla, having issues trying to build a mySQL query based on URL arguments. The code I have looks like this:

    $db =& JFactory::getDBO();
    $hpprice = JRequest::getVar('hprice');
    $lprice = JRequest::getVar('lprice');
    $city = JRequest::getVar('city');
    $zip = JRequest::getVar('zip');
    $bdrms = JRequest::getVar('bdrms');
    $bths = JRequest::getVar('bths');

    $query = "SELECT * FROM " . $db->nameQuote('#__mls') . " WHERE 1=1";
    $clauses = array();
    if ($zip != null) {
        $clauses[] = $db->nameQuote('MSTZIP') . " = " . $db->quote($zip);
    }
    if ($city != null) {
        $clauses[] = $db->nameQuote('MSTCITY') . " = '" . $db->quote($city) . "'";
    }
    if ($bdrms != null){
        $clauses[] = $db->nameQuote('MSTBDRMS')." >= ".$db->quote($bdrms);
    }
    if ($bths != null){
        $clauses[] = $db->nameQuote('MSTBATHS') . " >= " . $db->quote($bths);
    }
    if ($lprice != null){
        $clauses[] = $db->nameQuote('MSTLISTPRC') . " BETWEEN " . $db->quote($lprice) . " AND " . $db->quote($hprice);
    } 

    $query .= implode(" AND ", $clauses);

    $db->setQuery($query);
    $table = $db->loadRowList();
    return $table;

So, as you can see it, adds arguments to the mySQL query based on whether or not arugments exist in the URL. What I can't wrap my head around is building the array and imploding it.

Whenever I put an argument in the URL, all the table items populate. When I try to pass an argument, it comes up null. You can see this in action here . If you add another argument like zip to the URL, everything comes up NULL .

I think the problem is this " WHERE 1=1" .try to change this to this- " WHERE 1=1 " . Because the final query will be appended to this and you'll not get the desire result.For confirmation also echo $query see if it's a correct query.one more thing is '" . $db->quote($city) . "'" .remove '' as you are already adding this by a function.

//Update:

Better to use where method

Let me know if this does not work.

Not sure how this is diffent than your previous question.

$query->select('*"); $query->from($db->nameQuote('#__mls'));

 $query->where('1 = 1', AND);
if ($zip != null)
{
  $query->where (.$db->nameQuote('MSTZIP')." = ".$db->quote($zip)); 
}
if ($city != null) 
{ 
   $query->where($db->nameQuote('MSTCITY')." = '".$db->quote($city)); 
} 

Etc

There is no need for you to build any array; that it the whole point of having a databasequery api.

Final script ended up looking like this:

    $db =& JFactory::getDBO();
    $hprice = JRequest::getVar('hprice');
    $lprice = JRequest::getVar('lprice');
    $city = JRequest::getVar('city');
    $zip = JRequest::getVar('zip');
    $bdrms = JRequest::getVar('bdrms');
    $bths = JRequest::getVar('bths');

    $query = "SELECT * FROM " . $db->nameQuote('#__mls') . " WHERE 1=1 AND ";
    $clauses = array();
    if ($zip != null) {
        $clauses[] = "MSTZIP = " . $zip;
    }
    if ($city != null) {
        $clauses[] = "MSTCITY = " . $db->quote($city);
    }
    if ($bdrms != null){
        $clauses[] = "MSTBDRMS >= " . $bdrms;
    }
    if ($bths != null){
        $clauses[] = "MSTBATHS >= " . $bths;
    }
    if ($lprice != null){
        $clauses[] = "MSTLISTPRC BETWEEN " . $lprice . " AND " . $hprice;
    } 

    $query .= implode(" AND ", $clauses) . ";";

    $db->setQuery($query);
    $table = $db->loadRowList();
    return $table;

I ended up getting rid of nameQuote and quote except where needed/applicable. The script model I was working off of was from some tutorial. It worked for what I was doing previously, but not now for some reason. Last step would be to make the initial AND conditional, but that shouldn't take much. At least the framework is there now. Thanks to all who helped.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM