Frage

I'm trying out to use joomla to do my query code. But there is some error code shown that my courseID is in array and can't be use. Sorry i was still new to joomla and php >.<

Here is my code:

$campusID = $_POST['campusID'];

$courseID = $_POST['courseID'];

$from= $_POST['from'];

$to= $_POST['to'];

    // Get default database object
$db =JFactory::getDBO();

// Get a new JDatabaseQuery object
$query = $db->getQuery(true);



foreach($courseID as $courseID1){

// Build the query
$query->select($db->quoteName('startdate'));
$query->from($db->quoteName('intake'));
$query->where($db->quoteName('campusid').'='. $db->quote($campusID));
$query->where($db->quoteName('courseid').'='. $db->quote($courseID1));

// Set the query for the DB oject to execute
$db->setQuery($query);
// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList();

if($result){

echo "GOOD";

}

else{

echo "Error";

}

}

Eventually, $courseID is the value that i submitted from another page which the value is come from a check box and carry multiple value. What should i do to get the courseID value with array in query? ( I had try to edit the code, but no luck...it still echo me "Error".

War es hilfreich?

Lösung

By default

$query->where($condition)

appends conditions using an AND when you use several where's. So your code will produce something like

campusid = XX AND courseid = YY AND campusid = XX AND courseid = ZZ ...

So it doesn't work because courseid cannot be YY and ZZ at the same time.

You use a trick to solve this, using explode but before exploding, we must sanitize the received data.

This code is not tested :

$tmpIds = array();

foreach($courseID as $cId){
  $tmpIds[] = $db->quote($cId);   // sanitize the input
}

$courseID1 = explode($tmpIds,",");

$query->select($db->quoteName('startdate'));
$query->from($db->quoteName('intake'));
$query->where($db->quoteName('campusid').'='. $db->quote($campusID));
$query->where($db->quoteName('courseid').' IN ('. $courseID1 . ')';

// Set the query for the DB oject to execute
$db->setQuery($query);

// Get the DB object to load the results as a list of objects
$results = $db->loadObjectList();

if($result){
   echo "GOOD";    
} else {
   echo "Error";
}

As pointed on the notes below, you should avoid using $_POST, $_GET, $_FILES, etc... directly. Since Joomla! 2.5 JInput class (on 1.5 to 1.7 this was done with JRequest) is provided to access those variables.

Regards,

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top