filter for selected record - mysql

I selected a product and then I have this product in a table, when I want to select other product I have to filter the suggestion of the products it should show the options without the product to add to the table. But I dont know how, if you know an option or an idea I'd appreciate it.
query:
$Input = $request->key;
$almacenOrigen = $request->almacenOrigen;
$lengthSku = 16;
$Sku = substr($Input, 0, $lengthSku);
$name = DB::table('products')
->leftJoin('line_products', 'products.lineProduct_id', '=', 'line_products.id')
->leftJoin('inventories', 'products.id', '=', 'inventories.product_id')
->leftJoin('warehouses', 'inventories.warehouse_id', '=', 'warehouses.id')
->where('inventories.status_id', 1)
->where('products.sku', $Sku)
->where('warehouses.id', $almacenOrigen)
->whereNull('products.deleted_at')
->select(
'products.sku',
'products.reference',
'products.id',
'products.name as nameP',
'line_products.skuLength',
'line_products.expirationLength',
'line_products.lotLength',
'line_products.traceabilityLength',
'line_products.name as nameL',
'inventories.lot',
'inventories.warehouse_id',
'inventories.id as inventory_id',
'inventories.traceability'
)
->get();
$html = '';
if (!empty($name)) {
$Expiration = substr($Input, $name[0]->skuLength, $name[0]->expirationLength);
$yearToday = substr(Carbon::now(), 0, 2);
$year = $yearToday . substr($Expiration, 2, 2);
$month = substr($Expiration, 4, 2);
$day = substr($Expiration, 6, 2);
$Expiration = $year . '-' . $month . '-' . $day;
$Lot = substr($Input, $name[0]->skuLength + $name[0]->expirationLength, $name[0]->lotLength);
$Lot = substr($Lot, 2);
$Traceability = substr($Input, $name[0]->skuLength + $name[0]->expirationLength + $name[0]->lotLength, $name[0]->traceabilityLength);
$Traceability = substr($Traceability, 2);
foreach ($name as $value) {
$html .= '
<div>
<a style="color: #000000" class="suggest-element" exist="1" inventory_id="'.($value->inventory_id).'" lot="'.($value->lot).'" warehouse_id="'.($value->warehouse_id).'" "'
. '" sku="' . ($value->sku)
. '" reference="' . ($value->reference)
. '" caducidad="' . ($Expiration)
. '" lote="' . ($Lot)
. '" trazabilidad="' . ($Traceability)
. '" nameL="' . ($value->nameL)
. '" nameP="' . ($value->nameP)
. '" data="' . ($value->sku . $Expiration . $Lot . $Traceability)
. '" id="' . $value->id . '">'
. ($value->inventory_id . "-" .$value->sku .$Expiration . $Lot . $Traceability ) .
'</a>
</div>'
;
Example:
This are the suggestions, if I select id = 1 this is added in a table then when I want to select another the id=1 have not to appear.
|id |---- sku ----|
| 1 |108888893024899|
| 2 |108888893024896|
| 3 |108888893024895|

Related

How to define where condition (ex: userid != 0) in this below query?

How to define where condition in this below query?form the database except the zero value need to shown
private function totalVisitedPagesPerUser() {
$query = "SELECT COUNT(*), " . $this->_db->quoteName('customer_name') . "," .
"\n MAX(". $this->_db->quoteName('visit_timestamp') . ")," . $this->_db->quoteName('browser') . "," .
$this->_db->quoteName('os') . "," .
$this->_db->quoteName('session_id_person') . "," .
$this->_db->quoteName('ip') . "," .
"\n SUM(" . $this->_db->quoteName('impulse') . ")," .
$this->_db->quoteName('geolocation') .
"\n FROM ( SELECT * FROM #__realtimeanalytics_serverstats " . $this->whereQuery . " ORDER BY `visit_timestamp` DESC) AS INTABLE" .
$this->whereQuery .
"\n GROUP BY " . $this->_db->quoteName('session_id_person') .
"\n ORDER BY " . $this->_db->quoteName('customer_name');
$this->_db->setQuery($query);
$results = $this->_db->loadRowList();
if ($this->_db->getErrorNum()) {
throw new JRealtimeException(JText::sprintf('COM_JREALTIME_ERROR_RECORDS', $this->_db->getErrorMsg()), 'error');
}
return $results;
}
you can prepare where condition variable by checking conditions before query start, than append that variable at that point like:
$wherecondtion = "Where 1=1";
if($condition1){$wherecondtion."AND (add required filter 1)"}
if($condition2){$wherecondtion."AND (add required filter 2)"}
your subquery will be like:
"( SELECT * FROM #__realtimeanalytics_serverstats " . $wherecondtion . " ORDER BY `visit_timestamp` DESC)"
Is this the thing you needed or something different thing?

mysql query - to echo groups of products with foreach in stylable divs

hope somone can help me - I have this code:
$query = "SELECT * FROM `products` WHERE `category` = 100 AND `showme` = 1 `ORDER BY `nr` ASC";`
$result = mysql_query($query);
while($row = mysql_fetch_object($result)){
echo '<div class="'.$row->design.'">
<img src="'.$row->img.'" width="100%"><br>
<span><b>'.$row->name.'</b></span><br><span>'.$row->descr.'</span><br />
<span>'.$row->preprice.' </span><span>'.$row->price.'</span><span> '.$row->unit.'</span></div>
';
}
It displays like this: http://gartenundhof.de/gartenundhof-produkte5.php
It should look about like:http://gartenundhof.de/gartenundhof-produkte.php (which has 3 separate queries)
"SELECT * FROM 'products' ORDER BY 'category' ASC";
Try this
You can try it:
$query = "SELECT * FROM `products` WHERE `showme` = 1 `ORDER BY category,`nr` ASC";
$result = mysql_query($query);
$category = -1;
while($row = mysql_fetch_object($result)){
if($category != $row->category) {
$category = $row->category;
eho "<h2>$category</h2>";
}
echo '<div class="'.$row->design.'">
<img src="'.$row->img.'" width="100%"><br>
<span><b>'.$row->name.'</b></span><br><span>'.$row->descr.'</span><br />
<span>'.$row->preprice.' </span><span>'.$row->price.'</span><span> '.$row->unit.'</span></div>';
}
Assuming you have 2 tables Products and Categories:
$categories_query = "SELECT * FROM categories ORDER BY id ASC";
$categories = mysql_query($categories_query);
while($category = mysql_fetch_object($categories)) {
echo "Category: " . $category->name . "<br />";
$products_query = "SELECT * FROM products WHERE category_id = " . $category->id . " AND showme = 1 ORDER BY nr ASC";
while($product = mysql_fetch_object($products_query)) {
echo "Product: " . $product->name . "<br />";
}
}

2 or 3 Checkboxes in MySQL query

I created the following SQL Query, which works fine. However, I want to add 2 other checkbox $Post's from another label. Now I'm a little bit without any idea how I can add it. Do I have to add an "AND" between the tbs queries?
Here is the code
include "db_connect.inc.php";
$sql = "SELECT * FROM profiles";
$sql .= " WHERE profilename = '". $_POST["profilename"] ."' ";
$sql .= " AND ort = '". $_POST["ort"] ."' ";
$sql .= "AND jahren = '" . $_POST["alter"] . "' ";
$tbs = array();
foreach( array( 'tb1', 'tb2', 'tb3' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`grosse` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$tbs = array();
foreach( array( 'tb4', 'tb5', 'tb6', 'tb7' ) as $tb_key )
{
if ( empty( $_POST[$tb_key] ) ) continue;
$tbs[] = "`haare` LIKE '" . $_POST[$tb_key] . "'";
}
if ( !empty( $tbs ) )
{
$sql .= ' AND ( ' . implode( ' OR ', $tbs ) . ' )';
}
$res = mysqli_query($con, $sql);
$num = mysqli_num_rows($res);
if ($num==0) echo "Kein Profil gefunden";
echo "<table border='1'>";
echo "<tr><td>Profile</td><td>Alter</td>";
echo "<td>Ort</td><td>Brustgrösse</td>";
echo "<td>Haarfarbe</td></tr>";
while ($dsatz = mysqli_fetch_assoc($res))
{
echo "<tr>";
echo "<td>" . $dsatz["profilename"] . "</td>";
echo "<td>" .$dsatz["jahren"] . "</td>";
echo "<td>" .$dsatz["ort"] . "</td>";
echo "<td>" .$dsatz["grosse"] . "</td>";
echo "<td>" .$dsatz["haare"] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
i could it resolve it. I had a wrong entry in the DB. The query works fine!

mysql column substring ()

In the function below I am implementing substring for the field $field but I am getting,
Error Notice: Error: Unknown column 'EBook_at' in 'field list'
I know there is no column like EBook_at but how substring the $field.
Expecting Output:
/image/56789011/5678901145678
public function prependText($adjustment) {
$prepend_text = $adjustment[0];
$field = $adjustment[1];
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "'," . SUBSTR($field,0,8) . "`, `" . $field . "` )");
}
It is fixed by using below query :
$this->db->query('UPDATE ' . DB_PREFIX . "hj_import SET `" . $field . "` = CONCAT( '" . $this->db->escape($prepend_text) . "', substr($field, 1, 8 ),'/',`" . $field . "` )");

mysql_fetch_assoc() parameter 1 resource, object given in ... on line 6

Hi I've searched for a solution to this and found several answers and after many edits to the code and no success I'm asking here directly.
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = mysql_fetch_assoc($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
This returns: mysql_fetch_assoc() parameter 1 resource, object given in ... on line 6
I get where it is, just can't seem to find the problem.
Thanks in advance
Use tep_db_fetch_array()
$return_arr = array();
$fetch = tep_db_query("select * from products, " . TABLE_PRODUCTS_DESCRIPTION . " WHERE products.products_status = '1' and products.products_id = " . TABLE_PRODUCTS_DESCRIPTION . ".products_id and " . TABLE_PRODUCTS_DESCRIPTION . ".language_id = '" . (int)$languages_id . "' and " . TABLE_PRODUCTS_DESCRIPTION . ".products_name LIKE '%" . $_GET['term'] . "%' LIMIT 0,10");
echo $fetch; die;
if($fetch === FALSE) {
die(mysql_error());
}
while ($row = tep_db_fetch_array($fetch))
{
array_push($return_arr, $row['products_name']);
}
print json_encode($return_arr);
string mysqli_real_escape_string ( mysqli $link , string $escapestr )
link- A link identifier returned by mysqli_connect() or mysqli_init()
escapestr- The string to be escaped.
You can find an example http://php.net/manual/en/mysqli.real-escape-string.php the first parameter needs to be the link identifier returned by mysqli_connect()