-
Posts
24,563 -
Joined
-
Last visited
-
Days Won
822
Everything posted by Barand
-
Runing out of memory in pdo insert [merged with previous thread]
Barand replied to bores_escalovsk's topic in PHP Coding Help
see http://dev.mysql.com/doc/refman/5.6/en/insert-select.html -
Runing out of memory in pdo insert [merged with previous thread]
Barand replied to bores_escalovsk's topic in PHP Coding Help
Also it's extremely inefficient ot run queries inside loops, especially when a single query will do the job. EG INSERT INTO database2(col1,col2,col3) SELECT thing1, thing2, thing3 FROM database1 -
How can I enable posting form data on to the same page.
Barand replied to Nuzzy's topic in PHP Coding Help
I haven't a clue what you are talking about. -
How can I enable posting form data on to the same page.
Barand replied to Nuzzy's topic in PHP Coding Help
<?php if (isset($_POST['name']) && !empty($_POST['name'])) { $message = "Your name is " . $_POST['name']; } else { $message = "Please enter a name and click \"Submit\" "; } ?> <html> <head> <title>Example</title> </head> <body> <?php echo $message; ?> <hr/> <form method="post" action=""> Name <input type='text' name='name' placeholder="Enter your name"> <br/> <br/> <input type="submit" name="btnSubmit" value="Submit"> </form> </body> </html> -
How can I enable posting form data on to the same page.
Barand replied to Nuzzy's topic in PHP Coding Help
If the page your form's action attribute is blank, or the action is omitted, then it will post to itself by default -
Do you have any error reporting turned on? If not, put this at top of your page ini_set ("display_errors", "1"); error_reporting(E_ALL);
-
isset() return true or false, not the value of of the variable being tested. if (isset($_POST['zip']) && $_POST['zip'] != '') { if ($_POST['zip'] == 'NG15' { header('Location: https://www.bing.com'); } else { header('Location: http://www.google.co.uk') } } else { header("Location: postcodeform.html"); } For multiple postcodes you have several choices array text file database If you use an array, the test would be like this $codes = array( 'NG15', 'NG12', 'NG1' ); if (in_array($_POST['zip'], $codes)) If you use the other two then you could read from the db or text file and put the codes into an array then the processing from then on would be the same
-
Please use the forum's code tags when posting code. CURDATE() is a SQL function and you cannot use it directly in PHP, only inside a query. You need to call an update query $target = $con->real_escape_string($_POST['target']); $sql = "UPDATE emailtbl SET lastused = CURDATE() WHERE target = '$target' "; $con->query($sql);
-
Alternative method giving table headings from first row in csv file <table border=1 cellpadding="4" style="border-collapse: collapse;"> <?php $fdata = file('test.csv', FILE_IGNORE_NEW_LINES); echo '<tr><th>' . join('</th><th>', str_getcsv($fdata[0],"\t")) . '</th></tr>'; // headings $required = array(2,5,6,7,8,9,10); foreach ($required as $n) { echo '<tr><td>' . join('</td><td>', str_getcsv($fdata[$n], "\t")) . '</td></tr>'; } ?> </table>
-
CSV to Word Doc? Sounds like a simple MS Word mail merge. No PHP required.
-
try $sql = "SELECT date , views , views / HOUR(TIMEDIFF(NOW(), date)) as avg FROM content WHERE date > CURDATE() - INTERVAL 7 DAY ORDER BY avg DESC LIMIT 8"; $result=mysql_query($sql); echo "<table>"; while($rows=mysql_fetch_array($result)) { echo '<tr><td>' . join('</td><td>', $rows) . '</td></tr>'; } echo '</table>';
-
What version PHP are you using? DateTime requires 5.2 DateTime::diff requires 5.3 You may have to do it the old fashioned way eg $hrs = floor((time() - strtotime('2014-05-26 05:19:00')) / 3600);
-
if datetime published is '2014-05-26 05:19:00', then SELECT COUNT(*) / HOUR(TIMEDIFF(NOW(), '2014-05-26 05:19:00')) as avg FROM blog WHERE viewtime > '2014-05-26 05:19:00'; or, using PHP you can get the hours with $now = new DateTime(); $pubTime = new DateTime('2014-05-26 05:19:00'); $diff = $now->diff($pubTime); $hrs = $diff->days * 24 + $diff->h; //--> 249
-
An alternative structure would be CREATE TABLE `pois` ( `id` bigint(20) unsigned NOT NULL, `lat` float(10,7) NOT NULL, `lon` float(10,7) NOT NULL, PRIMARY KEY (`id`) ) CREATE TABLE `pois_tag` ( `poisid` int(11) NOT NULL DEFAULT '0', `tagname` varchar(45) NOT NULL DEFAULT '', `tagvalue` varchar(255) DEFAULT NULL, PRIMARY KEY (`poisid`,`tagname`) ) Where each tagname/value pair is stored as a row in a separate table with the pois id Processing would be like this <?php $db = new mysqli(HOST,USERNAME,PASSWORD,'test'); // use your credentials $xmlstr = <<<XML <data> <node id="2064639440" lat="49.4873181" lon="8.4710548"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="turkish"/> <tag k="email" v="info@lynso.de"/> <tag k="name" v="Kilim - Café und Bar Restaurant"/> <tag k="opening_hours" v="Su-Th 17:00-1:00; Fr, Sa 17:00-3:00"/> <tag k="operator" v="Cengiz Kaya"/> <tag k="phone" v="06 21 - 43 755 371"/> <tag k="website" v="http://www.kilim-mannheim.de/"/> </node> <node id="2126473801" lat="49.4851170" lon="8.4756295"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="italian"/> <tag k="email" v="mannheim1@vapiano.de"/> <tag k="fax" v="+49 621 1259 779"/> <tag k="name" v="Vapiano"/> <tag k="opening_hours" v="Su-Th 10:00-24:00; Fr-Sa 10:00-01:00"/> <tag k="operator" v="Vapiano"/> <tag k="phone" v="+49 621 1259 777"/> <tag k="website" v="http://www.vapiano.de/newsroom/?store=29"/> <tag k="wheelchair" v="yes"/> </node> <node id="667927886" lat="49.4909673" lon="8.4764904"> <tag k="addr:city" v="Mannheim"/> <tag k="addr:country" v="DE"/> <tag k="addr:housenumber" v="5"/> <tag k="addr:postcode" v="68161"/> <tag k="addr:street" v="Collinistraße"/> <tag k="amenity" v="restaurant"/> <tag k="name" v="Churrascaria Brasil Tropical"/> <tag k="phone" v="+496211225596"/> <tag k="wheelchair" v="limited"/> </node> <node id="689928440" lat="49.4798794" lon="8.4853418"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="greek"/> <tag k="email" v="epirus70@hotmail.de"/> <tag k="fax" v="0621/4407 762"/> <tag k="name" v="Epirus"/> <tag k="opening_hours" v="Mo-Sa 12:00-15:00,18:00-24:00"/> <tag k="phone" v="0621/4407 761"/> <tag k="smoking" v="separated"/> <tag k="website" v="http://epirus-ma.blogspot.com/"/> <tag k="wheelchair" v="no"/> </node> <node id="689928445" lat="49.4799409" lon="8.4851357"> <tag k="amenity" v="restaurant"/> <tag k="cuisine" v="italian"/> <tag k="email" v="gianlucascurti@ristorante-augusta.de"/> <tag k="name" v="Ristorante Augusta"/> <tag k="opening_hours" v="Mo-Fr 12:00-14:00,18:00-23:00;Su 12:00-14:00,18:00-23:00"/> <tag k="phone" v="0621 449872"/> <tag k="website" v="ristorante-augusta.com/"/> <tag k="wheelchair" v="no"/> </node> </data> XML; $fields = array('id','name','lat','lon'); $xml = simplexml_load_string($xmlstr); // // PROCESS XML RECORDS // $poisdata = array(); $tagdata = array(); foreach ($xml->node as $node) { $nodedata = array_fill_keys($fields,''); $nodedata['id'] = intval($node['id']); $nodedata['lat'] = isset($node['lat']) ? floatval($node['lat']) : 0; $nodedata['lon'] = isset($node['lon']) ? floatval($node['lon']) : 0; $poisdata[] = vsprintf("(%d, %10.7f, %10.7f)", $nodedata); foreach ($node->tag as $tag) { $k = (string)$tag['k']; $v = (string)$tag['v']; $tagdata[] = sprintf("(%d, '%s', '%s')" , $nodedata['id'] , $db->real_escape_string($k) , $db->real_escape_string($v)); } } // // STORE THE DATA // $sql = "REPLACE INTO pois ('id','lat','lon') VALUES\n" . join(",\n", $poisdata); $db->query($sql); $sql = "REPLACE INTO pois_tag (poisid, tagname, tagvalue) VALUES\n" . join(",\n", $tagdata); $db->query($sql); // // DISPLAY THE DATA // $currentTags = array(); $sql = "SELECT DISTINCT tagname FROM pois_tag ORDER BY tagname = 'name' DESC, tagname"; $res = $db->query($sql); while (list($tn) = $res->fetch_row()) { $currentTags[] = $tn; } $thead = "<tr><th>id</th><th>lat</th><th>lon</th><th>" . join('</th><th>', $currentTags) . "</th></tr>\n"; $currid = $currlat = $currlon = 0; $sql = "SELECT p.id, lat, lon, tagname, tagvalue FROM pois p LEFT JOIN pois_tag t ON t.poisid = p.id ORDER BY p.id"; $res = $db->query($sql); $tdata = ''; while (list($id, $lat, $lon, $t, $v) = $res->fetch_row()) { if ($currid != $id) { if ($currid) { $tdata .= "<tr><td>$currid</td><td>$currlat</td><td>$currlon</td><td>" . join('</td><td>', $poisrow) . "</td></tr>\n"; } $currid = $id; $currlat = $lat; $currlon = $lon; $poisrow = array_fill_keys($currentTags,''); } $poisrow[$t] = $v; } $tdata .= "<tr><td>$currid</td><td>$currlat</td><td>$currlon</td><td>" . join('</td><td>', $poisrow) . "</td></tr>\n"; ?> <html> <head> <meta name="generator" content="PhpED 12.0 (Build 12010, 64bit)"> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Tags</title> <meta name="author" content="Barand"> <meta name="creation-date" content="06/04/2014"> <style type="text/css"> body, td, th { font-family: arial, sans-serif; font-size: 10pt; } table { border-collapse: collapse; } th { background-color: #369; color: white; padding: 5px 2px; } td { background-color: #EEE; padding: 2px; } </style> </head> <body> <table border='1'> <?php echo $thead, $tdata; ?> </table> </body> </html> Which would give output like this
-
Your normalized tables would look something like this +-------+---------------------------+ | code | country | +-------+---------------------------+ | 1 | USA | | 2 | Mexico | | 3 | Canada | | 4 | Global | +-------+---------------------------+ | +------------------------------+ | +---------+----------+----------+--------------------+ | nsmid | country | server | serialno | +---------+----------+----------+--------------------+ | 1 | 1 | 30 | 002062008002220 | | 1 | 3 | 31 | 002062008002222 | | 2 | 2 | 30 | 002062008002233 | | 2 | 3 | 31 | 002062008002244 | +---------+----------+----------+--------------------+
-
Here's one reason - sql treats numbers as number2 mysql> SELECT * FROM test; +----+---------------------+-------+-------+ | id | creation | 30 | 31 | +----+---------------------+-------+-------+ | 1 | 2013-02-22 17:43:56 | aaaaa | bbbbb | | 2 | 2013-02-22 17:43:56 | ccccc | bbbbb | | 4 | 2013-02-22 17:43:56 | ddddd | bbbbb | | 5 | 2013-02-22 00:00:00 | ddddd | bbbbb | +----+---------------------+-------+-------+ 4 rows in set (0.00 sec) mysql> SELECT 30, 31 FROM test; +----+----+ | 30 | 31 | +----+----+ | 30 | 31 | | 30 | 31 | | 30 | 31 | | 30 | 31 | +----+----+ 4 rows in set (0.00 sec) Read the mysql manual regarding identifier names - you were given the link by Jaques2 in reply #2
-
try $(document).ready(function() { $(".country").change(function() { $.post ( "edit_form.php", {"id": $(this).val()}, function(data) { $(".city").html(data); }, "text" ); }); });
-
After 20+ posts you should know about the forum code tags. Please use them.
-
Try some debugging and adding an alert $(document).ready(function() { $(".country").change(function() { var id=$(this).val(); var dataString = 'id='+id; alert(datastring); //<-- what does this show $.ajax ({ type: "POST", url: "edit_form.php", data: dataString, cache: false, success: function(html) { $(".city").html(html); } }); }); }); and try it with datastring = '{"id":$(this).val()}';
-
First of all, use the correct quotes, your editor is using "smart quotes" and not ordinary single quotes. Then as Boompa suggested you should get Array ( [0] => Array ( [a] => Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 [5] => 6 [6] => 7 [7] => 8 [8] => 9 [9] => 10 [10] => 11 [11] => 12 ) ) ) Now you can see the indexes you need to provide to access "10"
-
This works (using arrays instead of db but principle is the same) example2.php example1.php
-
Help with arrays and for loops, any help would be great!
Barand replied to lynx2003's topic in PHP Coding Help
As you are giving us no information all I can do is wish you good luck and say goodbye. -
Help with arrays and for loops, any help would be great!
Barand replied to lynx2003's topic in PHP Coding Help
As already asked, what does your array look like? echo "<pre>", print_r($products, 1), "</pre>"; Also How are you building the array in the first place? What should your output look like? -
Help with basic PHP website that works with MySQL database
Barand replied to tekkenfan2's topic in PHP Coding Help
http://thewebmason.com/php-mysql-crud-tutorial-using-pdo-create-read-update-delete-part-1-create/ -
You shouldn't be storing derived values (such as totals) in the database