Skip to content

Commit 58d6e12

Browse files
committed
selectJoin method improvements
1 parent 21e6639 commit 58d6e12

File tree

1 file changed

+46
-11
lines changed

1 file changed

+46
-11
lines changed

lib/Db.class.php

Lines changed: 46 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -302,13 +302,14 @@ public function select($table, $where = [], $limit = null, $start = null, $order
302302
*
303303
* @param array $table_cols - items of the db tables we are retreiving the rows from and joining
304304
* @param array $conditions - associative array representing the WHERE clause filters
305+
* @param array $where (optional)
305306
* @param int $limit (optional) - the amount of rows to return
306307
* @param int $start (optional) - the row to start on, indexed by zero
307308
* @param array $order_by (optional) - an array with order by clause
308309
*
309310
* @return mixed - associate representing the fetched table row, false on failure
310311
*/
311-
public function selectJoin($table_cols = [], $conditions = [], $limit = null, $start = null, $order_by = [])
312+
public function selectJoin($table_cols = [], $conditions = [], $where = [], $limit = null, $start = null, $order_by = [])
312313
{
313314
// building query string
314315
$sql_str = 'SELECT ';
@@ -329,20 +330,44 @@ public function selectJoin($table_cols = [], $conditions = [], $limit = null, $s
329330

330331
}
331332

332-
$first = true;
333-
$i = 0;
334-
$table_first = null;
333+
if (!function_exists('array_key_first')) {
334+
function array_key_first(array $arr) {
335+
foreach($arr as $key => $unused) {
336+
return $key;
337+
}
338+
return NULL;
339+
}
340+
}
335341

336-
foreach ($table_cols as $table_name => $columns)
342+
$sql_str .= ' FROM ' . $this->prefix . array_key_first($table_cols);
343+
344+
foreach ($conditions as $cond_tbl => $cond_cols)
337345
{
338-
if ($first)
346+
$sql_str .= ' JOIN ' . $this->prefix . $cond_tbl . ' ON ' . $this->prefix . $cond_cols[0] . ' = ' . $cond_cols[1];
347+
}
348+
349+
$add_and = false;
350+
351+
if (!empty($where) and is_array($where))
352+
{
353+
// append WHERE if necessary
354+
$sql_str .= ' WHERE ';
355+
// add each clause using parameter array
356+
foreach ($where as $key => $val)
339357
{
340-
$sql_str .= ' FROM ' . $this->prefix . $table_name;
341-
$table_first = $table_name;
342-
$first = false;
343-
continue;
358+
// only add AND after the first clause item has been appended
359+
if ($add_and)
360+
{
361+
$sql_str .= ' AND ';
362+
}
363+
else
364+
{
365+
$add_and = true;
366+
}
367+
368+
// append clause item
369+
$sql_str .= $key . ' = :' . $key;
344370
}
345-
$sql_str .= ' JOIN ' . $this->prefix . $table_name . ' ON ' . $this->prefix . $table_first . '.' . $conditions[$i++] . ' = ' . $this->prefix . $table_name . '.id';
346371
}
347372

348373
// add the order by clause if we have one
@@ -379,6 +404,16 @@ public function selectJoin($table_cols = [], $conditions = [], $limit = null, $s
379404
}
380405

381406
$this->query = $this->dbh->prepare($sql_str);
407+
408+
if (!empty($where) and is_array($where))
409+
{
410+
// bind each parameter in the array
411+
foreach ($where as $key => $val)
412+
{
413+
$this->query->bindValue(':' . $key, $val);
414+
}
415+
}
416+
382417
$this->query->execute();
383418

384419
// now return the results, depending on if we want all or first row only

0 commit comments

Comments
 (0)