-
Posts
24,604 -
Joined
-
Last visited
-
Days Won
830
Everything posted by Barand
-
If you are using print_r(), put it between <pre>..</pre> tags to make it legible. echo '<pre>' . print_r($array, true) . '</pre>'; Try $items[$item->id] = $item->category; Try to limit the number of posts on the same question to 1.
-
$json = '[{"id":"1","category":"public health","type":"top"},{"id":"2","category":"environment","type":"top"},{"id":"3","category":"global unrest","type":"top"},{"id":"4","category":"military","type":"top"},{"id":"6","category":"super powers","type":"top"},{"id":"7","category":"technology","type":"top"},{"id":"8","category":"human rights","type":"top"},{"id":"60","category":"space race","type":"top"},{"id":"67","category":"globalism","type":"top"},{"id":"87","category":"government","type":"top"}]'; $array = json_decode($json, 1); // decode as an array $column = 'category'; $categories = array_column($array, $column); // get the $column values echo '<pre>' . print_r($categories, 1) . '</pre>'; gives Array ( [0] => public health [1] => environment [2] => global unrest [3] => military [4] => super powers [5] => technology [6] => human rights [7] => space race [8] => globalism [9] => government )
-
Stilll waiting for you to post the data in a processable form as requested.
-
When it's finished, send us a link to the site. I love buying from sites where the user can specify their own prices (0.01) for each item purchased 😀 All you should be sending from the form is product ID and qty. Price will be stored in your product table.
-
Post a json_encoded() version.
-
W3 has size classes (w3-tiny, w3-small, w3-medium, w3-large etc) Alternatively you can override the W3 class <input class='w3-input w3-border w3-light-grey w3-right' type="text" name="email" style='height: 25px;'> or in your css .w3-input { height: 25px; }
-
Does it require you to specify the port number?
-
Using <select><option>?</option></select> to display text.
Barand replied to Steve_Berry's topic in PHP Coding Help
That's because all you are doing is resetting the page <select onchange="reload(this.form)"> You need to send the form data. Try... <select name="page" onchange="this.form.submit()"> and change to if ($_SERVER['REQUEST_METHOD']=='POST') { -
What's the new code that doesn't work?
-
As I pointed out 6 replies ago, your data contains 4 columns, your query is trying to process 5 columns.
-
Typically I only use try/catch when using database functions (as they throw execeptions and not just set error codes). I turn on exceptions when I make the db connection. Having done that, I mostly leave the automatic reporting of the exceptions to PHP so no further checking required - if there's an error it gets reported. This done by $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // PDO mysqli_report(MYSQLI_REPORT_ERROR|MYSQLI_REPORT_STRICT); // MYSQLI Two situations where I resort to try/catch are transactions duplcate keys Transactions Say I were to write an order record then several order item records to another table. I need to be sure that all were successfully completed. If the last order item fails, I want to undo all of the inserts try { begin transaction write order record while there are order items write order item record end while commit // all ok so save the data } catch (exception) { rollback // oops! undo all transaction inserts throw exception // rethrow the exception so PHP reports an error occured } Duplcates I don't want two users to have the same username so I attempt the insert anyway and only worry about it if it turned out to be a duplcate (requires username column to be defined as unique) try { insert user record } catch (exception) { if exception error is duplicate key inform user else throw exception // nothing I can do so php reports error end if }
-
A "while()" loop executes code only if the condition is true. A "do...while()" loop executes at least once before testing the condition. That is what you want in this case.
-
Looks OK. I'd use a "do" loop i this case and count the records found. $start = time(); $query = $db->prepare("select count(*) from mytable where sku = ?"); do { sleep(1); $query->execute(['XYZ123']); $recs = $query->fetchColumn(); } while ($recs == 0 && time() < $start + 60); echo $recs == 0 ? "Try again later" : "$recs found";
-
Your query is trying to insert $column[0], $column[1], $column[3], $column[4], $column[5], According to your var_dump you have $column[0], $column[1], $column[2], $column[3]
-
Windows has an AT command as its method of running cron jobs.
-
Do you have empty lines in the CSV file?
-
You don't show what you finally do with $text. However, at the start $text = ''; Then, inside the for() loop, ... $text .= sprintf( ^ ... might help.
-
Alternatively ... WHERE server NOT IN ('gmail.com', 'yahoo.com')
-
Simple boolean algebra NOT (A OR B) = NOT A AND NOT B As you have it, if the server=yahoo then it is not equal to gmail - so that condition is satisfied and therefore set to NULL if the server=gmail then it is not equal to yahoo- so that condition is satisfied and therefore set to NULL
-
These are the results I get (wordlist contains 351,100 records) $t1 = microtime(1); $res = $db->query("SELECT word FROM wordlist WHERE MATCH (word) AGAINST ('sang*' IN BOOLEAN MODE)"); $t2 = microtime(1); printf('Query 1 : %0.4f seconds<br>', $t2 - $t1); $t1 = microtime(1); $res = $db->query("SELECT word FROM wordlist WHERE word LIKE 'sang%'"); $t2 = microtime(1); printf('Query 2 : %0.4f seconds<br>', $t2 - $t1); results (74 words found) Query 1 : 0.0026 seconds Query 2 : 0.0005 seconds
-
You are calling a new query each time through the loop. Call the query then loop throught the results. Stop trying to do everything in a single line.
-
foreach ($array[1] as $k => $code) { $price = $array[2][$k]; echo "$code is $price<br>"; }
-
Using <select><option>?</option></select> to display text.
Barand replied to Steve_Berry's topic in PHP Coding Help
Put the echo inside the loop to list all of them otherwise you'll only list the last one. You will need to restructure your code. -
Step 3 - after the file_get_contents() is there a loop like this { read record insert record into db } while not end of file and are there 100's/1000's of records?
-
You can't set display startup errors in the code. If you get a startup error, the code to tell them to display can't be executed.