2 or 3 Checkboxes in MySQL query - mysql

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!

Related

filter for selected record

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|

How to display sub-categories of a category page?

I have this code to display a menu in the header, but I want to show the child categories for the selected category page not the parents categories.
<?php
$categories = get_the_category();
$category_id = $categories[0]->cat_ID;
foreach ( $category_id as $navItem ) {
$class_names = $value = '';
$classes = empty( $navItem->classes ) ? array() : (array) $navItem->classes;
$classes[] = 'menu-item-' . $navItem->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $navItem ) );
if ( in_array( 'current-menu-item', $classes ) )
$class_names .= ' active';
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $navItem->ID, $navItem );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
echo '<li ' . $id . ' ' . $class_names . '>'.$navItem->title.'</li>';
}
?>
thank you
Ok I have made some search and I was able to fix this one
this is the code
<?php
if(is_category() && the_category_ID(false) != NULL)
{
$categories = get_categories(array("child_of" => the_category_ID(false), "hide_empty" => 0));
foreach ($categories as $category)
{
$class_names = $value = '';
$classes = (array) $navitem ='';
$classes[] = 'menu-item-' . $category->cat_ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $category ) );
if ( in_array( 'current-menu-item', $classes ) )
$class_names .= ' active';
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $category->cat_ID, $category );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
echo '<li ' . $id . ' ' . $class_names . '>'.$category->cat_name.'</li>';
}
}
?>
hope it helps someone too
the_category_ID()
this one gets the ID of the current page category and we use false so it wont print the ID
and then loop inside for the sub-categories or child categories and gets their names + slug for URL

Print out certain rows from a table

The names im using can be changed or removed in the database at times, as it is now i have to change every name in the script when that happends. Can i print it out w/o a IF for example?
<?php
$sql = "SELECT detail,
SUM(IF(depot = 'Huvuddepån', quantity, 0)) AS Huvuddepån,
SUM(IF(depot = 'Uddevalla', quantity, 0)) AS Uddevalla,
SUM(IF(depot = 'Jönköping', quantity, 0)) AS Jönköping,
SUM(IF(depot = 'Polyeten', quantity, 0)) AS Polyeten,
SUM(IF(depot = 'Krackern', quantity, 0)) AS Krackern
FROM quantities
GROUP BY detail";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>
What about this ? See sqlfiddle
SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = "',
depot,
'" THEN quantity ELSE 0 END) AS "',
depot,
'"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
It works with the following table :
CREATE TABLE quantities(
detail INT,
depot VARCHAR(20),
quantity INT
);
INSERT INTO quantities VALUES (1,"Huvuddepån",10);
INSERT INTO quantities VALUES (2,'Uddevalla',15);
INSERT INTO quantities VALUES (2,'Jönköping',20);
INSERT INTO quantities VALUES (3,'Polyeten',10);
INSERT INTO quantities VALUES (1,'Krackern',10);
INSERT INTO quantities VALUES (4,"fo'o6",10);
NOTE : Nevertheless, it will most likely break if one of your depot contains quotes ". If your data might contain such quotes, you should consider using REPLACE to replace those by \" in order to escape them.
The php code you should be using is the following one, I escaped the " in the sql because they weren't and I believe the problem came from here :
<?php
$sql = "SET #COLUMNS = NULL;
SELECT GROUP_CONCAT(
DISTINCT CONCAT(
'SUM(CASE WHEN depot = \"',
depot,
'\" THEN quantity ELSE 0 END) AS \"',
depot,
'\"'
)
) INTO #COLUMNS
FROM quantities;
SET #SQL = CONCAT(
'SELECT
detail,
',#COLUMNS,'
FROM
quantities
GROUP BY
detail'
);
PREPARE stmt FROM #SQL;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;";
$result = $conn->query($sql);
echo "<table border='0' cellspacing='3' cellpadding='3'>";
echo "<tr>";
echo "<td width='300'>"."<b>Detalj</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Huvuddepån</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Uddevalla</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Jönköping</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Polyeten</b>"."</td>";
echo "<td align='center' width='100'>"."<b>Krackern</b>"."</td>";
echo "</tr>";
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc())
{
echo "<tr>";
echo "<td>".$row["detail"]."</td>";
echo "<td align='center'>".$row["Huvuddepån"]."</td>";
echo "<td align='center'>".$row["Uddevalla"]."</td>";
echo "<td align='center'>".$row["Jönköping"]."</td>";
echo "<td align='center'>".$row["Polyeten"]."</td>";
echo "<td align='center'>".$row["Krackern"]."</td>";
echo "</tr>";
}
} else {
echo "0 results";
}
echo "</table>";
?>

Display data from mysql database using a table

I have table of items and I want to display all the item in a table form. Each row must contain a maximum of 3 cells. How can I do that?
$query = "SELECT * FROM newrequest";
$result = mysql_query($query);
echo "<table>
<tr>";
While($row = mysql_fetch_array($result))
{
$query_two = "SELECT * FROM tblmember WHERE customer_id = '${row['customer_id']}'";
$result_two = mysql_query($query_two);
$username = mysql_fetch_array($result_two)['username'];
echo "
<td> <p><b>Advertised by:</b> " .$username . " </p>
<img src= ".$row['location']." style= width:300px; >
<p><b>Product name:</b>" .$row['product_name'] . "</p>
<p><b>Product price:</b>" . $row['price'] . "</p>
</td>";
}
echo "</tr>
</table>";
mysql_close();
?>
<?php
echo '<table>';
$i = 1;
while($row = mysql_fetch_array($result)) {
$query_two = "SELECT * FROM tblmember WHERE customer_id = '${row['customer_id']}'";
$result_two = mysql_query($query_two);
$username = mysql_fetch_array($result_two)['username'];
$flag = false;
if($i%3 == 1) echo '<tr>';
echo '<td>
<td><img src="'.$row['location'].'" style="width:300px;"/>
<p><b>Product name:</b>' .$row['product_name'] . '</p>
<p><b>Product price:</b>' . $row['price'] . '</p></td>
</td>';
if($i%3 == 0) {
$flag = true;
echo '</tr>';
}
$i++;
}
if(!$flag) echo '</tr>';
echo '</table>';
mysql_close();
?>

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()