-
Posts
24,608 -
Joined
-
Last visited
-
Days Won
831
Everything posted by Barand
-
PDO. How to pass quote literal as part of query?
Barand replied to nik_jain's topic in PHP Coding Help
Post code we can read instead of redacted text and someone might look at it. -
Compare your $update->execute with mine.
-
$attribute would be the key and $label would be the value $taxonomy_of_interest[$attribute] = $label;
-
v5.6 equivalent would be $reset = isset($_POST['reset'][$id]) ? $_POST['reset'][$id] : 0;
-
The script is using PDO, not mysqli "??" is a PHPv7 operator
-
One way would be to use the $headings array. Provide a translation for all those you want to include and ignore those that are not in the array. You could also think about extending that array to provide the sequence for attribute output,
-
Attribute which are arrays (like "image" are ignored, as are attributes with no values. Where no heading translation is provided the raw attribute name is output. <?php $headings = [ 'attribute_pa_pack-quantity' => 'Pack Qty', 'attribute_pa_variation' => 'Variation', 'sku' => 'SKU', 'variation_description' => 'Var Desc', 'variation_id' => 'Id', 'price_html' => 'Price' ]; $systems = []; foreach ($variations as $var) { $atts = array_values($var['attributes']); $key = $atts[0]; $kv = count($var); $ka = count($var['attributes']); if (!isset($systems[$key])) { $systems[$key] = []; } $systems[$key][] = array_merge(array_slice($var['attributes'], 1, $ka-1, 1), array_slice($var, 1, $kv-1, 1)); } echo '<pre>', print_r($systems, 1), '</pre>'; ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 10pt; } .system { width: 30%; float: left; margin-right: 30px; } .item { padding: 5px; margin-bottom: 15px; } .hdg { display: inline-block; width: 150px; font-weight: 600; } </style> </head> <body> <?php foreach ($systems as $sys => $sdata) { echo "<div class='system'><h3>$sys</h3>\n"; foreach ($sdata as $item) { echo "<div class='item'>\n"; foreach ($item as $h => $v) { if ($v && !is_array($v)) { $hd = $headings[$h] ?? $h; // use attribute key if no translation available echo "<div class='hdg'>{$hd}</div>$v<br>\n"; } } echo "</div>\n"; } echo "</div>\n"; } ?> </body> </html>
-
No need to apologise. You are the one who is going to be adapting the sample code I gave you so it does what you really want to do.
-
Looks like I missed mine from my code $headings = [ 'attribute_pa_pack-quantity' => 'Pack Qty', 'attribute_pa_variation' => 'Variation', 'sku' => 'SKU', 'variation_description' => 'Var Desc', 'variation_id' => 'Id', 'price_html' => 'Price' ];
-
It seems to have $headings[$h] as an array. What does your $headings array look like?
-
This should get you on your way <?php $systems = []; foreach ($variations as $var) { $atts = array_values($var['attributes']); $key = $atts[0]; $kv = count($var); $ka = count($var['attributes']); if (!isset($systems[$key])) { $systems[$key] = []; } $systems[$key][] = array_merge(array_slice($var['attributes'], 1, $ka-1, 1), array_slice($var, 1, $kv-1, 1)); } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Example</title> <style type='text/css'> body { font-family: calibri, sans-serif; font-size: 10pt; } .system { width: 25%; float: left; margin-right: 30px; } .item { padding: 5px; margin-bottom: 15px; } .hdg { display: inline-block; width: 80px; font-weight: 600; } </style> </head> <body> <?php foreach ($systems as $sys => $sdata) { echo "<div class='system'><h3>$sys</h3>\n"; foreach ($sdata as $item) { echo "<div class='item'>\n"; foreach ($item as $h => $v) { if ($v) { echo "<div class='hdg'>{$headings[$h]}</div>$v<br>\n"; } } echo "</div>\n"; } echo "</div>\n"; } ?> which gives
-
The easy solution would be to use the array keys as the headings but they would need to be a little more user-friendly than 'attribute_pa_pack-quantity'. Alternatively you need to map each attribute name to an output heading ( EG 'attribute_pa_pack-quantity' => 'Pack Quantity')
-
You could do something like this, but you will have a problem with your final output's headings if you don't know in advance what your inputs are going to be. $systems = []; foreach ($variations as $var) { $atts = array_values($var['attributes']); $key = $atts[0]; $kv = count($var); $ka = count($var['attributes']); if (!isset($systems[$key])) { $systems[$key] = []; } $systems[$key][] = array_merge(array_slice($var['attributes'], 1, $ka-1, 1), array_slice($var, 1, $kv-1, 1)); } echo '<pre>', print_r($systems, 1), '</pre>';
-
revised $systems = []; foreach ($variations as $var) { $var['attributes'] = array_values($var['attributes']); // ADD THIS LINE TO CONVERT TO NUMERIC INDEX if (!isset($systems[$var['attributes'][0]])) { $systems[$var['attributes'][0]] = []; } $systems[$var['attributes'][0]][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', 'variation' => $var['attributes'][2], 'pack_qty' => $var['attributes'][1] ]; }
-
How are you creating the $variations array in the first place? "??" is PHP's null coalesce operator $id = $var['variation_id'] ?? ''; // equivalent to $id = isset($var['variation_id']) ? $var['variation_id'] : ''; it returns the first non-null value $a = null; $b = null; $c = 100; $d = 200; echo $a ?? $b ?? $c ?? $d; //--> 100
-
Try $systems = []; foreach ($variations as $var) { if (!isset($systems[$var['attributes']['attribute_pa_system']])) { $systems[$var['attributes']['attribute_pa_system']] = []; } $systems[$var['attributes']['attribute_pa_system']][] = [ 'sku' => $var['sku'], 'var_id' => $var['variation_id'] ?? '', 'variation' => $var['attributes']['attribute_pa_variation'], 'pack_qty' => $var['attributes']['attribute_pa_pack-quantity'] ]; } Giving Array ( [system-1] => Array ( [0] => Array ( [sku] => XT1ECWH [var_id] => 234 [variation] => n-a [pack_qty] => 2 ) ) [system-2] => Array ( [0] => Array ( [sku] => XT2ECLWH [var_id] => 236 [variation] => left [pack_qty] => 2 ) [1] => Array ( [sku] => XT2ECRWH [var_id] => 237 [variation] => right [pack_qty] => 2 ) ) [system-3] => Array ( [0] => Array ( [sku] => XT3ECWH [var_id] => [variation] => n-a [pack_qty] => 2 ) ) )
-
This does it with a single form <?php // // PROCESS POSTED DATA // if ($_SERVER['REQUEST_METHOD']=='POST') { $updt = $db->prepare("UPDATE extensions SET reset = ? WHERE id = ? "); foreach ($_POST['extid'] as $id) { $reset = $_POST['reset'][$id] ?? 0; // if checkbox wasn't posted then it is "0" $updt->execute( [ $reset, $id ] ); } } // // GET EXTENSIONS DATA // $exdata = ''; $res = $db->query("SELECT id , extension , reset FROM extensions "); foreach ($res as $r) { $chk = $r['reset']==1 ? 'checked':''; $exdata .= "<tr><td>{$r['extension']}</td> <td> <input type='hidden' name='extid[]' value='{$r['id']}'> <input type='checkbox' name='reset[{$r['id']}]' value='1' $chk> </td> </tr>\n"; } ?> <html> <head> <meta name="generator" content="PhpED 18.0 (Build 18044, 64bit)"> <title>test</title> </head> <body> <h1>Extensions</h1> <form method='post'> <table> <tr><th>Extension</th><th>Reset</th></tr> <?=$exdata?> </table> <input type='submit' name='btnSub' value='Submit'> </form> </body> </html>
-
Sybase select img from PHP sybase_query
Barand replied to abelhermar's topic in Other RDBMS and SQL dialects
How are you attempting to display the image in the browser? -
You are building your foreign keys the wrong way round, EG ALTER TABLE `programs` ADD FOREIGN KEY (`program_id`) REFERENCES `deal_type` (`program_id`); should be ALTER TABLE `deal_type` ADD FOREIGN KEY (`program_id`) REFERENCES `programs` (`program_id`); program_id is the primary key of the programs table. When it appears in another table (such as deal_type) it is, therefore, a foreign key linking back the programs record. Also CREATE TABLE `deal_type` ( `deal_type_id` int AUTO_INCREMENT, `affiliate_id` int, `program_id` int, `affiliate_deal_id` int, PRIMARY KEY(deal_type_id,affiliate_id,program_id,affiliate_deal_id) -- really? PK should be deal_type_id );
-
You can't use them for column and table names, they got that correct, but I have never had a problem when using LIMIT ?,?
-
Unfortunately I do not know your database or table structures and I cannot see the query you are running because it is obscured behind a function. So all I can do (as I did) is provide an example and leave the rest to you.
-
@Phi11W, thank you for reinforcing what I said in the first reply to this topic.
-
The problem with that is clicking takes you to that person's profile.
-
UPDATE mytable SET thedate = NULL WHERE thedate = '0000-00-00';
-
To use the results of a query you first have to execute the query (not just define a string with incorrect query syntax)