can I remove the OR from the end of foreach loop
if (isset($_POST['size'])) {
$where .= ' AND ';
$s = $_POST['size'];
$end = count($s);
foreach ($s as $ss) {
$where .= 'pSize=' . $ss;
$where .= ' OR ';
}
} else {
$s = '';
}
with your condition, i think you use IN for easy.
if (isset($_POST['size'])) {
$size = $_POST['size'];
if($size){
$where .= ' AND `pSize` IN ('.implode(",", $size).')';
}
}
this will also work in your case:
if (isset($_POST['size'])) {
$where .= ' AND ';
$s = $_POST['size'];
$end = count($s);
for($i=0;$i<count($s);$i++) {
$where .= 'pSize=' . $s[$i];
if($i != count($s)-1){
$where .= ' OR ';
}
}
} else {
$s = '';
}
One way to avoid that issue is create an array and implode.
if (isset($_POST['size'])) {
$where .= ' AND (';
$s = $_POST['size'];
$end = count($s);
//echo $end;
$aWhere = [];
foreach ($s as $ss) {
$aWhere[] = 'pSize=' . $ss;
}
$where .= implode(' OR ', $aWhere) . ')';
} else {
$s = '';
}
Maybe you need parentheses on the Where, I did it for you.
Take care with SQL injection, use prepared statements.
you can go for many ways
way 1:
you can go for for loop instead of foreach()
$a=sizeof($s);
$x=$a-1;
for($i=0;$i<$a;$i++){
if($i==$x){ $where .= 'pSize=' . $ss;
}
else{ $where .= 'pSize=' . $ss;
$where .= ' OR ';
}
}
way2 : use rtrim()
foreach ($s as $ss) {
$where .= 'pSize=' . $ss;
$where .= ' OR ';
}
$where=rtrim($where,' OR ');
There are many ways to do this. You can even use a for loop. If you'd prefer a foreach loop, you can create an iterator variable to mimic a for loop as follows:
if (isset($_POST['size'])) {
$where .= ' AND ';
$s = $_POST['size'];
$end = count($s);
$i = 0;
foreach ($s as $ss) {
$where .= 'pSize=' . $ss;
if (++$i != $end)
$where .= ' OR ';
}
} else {
$s = '';
}
Related
I want to override batchinsert, because I want to add ON DUPLICATE KEY UPDATE
$result = Yii::$app->db->createCommand()->batchInsert('product', ['asin', 'title', 'image', 'url', 'price'], $results)->execute();
public function batchInsert($table, $columns, $rows)
{
if (empty($rows)) {
return '';
}
$schema = $this->db->getSchema();
if (($tableSchema = $schema->getTableSchema($table)) !== null) {
$columnSchemas = $tableSchema->columns;
} else {
$columnSchemas = [];
}
$values = [];
foreach ($rows as $row) {
$vs = [];
foreach ($row as $i => $value) {
if (isset($columns[$i], $columnSchemas[$columns[$i]]) && !is_array($value)) {
$value = $columnSchemas[$columns[$i]]->dbTypecast($value);
}
if (is_string($value)) {
$value = $schema->quoteValue($value);
} elseif ($value === false) {
$value = 0;
} elseif ($value === null) {
$value = 'NULL';
}
$vs[] = $value;
}
$values[] = '(' . implode(', ', $vs) . ')';
}
$query = 'INSERT INTO ' . $schema->quoteTableName($table);
$duplicate = ' ON DUPLICATE KEY UPDATE ';
$last = end($columns);
reset($columns);
foreach ($columns as $i => $name) {
$columns[$i] = $schema->quoteColumnName($name);
$duplicate .= $schema->quoteColumnName($name) . ' = VALUES(' . $schema->quoteColumnName($name) . ')';
if ($name <> $last) {
$duplicate .= ', ';
}
}
$query .= ' (' . implode(', ', $columns) . ') ';
$query .= ' VALUES ' . implode(', ', $values);
$query .= $duplicate;
return $query;
}
This is working but now I need to override yii\db\QueryBuilder
If found this: Yii::$app->db->commandClass = new common\models\Command();
and I can override the command class, but the code in yii\db\Schema should be updated too
public function createQueryBuilder()
{
return new \common\models\QueryBuilder($this->db);
}
Or I need to do something completely different, how can I fix this?
According to this discussion about non standard sql commands of devs you can do following (not tested):
class MyQueryBuilder extends yii\db\mysql\QueryBuilder
{
public function batchInsert($table, $columns, $rows)
{
$sql = parent::batchInsert($table, $columns, $rows);
$sql .= 'ON DUPLICATE KEY UPDATE';
return $sql;
}
}
or you can do this:
$db = Yii::$app->db;
$sql = $db->queryBuilder->batchInsert($table, $fields, $rows);
$db->createCommand($sql . ' ON DUPLICATE KEY UPDATE')->execute();
<?php
namespace common\models;
use yii\db\mysql\QueryBuilder as baseQueryBuilder;
class QueryBuilder extends baseQueryBuilder
{
public function batchInsert($table, $columns, $rows)
{
$sql = parent::batchInsert($table, $columns, $rows);
$sql .= ' ON DUPLICATE KEY UPDATE ';
$schema = $this->db->getSchema();
$last = end($columns);
reset($columns);
foreach ($columns as $i => $column)
{
$columns[$i] = $schema->quoteColumnName($column);
$sql .= $schema->quoteColumnName($column) . ' = VALUES(' . $schema->quoteColumnName($column) . ')';
if ($column <> $last) {
$sql .= ', ';
}
}
return $sql;
}
}
$db = Yii::$app->db;
$queryBuilder = new \common\models\QueryBuilder(Yii::$app->db);
$query = $queryBuilder->batchInsert('product', ['asin', 'title', 'image', 'url', 'price'], $results);
$db->createCommand($query)->execute();
I am trying to create a class to save time on cleaning up my variables before sending them to the database to prevent sql injections. The basic systems is working now but i cant seem to get a where/or statement implemented. Does anyone know how to add this?
<?php
class Database {
private $db = '';
private $database = '';
function __construct($settings) {
$this->db = new mysqli('127.0.0.1', $settings['mysql_user']['username'], $settings['mysql_user']['password']);
$this->database = $settings['mysql_user']['database'];
print_r('Database Loaded!<br/>');
}
public function query($method, $database, $rows, $params, $where = array(), $or = array()) {
$count = 0;
$amount = count($rows);
$final_rows = '';
$final_data = '';
$bind_names = array();
$bind_names[0] = '';
$param_types = array(
"int" => "i",
"string" => "s",
"double" => "d",
"blob" => "b"
);
switch($method) {
case 'INSERT':
foreach ($rows as $row) {
$count = $count + 1;
$final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : '');
$final_data .= '?' . ($count != $amount ? ', ' : '');
}
$stmt = $this->db->prepare('INSERT INTO `' . $this->database . '`.`' . $database . '` (' . $final_rows . ') VALUES (' . $final_data . ')');
for ($i = 0; $i < count($params); $i++)
{
$bind_name = 'bind'.$i;
$$bind_name = $params[$i][1];
$bind_names[0] .= $param_types[$params[$i][0]];
$bind_names[] = &$$bind_name;
}
call_user_func_array( array ($stmt, 'bind_param'), $bind_names);
return $stmt->execute();
break;
case 'UPDATE':
foreach ($rows as $row) {
$count = $count + 1;
$final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : '');
$final_data .= '?' . ($count != $amount ? ', ' : '');
}
$stmt = $this->db->prepare('UPDATE `' . $this->database . '`.`' . $database . '` SET ' . $final_rows . '');
for ($i = 0; $i < count($params); $i++)
{
$bind_name = 'bind'.$i;
$$bind_name = $params[$i][1];
$bind_names[0] .= $param_types[$params[$i][0]];
$bind_names[] = &$$bind_name;
}
call_user_func_array( array ($stmt, 'bind_param'), $bind_names);
return $stmt->execute();
break;
case 'REPLACE':
foreach ($rows as $row) {
$count = $count + 1;
$final_rows .= '`' . $row . '`' . ($count != $amount ? ', ' : '');
$final_data .= '?' . ($count != $amount ? ', ' : '');
}
$stmt = $this->db->prepare('REPLACE INTO `' . $this->database . '`.`' . $database . '` (' . $final_rows . ') VALUES (' . $final_data . ')');
for ($i = 0; $i < count($params); $i++)
{
$bind_name = 'bind'.$i;
$$bind_name = $params[$i][1];
$bind_names[0] .= $param_types[$params[$i][0]];
$bind_names[] = &$$bind_name;
}
call_user_func_array( array ($stmt, 'bind_param'), $bind_names);
return $stmt->execute();
break;
}
}
}
?>
Going to make a few assumptions, but first I'll recommend you use an ORM before whipping up your own solution. Here's a good list of PHP libraries (I've linked to the database sections, which includes some very well done stand-alone ORMs https://github.com/ziadoz/awesome-php#database)
That being said I'm going to assume the $where and $or arrays are both for the WHERE construct and the items in $where are combined via AND and the $or is combined via OR.
Because you didn't describe what kind of output you were looking for I'm also assuming your $where and $or are key/value pairs which translates to "key=value AND key=value AND (key=value OR key=value)".
DISCLAIMER: This example is kind of hacky, but is the shortest/simplest way to get the example across.
$whereQuery = '';
foreach ($where as $key => $value) {
$whereQuery .= "$key = $value AND";
}
if ($or !== array()) {
$whereQuery .= '(';
foreach ($or as $key => $value) {
$whereQuery .= "$key = $value OR";
}
}
if ($whereQuery !== '') {
if (($temp = strlen($whereQuery) - strlen('AND')) >= 0 && strpos($whereQuery, 'AND', $temp) !== false) {
$whereQuery = substr($whereQuery, -4);
} else {
$whereQuery = substr($whereQuery, -3) . ')';
}
$whereQuery = "WHERE $whereQuery";
}
You can then stick the $whereQuery at the end of an UPDATE or SELECT. Even if $where and $or are empty it'll still work.
You could move the loops into functions and make it recursive if the $value was another array so you could create more complex WHERE statements.
This is my function:
$words = explode('. ',$desc);
$out = '';
foreach ($words as $key => $value) {
$out .= $value;
$rand = rand(0, 4);
if ($rand == 2) {
$out .= "\n";
} else {
$out .= ' ';
}
}
In short it's inserting new lines after random dots, but in this case dots are removed.
How could I do explode('. ',$desc), and leave dots in where they are?
Just put them back in when you concatenate.
$words = explode('. ',$desc);
$out = '';
foreach ($words as $key => $value) {
$out .= $value.'.';
$rand = mt_rand(0, 4);
if ($rand == 2) {
$out .= "\n";
} else {
$out .= ' ';
}
}
You should also use mt_rand(), it is a much better version of rand() unless you like not really random results.
Positive look behind without regex subject.
$words = preg_split('/(?<=\.)(?!\s*$)/', $desc);
Split at any non-character with a dot behind it.
Try this code..
$words = explode('. ',$desc);
$out = '';
foreach ($words as $key => $value) {
$out .= $value;
$rand = rand(0, 4);
if ($rand == 2) {
$out .= "<br>";
} else {
$out .= ' ';
}
}
Trying to loop through a querystring in php but only getting last value. What should I be doing to get all values?
example:
querystring = ?style=ranch&style=barn&style=colonial
php:
$sqlStyle = "SELECT DISTINCT COUNT(*) as count FROM houses_single ";
$i = 1;
foreach ($_GET as $key => $value) {
if ($i == 1){
$sqlStyle .= "where ";
}else{
$sqlStyle .= " and ";
}
$sqlStyle .= $key . " like '%" . $value ."%'";
$i++;
}
echo $sqlStyle;
Result:
SELECT DISTINCT COUNT(*) as count FROM houses_single Where Houses like '%colonial%'
The query parameter "style" is an array in this case and must be identified by square brackets - if not, the last key=value pair will overwrite the others.
?style[]=ranch&style[]=barn&style[]=colonial
$_GET['style'] is an array then you can loop over by using foreach:
foreach ($_GET['style'] as $value) {
// ...
}
if 'style' is not the only parameter you want to add, you can use a is_array() check in the foreach loop:
foreach ($_GET as $key => $value) {
if ($i == 1){
$sqlStyle .= "where ";
}else{
$sqlStyle .= " and ";
}
if(is_array($value)) {
$sec = array();
foreach($value as $second_level) {
$sec[] = $key . " LIKE '%" . $second_level."%'";
}
$sqlStyle .= implode(' AND ', $sec);
}
else {
$sqlStyle .= $key . " LIKE '%" . $value ."%'";
}
$i++;
}
echo $sqlStyle;
alternative without foreach:
<?php
$statement = "SELECT DISTINCT COUNT(*) as count FROM `houses_single`";
if(is_array($_GET)) {
$statement .= ' WHERE';
// create copy to keep the $_GET array
$add_where = $_GET;
array_walk(function($elem,$key){
is_array($elem) {
return implode(' AND ', array_map(function($sec) using ($key) {
return "$key LIKE '%$sec%'";
}, $elem);
}
else {
return "$key LIKE '%$elem%'";
}
},$add_where);
$statement .= implode(' AND ', $add_where);
}
(codes are untested)
Sidenode about safety: I hope you won't use this code snippet you provided in productive environment without any escaping of the parameters.
I like to sort an associative array into two div's based on a value.
Here's what I have now:
function showDivs($array) {
/*
* sort en group by
*/
$luxury = false;
$no_luxury = false;
$return = '';
while (list($key, $value) = each($array)) {
if ($value["MinimumRate"] < 500) {
if ($no_luxury === false) {
$return .= '<div id="no_luxury" >';
}
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
if ($no_luxury === false) {
$return .='</div>';
$no_luxury = true;
}
}
if ($value["MinimumRate"] > 500) {
if ($luxury === false) {
$return .='<div id="luxury" >';
}
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
if ($luxury === false) {
$return .='</div>';
$luxury = true;
}
}
}
return $return;
}
My code doesn't work correctly.
The div is being created and closed on the first item only. I think you meant to open it on start and close it in the end. Try this:
function showDivs($array) {
/*
* sort en group by
*/
$luxury = array();
$not_luxury = array();
foreach ($array as $value) {
if ($value["MinimumRate"] > 500) {
$luxury[] = $value;
} else {
$not_luxury[] = $value;
}
}
$return = '<div id="no_luxury" >';
foreach ($luxury as $value) {
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
}
$return .= '</div>';
$return .= '<div id="luxury" >';
foreach ($luxury as $value) {
$return .= $value['Description'] . " : " . $value["MinimumRate"] . "<br />";
}
$return .= '</div>';
return $return;
}
If the problem is when MinimumRate = 500, you whould do:
if ($value["MinimumRate"] <= 500) // this is your first 'if' in your code
or
if ($value["MinimumRate"] >= 500) // this is the second 'if' in your code